Setting the file. One moment. Decimal Write Test · Clickhouse Js Node Rowbinary · ClickHouse/agent-skills · Skills Docs(opens in a new tab)
tests/Decimal.write.test.ts
TypeScript·86 lines·3 KB
11
12describe("writeDecimal32", () => {
13 it("encodes 1.5", async () =>
14 expect(encode(writeDecimal32, parseDecimal("1.5", 4))).toEqual(
15 await query("SELECT CAST('1.5', 'Decimal32(4)') FORMAT RowBinary"),
16 ));
17 it("encodes -12345.6789", async () =>
18 expect(encode(writeDecimal32, parseDecimal("-12345.6789", 4))).toEqual(
19 await query(
20 "SELECT CAST('-12345.6789', 'Decimal32(4)') FORMAT RowBinary",
21 ),
22 ));
23 it("encodes 0", async () =>
24 expect(encode(writeDecimal32, parseDecimal("0", 4))).toEqual(
25 await query("SELECT CAST('0', 'Decimal32(4)') FORMAT RowBinary"),
26 ));
27});
28
29describe("writeDecimal64", () => {
30 it("encodes 1.50", async () =>
31 expect(encode(writeDecimal64, parseDecimal("1.50", 2))).toEqual(
32 await query("SELECT CAST('1.50', 'Decimal64(2)') FORMAT RowBinary"),
33 ));
34 it("encodes -9999999999.99", async () =>
35 expect(encode(writeDecimal64, parseDecimal("-9999999999.99", 2))).toEqual(
36 await query(
37 "SELECT CAST('-9999999999.99', 'Decimal64(2)') FORMAT RowBinary",
38 ),
39 ));
40});
41
42describe("writeDecimal128", () => {
43 it("encodes 3.1415926535", async () =>
44 expect(encode(writeDecimal128, parseDecimal("3.1415926535", 10))).toEqual(
45 await query(
46 "SELECT CAST('3.1415926535', 'Decimal128(10)') FORMAT RowBinary",
47 ),
48 ));
49 it("encodes -1.0000000001", async () =>
50 expect(encode(writeDecimal128, parseDecimal("-1.0000000001", 10))).toEqual(
51 await query(
52 "SELECT CAST('-1.0000000001', 'Decimal128(10)') FORMAT RowBinary",
53 ),
54 ));
55});
56
57describe("writeDecimal256", () => {
58 it("encodes 2.71828182845904523536", async () =>
59 expect(
60 encode(writeDecimal256, parseDecimal("2.71828182845904523536", 20)),
61 ).toEqual(
62 await query(
63 "SELECT CAST('2.71828182845904523536', 'Decimal256(20)') FORMAT RowBinary",
64 ),
65 ));
66 it("encodes -0.00000000000000000001", async () =>
67 expect(
68 encode(writeDecimal256, parseDecimal("-0.00000000000000000001", 20)),
69 ).toEqual(
70 await query(
71 "SELECT CAST('-0.00000000000000000001', 'Decimal256(20)') FORMAT RowBinary",
72 ),
73 ));
74});
75
76describe("parseDecimal", () => {
77 it("scales the integer and fraction parts", () =>
78 expect(parseDecimal("1.5000", 4)).toEqual([15000n, 4]));
79 it("handles a negative value", () =>
80 expect(parseDecimal("-12345.6789", 4)).toEqual([-123456789n, 4]));
81 it("handles zero", () => expect(parseDecimal("0.00", 2)).toEqual([0n, 2]));
82 it("right-pads a short fraction to the scale", () =>
83 expect(parseDecimal("1.5", 4)).toEqual([15000n, 4]));
84 it("truncates a fraction longer than the scale", () =>
85 expect(parseDecimal("1.56789", 2)).toEqual([156n, 2]));
86});