Skip to content

Commit

Permalink
Add migration endpoint including tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharon Funke committed Mar 24, 2017
1 parent fa2fce0 commit 202c3cf
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 9 deletions.
19 changes: 19 additions & 0 deletions migration-scripts/to-v2.0.0.js
@@ -0,0 +1,19 @@
// contains all scripts which shall be executed to migrate to tool version 2.0.0
// each module has to return a result object holding the modified item and a
// flag property indicating if item was changed or not
module.exports.migrate = function(item) {
let result = {
isChanged: false
}
if (item.parties) {
let truthyparties = item.parties.filter(party => {
return party.name !== undefined && party.name !== '';
});
if (truthyparties.length < item.parties.length) {
item.parties = truthyparties;
result.isChanged = true;
}
}
result.item = item;
return result;
}
39 changes: 39 additions & 0 deletions routes/migration.js
@@ -0,0 +1,39 @@
const Joi = require('joi');
const Boom = require('boom');

// register migration scripts here in order of version,
// i.e. list the smalles version first
const migrationScripts = [
require('../migration-scripts/to-v2.0.0.js')
]

module.exports = {
method: 'POST',
path:'/migration',
config: {
validate: {
payload: {
item: Joi.object().required()
}
}
},
handler: (request, reply) => {
let item = request.payload.item;
const results = migrationScripts.map(script => {
const result = script.migrate(item);
if (result.isChanged) {
item = result.item;
}
return result;
})
const isChanged = results.findIndex(result => {
return result.isChanged;
});
if (isChanged >= 0) {
return reply({
item: item
})
}
return reply().code(304);
}
}
4 changes: 3 additions & 1 deletion routes/routes.js
@@ -1,9 +1,11 @@
const htmlStatic = require('./rendering-info/html-static.js');
const stylesheet = require('./stylesheet.js');
const schema = require('./schema.js');
const migration = require('./migration.js');

module.exports = [
htmlStatic,
stylesheet,
schema
schema,
migration
]
2 changes: 1 addition & 1 deletion test/dom-test.js
@@ -1,7 +1,7 @@
const JsDom = require('jsdom');
const expect = require('chai').expect;

const mockData = require('./resources/mock-data');
const mockData = require('./resources/mock-data-v2.0.0');
require('svelte/ssr/register');
const staticTpl = require('../views/html-static.html');
var markup = staticTpl.render(JSON.parse(JSON.stringify(mockData)));
Expand Down
43 changes: 41 additions & 2 deletions test/e2e.js
Expand Up @@ -5,6 +5,16 @@ const expect = require('chai').expect;
const server = require('../server.js');
const plugins = require('../server-plugins.js');
const routes = require('../routes/routes.js');
const Joi = require('joi');
const Enjoi = require('enjoi');
const fs = require('fs');

const resourcesDir = __dirname + '/../resources/';

const schemaString = JSON.parse(fs.readFileSync(resourcesDir + 'schema.json', {
encoding: 'utf-8'
}));
const schema = Enjoi(schemaString);

server.register(plugins, err => {
Hoek.assert(!err, err);
Expand Down Expand Up @@ -42,15 +52,16 @@ describe('Q required API', () => {

});

const mockData = JSON.parse(JSON.stringify(require('./resources/mock-data.js')));
const mockDataV1 = JSON.parse(JSON.stringify(require('./resources/mock-data-v1.0.0')));
const mockDataV2 = JSON.parse(JSON.stringify(require('./resources/mock-data-v2.0.0')));

describe('rendering-info endpoints', () => {

it('should return 200 for /rendering-info/html-static', function(done) {
const request = {
method: 'POST',
url: '/rendering-info/html-static',
payload: JSON.stringify({ item: mockData })
payload: JSON.stringify({ item: mockDataV2 })
};
server.inject(request, (res) => {
expect(res.statusCode).to.be.equal(200);
Expand All @@ -59,3 +70,31 @@ describe('rendering-info endpoints', () => {
})

});

describe('migration endpoint', () => {

it('should pass validation against schema after migration via endpoint /migration', function(done) {
const request = {
method: 'POST',
url: '/migration',
payload: JSON.stringify({ item: mockDataV1 })
} ;
server.inject(request, (res) => {
expect(Joi.validate(res.result.item, schema).error).to.be.null;
done();
})
})

it('should return 304 for /migration', function(done) {
const request = {
method: 'POST',
url: '/migration',
payload: JSON.stringify({ item: mockDataV2 })
} ;
server.inject(request, (res) => {
expect(res.statusCode).to.be.equal(304);
done();
})
})

})
53 changes: 53 additions & 0 deletions test/resources/mock-data-v1.0.0.js
@@ -0,0 +1,53 @@
module.exports = {
title: "Sitzverteilung Parlament Frankreich",
sources: [
{
"text": "asdjfh",
"link": {
"url": "www.nzz.ch",
"isValid": true
}
}
],
subtitle: "Möglichkeit zusätzliche Informationen als Untertitel zu erfassen",
notes: "Jemand musste Josef K. verleumdet haben, denn ohne dass er etwas Böses getan hätte, wurde er eines Morgens verhaftet.",
totalSeats: 200,
parties: [
{
color:
{
colorCode: "#0084c7"
},
seats: 70,
previous: 71
},
{
name: "Sozialdemokratische Partei Deutschlands",
color:
{
colorCode: "#c31906",
classAttribute: "s-viz-color-party-sp-5"
},
seats: 80,
previous: 75
},
{
name: "Bündnis 90/Die Grünen",
color:
{
colorCode: "#66a622"
},
seats: 30,
previous: 24
},
{
name: "Die Linke",
color:
{
colorCode: "#a35fd1"
},
seats: 20,
previous: 15
}
]
}
Expand Up @@ -13,9 +13,8 @@ module.exports = {
notes: "Jemand musste Josef K. verleumdet haben, denn ohne dass er etwas Böses getan hätte, wurde er eines Morgens verhaftet.",
totalSeats: 200,
parties: [
{
{
name: "Die Union",
abbreviation: "CDU/CSU",
color:
{
colorCode: "#0084c7"
Expand All @@ -25,7 +24,6 @@ module.exports = {
},
{
name: "Sozialdemokratische Partei Deutschlands",
abbreviation: "SPD",
color:
{
colorCode: "#c31906",
Expand All @@ -36,7 +34,6 @@ module.exports = {
},
{
name: "Bündnis 90/Die Grünen",
abbreviation: "Die Grünen",
color:
{
colorCode: "#66a622"
Expand All @@ -46,7 +43,6 @@ module.exports = {
},
{
name: "Die Linke",
abbreviation: "Die Linke",
color:
{
colorCode: "#a35fd1"
Expand Down

1 comment on commit 202c3cf

@benib
Copy link
Contributor

@benib benib commented on 202c3cf Mar 28, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

implements #28

Please sign in to comment.