Setting the file. One moment. Composite Write Test · Clickhouse Js Node Rowbinary · ClickHouse/agent-skills · Skills Docs(opens in a new tab)
tests/Composite.write.test.ts
TypeScript·102 lines·3 KB
;
12import { writeInt32, writeUInt32 } from "../src/writers/integers.js";
13import { writeString } from "../src/writers/strings.js";
14
15describe("writeNullable", () => {
16 it("encodes a present value (flag 0 then the value)", async () =>
17 expect(encode(writeNullable(writeUInt32), 42)).toEqual(
18 await query("SELECT toNullable(toUInt32(42)) FORMAT RowBinary"),
19 ));
20 it("encodes NULL as the lone flag byte", async () =>
21 expect(encode(writeNullable(writeUInt32), null)).toEqual(
22 await query("SELECT CAST(NULL AS Nullable(UInt32)) FORMAT RowBinary"),
23 ));
24});
25
26describe("writeArray", () => {
27 it("encodes Array(UInt32)", async () =>
28 expect(encode(writeArray(writeUInt32), [1, 2, 3])).toEqual(
29 await query("SELECT [1, 2, 3]::Array(UInt32) FORMAT RowBinary"),
30 ));
31 it("encodes an empty array as a single 0x00 length", async () =>
32 expect(encode(writeArray(writeUInt32), [])).toEqual(
33 await query("SELECT []::Array(UInt32) FORMAT RowBinary"),
34 ));
35});
36
37describe("writeTuple / writeTupleNamed", () => {
38 it("encodes a positional Tuple(UInt32, String)", async () =>
39 expect(
40 encode(writeTuple<[number, string]>([writeUInt32, writeString]), [
41 7,
42 "hi",
43 ]),
44 ).toEqual(await query("SELECT (toUInt32(7), 'hi') FORMAT RowBinary")));
45
46 it("encodes a named Tuple in field order", async () =>
47 expect(
48 encode(
49 writeTupleNamed<{ id: number; name: string }>({
50 id: writeUInt32,
51 name: writeString,
52 }),
53 { id: 7, name: "hi" },
54 ),
55 ).toEqual(
56 await query(
57 "SELECT CAST((toUInt32(7), 'hi'), 'Tuple(id UInt32, name String)') FORMAT RowBinary",
58 ),
59 ));
60});
61
62describe("writeMap", () => {
63 it("encodes Map(String, UInt32) in insertion order", async () =>
64 expect(
65 encode(
66 writeMap(writeString, writeUInt32),
67 new Map([
68 ["a", 1],
69 ["b", 2],
70 ]),
71 ),
72 ).toEqual(
73 await query(
74 "SELECT map('a', toUInt32(1), 'b', toUInt32(2)) FORMAT RowBinary",
75 ),
76 ));
77});
78
79describe("writeVariant", () => {
80 // Variant(Int32, String) sorts by type name: ["Int32", "String"].
81 const writeV = writeVariant([writeInt32, writeString]);
82
83 it("encodes the Int32 alternative (discriminant 0)", async () =>
84 expect(encode(writeV, [0, -5])).toEqual(
85 await query(
86 "SELECT CAST(toInt32(-5), 'Variant(Int32, String)') SETTINGS allow_experimental_variant_type = 1 FORMAT RowBinary",
87 ),
88 ));
89
90 it("encodes the String alternative (discriminant 1)", async () =>
91 expect(encode(writeV, [1, "hello"])).toEqual(
92 await query(
93 "SELECT CAST('hello', 'Variant(Int32, String)') SETTINGS allow_experimental_variant_type = 1 FORMAT RowBinary",
94 ),
95 ));
96
97 it("encodes NULL as a single 0xFF byte", () =>
98 expect([...encode(writeV, null)]).toEqual([0xff]));
99
100 it("throws for an out-of-range discriminant", () =>
101 expect(() => encode(writeV, [9, 0])).toThrow(RangeError));
102});