issue #7155 - fre is slow. first pass at using temp table to (1) reduce the amount...
[xtuple] / test / database / financialreport.js
1 /*jshint trailing:true, white:true, indent:2, strict:true, curly:true,
2   immed:true, eqeqeq:true, forin:true, latedef:true,
3   newcap:true, noarg:true, undef:true */
4 /*global XT:true, describe:true, it:true, require:true, __dirname:true, before:true */
5
6 var _ = require("underscore"),
7   assert = require('chai').assert,
8   path = require('path');
9
10 (function () {
11   "use strict";
12
13   describe('The financialReport function', function () {
14
15     var loginData = require(path.join(__dirname, "../lib/login_data.js")).data,
16       datasource = require('../../../xtuple/node-datasource/lib/ext/datasource').dataSource,
17       config = require(path.join(__dirname, "../../node-datasource/config.js")),
18       creds = _.extend({}, config.databaseServer, {database: loginData.org}),
19       oldval    = -98.76,       // makes it easy to spot in test failures
20       changeval = 123.45,
21       freSql = "select financialreport(flhead_id, period_id, 'M', -1) as fr"
22              + "  from flhead, period"
23              + " where flhead_name = 'Basic Balance Sheet'"
24              + "   and current_date between period_start and period_end;",
25       valueSql = " select cast(sum(flrpt_ending) as text) as value"
26                + "   from flrpt"
27                + "   join accnt on flrpt_accnt_id = accnt_id"
28                + " where accnt_descrip = 'Cash at eBank'"
29                + ";";
30       ;
31     this.timeout(10*1000);      // the fre ain't speedy
32
33     it("should generate financial report data", function (done) {
34       datasource.query(freSql, creds, function (err, res) {
35         assert.equal(res.rowCount, 1);
36         assert.isTrue(res.rows[0].fr);
37         done();
38       });
39     });
40
41     it("should get the starting net asset value", function (done) {
42       datasource.query(valueSql, creds, function (err, res) {
43         assert.equal(res.rowCount, 1);
44         oldval = Number(res.rows[0].value);
45         done();
46       });
47     });
48
49     it("should insert a gl transaction", function (done) {
50       var sql = "select insertgltransaction('G/L', 'ST', 'FREtest',"
51               + "  'testing financialreport()', cr.accnt_id, dr.accnt_id,"
52               + "  -1, " + changeval + ", period_end) AS result"
53               + "  from accnt cr, accnt dr, period"
54               + " where cr.accnt_descrip = 'Cash at eBank'"
55               + "   and dr.accnt_descrip = 'Deferred Revenue'"
56               + "   and current_date between period_start and period_end;"
57               + valueSql;
58       datasource.query(sql, creds, function (err, res) {
59         assert.equal(res.rowCount, 1);
60         assert.isTrue(res.rows[0].result > 0);
61         done();
62       });
63     });
64
65     it("should regenerate financial report data", function (done) {
66       datasource.query(freSql, creds, function (err, res) {
67         assert.equal(res.rowCount, 1);
68         assert.isTrue(res.rows[0].fr);
69         done();
70       });
71     });
72
73     it("should show a net asset value change", function (done) {
74       datasource.query(valueSql, creds, function (err, res) {
75         assert.equal(res.rowCount, 1);
76         var newval = Number(res.rows[0].value);
77         assert.closeTo(oldval - newval, changeval, 0.001);
78         done();
79       });
80     });
81
82     after(function (done) {
83       done();   // is there anything to clean up?
84     });
85
86   });
87 }());
88
89
90