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

#18 storing events when they are created.

Also introduced mockery to mock db-access. Here we probably
need better modularisation and/or better dependecy management
(injecting of deps?).
This commit is contained in:
Ivar Conradi Østhus 2014-10-23 12:03:46 +02:00
parent 7e8c15c30b
commit 826f9d56e2
8 changed files with 115 additions and 69 deletions

View File

@ -2,8 +2,8 @@ var Promise = require('bluebird'),
dbPool = require('./dbPool');
function storeEvent(event) {
var sql = 'INSERT INTO events(type, user, data) VALUES ($1, $2, $3)';
var params = [event.type,event.user,event.data];
var sql = 'INSERT INTO events(type, created_by, data) VALUES ($1, $2, $3)';
var params = [event.type, event.createdBy, event.data];
return new Promise(function (resolve, reject) {
dbPool.query(sql, params, function (err) {
@ -15,5 +15,5 @@ function storeEvent(event) {
module.exports = {
store: storeEvent()
store: storeEvent
};

View File

@ -1,5 +1,5 @@
var Promise = require('bluebird'),
util = require('util'),
var util = require('util'),
eventDb = require('./eventDb'),
EventEmitter = require('events').EventEmitter;
function EventStore() {
@ -8,10 +8,10 @@ function EventStore() {
util.inherits(EventStore, EventEmitter);
EventStore.prototype.create = function (event) {
return new Promise(function (resolve) {
this.emit(event.type, event);
resolve();
}.bind(this));
var that = this;
return eventDb.store(event).then(function() {
that.emit(event.type, event);
});
};
module.exports = new EventStore();

View File

@ -23,7 +23,7 @@ module.exports = function (app) {
app.post('/features', function (req, res) {
var newFeature = req.body,
user = req.connection.remoteAddress;
createdBy = req.connection.remoteAddress;
db.getFeature(newFeature.name).then(function (feature) {
if (feature) {
@ -32,7 +32,7 @@ module.exports = function (app) {
} else {
eventStore.create({
type: eventType.featureCreated,
user: user,
createdBy: createdBy,
data: newFeature
}).then(function() {
res.status(201).end();

View File

@ -1,10 +1,11 @@
var eventStore = require('./eventStore'),
eventType = require('./eventType'),
featuresMock = require('./featuresMock'),
logger = require('./logger'),
Promise = require('bluebird');
eventStore.on(eventType.featureCreated, function (event) {
console.log('feature created event recieved', event);
logger.info('feature created event recieved', event);
}
);

View File

@ -1,54 +1,55 @@
{
"name": "unleash-server",
"description": "unleash your features",
"version": "0.0.1",
"keywords": [
"unleash",
"feature toggle",
"feature",
"toggle"
],
"repository": {
"type": "git",
"url": "ssh://git@github.com:finn-no/unleash.git"
},
"bugs": {
"url": "https://github.com/finn-no/unleash/issues"
},
"private": true,
"scripts": {
"start": "node server.js",
"start-dev": "NODE_ENV=local supervisor --ignore ./node_modules/,./public/js server.js",
"test": "jshint server.js lib public/scripts test && mocha test test/*",
"tdd": "mocha --watch test test/*",
"test-bamboo-ci": "mocha test test/*",
"db-create": "createdb unleash_${NODE_ENV:-dev}",
"db-drop": "dropdb unleash_${NODE_ENV:-dev}",
"db-migrate-up": "db-migrate --config config/database.json up",
"db-migrate-down": "db-migrate --config config/database.json down",
"db-setup": "npm run db-create; npm run db-migrate-up"
},
"dependencies": {
"bluebird": "2.2.2",
"body-parser": "1.4.3",
"db-migrate": "^0.7.1",
"errorhandler": "1.1.1",
"express": "4.9.8",
"express-validator": "2.6.0",
"ini": "1.3.0",
"log4js": "0.6.21",
"nconf": "0.6.9",
"pg": "^3.6.1",
"any-db": "2.1.0",
"any-db-pool": "2.1.0",
"any-db-postgres": "2.1.3"
},
"devDependencies": {
"chai": "1.9.1",
"jshint": "2.5.2",
"mocha": "1.20.1",
"supertest": "0.13.0",
"supervisor": "~0.6.0",
"xmlbuilder": "^2.4.4"
}
"name": "unleash-server",
"description": "unleash your features",
"version": "0.0.1",
"keywords": [
"unleash",
"feature toggle",
"feature",
"toggle"
],
"repository": {
"type": "git",
"url": "ssh://git@github.com:finn-no/unleash.git"
},
"bugs": {
"url": "https://github.com/finn-no/unleash/issues"
},
"private": true,
"scripts": {
"start": "node server.js",
"start-dev": "NODE_ENV=local supervisor --ignore ./node_modules/,./public/js server.js",
"test": "jshint server.js lib public/scripts test && mocha test test/*",
"tdd": "mocha --watch test test/*",
"test-bamboo-ci": "mocha test test/*",
"db-create": "createdb unleash_${NODE_ENV:-dev}",
"db-drop": "dropdb unleash_${NODE_ENV:-dev}",
"db-migrate-up": "db-migrate --config config/database.json up",
"db-migrate-down": "db-migrate --config config/database.json down",
"db-setup": "npm run db-create; npm run db-migrate-up"
},
"dependencies": {
"bluebird": "2.2.2",
"body-parser": "1.4.3",
"db-migrate": "^0.7.1",
"errorhandler": "1.1.1",
"express": "4.9.8",
"express-validator": "2.6.0",
"ini": "1.3.0",
"log4js": "0.6.21",
"nconf": "0.6.9",
"pg": "^3.6.1",
"any-db": "2.1.0",
"any-db-pool": "2.1.0",
"any-db-postgres": "2.1.3"
},
"devDependencies": {
"chai": "1.9.1",
"jshint": "2.5.2",
"mocha": "1.20.1",
"supertest": "0.13.0",
"supervisor": "~0.6.0",
"xmlbuilder": "^2.4.4",
"mockery": "~1.4.0"
}
}

View File

@ -0,0 +1,13 @@
var Promise = require('bluebird');
function storeEvent(event) {
console.log('using eventDbMock to store: ', event);
return new Promise(function (resolve) {
resolve();
});
}
module.exports = {
store: storeEvent
};

View File

@ -1,8 +1,27 @@
var assert = require('assert');
var eventType = require('../lib/eventType');
var eventStore = require('../lib/eventStore');
var assert = require('assert'),
mockery = require('mockery'),
eventType = require('../lib/eventType'),
eventStore;
describe('EventStore', function () {
before(function () {
mockery.enable({
warnOnReplace: false,
warnOnUnregistered: false,
useCleanCache: true
});
mockery.registerSubstitute('./eventDb', '../test/eventDbMock');
eventStore = require('../lib/eventStore');
});
after(function () {
mockery.disable();
mockery.deregisterAll();
});
describe('#create()', function () {
it('should emit event', function (done) {
eventStore.once(eventType.featureCreated, function (x) {
@ -20,7 +39,7 @@ describe('EventStore', function () {
eventStore.create({
type: eventType.featureCreated,
user: 'ole',
createdBy: 'ole',
data: eventData
});
});

View File

@ -1,4 +1,5 @@
var request = require('supertest');
var request = require('supertest'),
mockery = require('mockery'),
request = request('http://localhost:4242');
@ -6,10 +7,21 @@ describe('The api', function () {
var server;
before(function () {
mockery.enable({
warnOnReplace: false,
warnOnUnregistered: false,
useCleanCache: true
});
mockery.registerSubstitute('./eventDb', '../test/eventDbMock');
server = require('../server');
});
after(function () {
mockery.disable();
mockery.deregisterAll();
server.server.close();
});