docparse-check-imacros
Version:
check if a given bill already exists in the docparse database using the docparse server REST api
152 lines (146 loc) • 4.75 kB
JavaScript
var async = require('async');
var should = require('should');
var check = require('../index');
var readFile = require('imacros-read-file');
var bills = require('docparse-bills-imacros');
runTests(function (err, reply) {
if (err) {
alert('check test suite fails with error: ' + JSON.stringify(err));
return false;
}
iimDisplay('Success! Checks test suite passes');
});
function runTests(cb) {
var filePath = 'file:///users/noah/src/node/docparse/scrapers/imacros/check/test/config.json';
loadConfigFile(filePath, function (err, config) {
should.not.exist(err, 'error loading config file');
async.series([
function (cb) {
setTimeout(function () {
testNewBill(config, function (err, reply) {
if (err) { return cb(err); }
iimDisplay('test new bill check passes, testing on old bill');
cb();
}, 100);
});
},
function (cb) {
setTimeout(function () {
testExistingBillWithCache(config, cb);
}, 100);
},
function (cb) {
setTimeout(function () {
testExistingBillWithoutCache(config, cb);
}, 100);
}
], cb);
});
}
function testNewBill(config, cb) {
var supplierCode = 'NST';
var billNumber = 'fizzle';
iimDisplay('testing check download for new bill');
var data = {
supplierCode: supplierCode,
billNumber: billNumber,
config: config
};
check(data, function (err, reply) {
if (err) {
alert(err);
should.not.exist(err, 'error check for supplier code "' + supplierCode+ ' ", error: ' + err);
return cb(err);
}
if (!reply) {
return cb('check if we should download returned false when it should have returned true for a new bill');
}
cb();
});
}
function testExistingBillWithoutCache(config,cb) {
// get a list of existing bills
var supplierCode = 'NST';
var data = {
supplierCode: supplierCode,
config: config
};
iimDisplay('getting list of existing bills');
bills(data, function (err, billNumbers) {
if (err) {
alert('error getting list of existing bills: ' + err);
should.not.exist(err, 'error getting bills for supplier code "' + supplierCode+ ' ", error: ' + err);
return cb(err);
}
iimDisplay('got list of existing bills');
if (billNumbers.length === 0) {
return cb('failed to test existing bill, test runner was unabled to get a list of existing bills from the docparse api server');
}
var numBills = billNumbers.length;
var billNumber = billNumbers[0];
var checkData = {
supplierCode: supplierCode,
billNumber: billNumber,
config: config
};
check(checkData, function (err, reply) {
if (err) {
alert('error getting list of existing bills: ' + err);
should.not.exist(err, 'error getting bills for supplier code "' + supplierCode+ ' ", error: ' + err);
return cb(err);
}
should.exist(reply);
if (reply) {
return cb('check if we should download returned true when it should have returned false for an existing bill');
}
cb();
});
});
}
function testExistingBillWithCache(config,cb) {
// get a list of existing bills
var supplierCode = 'NST';
var data = {
supplierCode: supplierCode,
config: config
};
iimDisplay('getting list of existing bills');
bills(data, function (err, billNumbers) {
if (err) {
alert('error getting list of existing bills: ' + err);
should.not.exist(err, 'error getting bills for supplier code "' + supplierCode+ ' ", error: ' + err);
return cb(err);
}
iimDisplay('got list of existing bills');
if (billNumbers.length === 0) {
return cb('failed to test existing bill, test runner was unabled to get a list of existing bills from the docparse api server');
}
var numBills = billNumbers.length;
var billNumber = billNumbers[0];
var checkData = {
supplierCode: supplierCode,
billNumber: billNumber,
billNumbers: billNumbers,
config: config
};
check(checkData, function (err, reply) {
if (err) {
alert('error getting list of existing bills: ' + err);
should.not.exist(err, 'error getting bills for supplier code "' + supplierCode+ ' ", error: ' + err);
return cb(err);
}
should.exist(reply);
if (reply) {
return cb('check if we should download returned true when it should have returned false for an existing bill');
}
cb();
});
});
}
function loadConfigFile(filePath, cb) {
readFile(filePath, function (err, reply) {
if (err) { return cb(err); }
var data = JSON.parse(reply);
cb(null, data);
});
}