Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { serialize } from '../src/pg-serializer'
3
4describe(`pg-serializer`, () => {
5 describe(`serialize`, () => {
6 it(`should serialize null as empty string`, () => {
7 expect(serialize(null)).toBe(``)
8 })
9
10 it(`should serialize undefined as empty string`, () => {
11 expect(serialize(undefined)).toBe(``)
12 })
13
14 it(`should serialize strings as-is`, () => {
15 expect(serialize(`hello`)).toBe(`hello`)
16 expect(serialize(``)).toBe(``)
17 expect(serialize(`with spaces`)).toBe(`with spaces`)
18 })
19
20 it(`should serialize numbers as strings`, () => {
21 expect(serialize(42)).toBe(`42`)
22 expect(serialize(0)).toBe(`0`)
23 expect(serialize(-123)).toBe(`-123`)
24 expect(serialize(3.14)).toBe(`3.14`)
25 expect(serialize(Infinity)).toBe(`Infinity`)
26 expect(serialize(-Infinity)).toBe(`-Infinity`)
27 })
28
29 it(`should serialize bigints as strings`, () => {
30 expect(serialize(BigInt(42))).toBe(`42`)
31 expect(serialize(BigInt(0))).toBe(`0`)
32 expect(serialize(BigInt(-123))).toBe(`-123`)
33 expect(serialize(BigInt(`9007199254740993`))).toBe(`9007199254740993`)
34 expect(serialize(BigInt(`-9007199254740993`))).toBe(`-9007199254740993`)
35 })
36
37 it(`should serialize booleans as lowercase strings`, () => {
38 expect(serialize(true)).toBe(`true`)
39 expect(serialize(false)).toBe(`false`)
40 })
41
42 it(`should serialize dates as ISO strings`, () => {
43 const date = new Date(`2024-01-15T10:30:45.000Z`)
44 expect(serialize(date)).toBe(`2024-01-15T10:30:45.000Z`)
45 })
46
47 describe(`arrays`, () => {
48 it(`should serialize empty arrays`, () => {
49 expect(serialize([])).toBe(`{}`)
50 })
51
52 it(`should serialize arrays of numbers`, () => {
53 expect(serialize([1, 2, 3])).toBe(`{1,2,3}`)
54 })
55
56 it(`should serialize arrays of bigints`, () => {
57 expect(serialize([BigInt(1), BigInt(2), BigInt(3)])).toBe(`{1,2,3}`)
58 expect(serialize([BigInt(`9007199254740993`)])).toBe(
59 `{9007199254740993}`,
60 )
61 })
62
63 it(`should serialize arrays of strings with proper escaping`, () => {
64 expect(serialize([`a`, `b`, `c`])).toBe(`{"a","b","c"}`)
65 expect(serialize([`hello world`])).toBe(`{"hello world"}`)
66 })
67
68 it(`should escape quotes and backslashes in string array elements`, () => {
69 expect(serialize([`with"quote`])).toBe(`{"with\\"quote"}`)
70 expect(serialize([`with\\backslash`])).toBe(`{"with\\\\backslash"}`)
71 expect(serialize([`both"and\\`])).toBe(`{"both\\"and\\\\"}`)
72 })
73
74 it(`should serialize arrays with null/undefined as NULL`, () => {
75 expect(serialize([1, null, 3])).toBe(`{1,NULL,3}`)
76 expect(serialize([`a`, undefined, `c`])).toBe(`{"a",NULL,"c"}`)
77 })
78
79 it(`should serialize arrays of booleans`, () => {
80 expect(serialize([true, false, true])).toBe(`{true,false,true}`)
81 })
82
83 it(`should serialize arrays of dates`, () => {
84 const date = new Date(`2024-01-15T10:30:45.000Z`)
85 expect(serialize([date])).toBe(`{2024-01-15T10:30:45.000Z}`)
86 })
87
88 it(`should serialize mixed type arrays`, () => {
89 expect(serialize([1, `two`, true, null])).toBe(`{1,"two",true,NULL}`)
90 })
91
92 it(`should serialize nested arrays`, () => {
93 expect(
94 serialize([
95 [1, 2],
96 [3, 4],
97 ]),
98 ).toBe(`{{1,2},{3,4}}`)
99 })
100 })
101
102 describe(`error handling`, () => {
103 it(`should throw for unsupported types`, () => {
104 expect(() => serialize({ key: `value` })).toThrow(
105 `Cannot serialize value:`,
106 )
107 expect(() => serialize(Symbol(`test`))).toThrow(
108 `Cannot serialize value:`,
109 )
110 })
111
112 it(`should handle error message for non-JSON-serializable values`, () => {
113 // This tests that the error message doesn't throw when trying to stringify
114 // values that JSON.stringify can't handle
115 const circularObj: any = {}
116 circularObj.self = circularObj
117
118 expect(() => serialize(circularObj)).toThrow(`Cannot serialize value:`)
119 })
120
121 it(`should throw for functions`, () => {
122 expect(() => serialize(() => {})).toThrow(`Cannot serialize value:`)
123 })
124 })
125 })
126})