Skip to content

Commit 098eb4a

Browse files
committed
refactor: improve BSON Binary Vector tests titles
1 parent 847e733 commit 098eb4a

File tree

1 file changed

+69
-56
lines changed

1 file changed

+69
-56
lines changed

test/node/bson_binary_vector.spec.test.ts

Lines changed: 69 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const { toHex, fromHex } = BSON.onDemand.ByteUtils;
99
type VectorHexType = '0x03' | '0x27' | '0x10';
1010
type VectorTest = {
1111
description: string;
12-
vector?: (number | string)[];
12+
vector?: number[];
1313
valid: boolean;
1414
dtype_hex: VectorHexType;
1515
padding?: number;
@@ -46,7 +46,22 @@ function fixBits(f: number | string): number {
4646
return f;
4747
}
4848

49-
function make(vector: (number | string)[], dtype_hex: VectorHexType, padding?: number): Binary {
49+
function dtypeToHelper(dtype_hex: string) {
50+
switch (dtype_hex) {
51+
case '0x10' /* packed_bit */:
52+
return 'fromPackedBits';
53+
case '0x03' /* int8 */:
54+
return 'fromInt8Array';
55+
break;
56+
case '0x27' /* float32 */:
57+
return 'fromFloat32Array';
58+
break;
59+
default:
60+
throw new Error(`Unknown dtype_hex: ${dtype_hex}`);
61+
}
62+
}
63+
64+
function make(vector: number[], dtype_hex: VectorHexType, padding?: number): Binary {
5065
let binary: Binary;
5166
switch (dtype_hex) {
5267
case '0x10' /* packed_bit */:
@@ -88,17 +103,18 @@ const invalidTestExpectedError = new Map()
88103
'Insufficient vector data FLOAT32',
89104
'Invalid Vector: Float32 vector must contain a multiple of 4 bytes'
90105
)
91-
// skipped
92-
.set('Overflow Vector PACKED_BIT', false)
93-
.set('Underflow Vector PACKED_BIT', false)
94-
.set('Overflow Vector INT8', false)
95-
.set('Underflow Vector INT8', false)
96-
.set('INT8 with float inputs', false)
97-
.set('Vector with float values PACKED_BIT', false);
98-
99-
function testVectorBuilding(test: VectorTest, expectedErrorMessage: string) {
100-
describe('creating an instance of a Binary class using parameters', () => {
101-
it(`bson: ${test.description}`, function () {
106+
// These are not possible given the constraints of the input types allowed:
107+
// our helpers will throw an "unsupported_error" for these
108+
.set('Overflow Vector PACKED_BIT', 'unsupported_error')
109+
.set('Underflow Vector PACKED_BIT', 'unsupported_error')
110+
.set('Overflow Vector INT8', 'unsupported_error')
111+
.set('Underflow Vector INT8', 'unsupported_error')
112+
.set('INT8 with float inputs', 'unsupported_error')
113+
.set('Vector with float values PACKED_BIT', 'unsupported_error');
114+
115+
function testVectorInvalidInputValues(test: VectorTest, expectedErrorMessage: string) {
116+
describe('when creating a BSON Vector given invalid input values', () => {
117+
it(`either BSON.serialize() or Binary.${dtypeToHelper(test.dtype_hex)}() throws a BSONError`, function () {
102118
let thrownError: Error | undefined;
103119
try {
104120
const bin = make(test.vector!, test.dtype_hex, test.padding);
@@ -111,15 +127,14 @@ function testVectorBuilding(test: VectorTest, expectedErrorMessage: string) {
111127
expect(
112128
expectedErrorMessage,
113129
'We expect a certain error message but got an unsupported error'
114-
).to.be.false;
115-
this.skip();
130+
).to.equal('unsupported_error');
131+
} else {
132+
expect(thrownError, thrownError?.stack).to.be.instanceOf(BSONError);
133+
expect(thrownError?.message).to.match(new RegExp(expectedErrorMessage));
116134
}
117-
118-
expect(thrownError, thrownError?.stack).to.be.instanceOf(BSONError);
119-
expect(thrownError?.message).to.match(new RegExp(expectedErrorMessage));
120135
});
121136

122-
it(`ejson: ${test.description}`, function () {
137+
it(`either EJSON.stringify() or Binary.${dtypeToHelper(test.dtype_hex)}() throws a BSONError`, function () {
123138
let thrownError: Error | undefined;
124139
try {
125140
const bin = make(test.vector!, test.dtype_hex, test.padding);
@@ -132,19 +147,18 @@ function testVectorBuilding(test: VectorTest, expectedErrorMessage: string) {
132147
expect(
133148
expectedErrorMessage,
134149
'We expect a certain error message but got an unsupported error'
135-
).to.be.false;
136-
this.skip();
150+
).to.equal('unsupported_error');
151+
} else {
152+
expect(thrownError, thrownError?.stack).to.be.instanceOf(BSONError);
153+
expect(thrownError?.message).to.match(new RegExp(expectedErrorMessage));
137154
}
138-
139-
expect(thrownError, thrownError?.stack).to.be.instanceOf(BSONError);
140-
expect(thrownError?.message).to.match(new RegExp(expectedErrorMessage));
141155
});
142156
});
143157
}
144158

145-
function testVectorReserializing(test: VectorTest, expectedErrorMessage: string) {
146-
describe('creating an instance of a Binary class using canonical_bson', () => {
147-
it(`bson deserialize: ${test.description}`, function () {
159+
function testVectorInvalidBSONBytes(test: VectorTest, expectedErrorMessage: string) {
160+
describe('when creating a Binary Vector instance from invalid bytes', () => {
161+
it(`BSON.serialize() throw a BSONError`, function () {
148162
let thrownError: Error | undefined;
149163
const bin = BSON.deserialize(Buffer.from(test.canonical_bson!, 'hex'));
150164

@@ -158,7 +172,7 @@ function testVectorReserializing(test: VectorTest, expectedErrorMessage: string)
158172
expect(thrownError?.message).to.match(new RegExp(expectedErrorMessage));
159173
});
160174

161-
it(`ejson stringify: ${test.description}`, function () {
175+
it(`EJSON.stringify() throw a BSONError`, function () {
162176
let thrownError: Error | undefined;
163177
const bin = BSON.deserialize(Buffer.from(test.canonical_bson!, 'hex'));
164178

@@ -174,7 +188,7 @@ function testVectorReserializing(test: VectorTest, expectedErrorMessage: string)
174188
});
175189
}
176190

177-
describe('BSON Binary Vector spec tests', () => {
191+
describe.only('BSON Binary Vector spec tests', () => {
178192
const tests: Record<string, VectorSuite> = Object.create(null);
179193

180194
for (const file of fs.readdirSync(path.join(__dirname, 'specs/bson-binary-vector'))) {
@@ -197,20 +211,22 @@ describe('BSON Binary Vector spec tests', () => {
197211
* > MUST assert that the input float array is the same after encoding and decoding.
198212
*/
199213
for (const test of valid) {
200-
it(`encode ${test.description}`, function () {
201-
const bin = make(test.vector!, test.dtype_hex, test.padding);
214+
describe(test.description, () => {
215+
it(`calling Binary.${dtypeToHelper(test.dtype_hex)}() with input numbers and serializing it does not throw`, function () {
216+
const bin = make(test.vector!, test.dtype_hex, test.padding);
202217

203-
const buffer = BSON.serialize({ [suite.test_key]: bin });
204-
expect(toHex(buffer)).to.equal(test.canonical_bson!.toLowerCase());
205-
});
218+
const buffer = BSON.serialize({ [suite.test_key]: bin });
219+
expect(toHex(buffer)).to.equal(test.canonical_bson!.toLowerCase());
220+
});
206221

207-
it(`decode ${test.description}`, function () {
208-
const canonical_bson = fromHex(test.canonical_bson!.toLowerCase());
209-
const doc = BSON.deserialize(canonical_bson);
222+
it(`creating a Binary instance from BSON bytes does not throw`, function () {
223+
const canonical_bson = fromHex(test.canonical_bson!.toLowerCase());
224+
const doc = BSON.deserialize(canonical_bson);
210225

211-
expect(doc[suite.test_key].sub_type).to.equal(0x09);
212-
expect(doc[suite.test_key].buffer[0]).to.equal(+test.dtype_hex);
213-
expect(doc[suite.test_key].buffer[1]).to.equal(test.padding);
226+
expect(doc[suite.test_key].sub_type).to.equal(0x09);
227+
expect(doc[suite.test_key].buffer[0]).to.equal(+test.dtype_hex);
228+
expect(doc[suite.test_key].buffer[1]).to.equal(test.padding);
229+
});
214230
});
215231
}
216232
});
@@ -224,22 +240,19 @@ describe('BSON Binary Vector spec tests', () => {
224240
for (const test of invalid) {
225241
const expectedErrorMessage = invalidTestExpectedError.get(test.description);
226242

227-
if (test.vector != null && test.canonical_bson != null) {
228-
describe('both manual vector building and re-serializing', () => {
229-
testVectorBuilding(test, expectedErrorMessage);
230-
testVectorReserializing(test, expectedErrorMessage);
231-
});
232-
} else if (test.canonical_bson != null) {
233-
describe('vector re-serializing', () => {
234-
testVectorReserializing(test, expectedErrorMessage);
235-
});
236-
} else if (test.vector != null) {
237-
describe('manual vector building', () => {
238-
testVectorBuilding(test, expectedErrorMessage);
239-
});
240-
} else {
241-
throw new Error('not testing anything for: ' + util.inspect(test));
242-
}
243+
describe(test.description, () => {
244+
if (test.canonical_bson != null) {
245+
testVectorInvalidBSONBytes(test, expectedErrorMessage);
246+
}
247+
248+
if (test.vector != null) {
249+
testVectorInvalidInputValues(test, expectedErrorMessage);
250+
}
251+
252+
if (test.vector == null && test.canonical_bson == null) {
253+
throw new Error('not testing anything for: ' + util.inspect(test));
254+
}
255+
});
243256
}
244257
});
245258
});

0 commit comments

Comments
 (0)