KSDaemon/sails-hook-sequelize

Use sqlite for tests

mrded opened this issue · 3 comments

mrded commented

Just noticed that you use Postgres to run tests. Are there any reasons to use it over SQLite?

Using SQLite will simplify test environment. It will allow you to run tests without having a "real" database.

Well, actually i'm planning to have tests agains sqlite too :)
One reason for using PostgreSQL is that it has schemas, which is not available in other DBs. So to have tests against migrating and creating schemas — we use PostgreSQL

mrded commented

I see. I can explain how do I do that with mysql. Hopefully it will help you implementing it.

I have a db/migrations/1234567890-init-models.js file to init all database tables from models:

var glob = require('glob');
var path = require('path');
var models = {};

glob.sync('./api/models/*.js').forEach(function(file) {
  var modelName = path.basename(file, '.js');
  var model = require(path.resolve(file));

  models[modelName] = model;
});

module.exports = {
  up: function(queryInterface, Sequelize) {
    var Project = queryInterface.sequelize;

    for (var modelName in models) {
      Project.define(modelName, models[modelName].attributes, models[modelName].options);
    }

    return Project.sync({force: true});
  },
};

Then I have pretest in package.json like following sequelize db:drop; sequelize db:create; sequelize db:migrate to init database.

Then before each test case I truncate all tables:

beforeEach(function(done) {
  var models = Object.keys(sails.models).map(function(key) {
    return sails.models[key];
  }); 

  var destroyPromises = models.map(function(model) {
    return model.destroy({ truncate: true });
  });

  Promise.all(destroyPromises).then(function() {
    done();
  }).catch(done);
});

I've added some tests against sqlite db. It was helpful to test some cases.