beforeEach(function(done) { sails = new SailsApp(); sails.lift({ // configuration for testing purposes }, function(err, sails) { if (err) return done(err); // here you can load fixtures, etc. done(err, sails); }); });
grunt.registerTask('db:migrate', 'Run the database migrations', function (command) { var done = this.async(); var name = grunt.option('name'); var sails; var sailsConfig; var env; if (!command) { usage(grunt); return done(); } if (grunt.option('env')){ env = grunt.option('env'); }else{ env = process.env.NODE_ENV; } sailsConfig = { port: 0, log: { level: process.env.LOG_LEVEL || 'silent' }, environment: env, migrating: true }; // lift Sails to get the effective configuration. We don't actually need to // run it, and we certainly don't want any log messages. We just want the // config. sails = new Sails(); sails.lift(sailsConfig, function (err) { var url; var args; if (err) { grunt.fail.fatal('Could not lift sails', err); return done(err); } if (!sails.config.migrations) { grunt.fail.fatal('Migrations not configured. Please setup ./config/migrations.js'); return done(); } try { args = buildDbMigrateArgs(grunt, sails.config.migrations, command); grunt.log.writeln('+ db-migrate', args.join(' ')); url = sailsDbMigrate(args, sails, done); grunt.log.writeln('DATABASE_URL=' + url); } catch (err) { grunt.fail.fatal(err); done(err); } }); });
liftSails: function(cb) { var Sails = new SailsApp(); var app; process.chdir(path.resolve(__dirname, '..', 'fixtures', 'app')); fsx.copySync('localDiskDb.db', path.resolve('.tmp', 'localDiskDb.db')); Sails.load({ hooks: {grunt: false, views: false}, globals: false }, cb); },
before(function setup(done){ app.load({ globals: false, log: { level: 'warn' }, hooks: { // Inject the orm hook in this repo into this Sails app orm: require('../') }, loadHooks: ['moduleloader', 'userconfig', 'orm'], models: { migrate: 'safe' }, orm: { // THIS IS FOR EXPERIMENTAL USE ONLY! // (could change at any time) moduleDefinitions: { models: { foo: {} }, adapters: { 'sails-disk': {} } } } },done); });
it('should fail to load the orm hook', function (done){ // New up an instance of Sails. var app = new Sails(); // Load the app. app.load({ globals: false, log: { level: 'silent' }, hooks: { // Inject the orm hook in this repo into this Sails app orm: require('../') }, loadHooks: ['moduleloader', 'userconfig', 'orm'], orm: { // THIS IS FOR EXPERIMENTAL USE ONLY! // (could change at any time) moduleDefinitions: { models: { foo: {} } } } },function (err) { if (err) { // Ensure this error was due to the orm hook failing to load-- // and specifically that the proper error code was sent back. if (err.code === 'E_ADAPTER_NOT_INSTALLED') { return done(); } else { // console.log(err.code,_.keys(err), err.stack); return done(new Error('Expected `E_ADAPTER_NOT_INSTALLED`, but got a different error: \n'+err.stack+'\n(for ^^^, error code is `'+err.code+'`')); } } return done(new Error('Should have failed to load the ORM hook.')); }); });
describe('with the appropriate adapter(s) and `migrate: safe`', function (){ // New up an instance of Sails. var app = new Sails(); // Load the app. before(function setup(done){ app.load({ globals: false, log: { level: 'warn' }, hooks: { // Inject the orm hook in this repo into this Sails app orm: require('../') }, loadHooks: ['moduleloader', 'userconfig', 'orm'], models: { migrate: 'safe' }, orm: { // THIS IS FOR EXPERIMENTAL USE ONLY! // (could change at any time) moduleDefinitions: { models: { foo: {} }, adapters: { 'sails-disk': {} } } } },done); }); it('should have initialized the `orm` hook', function (){ assert(app.hooks.orm); }); it('should have set up a dictionary of models on the hook', function (){ assert(_.isObject(app.hooks.orm.models) && !_.isArray(app.hooks.orm.models)); }); it('should have set up a dictionary of adapters on the hook', function (){ assert(_.isObject(app.hooks.orm.adapters) && !_.isArray(app.hooks.orm.adapters)); }); it('should have also exposed `sails.models` as a direct reference to `sails.hooks.orm.models`', function (){ assert(app.models === app.hooks.orm.models); }); it('should contain the expected models in `sails.hooks.orm.models`', function (){ assert.equal(_.keys(app.models).length, 1); assert(_.isObject(app.models.foo), new Error('Should have a model under the `foo` key')); }); // Lower the app. after(function teardown(done) { app.lower(done); }); });//</with the appropriate adapter(s)>
after(function teardown(done) { app.lower(done); });