Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loopback testing with memory as datasource

I'm trying to use the memory-connector as a datasource when doing integration testing. But it seems to always connect to the mongodb-datasource.

One major hack i have done is to change the datasource for each model to memory. But there must be a better way to do this. I'm running my tests from a gulp-task. My roflmao model-memory-hack:

var models = require('../server/model-config.json');
  for (var key in models) {
    var model = loopback.getModel(key);
    loopback.configureModel(model, {dataSource: memory});
  }
}

Is there any way to change the datasource for the app? Or do i have to change the datasource for each individual model..?

A way of doing this is to change the environment variable during testing, but so far, no luck.. I'm doing this with the gulp-task preprocess.

Hopefully by changing the environment variable, it would use datasources.integrationtesting.js, in which i have memory as a datasource.

My gulp-task:

return gulp.src('integration-tests/*.js')
.pipe($.preprocess({context: {NODE_ENV: 'integrationtesting'}}))
.pipe($.mocha())

I'm using:

  • loopback-testing
  • gulp-mocha

Appreciate any comments.. : )

like image 258
Are Almaas Avatar asked Apr 30 '15 15:04

Are Almaas


1 Answers

I think what you're looking for are environment-specific configuration files. Basically, you just create a datasource with the same name, but different implementations in different environments. Your datasources.json file would be the default, but datasources.development.json would be used if NODE_ENV was set to development.

From that linked page, you might have this in datasources.json:

{
  db: {
    connector: 'mongodb',
    database: 'myapp',
    user: 'myapp',
    password: 'secret'
  }
}

And this in datasources.development.json:

{
  db: {
    connector: 'memory'
  }
} 
like image 69
Jordan Kasper Avatar answered Oct 21 '22 20:10

Jordan Kasper