Skip to content
This repository has been archived by the owner on Oct 1, 2021. It is now read-only.

Commit

Permalink
Added addtional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
al66 committed May 21, 2017
1 parent b1781ad commit 98bedca
Show file tree
Hide file tree
Showing 2 changed files with 228 additions and 94 deletions.
8 changes: 6 additions & 2 deletions lib/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ class Table {
let res = data;
let output = {};

Object.keys(this._table.output).forEach(function(key) {res[key] = null});
if (this._table.output) {
Object.keys(this._table.output).forEach(function(key) {res[key] = null});
}

let checkCond = function(cond) {
//console.log('check value %s = %s against cond:', cond[0], data[cond[0]], cond);
Expand All @@ -76,7 +78,9 @@ class Table {
if (rule.match.every(checkCond)) {
//res['z'] = 'Found ' + rule.desc;
//console.log('Found:', rule);
Object.keys(self._table.output).forEach(function(key) {res[key] = rule.output[key]});
if (self._table.output) {
Object.keys(self._table.output).forEach(function(key) {res[key] = rule.output[key]});
};
return true;
}
}
Expand Down
314 changes: 222 additions & 92 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,49 @@
var expect = require('chai').expect;
var Table = require('../index').Table;

var testTable = {
description: 'Simple table',
hitPolicy: 'Unique',
input: {
'x': {
description: 'Input x',
type: 'number'
},
'y': {
description: 'Input y',
type: 'number',
default: 2
}
},
output: {
'z': {
description: 'Output z',
type: 'number'
}
},
rules: {
'First rule': {
'x': '< 5, [20..30]',
'y': '> 6,[30..40]',
'z': '7'
},
'Second rule': {
'x': '> 5',
'z': '2'
}
}
};

var testData = [
{ 'x': '3', 'y':'2', 'z':null},
{ 'x': '3', 'y':'7', 'z':'7'},
{ 'x': '6', 'y':'7', 'z':'2'},
{ 'x': '20', 'y':'37', 'z':'7'},
{ 'x': '6', 'z':'2'}
]

describe('Compile table', function() {
let testTable = {
description: 'Simple table',
hitPolicy: 'Unique',
input: {
'x': {
description: 'Input x',
type: 'number'
},
'y': {
description: 'Input y',
type: 'number',
default: 2
}
},
output: {
'z': {
description: 'Output z',
type: 'number'
}
},
rules: {
'First rule': {
'x': '< 5, [20..30]',
'y': '> 6,[30..40]',
'z': '7'
},
'Second rule': {
'x': '> 5',
'z': '2'
}
}
};
describe('Init class', function() {
it('Init without table', function(done){
let table = new Table();
//expect(table).to.be.equals(expected);
done();
});
it('Init table', function(done){
let table = new Table(testTable);
expect(table.table.rules).to.be.equals(testTable.rules);
done();
});
it('Call compile', function(done){
Expand All @@ -62,6 +59,46 @@ describe('Compile table', function() {
});

describe('Test table 1', function() {
let testTable = {
description: 'Simple table',
hitPolicy: 'Unique',
input: {
'x': {
description: 'Input x',
type: 'number'
},
'y': {
description: 'Input y',
type: 'number',
default: 2
}
},
output: {
'z': {
description: 'Output z',
type: 'number'
}
},
rules: {
'First rule': {
'x': '< 5, [20..30]',
'y': '> 6,[30..40]',
'z': '7'
},
'Second rule': {
'x': '> 5',
'z': '2'
}
}
};
let testData = [
{ 'x': '3', 'y':'2', 'z':null},
{ 'x': '3', 'y':'7', 'z':'7'},
{ 'x': '6', 'y':'7', 'z':'2'},
{ 'x': '20', 'y':'37', 'z':'7'},
{ 'x': '6', 'z':'2'}
];

let table = new Table();
table.compile(testTable);
for (let i=0; i< testData.length; i++) {
Expand All @@ -73,62 +110,62 @@ describe('Test table 1', function() {
}
});

testTable = {
description: 'Applicant Risk rating',
hitPolicy: 'Unique',
input: {
'age': {
description: 'Applicant Age',
type: 'number'
},
'hist': {
description: 'Medical History',
type: 'string'
}
},
output: {
'risk': {
description: 'Risk Rating',
type: 'string'
}
},
rules: {
'older 60 / good': {
'age': '>60',
'hist': 'good',
'risk': 'Medium'
},
'older 60 / bad': {
'age': '>60',
'hist': 'bad',
'risk': 'High'
},
'mid age': {
'age': '[25..60]',
'risk': 'Medium'

describe('Test table 2', function() {
let testTable = {
description: 'Applicant Risk rating',
hitPolicy: 'Unique',
input: {
'age': {
description: 'Applicant Age',
type: 'number'
},
'hist': {
description: 'Medical History',
type: 'string'
}
},
'kids / good': {
'age': '<25',
'hist': 'good',
'risk': 'Low'
output: {
'risk': {
description: 'Risk Rating',
type: 'string'
}
},
'kids / bad)': {
'age': '<25',
'hist': 'bad',
'risk': 'Medium'
rules: {
'older 60 / good': {
'age': '>60',
'hist': 'good',
'risk': 'Medium'
},
'older 60 / bad': {
'age': '>60',
'hist': 'bad',
'risk': 'High'
},
'mid age': {
'age': '[25..60]',
'risk': 'Medium'
},
'kids / good': {
'age': '<25',
'hist': 'good',
'risk': 'Low'
},
'kids / bad)': {
'age': '<25',
'hist': 'bad',
'risk': 'Medium'
}
}
}
};

testData = [
{ 'age': '65', 'hist':'good', 'risk':'Medium'},
{ 'age': '70', 'hist':'bad', 'risk':'High'},
{ 'age': '26', 'hist':'good', 'risk':'Medium'},
{ 'age': '20', 'hist':'bad', 'risk':'Medium'},
{ 'age': '10', 'hist':'good', 'risk':'Low'}
];
};
let testData = [
{ 'age': '65', 'hist':'good', 'risk':'Medium'},
{ 'age': '70', 'hist':'bad', 'risk':'High'},
{ 'age': '26', 'hist':'good', 'risk':'Medium'},
{ 'age': '20', 'hist':'bad', 'risk':'Medium'},
{ 'age': '10', 'hist':'good', 'risk':'Low'}
];

describe('Test table 2', function() {
let table = new Table();
table.compile(testTable);
for (let i=0; i< testData.length; i++) {
Expand All @@ -139,3 +176,96 @@ describe('Test table 2', function() {
});
}
});


describe('Incomplete Table', function() {
let testTable = {
description: 'Simple table',
hitPolicy: 'Unique',
input: {
'x': {
description: 'Input x',
type: 'number'
},
'y': {
description: 'Input y',
type: 'number',
default: 2
}
},
output: {
'z': {
description: 'Output z',
type: 'number'
}
},
rules: {
'First rule': {
'x': '< 5, [20..30]',
'y': '> 6,[30..40]',
'z': '7'
},
'Second rule': {
'x': '> 5',
'z': '2'
}
}
};
it('Missing input', function(done) {
let testData = [
{ 'x': '3', 'y':'2', 'z':null},
{ 'x': '3', 'y':'7', 'z':null},
{ 'x': '6', 'y':'7', 'z':null},
{ 'x': '20', 'y':'37', 'z':null},
{ 'x': '6', 'z':null}
];
let t = testTable;
let d = testData;
t.input = null;
let table = new Table();
table.compile(t);
for (let i=0; i< d.length; i++) {
let result = table.result(d[i]);
expect(result.z).to.be.equals(d[i].z);
}
done();
});
it('Missing output', function(done) {
let testData = [
{ 'x': '3', 'y':'2'},
{ 'x': '3', 'y':'7'},
{ 'x': '6', 'y':'7'},
{ 'x': '20', 'y':'37'},
{ 'x': '6'}
];
let t = testTable;
let d = testData;
t.output = null;
let table = new Table();
table.compile(t);
for (let i=0; i< d.length; i++) {
let result = table.result(d[i]);
expect(result.z).to.be.undefined;
}
done();
});
it('Missing rules', function(done) {
let testData = [
{ 'x': '3', 'y':'2'},
{ 'x': '3', 'y':'7'},
{ 'x': '6', 'y':'7'},
{ 'x': '20', 'y':'37'},
{ 'x': '6'}
];
let t = testTable;
let d = testData;
t.rules = null;
let table = new Table();
table.compile(t);
for (let i=0; i< d.length; i++) {
let result = table.result(d[i]);
expect(result.z).to.be.undefined;
}
done();
});
});

0 comments on commit 98bedca

Please sign in to comment.