-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathsql-column-ir.test.ts
More file actions
191 lines (174 loc) · 6.51 KB
/
Copy pathsql-column-ir.test.ts
File metadata and controls
191 lines (174 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { describe, expect, it } from 'vitest';
import { SqlColumnDefaultIR } from '../src/ir/sql-column-default-ir';
import { SqlColumnIR } from '../src/ir/sql-column-ir';
describe('SqlColumnIR', () => {
it('id is the column name, prefixed by kind', () => {
const column = new SqlColumnIR({ name: 'email', nativeType: 'text', nullable: false });
expect(column.id).toBe('column:email');
});
it('nodeKind is the column kind', () => {
const column = new SqlColumnIR({ name: 'email', nativeType: 'text', nullable: false });
expect(column.nodeKind).toBe('sql-column');
});
describe('children', () => {
it('empty when the column has no default', () => {
const column = new SqlColumnIR({ name: 'email', nativeType: 'text', nullable: false });
expect(column.children()).toEqual([]);
});
it('yields a default node built from the resolved default', () => {
const column = new SqlColumnIR({
name: 'status',
nativeType: 'text',
nullable: false,
resolvedNativeType: 'text',
resolvedDefault: { kind: 'literal', value: 'draft' },
});
expect(column.children()).toEqual([
new SqlColumnDefaultIR({
resolved: { kind: 'literal', value: 'draft' },
nativeTypeContext: 'text',
}),
]);
});
it('yields a default node from a raw default alone (unparseable or hand-built)', () => {
const column = new SqlColumnIR({
name: 'status',
nativeType: 'text',
nullable: false,
default: "'draft'::text",
});
expect(column.children()).toEqual([new SqlColumnDefaultIR({ raw: "'draft'::text" })]);
});
it('the default node id is the fixed sentinel', () => {
const column = new SqlColumnIR({
name: 'status',
nativeType: 'text',
nullable: false,
default: "'x'",
});
expect(column.children()[0]?.id).toBe('default');
});
});
describe('isEqualTo (own attributes only — never the default)', () => {
it('true when nativeType and nullability match, even when defaults differ', () => {
const a = new SqlColumnIR({
name: 'email',
nativeType: 'text',
nullable: false,
default: "'x'",
});
const b = new SqlColumnIR({
name: 'email',
nativeType: 'text',
nullable: false,
default: "'y'",
});
expect(a.isEqualTo(b)).toBe(true);
});
it('false when nativeType differs', () => {
const a = new SqlColumnIR({ name: 'email', nativeType: 'text', nullable: false });
const b = new SqlColumnIR({ name: 'email', nativeType: 'varchar', nullable: false });
expect(a.isEqualTo(b)).toBe(false);
});
it('false when nullable differs', () => {
const a = new SqlColumnIR({ name: 'email', nativeType: 'text', nullable: false });
const b = new SqlColumnIR({ name: 'email', nativeType: 'text', nullable: true });
expect(a.isEqualTo(b)).toBe(false);
});
it('false when many differs in raw mode', () => {
const a = new SqlColumnIR({ name: 'tags', nativeType: 'text', nullable: false });
const b = new SqlColumnIR({ name: 'tags', nativeType: 'text', nullable: false, many: true });
expect(a.isEqualTo(b)).toBe(false);
});
it('compares resolvedNativeType when both sides carry it, ignoring raw drift', () => {
const expected = new SqlColumnIR({
name: 'email',
nativeType: 'character varying(255)',
nullable: false,
resolvedNativeType: 'character varying(255)',
});
const actual = new SqlColumnIR({
name: 'email',
nativeType: 'varchar(255)',
nullable: false,
resolvedNativeType: 'character varying(255)',
});
expect(expected.isEqualTo(actual)).toBe(true);
});
it('false when resolvedNativeType differs', () => {
const expected = new SqlColumnIR({
name: 'n',
nativeType: 'int4',
nullable: false,
resolvedNativeType: 'int4',
});
const actual = new SqlColumnIR({
name: 'n',
nativeType: 'int4',
nullable: false,
resolvedNativeType: 'int8',
});
expect(expected.isEqualTo(actual)).toBe(false);
});
it('array-ness rides on resolvedNativeType ([] suffix), not the many flag', () => {
const expected = new SqlColumnIR({
name: 'tags',
nativeType: 'text[]',
nullable: false,
resolvedNativeType: 'text[]',
});
const actual = new SqlColumnIR({
name: 'tags',
nativeType: 'text',
nullable: false,
many: true,
resolvedNativeType: 'text[]',
});
expect(expected.isEqualTo(actual)).toBe(true);
});
it('falls back to raw comparison when either side lacks resolvedNativeType', () => {
const withResolved = new SqlColumnIR({
name: 'c',
nativeType: 'text',
nullable: false,
resolvedNativeType: 'text',
});
const rawOnly = new SqlColumnIR({ name: 'c', nativeType: 'text', nullable: false });
expect(withResolved.isEqualTo(rawOnly)).toBe(true);
expect(rawOnly.isEqualTo(withResolved)).toBe(true);
});
});
describe('resolved fields', () => {
it('carries resolvedNativeType and resolvedDefault when supplied', () => {
const column = new SqlColumnIR({
name: 'email',
nativeType: 'character varying(255)',
nullable: false,
resolvedNativeType: 'character varying(255)',
resolvedDefault: { kind: 'literal', value: 'x' },
});
expect(column.resolvedNativeType).toBe('character varying(255)');
expect(column.resolvedDefault).toEqual({ kind: 'literal', value: 'x' });
});
});
describe('identity columns (introspected, no raw default)', () => {
it('yields a default child node from resolvedDefault alone, with no raw default', () => {
// A `GENERATED ... AS IDENTITY` column has no `column_default` at
// all — the postgres control adapter sets `resolvedDefault` directly
// to `autoincrement()` without a raw expression to parse, so
// `children()` must still produce a default node without a `default`
// (raw) field.
const column = new SqlColumnIR({
name: 'id',
nativeType: 'int4',
nullable: false,
resolvedDefault: { kind: 'function', expression: 'autoincrement()' },
});
expect(column.children()).toEqual([
new SqlColumnDefaultIR({
resolved: { kind: 'function', expression: 'autoincrement()' },
}),
]);
});
});
});