Skip to content

Commit

Permalink
Improve unit test coverage % (#75)
Browse files Browse the repository at this point in the history
* Adding more edge scanarios test cases

* more edge case test scenarios

* some more fixes

* more checks towards greener unit tests

* cleanup

* small fix
  • Loading branch information
radomir-sebek authored and schrink committed Apr 23, 2018
1 parent f923de3 commit b9a2923
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 29 deletions.
18 changes: 4 additions & 14 deletions modules/Database/Arangojs.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,11 @@ class ArangoJS {
*/
runQuery(queryString, params) {
return new Promise((resolve, reject) => {
try {
this.db.query(queryString, params).then((cursor) => {
resolve(cursor.all());
}).catch((err) => {
console.log(err);
reject(err);
});
} catch (err) {
console.log(err);
this.db.query(queryString, params).then((cursor) => {
resolve(cursor.all());
}).catch((err) => {
reject(err);
}
});
});
}

Expand Down Expand Up @@ -108,7 +102,6 @@ class ArangoJS {
reject(err);
},
).catch((err) => {
console.log(err);
reject(err);
});
});
Expand All @@ -131,7 +124,6 @@ class ArangoJS {
reject(err);
},
).catch((err) => {
console.log(err);
reject(err);
});
});
Expand Down Expand Up @@ -166,7 +158,6 @@ class ArangoJS {
}
},
).catch((err) => {
console.log(err);
reject(err);
});
});
Expand All @@ -188,7 +179,6 @@ class ArangoJS {
}
},
).catch((err) => {
console.log(err);
reject(err);
});
});
Expand Down
48 changes: 42 additions & 6 deletions test/modules/arangojs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const vertexOne = databaseData.vertices[0];
const vertexTwo = databaseData.vertices[1];
const edgeOne = databaseData.edges[0];
const newImportValue = 2520345631;
const oneMoreImportValue = 2520345639;

let systemDb;
let testDb;
Expand All @@ -33,18 +34,13 @@ describe('Arangojs module ', async () => {
testDb = new ArangoJs(myUserName, myPassword, myDatabaseName, '127.0.0.1', '8529');
});

after('drop testDb db', async () => {
systemDb = new Database();
systemDb.useBasicAuth('root', 'root');
await systemDb.dropDatabase(myDatabaseName);
});

it('.identify() should return correct name', () => {
assert(testDb.identify(), 'ArangoJS');
});

