Skip to content

Commit a6d567c

Browse files
authored
Vector function error message improvement and expanded 6.0 tests (#1338)
1 parent 76aafb7 commit a6d567c

File tree

4 files changed

+122
-4
lines changed

4 files changed

+122
-4
lines changed

packages/bolt-connection/test/bolt/bolt-protocol-v6x0.test.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
internal,
3838
vector,
3939
json,
40+
UnsupportedType,
4041
ProtocolVersion
4142
} from 'neo4j-driver-core'
4243

@@ -1186,6 +1187,89 @@ describe('#unit BoltProtocolV6x0', () => {
11861187
const unpacked = protocol.unpack(buffer)
11871188
expect(unpacked).toEqual(struct)
11881189
})
1190+
1191+
it.each([
1192+
[
1193+
'float32',
1194+
new structure.Structure(0x56, [[0xC6], new Int8Array(Float32Array.from([13, Infinity, -Infinity, NaN]).buffer)]),
1195+
vector(Float32Array.from([13, Infinity, -Infinity, NaN]))
1196+
],
1197+
[
1198+
'float64',
1199+
new structure.Structure(0x56, [[0xC1], new Int8Array(Float64Array.from([13, Infinity, -Infinity, NaN]).buffer)]),
1200+
vector(Float64Array.from([13, Infinity, -Infinity, NaN]))
1201+
],
1202+
[
1203+
'int8',
1204+
new structure.Structure(0x56, [[0xC8], Int8Array.from([-128, 127])]),
1205+
vector(Int8Array.from([-128, 127]))
1206+
],
1207+
[
1208+
'int16',
1209+
new structure.Structure(0x56, [[0xC9], new Int8Array(Int16Array.from([-32768, 32767]).buffer)]),
1210+
vector(Int16Array.from([-32768, 32767]))
1211+
],
1212+
[
1213+
'int32',
1214+
new structure.Structure(0x56, [[0xCA], new Int8Array(Int32Array.from([-2147483648, 2147483647]).buffer)]),
1215+
vector(Int32Array.from([-2147483648, 2147483647]))
1216+
],
1217+
[
1218+
'int64',
1219+
new structure.Structure(0x56, [[0xCB], new Int8Array(BigInt64Array.from([-9223372036854775808n, 9223372036854775807n]).buffer)]),
1220+
vector(BigInt64Array.from([-9223372036854775808n, 9223372036854775807n]))
1221+
]
1222+
])('should unpack (%s) vector', (_, struct, object) => {
1223+
const buffer = alloc(256)
1224+
const protocol = new BoltProtocolV6x0(
1225+
new utils.MessageRecordingConnection(),
1226+
buffer,
1227+
{
1228+
disableLosslessIntegers: true
1229+
}
1230+
)
1231+
1232+
const packable = protocol.packable(struct)
1233+
1234+
expect(packable).not.toThrow()
1235+
1236+
buffer.reset()
1237+
1238+
const unpacked = protocol.unpack(buffer)
1239+
expect(unpacked.asTypedArray().buffer).toEqual(object.asTypedArray().buffer) // compares buffers to successfully compare BigInts
1240+
expect(unpacked.getType()).toEqual(object.getType())
1241+
})
1242+
1243+
it.each([
1244+
[
1245+
'without message',
1246+
new structure.Structure(0x3F, ['Quantum Integer', 6, 10, {}]),
1247+
new UnsupportedType('Quantum Integer', 6, 10)
1248+
],
1249+
[
1250+
'with message',
1251+
new structure.Structure(0x3F, ['Quantum Integer', 6, 10, { message: 'Quantum computing is always 10 years away' }]),
1252+
new UnsupportedType('Quantum Integer', 6, 10, 'Quantum computing is always 10 years away')
1253+
]
1254+
])('should unpack unknown type (%s)', (_, struct, object) => {
1255+
const buffer = alloc(256)
1256+
const protocol = new BoltProtocolV6x0(
1257+
new utils.MessageRecordingConnection(),
1258+
buffer,
1259+
{
1260+
disableLosslessIntegers: true
1261+
}
1262+
)
1263+
1264+
const packable = protocol.packable(struct)
1265+
1266+
expect(packable).not.toThrow()
1267+
1268+
buffer.reset()
1269+
1270+
const unpacked = protocol.unpack(buffer)
1271+
expect(unpacked).toEqual(object)
1272+
})
11891273
})
11901274

11911275
describe('result metadata enrichment', () => {

packages/core/src/vector.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ export default class Vector<K extends Float32Array | Float64Array | Int8Array |
6969
) {
7070
throw newError('The neo4j Vector class does not support Unsigned Integer Arrays, please use a signed IntArray')
7171
} else {
72-
throw newError(`The neo4j Vector class is a wrapper for TypedArrays. got ${typeof typedArray}`)
72+
// @ts-expect-error
73+
throw newError(`The neo4j Vector class is a wrapper for TypedArrays. got ${(typedArray.toString() as string)}`)
7374
}
7475
this._typedArray = typedArray
7576
}
@@ -128,7 +129,19 @@ Object.defineProperty(Vector.prototype, VECTOR_IDENTIFIER_PROPERTY, {
128129
* @return {Vector} - The Neo4j Vector ready to be used as a query parameter
129130
*/
130131
export function vector<K extends Float32Array | Float64Array | Int8Array | Int16Array | Int32Array | BigInt64Array> (typedArray: K): Vector<K> {
131-
return new Vector(typedArray)
132+
try {
133+
return new Vector(typedArray)
134+
} catch {
135+
if (
136+
typedArray instanceof Uint8Array ||
137+
typedArray instanceof Uint16Array ||
138+
typedArray instanceof Uint32Array ||
139+
typedArray instanceof BigUint64Array
140+
) {
141+
throw newError('Invalid argument type passed to vector constructor function: should be signed integer or float TypedArray, got unsigned integer TypedArray')
142+
}
143+
throw newError(`Invalid argument type passed to vector constructor function: should be TypedArray, got ${typedArray.toString()}`)
144+
}
132145
}
133146

134147
/**

packages/core/test/vector-type.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ describe('Vector', () => {
3131
expect(vec.getType()).toEqual(expectedType)
3232
expect(vec.asTypedArray()).toEqual(typedArray)
3333
})
34+
35+
it.each([
36+
['array', [], 'should be TypedArray, got'],
37+
['Unsigned TypedArray', Uint16Array.from([]), 'should be signed integer or float TypedArray, got unsigned integer TypedArray']
38+
])('should fail to create create vector from (%s)', (_, typedArray, expectedMessage) => {
39+
// @ts-expect-error
40+
expect(() => vector(typedArray)).toThrow(expectedMessage)
41+
})
3442
})
3543
describe('.toString()', () => {
3644
it.each([

packages/neo4j-driver-deno/lib/core/vector.ts

Lines changed: 15 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)