From 350362190b89d29bb906c4563afeda7f00331926 Mon Sep 17 00:00:00 2001 From: Dario Marcelino Date: Mon, 23 Feb 2015 14:26:06 +0000 Subject: [PATCH] Issue balderdashy/waterline#846: added tests for migration strategy `create` --- interfaces/migratable/migrate.create.test.js | 121 ++++++++++++++++++ interfaces/migratable/support/bootstrapFn.js | 9 +- .../support/fixtures/create.fixture.js | 18 +++ 3 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 interfaces/migratable/migrate.create.test.js create mode 100644 interfaces/migratable/support/fixtures/create.fixture.js diff --git a/interfaces/migratable/migrate.create.test.js b/interfaces/migratable/migrate.create.test.js new file mode 100644 index 0000000..67f0cea --- /dev/null +++ b/interfaces/migratable/migrate.create.test.js @@ -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(); + }); + }); + + }); + } +}); diff --git a/interfaces/migratable/support/bootstrapFn.js b/interfaces/migratable/support/bootstrapFn.js index e883776..d8e5a34 100644 --- a/interfaces/migratable/support/bootstrapFn.js +++ b/interfaces/migratable/support/bootstrapFn.js @@ -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(); diff --git a/interfaces/migratable/support/fixtures/create.fixture.js b/interfaces/migratable/support/fixtures/create.fixture.js new file mode 100644 index 0000000..619b711 --- /dev/null +++ b/interfaces/migratable/support/fixtures/create.fixture.js @@ -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' + } + +});