-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathPostgresDriver.test.ts
More file actions
206 lines (183 loc) · 5.63 KB
/
Copy pathPostgresDriver.test.ts
File metadata and controls
206 lines (183 loc) · 5.63 KB
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
import { PostgresDBRunner } from '@cubejs-backend/testing-shared';
import { StartedTestContainer } from 'testcontainers';
import { PostgresDriver } from '../src';
const streamToArray = require('stream-to-array');
function largeParams(): Array<string> {
return new Array(65536).fill('foo');
}
describe('PostgresDriver', () => {
let container: StartedTestContainer;
let driver: PostgresDriver;
jest.setTimeout(2 * 60 * 1000);
beforeAll(async () => {
container = await PostgresDBRunner.startContainer({ volumes: [] });
driver = new PostgresDriver({
host: container.getHost(),
port: container.getMappedPort(5432),
user: 'test',
password: 'test',
database: 'test',
});
await driver.query('CREATE SCHEMA IF NOT EXISTS test;', []);
});
afterAll(async () => {
await container.stop();
});
test('type coercion', async () => {
await driver.query('CREATE TYPE CUBEJS_TEST_ENUM AS ENUM (\'FOO\');', []);
const data = await driver.query(
`
SELECT
CAST('2020-01-01' as DATE) as date,
CAST('2020-01-01 00:00:00' as TIMESTAMP) as timestamp,
CAST('2020-01-01 00:00:00+02' as TIMESTAMPTZ) as timestamptz,
CAST('1.0' as DECIMAL(10,2)) as decimal,
CAST('FOO' as CUBEJS_TEST_ENUM) as enum
`,
[]
);
expect(data).toEqual([
{
// Date in UTC
date: '2020-01-01T00:00:00.000',
timestamp: '2020-01-01T00:00:00.000',
// converted to utc
timestamptz: '2019-12-31T22:00:00.000',
// Numerics as string
decimal: '1.00',
// Enum datatypes as string
enum: 'FOO',
}
]);
});
test('too many params', async () => {
await expect(
driver.query(`SELECT 'foo'::TEXT;`, largeParams())
)
.rejects
.toThrow('PostgreSQL protocol does not support more than 65535 parameters, but 65536 passed');
});
test('stream', async () => {
await driver.uploadTable(
'test.streaming_test',
[
{ name: 'id', type: 'bigint' },
{ name: 'created', type: 'date' },
{ name: 'price', type: 'decimal' }
],
{
rows: [
{ id: 1, created: '2020-01-01', price: '100' },
{ id: 2, created: '2020-01-02', price: '200' },
{ id: 3, created: '2020-01-03', price: '300' }
]
}
);
const tableData = await driver.stream('select * from test.streaming_test', [], {
highWaterMark: 1000,
});
try {
expect(await tableData.types).toEqual([
{
name: 'id',
type: 'bigint'
},
{
name: 'created',
type: 'date'
},
{
name: 'price',
type: 'decimal'
},
]);
expect(await streamToArray(tableData.rowStream)).toEqual([
{ id: '1', created: '2020-01-01T00:00:00.000', price: '100' },
{ id: '2', created: '2020-01-02T00:00:00.000', price: '200' },
{ id: '3', created: '2020-01-03T00:00:00.000', price: '300' }
]);
} finally {
await (<any> tableData).release();
}
});
test('stream (array-typed columns)', async () => {
// Streaming must not fail when a query returns array-typed columns.
// Array types are reported as `text` and node-postgres parses them into
// JS arrays. See CORE-522.
const tableData = await driver.stream(
`SELECT
ARRAY['oops', 'test']::text[] as text_array,
ARRAY[1, 2, 3]::int[] as int_array`,
[],
{
highWaterMark: 1000,
}
);
try {
expect(await tableData.types).toEqual([
{
name: 'text_array',
type: 'text'
},
{
name: 'int_array',
type: 'text'
},
]);
expect(await streamToArray(tableData.rowStream)).toEqual([
{ text_array: ['oops', 'test'], int_array: [1, 2, 3] },
]);
} finally {
await (<any> tableData).release();
}
});
test('stream (exception)', async () => {
try {
await driver.stream('select * from test.random_name_for_table_that_doesnot_exist_sql_must_fail', [], {
highWaterMark: 1000,
});
throw new Error('stream must throw an exception');
} catch (e: any) {
expect(e.message).toEqual(
'relation "test.random_name_for_table_that_doesnot_exist_sql_must_fail" does not exist'
);
}
});
test('stream (too many params)', async () => {
try {
await driver.stream('select * from test.streaming_test', largeParams(), {
highWaterMark: 1000,
});
throw new Error('stream must throw an exception');
} catch (e: any) {
expect(e.message).toEqual(
'PostgreSQL protocol does not support more than 65535 parameters, but 65536 passed'
);
}
});
test('table name check', async () => {
const tblName = 'really-really-really-looooooooooooooooooooooooooooooooooooooooooooooooooooong-table-name';
try {
await driver.createTable(tblName, [{ name: 'id', type: 'bigint' }]);
throw new Error('createTable must throw an exception');
} catch (e: any) {
expect(e.message).toEqual(
'PostgreSQL can not work with table names longer than 63 symbols. ' +
`Consider using the 'sqlAlias' attribute in your cube definition for ${tblName}.`
);
}
});
// Note: This test MUST be the last in the list.
test('release', async () => {
expect(async () => {
await driver.release();
}).not.toThrowError(
/Called end on pool more than once/
);
expect(async () => {
await driver.release();
}).not.toThrowError(
/Called end on pool more than once/
);
});
});