b80e286bc100678c0507b1d5297ab00f11d19951
[xtuple] / test / database / gotchas.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, after:true */
5
6 var _ = require("underscore"),
7   assert = require('chai').assert,
8   datasource = require('../../node-datasource/lib/ext/datasource').dataSource,
9   path = require('path');
10
11 (function () {
12   "use strict";
13   describe('The database', function () {
14     this.timeout(10 * 1000);
15
16     var loginData = require(path.join(__dirname, "../lib/login_data.js")).data,
17       datasource = require('../../../xtuple/node-datasource/lib/ext/datasource').dataSource,
18       config = require(path.join(__dirname, "../../node-datasource/config.js")),
19       creds = config.databaseServer,
20       databaseName = loginData.org;
21
22     it('must not have any overriden tables or views', function (done) {
23       var sql = "select pub.relname " +
24         "from pg_class pub " +
25         "inner join pg_namespace pubns on pub.relnamespace = pubns.oid and pubns.nspname = 'public' " +
26         "inner join pg_class xt on pub.relname = xt.relname " +
27         "inner join pg_namespace xtns on xt.relnamespace = xtns.oid and xtns.nspname = 'xt' " +
28         "where pub.relkind NOT IN ('i', 'c'); ";
29
30       creds.database = databaseName;
31       datasource.query(sql, creds, function (err, res) {
32         assert.isNull(err);
33         assert.equal(1, res.rowCount, JSON.stringify(res.rows));
34         // TODO: we should eliminate this override as well, either by renaming and migrating
35         // the xt table, or by removing the public table if (as we suspect) it is not used.
36         assert.equal(res.rows[0].relname, "potype");
37         done();
38       });
39     });
40
41     it('must only override a few whitelisted functions', function (done) {
42       var sql = "select pub.proname " +
43         "from pg_proc pub " +
44         "inner join pg_namespace pubns on pub.pronamespace = pubns.oid and pubns.nspname = 'public' " +
45         "inner join pg_proc xt on pub.proname = xt.proname " +
46         "inner join pg_namespace xtns on xt.pronamespace = xtns.oid and xtns.nspname = 'xt'; ";
47
48       creds.database = databaseName;
49       datasource.query(sql, creds, function (err, res) {
50         var overriddenFunctions = _.map(res.rows, function (row) {
51             return row.proname;
52           }),
53           whitelist = ["cntctmerge", "cntctrestore", "createuser",
54             "mergecrmaccts", "trylock", "undomerge"],
55           illegalFunctions = _.difference(overriddenFunctions, whitelist);
56
57         assert.isNull(err);
58         assert.equal(illegalFunctions.length, 0, JSON.stringify(illegalFunctions));
59         done();
60       });
61     });
62
63   });
64 }());
65
66