Skip to content

Commit

Permalink
fix(fix get all revisions): fix get all revisions
Browse files Browse the repository at this point in the history
ix get all revisions
  • Loading branch information
Manor committed Feb 4, 2019
1 parent ca09215 commit 3930330
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 30 deletions.
4 changes: 3 additions & 1 deletion src/tests/helpers/artilleryValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ function verifyArtillery(req, res, next) {
if (body.type === consts.TEST_TYPE_CUSTOM) {
let validationOutput = artilleryCheck.validate(body.artillery_test);
if (!validationOutput.valid) {
return res.status(400).json({message: 'The artillery json is not valid. Errors: ' + validationOutput.errors.map(error => error.description)});
const error = new Error('The artillery json is not valid. Errors: ' + validationOutput.errors.map(error => error.description));
error.statusCode = 400;
return next(error);
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/tests/models/database/sequelize/sequelizeConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ async function insertTest(testInfo, testJson, id, revisionId){
test_id: id,
name: testInfo.name,
type: testInfo.type,
description: testInfo.description,
updated_at: Date.now(),
raw_data: JSON.stringify(testInfo.scenarios),
artillery_json: JSON.stringify(testJson),
Expand All @@ -105,7 +106,7 @@ async function getTests() {
}
async function getAllTestRevisions(id){
const test = client.model('test');
let allTests = await test.findAll({ where: { test_id: id }, order: [['updated_at', 'DESC'], ['id', 'DESC']] });
let allTests = await test.findAll({ where: { test_id: id }, order: [['updated_at', 'ASC'], ['id', 'ASC']] });
allTests = sanitizeTestResult(allTests);
return allTests;
}
Expand Down Expand Up @@ -181,7 +182,10 @@ function sanitizeTestResult(data) {
const result = data.map(function (test) {
const dataValues = test.dataValues;
dataValues.artillery_json = JSON.parse(dataValues.artillery_json);
dataValues.raw_data = JSON.parse(dataValues.raw_data);
dataValues.id = dataValues.test_id;
delete dataValues.test_id;
delete dataValues.created_at;
return dataValues;
});
return result;
Expand Down
4 changes: 1 addition & 3 deletions src/tests/models/manager.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
'use strict';
const testGenerator = require('./testGenerator'),
database = require('./database'),
logger = require('../../common/logger'),
uuid = require('uuid'),
{ ERROR_MESSAGES } = require('../../common/consts');

Expand All @@ -17,8 +16,7 @@ async function upsertTest(testRawData, existingTestId) {
const testArtilleryJson = await testGenerator.createTest(testRawData);
let id = existingTestId || uuid();
let revisionId = uuid.v4();
const result = await database.insertTest(testRawData, testArtilleryJson, id, revisionId);
logger.info(result, 'Test created successfully and saved to Cassandra');
await database.insertTest(testRawData, testArtilleryJson, id, revisionId);
return { id: id, revision_id: revisionId };
}

Expand Down
11 changes: 5 additions & 6 deletions tests/unit-tests/tests/helpers/artilleryValidator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,22 @@ describe('Artillery validator tests', function () {
}]
};

req = {body: {type: consts.TEST_TYPE_CUSTOM, artillery_test: validArtilleryJson}};
req = { body: { type: consts.TEST_TYPE_CUSTOM, artillery_test: validArtilleryJson } };
await artilleryValidator.verifyArtillery(req, res, nextStub);
should(nextStub.calledOnce).eql(true);
});

it('The artillery json is not a valid json', async () => {
let invalidArtilleryJson = {};

req = {body: {type: consts.TEST_TYPE_CUSTOM, artillery_test: invalidArtilleryJson}};
req = { body: { type: consts.TEST_TYPE_CUSTOM, artillery_test: invalidArtilleryJson } };
await artilleryValidator.verifyArtillery(req, res, nextStub);
should(resJsonStub.args[0][0]).eql({message: 'The artillery json is not valid. Errors: Required property \'scenarios\' is missing'});
should(resStatusStub.args[0][0]).eql(400);
should(nextStub.calledOnce).eql(false);
should(nextStub.args[0][0].message).eql('The artillery json is not valid. Errors: Required property \'scenarios\' is missing');
should(nextStub.args[0][0].statusCode).eql(400);
});

it('The request is not a custom test', async () => {
req = {body: {type: consts.TEST_TYPE_PAYMENTSOS, scenarios: {}}};
req = { body: { type: consts.TEST_TYPE_PAYMENTSOS, scenarios: {} } };
await artilleryValidator.verifyArtillery(req, res, nextStub);
should(nextStub.calledOnce).eql(true);
});
Expand Down
49 changes: 30 additions & 19 deletions tests/unit-tests/tests/models/sequelizeConnector-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ describe('Testing sequelize connector', function () {

describe('insertTest', function () {
it('when succeed insert test', async function () {
await sequelizeConnector.insertTest({ name: 'name', type: 'type', scenarios: { s: '1' } }, { name: 'name', type: 'type', scenarios: { s: '1' } }, 'id', 'revisionId');
await sequelizeConnector.insertTest({ name: 'name', description: 'desc', type: 'type', scenarios: { s: '1' } }, { name: 'name', description: 'desc', type: 'type', scenarios: { s: '1' } }, 'id', 'revisionId');
const client = sequelizeConnector.__get__('client');
should(client.model.args).eql([['test']]);
should(createStub.args).eql([
[
{
'artillery_json': '{"name":"name","type":"type","scenarios":{"s":"1"}}',
'artillery_json': '{"name":"name","description":"desc","type":"type","scenarios":{"s":"1"}}',
'name': 'name',
'description': 'desc',
'raw_data': '{"s":"1"}',
'revision_id': 'revisionId',
'test_id': 'id',
Expand All @@ -80,8 +81,8 @@ describe('Testing sequelize connector', function () {
describe('getTest', function () {
it('when succeed getTest', async function () {
findAllStub.returns([
{ dataValues: { artillery_json: JSON.stringify({ art: '1' }), test_id: 'test_id1' } },
{ dataValues: { artillery_json: JSON.stringify({ art: '2' }), test_id: 'test_id2' } }
{ dataValues: { artillery_json: JSON.stringify({ art: '1' }), raw_data: JSON.stringify({ raw: '1' }), test_id: 'test_id1' } },
{ dataValues: { artillery_json: JSON.stringify({ art: '2' }), raw_data: JSON.stringify({ raw: '1' }), test_id: 'test_id2' } }
]);
const result = await sequelizeConnector.getTest('id');
const client = sequelizeConnector.__get__('client');
Expand Down Expand Up @@ -110,7 +111,9 @@ describe('Testing sequelize connector', function () {
'art': '1'
},
'id': 'test_id1',
'test_id': 'test_id1'
'raw_data': {
'raw': '1'
}
});
});
it('when getTest not found - should return undefined', async function () {
Expand Down Expand Up @@ -174,8 +177,8 @@ describe('Testing sequelize connector', function () {
describe('get all test revisions', function () {
it('when succeed get all revisions', async function () {
findAllStub.returns([
{ dataValues: { artillery_json: JSON.stringify({ art: '1' }), test_id: 'test_id1' } },
{ dataValues: { artillery_json: JSON.stringify({ art: '2' }), test_id: 'test_id1' } }
{ dataValues: { artillery_json: JSON.stringify({ art: '1' }), raw_data: JSON.stringify({ raw: '1' }), test_id: 'test_id1' } },
{ dataValues: { artillery_json: JSON.stringify({ art: '2' }), raw_data: JSON.stringify({ raw: '2' }), test_id: 'test_id1' } }
]);
const result = await sequelizeConnector.getAllTestRevisions('id');
const client = sequelizeConnector.__get__('client');
Expand All @@ -186,11 +189,11 @@ describe('Testing sequelize connector', function () {
'order': [
[
'updated_at',
'DESC'
'ASC'
],
[
'id',
'DESC'
'ASC'
]
],
'where': {
Expand All @@ -205,14 +208,18 @@ describe('Testing sequelize connector', function () {
'art': '1'
},
'id': 'test_id1',
'test_id': 'test_id1'
'raw_data': {
'raw': '1'
}
},
{
'artillery_json': {
'art': '2'
},
'id': 'test_id1',
'test_id': 'test_id1'
'raw_data': {
'raw': '2'
}
}
]);
});
Expand All @@ -227,11 +234,11 @@ describe('Testing sequelize connector', function () {
'order': [
[
'updated_at',
'DESC'
'ASC'
],
[
'id',
'DESC'
'ASC'
]
],
'where': {
Expand All @@ -257,11 +264,11 @@ describe('Testing sequelize connector', function () {
'order': [
[
'updated_at',
'DESC'
'ASC'
],
[
'id',
'DESC'
'ASC'
]
],
'where': {
Expand All @@ -277,8 +284,8 @@ describe('Testing sequelize connector', function () {
describe('getTests', function () {
it('when succeed getTests', async function () {
findAllStub.returns([
{ dataValues: { artillery_json: JSON.stringify({ art: '1' }), test_id: 'test_id1' } },
{ dataValues: { artillery_json: JSON.stringify({ art: '2' }), test_id: 'test_id2' } }
{ dataValues: { artillery_json: JSON.stringify({ art: '1' }), raw_data: JSON.stringify({ raw: '1' }), test_id: 'test_id1' } },
{ dataValues: { artillery_json: JSON.stringify({ art: '2' }), raw_data: JSON.stringify({ raw: '2' }), test_id: 'test_id2' } }
]);
const result = await sequelizeConnector.getTests();
const client = sequelizeConnector.__get__('client');
Expand All @@ -305,14 +312,18 @@ describe('Testing sequelize connector', function () {
'art': '1'
},
'id': 'test_id1',
'test_id': 'test_id1'
'raw_data': {
'raw': '1'
}
},
{
'artillery_json': {
'art': '2'
},
'id': 'test_id2',
'test_id': 'test_id2'
'raw_data': {
'raw': '2'
}
}
]);
});
Expand Down

0 comments on commit 3930330

Please sign in to comment.