docparse-supplier-nge
Version:
process ngrid electric utility bill data for use in the docparse system
217 lines (196 loc) • 7.44 kB
JavaScript
var ce = require('cloneextend');
var mongoose = require('mongoose');
var fs = require('fs');
var should = require('should');
var assert = require('assert');
var path = require('path');
var inspect = require('eyespect').inspector();
var scraperProcessData = require('../scraperProcessData');
// Utility
var getCostDelivery = require('../lib/parse/getCostDelivery');
var getUse = require('../lib/parse/getUse');
var getCostOther = require('../lib/parse/otherCosts/getCostOther');
// Supply
var getSupplyText = require('../lib/parse/getSupplyText');
var getCostSupply = require('../lib/parse/getCostSupply');
var getSupplyData = require('../lib/parse/getSupplyData');
var isCompSupply = require('../lib/parse/isCompSupply');
var getCompSupplier = require('../lib/parse/getCompSupplier');
var getCostOtherSupply = require('../lib/parse/comp/getCostOther');
// Models
var Bill = require('docparse-bill');
var Invoice = require('docparse-invoice');
var createSupplier = require('docparse-create-supplier');
var getUtilityData = require('../lib/parse/getUtilityData');
describe('Parse Bill with competitive supply data', function() {
var data, pagesText, testData;
// desired defaults
var supplyText = 'Supply Services\n SUPPLIER NextEra Energy Services MA\n LLC\n 20455 SH 249\n SUITE 200\n HOUSTON TX 77070\n PHONE 877-528-2890 ACCOUNT NO 199991\n\n\n Electricity Supply 0.07251 x 377000 kWh 27336.27\n Sales Tax 6.25 % 1708.52\n Total Supply Services $ 29044.79';
var costSupply = '27336.27';
var costOtherTotal = '7.46';
var toPaySupply = '29044.79';
var costOtherSupplyTotal = '1708.52';
before(function (done) {
mongoose.connect('mongodb://localhost/test');
Bill.remove({}, function (err) {
should.not.exist(err);
Invoice.remove({}, function (err) {
should.not.exist(err);
var filePath = path.join(__dirname, 'data', 'compSupply.json');
assert.ok(fs.existsSync(filePath), 'test NGE json file not found at path: ' + filePath);
var text = fs.readFileSync(filePath, 'utf8');
should.exist(text ,'nge text not read from file at path: ' + filePath);
data = JSON.parse(text);
pagesText = data.textPages.join(' ').replace(/,/g, '');
testData = {
text: pagesText
};
var compSupplierData = {
supplierCode: 'NEE',
supplierName: 'Next-Era Energy',
remove: true
};
createSupplier(compSupplierData, function (err, reply) {
should.not.exist(err);
should.exist(reply);
done();
});
});
});
});
after(function (done) { mongoose.disconnect(done); });
it('should get cost delivery correctly', function (done) {
getCostDelivery(pagesText, function (err, reply) {
should.not.exist(err, 'error getting cost: ' + err);
should.exist(reply);
var desired = '15454.76';
desired.should.eql(reply);
done();
});
});
it('should get use correctly', function (done) {
getUse(pagesText, function (err, reply) {
should.not.exist(err, 'error getting cost: ' + err);
should.exist(reply);
// inspect(reply, 'use reply');
var desiredQuantity = '377000';
var desiredUnits = 'kwh';
desiredQuantity.should.eql(reply.quantity);
desiredUnits.should.eql(reply.units);
done();
});
});
it('should get costOther correctly', function (done) {
getCostOther(pagesText, function (err, reply) {
should.not.exist(err, 'error getting cost: ' + err);
should.exist(reply);
// inspect(reply, 'costOther reply');
var desiredTotal = '7.46';
var desiredNumCosts = 4;
desiredTotal.should.eql(reply.total);
desiredNumCosts.should.eql(reply.costs.length);
done();
});
});
it('should get utility data correctly', function (done) {
getUtilityData(testData, function (err, reply) {
should.not.exist(err, 'error getting utility data: ' + err);
should.exist(reply);
// inspect(reply, 'utility data reply');
var desiredCostDelivery = '15454.76';
desiredCostDelivery.should.eql(reply.costDelivery);
done();
});
});
it('should get supply text correctly', function () {
var text = getSupplyText(testData.text);
supplyText.should.eql(text);
});
it('should get cost supply correctly', function (done) {
getCostSupply(supplyText, function (err, reply) {
should.not.exist(err, 'error getting cost supply: ' + err);
should.exist(reply);
costSupply.should.eql(reply);
done();
});
});
it('comp supply should be true', function () {
var isComp = isCompSupply(supplyText);
assert.ok(isComp, 'competitive supply should be true');
})
;
it('should find comp supplier in database correctly', function (done) {
getCompSupplier(supplyText, function (err, reply) {
should.not.exist(err);
should.exist(reply);
// inspect(reply.toObject(), 'get comp supplier reply');
done();
});
});
it('should comp supply cost other correctly', function (done) {
var data = {
costSupply: costSupply,
text: supplyText
};
getCostOtherSupply(data, function (err, reply) {
should.not.exist(err);
should.exist(reply);
costOtherSupplyTotal.should.eql(reply.costOther.total);
toPaySupply.should.eql(reply.costTotal);
done();
});
});
it('should get supply data correctly', function(done) {
var getData = require('../lib/parse/getData');
getData(testData, function (err, reply) {
if (err) {
inspect(err, 'error getting data');
}
should.not.exist(err, 'error parsing data: ' + err);
should.exist(reply, 'no reply returned from parsing scraper data');
reply.should.have.property('costOther');
costOtherTotal.should.eql(reply.costOther.total);
done();
});
});
it('should get supply data correctly', function(done) {
scraperProcessData(data, function (err, reply) {
if (err) {
inspect(err, 'error processing scraper data');
}
should.not.exist(err, 'error parsing data: ' + err);
should.exist(reply, 'no reply returned from parsing scraper data');
var bill = reply.toObject();
var invoiceIDs = bill.invoices;
invoiceIDs.length.should.eql(2);
var stdInvoiceID = invoiceIDs[0];
var compInvoiceID = invoiceIDs[1];
Invoice.findById(stdInvoiceID, function (err, invoiceReply) {
should.not.exist(err);
should.exist(invoiceReply);
var stdInvoice = invoiceReply.toObject();
stdInvoice.data_type.should.eql('pdf');
Invoice.findById(compInvoiceID, function (err, compInvoiceReply) {
should.not.exist(err);
should.exist(compInvoiceReply);
var compInvoice = compInvoiceReply.toObject();
compInvoice.data_type.should.eql('pdf');
var billingSupplierCode = 'NGE';
billingSupplierCode.should.eql(compInvoice.billing_supplier_code);
done();
});
});
});
});
it('should get handle duplicate bills correctl', function(done) {
scraperProcessData(data, function (err, reply) {
if (err) {
inspect(err, 'error processing scraper data');
return;
}
should.not.exist(err, 'error parsing data: ' + err);
should.exist(reply);
done();
});
});
});