-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
postgres.js
167 lines (154 loc) · 4.28 KB
/
postgres.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const knex = require('../../../knex');
const expect = require('chai').expect;
const sinon = require('sinon');
const pgDialect = require('../../../lib/dialects/postgres/index.js');
const pg = require('pg');
const _ = require('lodash');
describe('Postgres Unit Tests', function() {
let checkVersionStub, querySpy;
before(() => {
const fakeConnection = {
query: (...args) => {
const cb = args.find((arg) => {
return _.isFunction(arg);
});
cb();
},
on: _.noop,
};
querySpy = sinon.spy(fakeConnection, 'query');
checkVersionStub = sinon
.stub(pgDialect.prototype, 'checkVersion')
.callsFake(function() {
return Promise.resolve('9.6');
});
sinon.stub(pg.Client.prototype, 'connect').callsFake(function(cb) {
cb(null, fakeConnection);
});
});
afterEach(() => {
querySpy.resetHistory();
});
after(() => {
sinon.restore();
});
it('does not resolve client version if specified explicitly', (done) => {
const knexInstance = knex({
client: 'postgresql',
version: '10.5',
connection: {
pool: {},
},
});
knexInstance.raw('select 1 as 1').then((result) => {
expect(checkVersionStub.notCalled).to.equal(true);
knexInstance.destroy();
done();
});
});
it('escape statements correctly', async () => {
const knexInstance = knex({
client: 'postgresql',
version: '10.5',
connection: {
pool: {},
},
});
const sql = knexInstance('projects')
.where('id = 1 UNION SELECT 1, version();', 1)
.toSQL();
expect(sql.sql).to.equal(
'select * from "projects" where "id = 1 UNION SELECT 1, version();" = ?'
);
const sql2 = knexInstance('projects')
.where('id = 1" UNION SELECT 1, version();', 1)
.toSQL();
expect(sql2.sql).to.equal(
'select * from "projects" where "id = 1"" UNION SELECT 1, version();" = ?'
);
});
it('resolve client version if not specified explicitly', (done) => {
const knexInstance = knex({
client: 'postgresql',
connection: {
pool: {},
},
});
knexInstance.raw('select 1 as 1').then((result) => {
expect(checkVersionStub.calledOnce).to.equal(true);
knexInstance.destroy();
done();
});
});
it('Validates searchPath as Array/String', function() {
const knexInstance = knex({
client: 'pg',
});
expect(function() {
knexInstance.client.setSchemaSearchPath(null, {});
}).to.throw(TypeError);
expect(function() {
knexInstance.client.setSchemaSearchPath(null, 4);
}).to.throw(TypeError);
const fakeQueryFn = function(expectedSearchPath) {
return {
query: function(sql, callback) {
try {
expect(sql).to.equal('set search_path to ' + expectedSearchPath);
callback(null);
} catch (error) {
callback(error);
}
},
};
};
return knexInstance.client
.setSchemaSearchPath(fakeQueryFn('"public,knex"'), 'public,knex')
.then(function() {
return knexInstance.client.setSchemaSearchPath(
fakeQueryFn('"public","knex"'),
['public', 'knex']
);
})
.then(function() {
return knexInstance.client.setSchemaSearchPath(
fakeQueryFn('"public"'),
'public'
);
});
});
it('Uses documented query config as param when providing bindings', (done) => {
const knexInstance = knex({
client: 'postgresql',
connection: {},
});
knexInstance.raw('select 1 as ?', ['foo']).then((result) => {
sinon.assert.calledOnce(querySpy);
sinon.assert.calledWithExactly(
querySpy,
{
text: 'select 1 as $1',
values: ['foo'],
},
sinon.match.func
);
knexInstance.destroy();
done();
});
});
it('should configure knex connection as async function', async () => {
const knexInstance = knex({
client: 'pg',
connection: async function() {
return {
host: 'localhost',
};
},
});
await knexInstance.raw('select 1 as 1');
expect(knexInstance.client.connectionSettings).to.deep.equal({
host: 'localhost',
});
knexInstance.destroy();
});
});