Skip to content

Commit 87609cf

Browse files
committed
Add initial postgres driver tests
1 parent eb49409 commit 87609cf

File tree

3 files changed

+139
-13
lines changed

3 files changed

+139
-13
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@
4949
"async" : "*",
5050
"mocha" : "1.13.0",
5151
"should" : "1.2.2",
52-
"mongodb" : "1.3.19"
52+
"mongodb" : "1.3.19",
53+
"glob" : "3.2.8"
5354
},
5455
"optionalDependencies": {}
5556
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
var _ = require('lodash');
2+
var should = require('should');
3+
var Driver = require('../../../lib/Drivers/DML/postgres').Driver;
4+
var helper = require('../../support/spec_helper');
5+
var common = require('../../common');
6+
7+
if (common.protocol() == "mongodb") return;
8+
9+
describe("Postgres driver", function() {
10+
describe("#propertyToValue", function () {
11+
describe("type object", function () {
12+
function evaluate (input) {
13+
var driver = new Driver({}, {}, {});
14+
return driver.propertyToValue(input, { type: 'object' });
15+
}
16+
17+
it("should not change null", function () {
18+
should.strictEqual(evaluate(null), null);
19+
});
20+
21+
it("should not change buffer", function () {
22+
var b = new Buffer('abc');
23+
should.strictEqual(evaluate(b), b);
24+
});
25+
26+
it("should encode everything else as a Buffer", function () {
27+
var input = { abc: 123 };
28+
var out = evaluate(input);
29+
30+
should(out instanceof Buffer);
31+
should.equal(JSON.stringify(input), out.toString());
32+
});
33+
});
34+
35+
describe("date", function () {
36+
function evaluate (input, opts) {
37+
if (!opts) opts = {};
38+
var driver = new Driver(opts.config, {}, {});
39+
return driver.propertyToValue(input, { type: 'date' });
40+
}
41+
42+
it("should do nothing when timezone isn't configured", function () {
43+
var input = new Date();
44+
var inputStr = input.toString();
45+
var out = evaluate(input);
46+
47+
should.strictEqual(input, out);
48+
should.equal(inputStr, out.toString());
49+
});
50+
51+
// I'm not sure if the timezone tests are right
52+
it("should offset time by specified timezone amount for + timezones", function () {
53+
var input = new Date(Date.UTC(2014,2,5,13,14,0,0));
54+
var inputStr = input.toUTCString();
55+
var out = evaluate(input, { config: { timezone: '+06:11' } });
56+
57+
should.equal(out.toUTCString(), 'Wed, 05 Mar 2014 08:25:00 GMT');
58+
});
59+
60+
// The HH:mm for this one seem odd
61+
it("should offset time by specified timezone amount for + timezones", function () {
62+
var input = new Date(Date.UTC(2014,2,5,13,14,0,0));
63+
var inputStr = input.toUTCString();
64+
var out = evaluate(input, { config: { timezone: '-06:11' } });
65+
66+
should.equal(out.toUTCString(), 'Tue, 04 Mar 2014 20:03:00 GMT');
67+
});
68+
// --------------
69+
});
70+
71+
describe("type point", function () {
72+
function evaluate (input) {
73+
var driver = new Driver({}, {}, {});
74+
return driver.propertyToValue(input, { type: 'point' });
75+
}
76+
77+
it("should encode correctly", function () {
78+
var out = evaluate({ x: 5, y: 7 });
79+
80+
should(out instanceof Function);
81+
should.equal(out(), "POINT(5, 7)");
82+
});
83+
});
84+
85+
describe("custom type", function () {
86+
var customType = {
87+
propertyToValue: function (input) {
88+
return input + ' QWERTY';
89+
}
90+
};
91+
92+
function evaluate (input, customTypes) {
93+
var driver = new Driver({}, {}, {});
94+
if (customType) {
95+
for (var k in customTypes) {
96+
driver.customTypes[k] = customTypes[k];
97+
}
98+
}
99+
return driver.propertyToValue(input, { type: 'qwerty' });
100+
}
101+
102+
it("should do custom type conversion if provided", function () {
103+
var opts = { qwerty: customType };
104+
var out = evaluate('f', opts);
105+
106+
should.equal(out, 'f QWERTY');
107+
});
108+
109+
it("should not do custom type conversion if not provided", function () {
110+
var opts = { qwerty: {} };
111+
var out = evaluate('f', opts);
112+
113+
should.equal(out, 'f');
114+
});
115+
});
116+
117+
it("should do nothing for other types", function () {
118+
function evaluate (input, type) {
119+
var driver = new Driver({}, {}, {});
120+
return driver.propertyToValue(input, { type: type });
121+
}
122+
123+
should.strictEqual(evaluate('abc', { type: 'string' }), 'abc');
124+
should.strictEqual(evaluate(42, { type: 'number' }), 42);
125+
should.strictEqual(evaluate(undefined, { type: 'bleh' }), undefined);
126+
});
127+
128+
});
129+
130+
});

test/run.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
var Mocha = require("mocha");
2-
var fs = require("fs");
2+
var glob = require("glob");
33
var path = require("path");
44
var common = require("./common");
55
var logging = require("./logging");
6-
var location = path.normalize(path.join(__dirname, "integration"));
6+
var location = path.normalize(path.join(__dirname, "integration", "**", "*.js"));
77
var mocha = new Mocha({
88
reporter: "progress"
99
});
@@ -20,14 +20,9 @@ switch (common.hasConfig(common.protocol())) {
2020
runTests();
2121

2222
function runTests() {
23-
fs.readdirSync(location).filter(function (file) {
24-
return file.substr(-3) === '.js';
25-
}).forEach(function (file) {
23+
glob.sync(location).forEach(function (file) {
2624
if (!shouldRunTest(file)) return;
27-
28-
mocha.addFile(
29-
path.join(location, file)
30-
);
25+
mocha.addFile(file);
3126
});
3227

3328
logging.info("Testing **%s**", common.getConnectionString());
@@ -38,11 +33,11 @@ function runTests() {
3833
}
3934

4035
function shouldRunTest(file) {
41-
var name = file.substr(0, file.length - 3);
36+
var name = path.basename(file).slice(0, -3)
4237
var proto = common.protocol();
38+
var exclude = ['model-aggregate','property-number-size','smart-types'];
4339

44-
if (proto == "mongodb" && [ "model-aggregate",
45-
"property-number-size", "smart-types" ].indexOf(name) >= 0) return false;
40+
if (proto == "mongodb" && exclude.indexOf(name) >= 0) return false;
4641

4742
return true;
4843
}

0 commit comments

Comments
 (0)