Permalink
Browse files

Add ability to configure test adapters with env vars or a json file

  • Loading branch information...
1 parent 6d0ff64 commit d6784c21890251ddb3ffe95201eb8683f0edc35b @ben-ng ben-ng committed Jul 1, 2013
View
@@ -15,3 +15,4 @@ site/log/*
.log
npm-debug.log
doc/
+test/db.json
View
@@ -1,9 +1,16 @@
var t = new jake.TestTask('Model', function () {
+ // FIXME: Events fail if run after integration tests
+ // This line forces them to run first so the tests pass
+ this.testFiles.include('test/unit/events.js');
+
this.testFiles.include('test/*.js');
this.testFiles.include('test/**/*.js');
this.testFiles.exclude('test/fixtures/*.js');
this.testFiles.exclude('test/integration/adapters/shared.js');
+ this.testFiles.exclude('test/config.js');
+ this.testFiles.exclude('test/db.json');
+ this.testFiles.exclude('test/db.sample.json');
});
@@ -14,6 +21,8 @@ var t = new jake.TestTask('Model', function () {
this.testFiles.exclude('test/integration/*.js');
this.testFiles.exclude('test/integration/**/*.js');
this.testFiles.exclude('test/fixtures/*.js');
+ this.testFiles.exclude('test/config.js');
+ this.testFiles.exclude('test/db.sample.json');
});
var p = new jake.NpmPublishTask('model', [
View
@@ -613,6 +613,14 @@ if (!someTeam.players) {
}
```
+## Testing
+
+Run the tests with `jake test`. Run only unit tests with `jake test-no-integration`.
+
+The integration tests require mongo and postgres. To run the tests on a specific adapter, use `jake test[mongo]`, `jake test[postgres]`, or `jake test[memory]`.
+
+Configure adapter options by creating a `test/db.json` file. See `test/db.sample.json` for available options.
+
- - -
Model JavaScript ORM copyright 2112 mde@fleegix.org.
View
@@ -0,0 +1,39 @@
+(function () {
+ var userOpts
+ , fs = require('fs')
+ , utils = require('utilities')
+ , path = require('path')
+ , userOptsFile = path.join(__dirname, 'db')
+ , existsSync
+ , config = {
+ mongo: {
+ dbname: process.env.MONGO_DATABASE || 'model_test'
+ }
+ , postgres: {
+ user: process.env.POSTGRES_USER || 'postgres'
+ , database: process.env.POSTGRES_DATABASE || 'model_test'
+ , host: process.env.POSTGRES_HOST || '127.0.0.1'
+ }
+ };
+
+ try {
+ userOpts = require(userOptsFile);
+ utils.object.merge(config, userOpts);
+ }
+ catch (e) {
+ //Support node 0.6
+ if(typeof fs.existsSync != 'function') {
+ existsSync = path.exists;
+ }
+ else {
+ existsSync = fs.existsSync;
+ }
+
+ // Check if JSON parsing failed
+ if(existsSync(userOptsFile)) {
+ throw new Error("Could not parse user options, check if your file is valid JSON");
+ }
+ }
+
+ module.exports = config;
+}());
View
@@ -0,0 +1,10 @@
+{
+ "mongo": {
+ "dbname": "model_test"
+ }
+, "postgres": {
+ "user": "mde"
+ , "database": "model_test"
+ , "host": "127.0.0.1"
+ }
+}
@@ -9,6 +9,7 @@ var utils = require('utilities')
, User = require('../../../fixtures/user').User
, Profile = require('../../../fixtures/profile').Profile
, Account = require('../../../fixtures/account').Account
+ , config = require('../../../config')
, shared = require('../shared');
tests = {
@@ -22,9 +23,7 @@ tests = {
, 'Team'
]
, models = [];
- adapter = new Adapter({
- dbname: 'model_test'
- });
+ adapter = new Adapter(config.mongo);
model.adapters = {};
relations.forEach(function (r) {
@@ -12,6 +12,7 @@ var utils = require('utilities')
, Account = require('../../../fixtures/account').Account
, Team = require('../../../fixtures/team').Team
, Membership = require('../../../fixtures/membership').Membership
+ , config = require('../../../config')
, shared = require('../shared');
tests = {
@@ -26,11 +27,7 @@ tests = {
]
, models = [];
- adapter = new Adapter({
- database: 'model_test'
- , user: process.env.CI ? 'postgres' : null
- , host: process.env.CI ? '127.0.0.1' : null
- });
+ adapter = new Adapter(config.postgres);
adapter.once('connect', function () {
var sql = '';
@@ -10,6 +10,7 @@ var assert = require('assert')
, Membership = require('../../fixtures/membership').Membership
, Team = require('../../fixtures/team').Team
, generator = require('../../../lib/generators/sql')
+ , config = require('../../config')
, tests
, arrIncl;
@@ -23,7 +24,7 @@ arrIncl = function (array, item) {
tests = {
'test createTable in DB, string id': function (next) {
- var client = new pg.Client('postgres://' + (process.env.CI ? 'postgres' : 'mde') + '@localhost/model_test');
+ var client = new pg.Client('postgres://' + config.postgres.user + '@' + config.postgres.host + '/' + config.postgres.database);
var sql = generator.createTable(['Zooby']);
client.connect(function () {
client.on('drain', client.end.bind(client));

0 comments on commit d6784c2

Please sign in to comment.