|
1 | 1 | import * as util from 'util';
|
2 | 2 | import * as fs from 'fs';
|
3 | 3 | import * as path from 'path';
|
| 4 | +import * as assert from 'node:assert/strict'; |
4 | 5 | import { BSON, BSONError, Binary, EJSON } from '../register-bson';
|
5 | 6 | import { expect } from 'chai';
|
6 | 7 |
|
@@ -114,88 +115,68 @@ const invalidTestsWhereHelpersDoNotThrow = new Set()
|
114 | 115 | .add('FLOAT32 with padding')
|
115 | 116 | .add('INT8 with padding');
|
116 | 117 |
|
| 118 | +function catchError<T>( |
| 119 | + fn: () => T |
| 120 | +): { status: 'returned'; result: T } | { status: 'thrown'; result: Error } { |
| 121 | + try { |
| 122 | + return { status: 'returned', result: fn() }; |
| 123 | + } catch (error) { |
| 124 | + return { status: 'thrown', result: error }; |
| 125 | + } |
| 126 | +} |
| 127 | + |
117 | 128 | function testVectorInvalidInputValues(test: VectorTest, expectedErrorMessage: string) {
|
118 | 129 | describe('when creating a BSON Vector given invalid input values', () => {
|
119 |
| - it(`BSON.serialize() throws a BSONError`, function () { |
120 |
| - let thrownError: Error | undefined; |
121 |
| - |
122 |
| - let bin; |
123 |
| - try { |
124 |
| - bin = make(test.vector!, test.dtype_hex, test.padding); |
125 |
| - } catch (error) { |
126 |
| - thrownError = error; |
127 |
| - } |
128 |
| - |
129 |
| - if (thrownError?.message.startsWith('unsupported_error')) { |
130 |
| - expect( |
131 |
| - expectedErrorMessage, |
132 |
| - 'We expect a certain error message but got an unsupported error' |
133 |
| - ).to.equal('unsupported_error'); |
134 |
| - return; |
135 |
| - } |
136 |
| - |
137 |
| - try { |
138 |
| - BSON.serialize({ bin }); |
139 |
| - } catch (error) { |
140 |
| - thrownError = error; |
141 |
| - } |
142 |
| - |
143 |
| - expect(thrownError, thrownError?.stack).to.be.instanceOf(BSONError); |
144 |
| - expect(thrownError?.message).to.match(new RegExp(expectedErrorMessage)); |
145 |
| - }); |
146 |
| - |
147 |
| - it(`Binary.${dtypeToHelper(test.dtype_hex)}() throws a BSONError`, function () { |
148 |
| - let thrownError: Error | undefined; |
149 |
| - try { |
150 |
| - make(test.vector!, test.dtype_hex, test.padding); |
151 |
| - } catch (error) { |
152 |
| - thrownError = error; |
153 |
| - } |
154 |
| - |
155 |
| - if (invalidTestsWhereHelpersDoNotThrow.has(test.description)) { |
156 |
| - expect(thrownError).to.not.exist; |
157 |
| - return; |
158 |
| - } |
159 |
| - |
160 |
| - if (thrownError?.message.startsWith('unsupported_error')) { |
161 |
| - expect( |
162 |
| - expectedErrorMessage, |
163 |
| - 'We expect a certain error message but got an unsupported error' |
164 |
| - ).to.equal('unsupported_error'); |
165 |
| - return; |
166 |
| - } |
167 |
| - |
168 |
| - expect(thrownError, thrownError?.stack).to.be.instanceOf(BSONError); |
169 |
| - expect(thrownError?.message).to.match(new RegExp(expectedErrorMessage)); |
170 |
| - }); |
171 |
| - |
172 |
| - it(`EJSON.stringify() throws a BSONError`, function () { |
173 |
| - let thrownError: Error | undefined; |
174 |
| - |
175 |
| - let bin; |
176 |
| - try { |
177 |
| - bin = make(test.vector!, test.dtype_hex, test.padding); |
178 |
| - } catch (error) { |
179 |
| - thrownError = error; |
180 |
| - } |
| 130 | + const binaryCreation = catchError(make.bind(null, test.vector!, test.dtype_hex, test.padding)); |
| 131 | + const bsonBytesCreation = |
| 132 | + binaryCreation.status !== 'thrown' |
| 133 | + ? catchError(BSON.serialize.bind(null, { bin: binaryCreation.result })) |
| 134 | + : undefined; |
| 135 | + const ejsonStringCreation = |
| 136 | + binaryCreation.status !== 'thrown' |
| 137 | + ? catchError(BSON.EJSON.stringify.bind(null, { bin: binaryCreation.result })) |
| 138 | + : undefined; |
| 139 | + |
| 140 | + const binaryHelperValidations = [ |
| 141 | + 'Padding specified with no vector data PACKED_BIT', |
| 142 | + 'Exceeding maximum padding PACKED_BIT', |
| 143 | + 'Negative padding PACKED_BIT', |
| 144 | + ...Array.from(invalidTestExpectedError.entries()) |
| 145 | + .filter(([, v]) => v === 'unsupported_error') |
| 146 | + .map(([k]) => k) |
| 147 | + ]; |
| 148 | + |
| 149 | + const errorType = expectedErrorMessage === 'unsupported_error' ? Error : BSONError; |
| 150 | + const errorName = expectedErrorMessage === 'unsupported_error' ? 'Error' : 'BSONError'; |
| 151 | + |
| 152 | + const check = outcome => { |
| 153 | + expect(outcome).to.exist; |
| 154 | + expect(outcome.status).to.equal('thrown'); |
| 155 | + expect(outcome.result).to.be.instanceOf(errorType); |
| 156 | + expect(outcome.result) |
| 157 | + .to.have.property('message') |
| 158 | + .that.matches(new RegExp(expectedErrorMessage)); |
| 159 | + }; |
| 160 | + |
| 161 | + if (binaryHelperValidations.includes(test.description)) { |
| 162 | + it(`Binary.${dtypeToHelper(test.dtype_hex)}() throws a ${errorName}`, function () { |
| 163 | + check(binaryCreation); |
| 164 | + }); |
| 165 | + } else { |
| 166 | + expect(errorName).to.equal('BSONError'); // unsupported_error are only when making vectors |
181 | 167 |
|
182 |
| - if (thrownError?.message.startsWith('unsupported_error')) { |
183 |
| - expect( |
184 |
| - expectedErrorMessage, |
185 |
| - 'We expect a certain error message but got an unsupported error' |
186 |
| - ).to.equal('unsupported_error'); |
187 |
| - return; |
188 |
| - } |
| 168 | + it(`Binary.${dtypeToHelper(test.dtype_hex)}() not throw`, function () { |
| 169 | + expect(binaryCreation).to.have.property('status', 'returned'); |
| 170 | + }); |
189 | 171 |
|
190 |
| - try { |
191 |
| - BSON.EJSON.stringify({ bin }); |
192 |
| - } catch (error) { |
193 |
| - thrownError = error; |
194 |
| - } |
| 172 | + it(`BSON.serialize() throws a BSONError`, function () { |
| 173 | + check(bsonBytesCreation); |
| 174 | + }); |
195 | 175 |
|
196 |
| - expect(thrownError, thrownError?.stack).to.be.instanceOf(BSONError); |
197 |
| - expect(thrownError?.message).to.match(new RegExp(expectedErrorMessage)); |
198 |
| - }); |
| 176 | + it(`EJSON.stringify() throws a BSONError`, function () { |
| 177 | + check(ejsonStringCreation); |
| 178 | + }); |
| 179 | + } |
199 | 180 | });
|
200 | 181 | }
|
201 | 182 |
|
|
0 commit comments