it('should see one system and one custom database', async () => {
expect(testDb.db.name).to.be.equal('testDb');
expect(systemDb).to.be.an.instanceof(Database);
const listOfDatabases = await testDb.db.listDatabases();
assert.equal(listOfDatabases[0], '_system');
assert.equal(listOfDatabases[1], 'testDb');
Expand Down Expand Up @@ -97,6 +93,14 @@ describe('Arangojs module ', async () => {
assert.equal(info.length, 2);
});

it('.createEdgeCollection() with system collection name should be illegal', async () => {
try {
await testDb.createCollection('_statistics');
} catch (e) {
assert.isTrue(e.toString().indexOf('ArangoError: illegal name') >= 0);
}
});

it('.createEdgeCollection() with null as collection name', async () => {
try {
await testDb.createEdgeCollection(null);
Expand Down Expand Up @@ -195,6 +199,32 @@ describe('Arangojs module ', async () => {
});
});

it('updateDocument() should also add/append data', async () => {
const updatetedEdgeOne = {
_key: '6eb743d84a605b2ab6be67a373b883d4',
edge_type: 'OWNED_BY',
data_provider: 'WALLET_ID',
imports: [1520345631, 1234567890],
_from: 'ot_vertices/2e0b1ba163be76138d51a0b8258e97d7',
_to: 'ot_vertices/cd923bec4266a7f63b68722da254f205',
};

try {
testDb.updateDocument(
edgeCollectionName,
// eslint-disable-next-line no-underscore-dangle
updatetedEdgeOne,
);
} catch (error) {
console.log(error);
}

// check value of new imports
await testDb.getDocument(edgeCollectionName, edgeOne._key).then((response) => {
assert.isTrue(response.imports.length === 2);
});
});

it('getVerticesByImportId() ', async () => {
await testDb.getVerticesByImportId(vertexOne.imports[0]).then((response) => {
assert.deepEqual(response[0].data, vertexOne.data);
Expand All @@ -216,5 +246,11 @@ describe('Arangojs module ', async () => {
assert.deepEqual(response[0].data_provider, vertexOne.data_provider);
});
});

after('drop testDb db', async () => {
systemDb = new Database();
systemDb.useBasicAuth('root', 'root');
await systemDb.dropDatabase(myDatabaseName);
});
});

11 changes: 6 additions & 5 deletions test/modules/challenge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ const assert = require('assert').strict;
const Challenge = require('../../modules/Challenge');
const SystemStorage = require('../../modules/Database/SystemStorage');

// TODO: Rewrite this for Mocha tests.


// Global declarations.
const vertexData = [
{ vertexKey: 'vertex0', data: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt' },
Expand Down Expand Up @@ -114,6 +111,8 @@ function testGenerateTests() {
Challenge.generateTests(dataCreatorId, importId, 0, startTime, endTime, 32, vertexData);
};
assert.throws(testFunc, 'Zero tests asked. Should crash!');

// Negative amount of challenges to generate.
testFunc = function testFunc() {
Challenge.generateTests(dataCreatorId, importId, -1, startTime, endTime, 32, vertexData);
};
Expand All @@ -124,6 +123,8 @@ function testGenerateTests() {
Challenge.generateTests(dataCreatorId, importId, 10, startTime, endTime, 0, vertexData);
};
assert.throws(testFunc, 'Zero block size asked. Should crash!');

// Negative block size.
testFunc = function testFunc() {
Challenge.generateTests(dataCreatorId, importId, 10, startTime, endTime, -1, vertexData);
};
Expand All @@ -139,7 +140,7 @@ describe('Challenge tests', () => {
];

blockTests.forEach((test) => {
it(`should correctly generate blocks of ${test.args[1]} size`, () => {
it(`should correctly generate blocks of ${test.args[1]} bytes`, () => {
const blocks = Challenge.__getBlocks__(test.args[0], test.args[1]);
checkBlocks(blocks, test.args[0]);
});
Expand Down Expand Up @@ -170,7 +171,7 @@ describe('Challenge tests', () => {
];

challengeTests.forEach((test) => {
it(`should correctly generate ${test.args[2]} challenges of ${test.args[5]} size`, () => {
it(`should correctly generate ${test.args[2]} challenges of ${test.args[5]} bytes`, () => {
const tests = Challenge.generateTests(...test.args);
checkForTests(
tests, test.args[3], test.args[4],
Expand Down
108 changes: 106 additions & 2 deletions test/modules/graph.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,60 @@
const {
describe, it, afterEach, beforeEach,
describe, it, afterEach, beforeEach, before, after,
} = require('mocha');
const { assert } = require('chai');
const sinon = require('sinon');
const Graph = require('../../modules/Graph');
const GraphInstance = require('../../modules/GraphInstance');
var models = require('../../models');
const Encryption = require('../../modules/Encryption');
const SystemStorage = require('../../modules/Database/SystemStorage');
const Storage = require('../../modules/Storage');
const Utilities = require('../../modules/Utilities');
// eslint-disable-next-line prefer-destructuring
const Database = require('arangojs').Database;
const GraphStorage = require('../../modules/Database/GraphStorage');
const GSInstance = require('../../modules/GraphStorageInstance');
const databaseData = require('./test_data/database-data.js');
const deasync = require('deasync-promise');

const myUserName = 'otuser';
const myPassword = 'otpass';
const myDatabaseName = 'test_graph';

const deasync = require('deasync-promise');

let selectedDatabase;
let systemDb;

describe('graph module ', () => {
before('loadSelectedDatabaseInfo() and init GraphStorage', async () => {
Storage.models = deasync(models.sequelize.sync()).models;
selectedDatabase = await Utilities.loadSelectedDatabaseInfo();
assert.hasAllKeys(selectedDatabase, ['id', 'database_system', 'username', 'password',
'host', 'port', 'max_path_length', 'database']);

systemDb = new Database();
systemDb.useBasicAuth('root', 'root');
await systemDb.createDatabase(
myDatabaseName,
[{ username: myUserName, passwd: myPassword, active: true }],
);
selectedDatabase.database = myDatabaseName;

GSInstance.db = new GraphStorage(selectedDatabase);
GraphInstance.g = new Graph();
});

after('drop myDatabaseName db', async () => {
systemDb = new Database();
systemDb.useBasicAuth('root', 'root');
await systemDb.dropDatabase(myDatabaseName);
});

// TODO reenable with fix of .skipped tests
// beforeEach('create stubs', async () => {
// this.encrytionMock = sinon.sandbox.mock(Encryption);
// });

// TODO reenable with fix of .skipped tests
// afterEach('restore stubs', async () => {
// this.encrytionMock.restore();
Expand Down Expand Up @@ -323,6 +363,7 @@ describe('graph module ', () => {
assert.isNotNull(encryptedData);
assert.equal(encryptedData, encryptedVertex.data);
});

it('decryptVertices() of encryptVertices() should give back original data', async () => {
const vertexData = 1;

Expand Down Expand Up @@ -365,4 +406,67 @@ describe('graph module ', () => {
assert.isNotNull(encryptedData);
assert.equal(encryptedData, encryptedVertex.data);
});

it('.findVertices() with empty query should fail', async () => {
try {
await GraphInstance.g.findVertices();
} catch (error) {
// Utilities.isEmptyObject() will complain
assert.isTrue(error.toString().indexOf('Cannot convert undefined or null to object') >= 0);
}
});

it('.findVertices() when still not connected to graph db should fail', async () => {
const queryObject = {
uid: '123',
vertex_type: 'BATCH',
};
try {
const result = await GraphInstance.g.findVertices(queryObject);
} catch (error) {
assert.isTrue(error.toString().indexOf('Error: Not connected to graph database') >= 0);
}
});

it('.findVertices() on top of empty collection should find nothing', async () => {
await GSInstance.db.connect();
await GSInstance.db.createCollection('ot_vertices').then((response) => {
assert.equal(response, 'Collection created');
});
const queryObject = {
uid: '123',
vertex_type: 'BATCH',
};
await GraphInstance.g.findVertices(queryObject).then((response) => {
assert.isEmpty(response);
assert.isTrue(typeof (response) === 'object');
});
});

it('.findTraversalPath() with non valid startVertex should fail', async () => {
// db alredy connected and ot_vertices exists
const myStartVertex = {
_id: undefined,
};
try {
const response = await GraphInstance.g.findTraversalPath(myStartVertex);
assert.isEmpty(response);
assert.isTrue(typeof (response) === 'object');
} catch (error) {
console.log(error);
}
});

it('.findTraversalPath() with non existing startVertex should fail', async () => {
// db alredy connected and ot_vertices exists
const myStartVertex = {
_id: 0,
};

try {
const response = await GraphInstance.g.findTraversalPath(myStartVertex);
} catch (error) {
assert.equal(error.code, 404);
}
});
});
Loading

0 comments on commit b9a2923

Please sign in to comment.