Skip to content

Commit 2e5b182

Browse files
authored
Merge pull request #72 from libsql/num-tests
Add more tests for handling of numeric values
2 parents 331bfc7 + 63a208e commit 2e5b182

File tree

2 files changed

+41
-30
lines changed

2 files changed

+41
-30
lines changed

src/__tests__/client.test.ts

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -216,34 +216,46 @@ describe("values", () => {
216216
testRoundtrip("string with unicode",
217217
"žluťoučký kůň úpěl ďábelské ódy", "žluťoučký kůň úpěl ďábelské ódy");
218218

219-
testRoundtrip("zero number", 0, 0);
220-
testRoundtrip("integer number", -2023, -2023);
221-
testRoundtrip("float number", 12.345, 12.345);
222-
223-
describe("'number' int mode", () => {
224-
testRoundtrip("zero integer", 0n, 0, "number");
225-
testRoundtrip("small integer", -42n, -42, "number");
226-
testRoundtrip("largest safe integer", 9007199254740991n, 9007199254740991, "number");
227-
testRoundtripError("smallest unsafe integer", 9007199254740992n, RangeError, "number");
228-
testRoundtripError("large unsafe integer", -1152921504594532842n, RangeError, "number");
219+
describe("number", () => {
220+
const intModes: Array<libsql.IntMode> = ["number", "bigint", "string"];
221+
for (const intMode of intModes) {
222+
testRoundtrip("zero", 0, 0, intMode);
223+
testRoundtrip("integer", -2023, -2023, intMode);
224+
testRoundtrip("float", 12.345, 12.345, intMode);
225+
testRoundtrip("large positive float", 1e18, 1e18, intMode);
226+
testRoundtrip("large negative float", -1e18, -1e18, intMode);
227+
testRoundtrip("MAX_VALUE", Number.MAX_VALUE, Number.MAX_VALUE, intMode);
228+
testRoundtrip("-MAX_VALUE", -Number.MAX_VALUE, -Number.MAX_VALUE, intMode);
229+
testRoundtrip("MIN_VALUE", Number.MIN_VALUE, Number.MIN_VALUE, intMode);
230+
}
229231
});
230232

231-
describe("'bigint' int mode", () => {
232-
testRoundtrip("zero integer", 0n, 0n, "bigint");
233-
testRoundtrip("small integer", -42n, -42n, "bigint");
234-
testRoundtrip("large positive integer", 1152921504608088318n, 1152921504608088318n, "bigint");
235-
testRoundtrip("large negative integer", -1152921504594532842n, -1152921504594532842n, "bigint");
236-
testRoundtrip("largest positive integer", 9223372036854775807n, 9223372036854775807n, "bigint");
237-
testRoundtrip("largest negative integer", -9223372036854775808n, -9223372036854775808n, "bigint");
238-
});
233+
describe("bigint", () => {
234+
describe("'number' int mode", () => {
235+
testRoundtrip("zero integer", 0n, 0, "number");
236+
testRoundtrip("small integer", -42n, -42, "number");
237+
testRoundtrip("largest safe integer", 9007199254740991n, 9007199254740991, "number");
238+
testRoundtripError("smallest unsafe integer", 9007199254740992n, RangeError, "number");
239+
testRoundtripError("large unsafe integer", -1152921504594532842n, RangeError, "number");
240+
});
239241

240-
describe("'string' int mode", () => {
241-
testRoundtrip("zero integer", 0n, "0", "string");
242-
testRoundtrip("small integer", -42n, "-42", "string");
243-
testRoundtrip("large positive integer", 1152921504608088318n, "1152921504608088318", "string");
244-
testRoundtrip("large negative integer", -1152921504594532842n, "-1152921504594532842", "string");
245-
testRoundtrip("largest positive integer", 9223372036854775807n, "9223372036854775807", "string");
246-
testRoundtrip("largest negative integer", -9223372036854775808n, "-9223372036854775808", "string");
242+
describe("'bigint' int mode", () => {
243+
testRoundtrip("zero integer", 0n, 0n, "bigint");
244+
testRoundtrip("small integer", -42n, -42n, "bigint");
245+
testRoundtrip("large positive integer", 1152921504608088318n, 1152921504608088318n, "bigint");
246+
testRoundtrip("large negative integer", -1152921504594532842n, -1152921504594532842n, "bigint");
247+
testRoundtrip("largest positive integer", 9223372036854775807n, 9223372036854775807n, "bigint");
248+
testRoundtrip("largest negative integer", -9223372036854775808n, -9223372036854775808n, "bigint");
249+
});
250+
251+
describe("'string' int mode", () => {
252+
testRoundtrip("zero integer", 0n, "0", "string");
253+
testRoundtrip("small integer", -42n, "-42", "string");
254+
testRoundtrip("large positive integer", 1152921504608088318n, "1152921504608088318", "string");
255+
testRoundtrip("large negative integer", -1152921504594532842n, "-1152921504594532842", "string");
256+
testRoundtrip("largest positive integer", 9223372036854775807n, "9223372036854775807", "string");
257+
testRoundtrip("largest negative integer", -9223372036854775808n, "-9223372036854775808", "string");
258+
});
247259
});
248260

249261
const buf = new ArrayBuffer(256);
@@ -255,11 +267,10 @@ describe("values", () => {
255267
testRoundtrip("Uint8Array", array, buf);
256268

257269
testRoundtrip("null", null, null);
258-
testRoundtrip("true", true, 1);
259-
testRoundtrip("false", false, 0);
270+
testRoundtrip("true", true, 1n, "bigint");
271+
testRoundtrip("false", false, 0n, "bigint");
260272

261-
testRoundtrip("bigint", -1000n, -1000);
262-
testRoundtrip("Date", new Date("2023-01-02T12:34:56Z"), 1672662896000);
273+
testRoundtrip("Date", new Date("2023-01-02T12:34:56Z"), 1672662896000, "bigint");
263274

264275
// @ts-expect-error
265276
testRoundtripError("undefined produces error", undefined, TypeError);

src/sqlite3.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ function valueToSql(value: InValue): unknown {
301301
}
302302
return value;
303303
} else if (typeof value === "boolean") {
304-
return value ? 1 : 0;
304+
return value ? 1n : 0n;
305305
} else if (value instanceof ArrayBuffer) {
306306
return Buffer.from(value);
307307
} else if (value instanceof Date) {

0 commit comments

Comments
 (0)