From 393033099930ec8cfa7815bb3fa284f99735d103 Mon Sep 17 00:00:00 2001 From: Manor Date: Mon, 4 Feb 2019 17:07:34 +0200 Subject: [PATCH] fix(fix get all revisions): fix get all revisions ix get all revisions --- src/tests/helpers/artilleryValidator.js | 4 +- .../database/sequelize/sequelizeConnector.js | 6 ++- src/tests/models/manager.js | 4 +- .../tests/helpers/artilleryValidator-test.js | 11 ++--- .../tests/models/sequelizeConnector-test.js | 49 ++++++++++++------- 5 files changed, 44 insertions(+), 30 deletions(-) diff --git a/src/tests/helpers/artilleryValidator.js b/src/tests/helpers/artilleryValidator.js index db567dbf5..a3a1cce4a 100644 --- a/src/tests/helpers/artilleryValidator.js +++ b/src/tests/helpers/artilleryValidator.js @@ -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); } } diff --git a/src/tests/models/database/sequelize/sequelizeConnector.js b/src/tests/models/database/sequelize/sequelizeConnector.js index 400bca680..c89fd9720 100644 --- a/src/tests/models/database/sequelize/sequelizeConnector.js +++ b/src/tests/models/database/sequelize/sequelizeConnector.js @@ -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), @@ -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; } @@ -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; diff --git a/src/tests/models/manager.js b/src/tests/models/manager.js index d45d968fc..0c524cc89 100644 --- a/src/tests/models/manager.js +++ b/src/tests/models/manager.js @@ -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'); @@ -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 }; } diff --git a/tests/unit-tests/tests/helpers/artilleryValidator-test.js b/tests/unit-tests/tests/helpers/artilleryValidator-test.js index a01e4e614..5514734f6 100644 --- a/tests/unit-tests/tests/helpers/artilleryValidator-test.js +++ b/tests/unit-tests/tests/helpers/artilleryValidator-test.js @@ -65,7 +65,7 @@ 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); }); @@ -73,15 +73,14 @@ describe('Artillery validator tests', function () { 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); }); diff --git a/tests/unit-tests/tests/models/sequelizeConnector-test.js b/tests/unit-tests/tests/models/sequelizeConnector-test.js index bfa0c2074..1f2793c3f 100644 --- a/tests/unit-tests/tests/models/sequelizeConnector-test.js +++ b/tests/unit-tests/tests/models/sequelizeConnector-test.js @@ -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', @@ -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'); @@ -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 () { @@ -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'); @@ -186,11 +189,11 @@ describe('Testing sequelize connector', function () { 'order': [ [ 'updated_at', - 'DESC' + 'ASC' ], [ 'id', - 'DESC' + 'ASC' ] ], 'where': { @@ -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' + } } ]); }); @@ -227,11 +234,11 @@ describe('Testing sequelize connector', function () { 'order': [ [ 'updated_at', - 'DESC' + 'ASC' ], [ 'id', - 'DESC' + 'ASC' ] ], 'where': { @@ -257,11 +264,11 @@ describe('Testing sequelize connector', function () { 'order': [ [ 'updated_at', - 'DESC' + 'ASC' ], [ 'id', - 'DESC' + 'ASC' ] ], 'where': { @@ -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'); @@ -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' + } } ]); });