/
query.test.js
209 lines (187 loc) · 8.75 KB
/
query.test.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const R = require('ramda');
const expect = require('expect.js');
const createPostgres = require('../..');
const config = {
withSql: {
user: 'postgres',
database: 'postgres',
password: 'password',
host: 'localhost',
port: 5432,
max: 10,
idleTimeoutMillis: 30000,
sql: 'test/fixtures/sql',
},
};
describe('Handy pg query test', () => {
const pgComponent = createPostgres({ configPath: 'withSql' });
let formattedQuery;
let insert;
let update;
let query;
let explain;
let formattedExplain;
before(async () => {
const api = await pgComponent.start(config);
formattedQuery = api.formattedQuery;
query = api.query;
insert = api.insert;
update = api.update;
explain = api.explain;
formattedExplain = api.formattedExplain;
await formattedQuery('drop-films');
await formattedQuery('create-films');
});
beforeEach(() => formattedQuery('truncate-films'));
after(async () => {
await formattedQuery('drop-films');
await pgComponent.stop();
});
it('executes a raw query', () =>
query('SELECT 5 AS value;')
.then(({ rows }) => expect(rows[0].value).to.be(5))
.catch((err) => expect(err).to.be(null))
);
it('executes multipleStatements in a raw query', () =>
query('SELECT 1 AS one; SELECT 2 AS two;')
.then(([resultOne, resultTwo]) => {
expect(resultOne.rows[0].one).to.be(1);
expect(resultTwo.rows[0].two).to.be(2);
})
.catch((err) => expect(err).to.be(null))
);
it('executes parameterised unformatted query', () =>
query('select-parameterised', 100)
.then(({ rows }) => expect(R.head(rows).number).to.equal(100))
.catch((err) => expect(err).to.be(null))
);
it('complains if an inexistent shorthand is used', () =>
query('inexistent-shorthand')
.catch((err) => expect(err.message).to.be('syntax error at or near "inexistent"'))
);
it('executes a shorthand unformatted query', () =>
query('select-1')
.then(({ rows }) => expect(R.head(rows).number).to.equal(1))
.catch((err) => expect(err).to.be(null))
);
it('executes parameterised query', () =>
formattedQuery('select-formatted', [22, 'catch'])
.then(({ rows }) => expect(R.head(rows).catch).to.equal(22))
.catch((err) => expect(err).to.be(null))
);
it('inserts rows using the shorthand without returning clause', () => {
const movie = { code: 'kurtz', title: 'Apocalypse Now', dateProd: new Date('1979-08-15'), kind: 'drama', len: 153 };
return insert('films', movie)
.then(({ rows }) => expect(rows).to.eql([]))
.then(() => formattedQuery('select-all', ['films']))
.then(({ rows }) => expect(R.head(rows)).to.eql(movie))
.catch((err) => expect(err).to.be(null));
});
it('inserts rows using the shorthand with returning clause set to false', () => {
const movie = { code: 'kurtz', title: 'Apocalypse Now', dateProd: new Date('1979-08-15'), kind: 'drama', len: 153 };
return insert('films', movie, { returning: false })
.then(({ rows }) => expect(rows).to.eql([]))
.then(() => formattedQuery('select-all', ['films']))
.then(({ rows }) => expect(R.head(rows)).to.eql(movie))
.catch((err) => expect(err).to.be(null));
});
it('inserts rows using the shorthand with returning clause set to true', () => {
const movie = { code: 'kurtz', title: 'Apocalypse Now', dateProd: new Date('1979-08-15'), kind: 'drama', len: 153 };
return insert('films', movie, { returning: true })
.then(({ rows }) => expect(R.head(rows)).to.eql(movie))
.then(() => formattedQuery('select-all', ['films']))
.then(({ rows }) => expect(R.head(rows)).to.eql(movie))
.catch((err) => expect(err).to.be(null));
});
it('updates rows using the shorthand with neither where clause nor returning clause', () => {
const movie1 = { code: 'kurtz', title: 'Apocalypse Now', dateProd: new Date('1979-08-15'), kind: 'drama', len: 153 };
const movie2 = { code: 'pulpf', title: 'Pulp Fiction', kind: 'cult', dateProd: null, len: 178 };
return insert('films', [movie1, movie2])
.then(() => update('films', { kind: 'super-cool' }))
.then(({ rows }) => expect(rows).to.eql([]))
.then(() => formattedQuery('select-all', ['films']))
.then(({ rows }) => {
expect(rows[0]).to.eql(R.merge(movie1, { kind: 'super-cool' }));
expect(rows[1]).to.eql(R.merge(movie2, { kind: 'super-cool' }));
})
.catch((err) => expect(err).to.be(null));
});
it('updates rows using the shorthand with a where clause but no returning clause', () => {
const movie1 = { code: 'kurtz', title: 'Apocalypse Now', dateProd: new Date('1979-08-15'), kind: 'drama', len: 153 };
const movie2 = { code: 'pulpf', title: 'Pulp Fiction', kind: 'cult', dateProd: null, len: 178 };
return insert('films', [movie1, movie2])
.then(() => update('films', { kind: 'super-cool' }, { code: 'pulpf' }))
.then(({ rows }) => expect(rows).to.eql([]))
.then(() => formattedQuery('select-all', ['films']))
.then(({ rows }) => {
expect(rows[0]).to.eql(movie1);
expect(rows[1]).to.eql(R.merge(movie2, { kind: 'super-cool' }));
})
.catch((err) => expect(err).to.be(null));
});
it('updates rows using the shorthand with a where clause and returning clause set to false', () => {
const movie1 = { code: 'kurtz', title: 'Apocalypse Now', dateProd: new Date('1979-08-15'), kind: 'drama', len: 153 };
const movie2 = { code: 'pulpf', title: 'Pulp Fiction', kind: 'cult', dateProd: null, len: 178 };
return insert('films', [movie1, movie2])
.then(() => update('films', { kind: 'super-cool' }, { code: 'pulpf'}, { returning: false }))
.then(({ rows }) => expect(rows).to.eql([]))
.then(() => formattedQuery('select-all', ['films']))
.then(({ rows }) => {
expect(rows[0]).to.eql(movie1);
expect(rows[1]).to.eql(R.merge(movie2, { kind: 'super-cool' }));
})
.catch((err) => expect(err).to.be(null));
});
it('updates rows using the shorthand with a where clause and returning clause set to true', () => {
const movie1 = { code: 'kurtz', title: 'Apocalypse Now', dateProd: new Date('1979-08-15'), kind: 'drama', len: 153 };
const movie2 = { code: 'pulpf', title: 'Pulp Fiction', kind: 'cult', dateProd: null, len: 178 };
return insert('films', [movie1, movie2])
.then(() => update('films', { kind: 'super-cool' }, { code: 'pulpf' }, { returning: true }))
.then(({ rows }) => expect(rows[0]).to.eql(R.merge(movie2, { kind: 'super-cool' })))
.then(() => formattedQuery('select-all', ['films']))
.then(({ rows }) => {
expect(rows[0]).to.eql(movie1);
expect(rows[1]).to.eql(R.merge(movie2, { kind: 'super-cool' }));
})
.catch((err) => expect(err).to.be(null));
});
it('updates rows using the shorthand without a where clause and returning clause set to false', () => {
const movie1 = { code: 'kurtz', title: 'Apocalypse Now', dateProd: new Date('1979-08-15'), kind: 'drama', len: 153 };
const movie2 = { code: 'pulpf', title: 'Pulp Fiction', kind: 'cult', dateProd: null, len: 178 };
return insert('films', [movie1, movie2])
.then(() => update('films', { kind: 'super-cool' }, null, { returning: false }))
.then(({ rows }) => expect(rows).to.eql([]))
.then(() => formattedQuery('select-all', ['films']))
.then(({ rows }) => {
expect(rows[0]).to.eql(R.merge(movie1, { kind: 'super-cool' }));
expect(rows[1]).to.eql(R.merge(movie2, { kind: 'super-cool' }));
})
.catch((err) => expect(err).to.be(null));
});
it('updates rows using the shorthand without a where clause and returning clause set to true', () => {
const movie1 = { code: 'kurtz', title: 'Apocalypse Now', dateProd: new Date('1979-08-15'), kind: 'drama', len: 153 };
const movie2 = { code: 'pulpf', title: 'Pulp Fiction', kind: 'cult', dateProd: null, len: 178 };
return insert('films', [movie1, movie2])
.then(() => update('films', { kind: 'super-cool' }, null, { returning: true }))
.then(({ rows }) => {
expect(rows[0]).to.eql(R.merge(movie1, { kind: 'super-cool' }));
expect(rows[1]).to.eql(R.merge(movie2, { kind: 'super-cool' }));
})
.then(() => formattedQuery('select-all', ['films']))
.then(({ rows }) => {
expect(rows[0]).to.eql(R.merge(movie1, { kind: 'super-cool' }));
expect(rows[1]).to.eql(R.merge(movie2, { kind: 'super-cool' }));
})
.catch((err) => expect(err).to.be(null));
});
it('plans a parameterised unformatted query', () =>
explain('select-parameterised', 100)
.then(({ rows }) => expect(R.head(rows)).to.have.key('QUERY PLAN'))
.catch((err) => expect(err).to.be(null))
);
it('plans a parameterised formatted query', () =>
formattedExplain('select-formatted', [22, 'catch'])
.then(({ rows }) => expect(R.head(rows)).to.have.key('QUERY PLAN'))
.catch((err) => expect(err).to.be(null))
);
});