-
Notifications
You must be signed in to change notification settings - Fork 240
/
Copy pathoutbound-serializer.test.ts
310 lines (283 loc) · 8.5 KB
/
outbound-serializer.test.ts
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import { describe, it, expect } from 'vitest'
import { serialize } from '../src/serializer'
import BufferList from './testing/buffer-list'
describe('serializer', () => {
it('builds startup message', () => {
const actual = serialize.startup({
user: 'brian',
database: 'bang',
})
expect(actual).toEqual(
new BufferList()
.addInt16(3)
.addInt16(0)
.addCString('user')
.addCString('brian')
.addCString('database')
.addCString('bang')
.addCString('client_encoding')
.addCString('UTF8')
.addCString('')
.join(true),
)
})
it('builds password message', () => {
const actual = serialize.password('!')
expect(actual).toEqual(new BufferList().addCString('!').join(true, 'p'))
})
it('builds request ssl message', () => {
const actual = serialize.requestSsl()
const expected = new BufferList().addInt32(80877103).join(true)
expect(actual).toEqual(expected)
})
it('builds SASLInitialResponseMessage message', () => {
const actual = serialize.sendSASLInitialResponseMessage('mech', 'data')
expect(actual).toEqual(
new BufferList()
.addCString('mech')
.addInt32(4)
.addString('data')
.join(true, 'p'),
)
})
it('builds SCRAMClientFinalMessage message', () => {
const actual = serialize.sendSCRAMClientFinalMessage('data')
expect(actual).toEqual(new BufferList().addString('data').join(true, 'p'))
})
it('builds query message', () => {
const txt = 'select * from boom'
const actual = serialize.query(txt)
expect(actual).toEqual(new BufferList().addCString(txt).join(true, 'Q'))
})
describe('parse message', () => {
it('builds parse message', () => {
const actual = serialize.parse({ text: '!' })
const expected = new BufferList()
.addCString('')
.addCString('!')
.addInt16(0)
.join(true, 'P')
expect(actual).toEqual(expected)
})
it('builds parse message with named query', () => {
const actual = serialize.parse({
name: 'boom',
text: 'select * from boom',
types: [],
})
const expected = new BufferList()
.addCString('boom')
.addCString('select * from boom')
.addInt16(0)
.join(true, 'P')
expect(actual).toEqual(expected)
})
it('with multiple parameters', () => {
const actual = serialize.parse({
name: 'force',
text: 'select * from bang where name = $1',
types: [1, 2, 3, 4],
})
const expected = new BufferList()
.addCString('force')
.addCString('select * from bang where name = $1')
.addInt16(4)
.addInt32(1)
.addInt32(2)
.addInt32(3)
.addInt32(4)
.join(true, 'P')
expect(actual).toEqual(expected)
})
})
describe('bind messages', () => {
it('with no values', () => {
const actual = serialize.bind()
const expectedBuffer = new BufferList()
.addCString('')
.addCString('')
.addInt16(0)
.addInt16(0)
.addInt16(0)
.join(true, 'B')
expect(actual).toEqual(expectedBuffer)
})
it('with named statement, portal, and values', () => {
const actual = serialize.bind({
portal: 'bang',
statement: 'woo',
values: ['1', 'hi', null, 'zing'],
})
const expectedBuffer = new BufferList()
.addCString('bang') // portal name
.addCString('woo') // statement name
.addInt16(4)
.addInt16(0)
.addInt16(0)
.addInt16(0)
.addInt16(0)
.addInt16(4)
.addInt32(1)
.add(new TextEncoder().encode('1'))
.addInt32(2)
.add(new TextEncoder().encode('hi'))
.addInt32(-1)
.addInt32(4)
.add(new TextEncoder().encode('zing'))
.addInt16(0)
.join(true, 'B')
expect(actual).toEqual(expectedBuffer)
})
})
it('with custom valueMapper', () => {
const actual = serialize.bind({
portal: 'bang',
statement: 'woo',
values: ['1', 'hi', null, 'zing'],
valueMapper: () => null,
})
const expectedBuffer = new BufferList()
.addCString('bang') // portal name
.addCString('woo') // statement name
.addInt16(4)
.addInt16(0)
.addInt16(0)
.addInt16(0)
.addInt16(0)
.addInt16(4)
.addInt32(-1)
.addInt32(-1)
.addInt32(-1)
.addInt32(-1)
.addInt16(0)
.join(true, 'B')
expect(actual).toEqual(expectedBuffer)
})
it('with named statement, portal, and buffer value', () => {
const actual = serialize.bind({
portal: 'bang',
statement: 'woo',
values: ['1', 'hi', null, new TextEncoder().encode('zing')],
})
const expectedBuffer = new BufferList()
.addCString('bang') // portal name
.addCString('woo') // statement name
.addInt16(4) // value count
.addInt16(0) // string
.addInt16(0) // string
.addInt16(0) // string
.addInt16(1) // binary
.addInt16(4)
.addInt32(1)
.add(new TextEncoder().encode('1'))
.addInt32(2)
.add(new TextEncoder().encode('hi'))
.addInt32(-1)
.addInt32(4)
.add(new TextEncoder().encode('zing'))
.addInt16(0)
.join(true, 'B')
expect(actual).toEqual(expectedBuffer)
})
describe('builds execute message', () => {
it('for unamed portal with no row limit', () => {
const actual = serialize.execute()
const expectedBuffer = new BufferList()
.addCString('')
.addInt32(0)
.join(true, 'E')
expect(actual).toEqual(expectedBuffer)
})
it('for named portal with row limit', () => {
const actual = serialize.execute({
portal: 'my favorite portal',
rows: 100,
})
const expectedBuffer = new BufferList()
.addCString('my favorite portal')
.addInt32(100)
.join(true, 'E')
expect(actual).toEqual(expectedBuffer)
})
})
it('builds flush command', () => {
const actual = serialize.flush()
const expected = new BufferList().join(true, 'H')
expect(actual).toEqual(expected)
})
it('builds sync command', () => {
const actual = serialize.sync()
const expected = new BufferList().join(true, 'S')
expect(actual).toEqual(expected)
})
it('builds end command', () => {
const actual = serialize.end()
const expected = new Uint8Array([0x58, 0, 0, 0, 4])
expect(actual).toEqual(expected)
})
describe('builds describe command', () => {
it('describe statement', () => {
const actual = serialize.describe({ type: 'S', name: 'bang' })
const expected = new BufferList()
.addChar('S')
.addCString('bang')
.join(true, 'D')
expect(actual).toEqual(expected)
})
it('describe unnamed portal', () => {
const actual = serialize.describe({ type: 'P' })
const expected = new BufferList()
.addChar('P')
.addCString('')
.join(true, 'D')
expect(actual).toEqual(expected)
})
})
describe('builds close command', () => {
it('describe statement', () => {
const actual = serialize.close({ type: 'S', name: 'bang' })
const expected = new BufferList()
.addChar('S')
.addCString('bang')
.join(true, 'C')
expect(actual).toEqual(expected)
})
it('describe unnamed portal', () => {
const actual = serialize.close({ type: 'P' })
const expected = new BufferList()
.addChar('P')
.addCString('')
.join(true, 'C')
expect(actual).toEqual(expected)
})
})
describe('copy messages', () => {
it('builds copyFromChunk', () => {
const actual = serialize.copyData(new Uint8Array([1, 2, 3]))
const expected = new BufferList()
.add(new Uint8Array([1, 2, 3]))
.join(true, 'd')
expect(actual).toEqual(expected)
})
it('builds copy fail', () => {
const actual = serialize.copyFail('err!')
const expected = new BufferList().addCString('err!').join(true, 'f')
expect(actual).toEqual(expected)
})
it('builds copy done', () => {
const actual = serialize.copyDone()
const expected = new BufferList().join(true, 'c')
expect(actual).toEqual(expected)
})
})
it('builds cancel message', () => {
const actual = serialize.cancel(3, 4)
const expected = new BufferList()
.addInt16(1234)
.addInt16(5678)
.addInt32(3)
.addInt32(4)
.join(true)
expect(actual).toEqual(expected)
})
})