Skip to content

Commit bce2060

Browse files
author
Pavel Romanov
committed
Some tests and little fixes
1 parent 0d5ee91 commit bce2060

File tree

4 files changed

+76
-9
lines changed

4 files changed

+76
-9
lines changed

connections/abstract-connection.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class AbstractConnection {
2121
/**
2222
* @abstract
2323
* @async
24-
* @param {{name: String, sql:String, addonds: Object}} queryObject query data
24+
* @param {{name: String, sql:String, addons: Object}} queryObject query data
2525
* @param {Object} queryParams named params
2626
* @param {Object} queryOptions options
2727
* @returns {Promise<Array>} query result

connections/postgres-connection.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class PostgresConnection extends AbstractConnection {
1212
* @private
1313
*/
1414
async _executeQuery(query, params) {
15-
const {rows} = await this.client.query(query);
15+
const {rows} = await this.client.query(query, params);
1616
return rows;
1717
}
1818

@@ -29,7 +29,10 @@ class PostgresConnection extends AbstractConnection {
2929
*/
3030
query(queryObject, queryParams, queryOptions = {}) {
3131
const {sql, addons} = queryObject;
32-
const {templateParams} = queryOptions;
32+
if (!sql) {
33+
throw new TypeError('Invalid query object, "sql" property missing');
34+
}
35+
const {templateParams = {}} = queryOptions;
3336
let queryText = sql;
3437

3538
if (addons) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"license": "MIT",
99
"version": "0.0.2",
1010
"scripts": {
11-
"autofix": "eslint --fix .",
11+
"autofix": "eslint --fix ."
1212
},
1313
"dependencies": {
1414
"pg": "^7.4.1",

test/postgres-connection.test.js

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,72 @@
1-
const jest = require('jest');
2-
const QueryTemplater = require('query-template');
3-
4-
jest.mock('query-template');
1+
// const jest = require('jest');
2+
const PostgresConnection = require('../connections/postgres-connection.js');
53

64
describe('Postgres connection', () => {
5+
const client = {
6+
query: jest.fn((query, params) => Promise.resolve({query, params})),
7+
};
8+
9+
beforeEach(() => {
10+
client.query.mockClear();
11+
});
12+
13+
const pq = new PostgresConnection({
14+
client,
15+
type: 'pg',
16+
});
17+
18+
test('should handle simple query with right format', async () => {
19+
const query = {
20+
sql: 'SELECT * FROM table WHERE field1 = :field1 {{t1}}',
21+
addons: {
22+
t1: {
23+
sql: 'AND field2 = :field2',
24+
options: {propertyName: 'field2t'},
25+
},
26+
},
27+
};
28+
29+
await pq.query(query, {field1: 1, field2: 2}, {templateParams: {field2t: true}});
30+
expect(client.query.mock.calls[0][0]).toBe('SELECT * FROM table WHERE field1 = $1 AND field2 = $2');
31+
expect(client.query.mock.calls[0][1]).toMatchObject([1, 2]);
32+
});
33+
34+
test('should handle simple query without template params', async () => {
35+
const query = {
36+
sql: 'SELECT * FROM table WHERE field1 = :field1 {{t1}}',
37+
addons: {
38+
t1: {
39+
sql: 'AND field2 = :field2',
40+
options: {propertyName: 'field2'},
41+
},
42+
},
43+
};
44+
45+
await pq.query(query, {field1: 1, field2: 2});
46+
expect(client.query.mock.calls[0][0]).toBe('SELECT * FROM table WHERE field1 = $1 AND field2 = $2');
47+
expect(client.query.mock.calls[0][1]).toMatchObject([1, 2]);
48+
});
49+
50+
test('should handle simple query without templates', async () => {
51+
const query = {
52+
sql: 'SELECT * FROM table WHERE field1 = :field1',
53+
};
54+
55+
await pq.query(query, {field1: 1, field2: 2});
56+
expect(client.query.mock.calls[0][0]).toBe('SELECT * FROM table WHERE field1 = $1');
57+
expect(client.query.mock.calls[0][1]).toMatchObject([1]);
58+
});
59+
60+
test('should throw on invalid query format', async () => {
61+
const query = {
62+
addons: {
63+
t1: {
64+
sql: 'AND field2 = :field2',
65+
options: {propertyName: 'field2'},
66+
},
67+
},
68+
};
769

8-
});
70+
expect(() => pq.query(query, {field1: 1, field2: 2})).toThrow(/sql/);
71+
});
72+
});

0 commit comments

Comments
 (0)