Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop the entire sails-memory database?

I'm using 'sails-memory' as the database for my Sails unit tests and ideally would like to clear the entire database after individual tests. Is there a way I can drop the entire database?

like image 812
HolySamosa Avatar asked Sep 26 '14 16:09

HolySamosa


2 Answers

I found another method which seems to work. This emits an event which tells the orm hook to reload before each test. If using the memory db, or the disk db with the 'drop' migrate option, it accomplishes the desired.

beforeEach((done) => {
  "use strict";
  // Drops database between each test.  This works because we use
  // the memory database
  sails.once('hook:orm:reloaded', done);
  sails.emit('hook:orm:reload');
});
like image 52
Russ Egan Avatar answered Nov 13 '22 19:11

Russ Egan


You could lift your sails app before each test, rebuilding your database (migrate: 'drop'). Here is an example:

Sails = require('sails/lib/app');
app = Sails();

var testConfig = {
    environment: 'test',
    port: 1337,
    log: {
        level: 'error'
    },
    connections: {
        testDB: {
            adapter: 'sails-memory'
        }
    },
    connection: 'testDB',

    //wipe/drop ALL my data and rebuild models every time
    migrate: 'drop'
};

beforeEach(function (done) {
    // start sails app for tests
    app.lift(testConfig, function (err, sails) {
        done(err);
    });
});

//tests...
like image 36
Victor Avatar answered Nov 13 '22 20:11

Victor