Skip to content

Commit

Permalink
Merge pull request #34 from dmarcelino/846-migrate_create
Browse files Browse the repository at this point in the history
#846: adds tests for migration strategy `create`
  • Loading branch information
particlebanana committed Jan 29, 2016
2 parents fc568df + 9e314bc commit d452e84
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 1 deletion.
121 changes: 121 additions & 0 deletions interfaces/migratable/migrate.create.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Module dependencies
*/

var Waterline = require('waterline'),
assert = require('assert'),
bootstrapFn = require('./support/bootstrapFn');

var newFixture = {
CreateFixture : Waterline.Collection.extend({

tableName : 'create',
connection : 'migratable',
migrate : 'create',

attributes : {
name : 'string',
age : 'integer',
gender : 'string'
}
})
};


describe('Migratable Interface', function() {

describe('migrate: "create"', function() {
runTests("Create");
});

function runTests(collectionName) {

it('should have the proper migrate setting when bootstrapping', function() {
assert(Migratable[collectionName].migrate === 'create');
});

it('should have tables', function(done) {
Migratable[collectionName].describe(function(err, schema) {
assert(!err);
assert(schema);
done();
});
});


describe('teardown and migrate existing data', function() {

before(function(done) {
Migratable[collectionName].create({ name: 'blackbeard' }, done);
});

it('should retain the data when bootstrapped the second time', function(done) {
Migratable.waterline.teardown(function(err) {
bootstrapFn(function(err, obj) {
assert(!err);
var ontology = obj.ontology;
ontology.collections[collectionName.toLowerCase()].findOne({name: 'blackbeard'})
.exec(function(err, pirate) {
assert(!err);
assert(pirate.name, 'blackbeard');
done();
});
});
});
});
});


describe('teardown and migrate existing data after adding property', function() {

var collection;

before(function(done) {
Migratable[collectionName].create({ name : 'bluebeard' }, function(err) {
if(err) { return done(err); }
Migratable.waterline.teardown(function(err) {
bootstrapFn(newFixture, function(err, obj) {
assert(!err);
var ontology = obj.ontology;
ontology.collections[collectionName.toLowerCase()].findOne({name: 'bluebeard'});
collection = ontology.collections[collectionName.toLowerCase()];
done();
});
});
});
});

it('should retain the data when bootstrapped the second time', function(done) {
collection.findOne({name: 'bluebeard'})
.exec(function(err, pirate) {
assert(!err);
assert(pirate.name, 'bluebeard');
done();
});
});

it('should have new attribute', function(done) {
collection.describe(function(err, schema) {
assert(!err);
assert(schema);
assert(schema.name);
assert(schema.gender);
done();
});
});


it('should be able to record data using new attribute', function(done) {
collection.create({ name: 'whitebeard', gender: 'male' })
.exec(function(err, newPirate) {
assert(!err);
console.log('\n newPirate:', newPirate);
assert(newPirate.name, 'whitebeard');
assert(newPirate.gender, 'male'); // requires addAttribute
done();
});
});

});
}
});
9 changes: 8 additions & 1 deletion interfaces/migratable/support/bootstrapFn.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ var fixtures = {
UserFixture: require('./fixtures/crud.fixture'),
ProjectFixture: require('./fixtures/schema.fixture'),
AlterFixture: require('./fixtures/alter.fixture'),
CreateFixture: require('./fixtures/create.fixture'),
CustomFixture: require('./fixtures/custom.fixture'),
DropFixture: require('./fixtures/drop.fixture'),
SafeFixture: require('./fixtures/safe.fixture')
};


module.exports = function(cb) {
module.exports = function(newFixtures, cb) {
if(!cb){
cb = newFixtures;
newFixtures = undefined;
}
newFixtures = newFixtures || {};
fixtures = _.defaults(newFixtures, fixtures);

var waterline = new Waterline();

Expand Down
18 changes: 18 additions & 0 deletions interfaces/migratable/support/fixtures/create.fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Dependencies
*/

var Waterline = require('waterline');

module.exports = Waterline.Collection.extend({

tableName: 'create',
connection: 'migratable',
migrate: 'create',

attributes: {
name: 'string',
age: 'integer'
}

});

0 comments on commit d452e84

Please sign in to comment.