1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

Use TEST_DATABASE_URL for running test against a separate DB.

This commit is contained in:
Jari Bakken 2014-12-12 14:11:09 +01:00 committed by Ivar Conradi Østhus
parent 1aca1aac15
commit f999388e03
4 changed files with 36 additions and 14 deletions

View File

@ -1,7 +1,7 @@
language: node_js language: node_js
node_js: node_js:
- "0.10" - "0.10"
env: DATABASE_URL=postgres://postgres@localhost:5432/unleash_test env: TEST_DATABASE_URL=postgres://postgres@localhost:5432/unleash_test
before_script: before_script:
- echo '--timeout 10000' > test/mocha.opts - echo '--timeout 10000' > test/mocha.opts
- psql -c 'create database unleash_test;' -U postgres - psql -c 'create database unleash_test;' -U postgres

View File

@ -13,19 +13,22 @@ Known client implementations:
## Development ## Development
### Create a local unleash-db on postgres ### Create a local unleash databases in postgres
```bash ```bash
$ psql postgres <<SQL $ psql postgres <<SQL
CREATE USER unleash_user WITH PASSWORD 'passord'; CREATE USER unleash_user WITH PASSWORD 'passord';
CREATE DATABASE unleash; CREATE DATABASE unleash;
GRANT ALL PRIVILEGES ON DATABASE unleash to unleash_user; GRANT ALL PRIVILEGES ON DATABASE unleash to unleash_user;
CREATE DATABASE unleash_test;
GRANT ALL PRIVILEGES ON DATABASE unleash_test to unleash_user;
SQL SQL
``` ```
Then set DATABASE_URI env var: Then set env vars:
``` ```
export DATABASE_URL=postgres://unleash_user:passord@localhost:5432/unleash export DATABASE_URL=postgres://unleash_user:passord@localhost:5432/unleash
export TEST_DATABASE_URL=postgres://unleash_user:passord@localhost:5432/unleash_test
``` ```
### Commands ### Commands
@ -34,10 +37,13 @@ export DATABASE_URL=postgres://unleash_user:passord@localhost:5432/unleash
// Install dependencies // Install dependencies
npm install npm install
// Make sure DATABASE_URL is set and run migrations in your local DB // Run migrations in your local DBs
export DATABASE_URL=postgres://unleash_user:passord@localhost:5432/unleash export DATABASE_URL=postgres://unleash_user:passord@localhost:5432/unleash
./node_modules/.bin/db-migrate up ./node_modules/.bin/db-migrate up
export TEST_DATABASE_URL=postgres://unleash_user:passord@localhost:5432/unleash
./node_modules/.bin/db-migrate up
// Start server in dev-mode: // Start server in dev-mode:
npm run dev npm run dev
@ -50,7 +56,6 @@ http://localhost:4242/features
// Execute tests: // Execute tests:
npm test npm test
// Run tests with postgres running in docker: // Run tests with postgres running in docker:
npm run docker-test npm run docker-test
``` ```

View File

@ -4,18 +4,35 @@ var fs = require('fs');
var ini = require('ini'); var ini = require('ini');
var knex = require('knex'); var knex = require('knex');
function isTestEnv() {
return process.env.NODE_ENV === 'test';
}
function getDatabaseIniUrl() {
// Finn specific way of delivering env variables
var databaseini = nconf.argv().get('databaseini');
var config = ini.parse(fs.readFileSync(databaseini, 'utf-8'));
logger.info('unleash started with databaseini: ' + databaseini);
return config.DATABASE_URL;
}
function getTestDatabaseUrl() {
if (process.env.TEST_DATABASE_URL) {
logger.info('unleash started with TEST_DATABASE_URL');
return process.env.TEST_DATABASE_URL;
} else {
throw new Error('please set TEST_DATABASE_URL');
}
}
function getDatabaseUrl() { function getDatabaseUrl() {
if (process.env.DATABASE_URL) { if (process.env.DATABASE_URL) {
logger.info('unleash started with DATABASE_URL'); logger.info('unleash started with DATABASE_URL');
return process.env.DATABASE_URL; return process.env.DATABASE_URL;
} else if (nconf.argv().get('databaseini') !== undefined) { } else if (nconf.argv().get('databaseini') !== undefined) {
// Finn specific way of delivering env variables return getDatabaseIniUrl();
var databaseini = nconf.argv().get('databaseini');
logger.info('unleash started with databaseini: ' + databaseini);
var config = ini.parse(fs.readFileSync(databaseini, 'utf-8'));
return config.DATABASE_URL;
} }
throw new Error('please set DATABASE_URL or pass --databaseini'); throw new Error('please set DATABASE_URL or pass --databaseini');
@ -24,7 +41,7 @@ function getDatabaseUrl() {
function createDbPool() { function createDbPool() {
return knex({ return knex({
client: 'pg', client: 'pg',
connection: getDatabaseUrl(), connection: isTestEnv() ? getTestDatabaseUrl() : getDatabaseUrl(),
pool: {min: 2, max: 20} pool: {min: 2, max: 20}
}); });
} }

View File

@ -64,7 +64,7 @@ function destroyFeatures() {
} }
function resetDatabase() { function resetDatabase() {
return Promise.all([destroyStrategies(), destroyFeatures()]).then(function (values) { console.log(values); }); return Promise.all([destroyStrategies(), destroyFeatures()]);
} }
function setupDatabase() { function setupDatabase() {