Skip to content

Instantly share code, notes, and snippets.

@Schniz
Last active November 17, 2015 07:04
Show Gist options
  • Save Schniz/6fe237d2af38c4818631 to your computer and use it in GitHub Desktop.
Save Schniz/6fe237d2af38c4818631 to your computer and use it in GitHub Desktop.
Using RethinkDB for testing. Kill the last server, start a new server with a random port, migrate, seed.
import rethinkdbdash from 'rethinkdbdash';
import { readFileSync } from 'fs';
import { resolve } from 'path';
let port = undefined;
if (process.env.NODE_ENV === 'test') {
port = parseInt(readFileSync(resolve(__dirname, '../.testData/rethink.port')));
}
export default rethinkdbdash({ db: 'shiftlift', port });
kill -9 $(cat .testData/rethink.pid) &> /dev/null
rm -rf .testData/rethink{.pid,.port,_data} &> /dev/null
RETHINK_PORT=$(get-random-port)
echo $RETHINK_PORT > .testData/rethink.port
rethinkdb --no-http-admin --cluster-port $(get-random-port) --driver-port $RETHINK_PORT --pid-file .testData/rethink.pid -d .testData/rethink_data &
sleep 0.5
rethink-migrate up --port $RETHINK_PORT --db shiftlift
NODE_ENV=test babel-node scripts/seed.js
import { readdirSync } from 'fs';
import { resolve } from 'path';
const r = require(resolve(__dirname, '../lib/rethinkdb'));
let seedPath = resolve(__dirname, './seed');
let tables = readdirSync(seedPath);
let regex = /(.+)\.json$/;
const log = (data) => console.log(`seed: ${data}`); //eslint-disable-line
Promise.all(tables.map(tableFile => {
let table = regex.exec(tableFile)[1];
let data = require(resolve(seedPath, tableFile));
log(data);
return r.table(table).insert(data).run().then(() => log(`succeed seeding ${table}`));
})).then(() => log('seed complete')).then(() => process.exit(0)); // eslint-disable-line
@Schniz
Copy link
Author

Schniz commented Nov 17, 2015

when i connect to rethinkdb I check if NODE_ENV==='test', and if its true I fetch the port from the rethink.port file

@Schniz
Copy link
Author

Schniz commented Nov 17, 2015

npm install --save-dev rethink-migrate get-random-port

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment