Commit a251107c by Tomas Dvorak

tests added for excludeSubkeys and arrayEscape

parent 7dc3c73c
import assert from 'assert';
import Tester from './utils/Tester.js';
const test = new Tester('test', 'users');
const sampleData = [
{name:"Walter", someNestedObject:{a:{b:{c:{d:{e:1}}}}}, otherNestedObject:{a:{b:{c:{d:{e:1}}}}}}
];
describe('Exclude subkeys', () => {
beforeEach(() => test.init(sampleData));
afterEach(() => test.cleanUp());
it('should exclude some subkeys', async () => {
const results = await test.runJsonAnalysis({collection:'users',excludeSubkeys:['someNestedObject.a.b']}, true);
results.validateResultsCount(11);
results.validate('_id', 1, 100.0, {ObjectId: 1});
results.validate('name', 1, 100.0, {String: 1});
results.validate('someNestedObject', 1, 100.0, {Object: 1});
results.validate('someNestedObject.a', 1, 100.0, {Object: 1});
results.validate('someNestedObject.a.b', 1, 100.0, {Object: 1});
// no more descendants of someNestedObject.a.b, they are excluded
results.validate('otherNestedObject', 1, 100.0, {Object: 1});
results.validate('otherNestedObject.a', 1, 100.0, {Object: 1});
results.validate('otherNestedObject.a.b', 1, 100.0, {Object: 1});
results.validate('otherNestedObject.a.b.c', 1, 100.0, {Object: 1});
results.validate('otherNestedObject.a.b.c.d', 1, 100.0, {Object: 1});
results.validate('otherNestedObject.a.b.c.d.e', 1, 100.0, {Number: 1});
});
it('should exclude some subkeys excluding root', async () => {
const results = await test.runJsonAnalysis({collection:'users',excludeSubkeys:['someNestedObject']}, true);
results.validateResultsCount(9);
results.validate('_id', 1, 100.0, {ObjectId: 1});
results.validate('name', 1, 100.0, {String: 1});
results.validate('someNestedObject', 1, 100.0, {Object: 1});
// no more descendants of someNestedObject, they are excluded
results.validate('otherNestedObject', 1, 100.0, {Object: 1});
results.validate('otherNestedObject.a', 1, 100.0, {Object: 1});
results.validate('otherNestedObject.a.b', 1, 100.0, {Object: 1});
results.validate('otherNestedObject.a.b.c', 1, 100.0, {Object: 1});
results.validate('otherNestedObject.a.b.c.d', 1, 100.0, {Object: 1});
results.validate('otherNestedObject.a.b.c.d.e', 1, 100.0, {Number: 1});
});
});
......@@ -2,7 +2,7 @@ import Tester from './utils/Tester.js';
const test = new Tester('test', 'users');
const sampleData = [
{title:'Article 1', comments:[{author:'John', body:'it works', visible:true }]},
{title:'Article 1', comments:[{author:'John', body:'it works', visible:true, '123key': '123value' }]},
{title:'Article 2', comments:[{author:'Tom', body:'thanks'}, {author:'Mark', body:1}]}
];
......@@ -16,8 +16,7 @@ describe('Unnamed object analysis', () => {
it('should handle keys of unnamed object', async () => {
const results = await test.runJsonAnalysis({collection:'users'}, true);
results.validateResultsCount(6);
results.validateResultsCount(7);
results.validate('_id', 2, 100.0, {ObjectId: 2});
results.validate('title', 2, 100.0, {String: 2});
results.validate('comments', 2, 100.0, {Array: 2});
......@@ -26,5 +25,16 @@ describe('Unnamed object analysis', () => {
results.validate('comments.XX.author', 2, 100.0, {String: 2});
results.validate('comments.XX.body', 2, 100.0, {String: 2, Number:1});
results.validate('comments.XX.visible', 1, 50.0, {Boolean: 1});
results.validate('comments.XX.123key', 1, 50.0, {String: 1});
});
it('should use different array escape key', async () => {
const results = await test.runJsonAnalysis({collection:'users', arrayEscape:'YY'}, true);
results.validateResultsCount(7);
// unnamed objects are prefixed with .YY key
results.validate('comments.YY.author', 2, 100.0, {String: 2});
results.validate('comments.YY.body', 2, 100.0, {String: 2, Number:1});
results.validate('comments.YY.visible', 1, 50.0, {Boolean: 1});
results.validate('comments.YY.123key', 1, 50.0, {String: 1});
});
});
......@@ -10,7 +10,7 @@ export default class JsonValidator {
validate(key, totalOccurrences, percentContaining, types) {
const row = this.results.filter(item => item._id.key === key)[0];
if(typeof row === 'undefined') {
throw new Error(`Key '${key}' is not present in results`);
throw new Error(`Key '${key}' not present in results. Known keys are: [${this.results.map(item => item._id.key).join(',')}].`);
}
equal(row.totalOccurrences, totalOccurrences, `TotalOccurrences of key ${key} does not match`);
equal(row.percentContaining, percentContaining, `PercentContaining of key ${key} does not match`);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment