-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathexport-utils.test.ts
More file actions
261 lines (216 loc) · 8.7 KB
/
export-utils.test.ts
File metadata and controls
261 lines (216 loc) · 8.7 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/**
* Unit tests for export-utils.ts
*
* Validates consistency between META_TABLE_CONFIG, META_TABLE_ORDER,
* and the queryAndParse calls in both export-meta.ts and export-graphql-meta.ts.
* Also tests shared utility functions.
*/
import { readFileSync } from 'fs';
import { join } from 'path';
import path from 'path';
import {
META_TABLE_CONFIG,
META_TABLE_ORDER,
EXPORT_SCHEMAS,
EXPORT_BLACKLIST,
DB_REQUIRED_EXTENSIONS,
SERVICE_REQUIRED_EXTENSIONS,
META_COMMON_HEADER,
META_COMMON_FOOTER,
makeReplacer,
normalizeOutdir
} from '../src/export-utils';
// =============================================================================
// Config / Order consistency
// =============================================================================
describe('META_TABLE_CONFIG and META_TABLE_ORDER consistency', () => {
it('every key in META_TABLE_ORDER should exist in META_TABLE_CONFIG', () => {
const configKeys = new Set(Object.keys(META_TABLE_CONFIG));
const missingFromConfig: string[] = [];
for (const tableName of META_TABLE_ORDER) {
if (!configKeys.has(tableName)) {
missingFromConfig.push(tableName);
}
}
expect(missingFromConfig).toEqual([]);
});
it('every key in META_TABLE_CONFIG should exist in META_TABLE_ORDER (no orphaned configs)', () => {
const orderSet = new Set<string>(META_TABLE_ORDER as unknown as string[]);
const configKeys = Object.keys(META_TABLE_CONFIG);
const missingFromOrder: string[] = [];
for (const key of configKeys) {
if (!orderSet.has(key)) {
missingFromOrder.push(key);
}
}
expect(missingFromOrder).toEqual([]);
});
it('META_TABLE_ORDER should not have duplicates', () => {
const seen = new Set<string>();
const duplicates: string[] = [];
for (const name of META_TABLE_ORDER) {
if (seen.has(name)) {
duplicates.push(name);
}
seen.add(name);
}
expect(duplicates).toEqual([]);
});
it('every config entry should have a valid schema from EXPORT_SCHEMAS', () => {
const validSchemas = new Set<string>(EXPORT_SCHEMAS as unknown as string[]);
for (const [key, config] of Object.entries(META_TABLE_CONFIG)) {
expect(validSchemas.has(config.schema)).toBe(true);
expect(config.table).toBeTruthy();
expect(Object.keys(config.fields).length).toBeGreaterThan(0);
}
});
it('no table in META_TABLE_ORDER should be in EXPORT_BLACKLIST', () => {
const blacklisted = (META_TABLE_ORDER as unknown as string[]).filter(t => EXPORT_BLACKLIST.has(t));
expect(blacklisted).toEqual([]);
});
it('every config entry should have an id field of type uuid', () => {
for (const [key, config] of Object.entries(META_TABLE_CONFIG)) {
expect(config.fields).toHaveProperty('id');
expect(config.fields.id).toBe('uuid');
}
});
it('every config entry (except database) should have a database_id field', () => {
for (const [key, config] of Object.entries(META_TABLE_CONFIG)) {
if (key === 'database') continue;
expect(config.fields).toHaveProperty('database_id');
expect(config.fields.database_id).toBe('uuid');
}
});
});
// =============================================================================
// Cross-flow table parity: both flows loop over META_TABLE_ORDER,
// so parity is guaranteed by construction. Verify both files import it.
// =============================================================================
describe('SQL and GraphQL flow table parity', () => {
let sqlSource: string;
let gqlSource: string;
beforeAll(() => {
sqlSource = readFileSync(join(__dirname, '../src/export-meta.ts'), 'utf-8');
gqlSource = readFileSync(join(__dirname, '../src/export-graphql-meta.ts'), 'utf-8');
});
it('SQL flow should iterate META_TABLE_ORDER', () => {
expect(sqlSource).toContain('META_TABLE_ORDER');
expect(sqlSource).toMatch(/for\s*\(\s*const\s+\w+\s+of\s+META_TABLE_ORDER\s*\)/);
});
it('GraphQL flow should iterate META_TABLE_ORDER', () => {
expect(gqlSource).toContain('META_TABLE_ORDER');
expect(gqlSource).toMatch(/for\s*\(\s*const\s+\w+\s+of\s+META_TABLE_ORDER\s*\)/);
});
it('SQL flow should support auto-discovery via EXPORT_SCHEMAS', () => {
expect(sqlSource).toContain('EXPORT_SCHEMAS');
expect(sqlSource).toContain('information_schema.tables');
});
it('SQL flow should respect EXPORT_BLACKLIST', () => {
expect(sqlSource).toContain('EXPORT_BLACKLIST');
});
});
// =============================================================================
// Shared constants
// =============================================================================
describe('Shared constants', () => {
it('DB_REQUIRED_EXTENSIONS should include core PostgreSQL extensions', () => {
const expected = ['plpgsql', 'uuid-ossp', 'citext', 'pgcrypto', 'btree_gin', 'btree_gist', 'postgis', 'hstore', 'vector'];
for (const ext of expected) {
expect(DB_REQUIRED_EXTENSIONS).toContain(ext);
}
});
it('SERVICE_REQUIRED_EXTENSIONS should include metaschema dependencies', () => {
expect(SERVICE_REQUIRED_EXTENSIONS).toContain('plpgsql');
expect(SERVICE_REQUIRED_EXTENSIONS).toContain('metaschema-schema');
expect(SERVICE_REQUIRED_EXTENSIONS).toContain('services');
});
it('META_COMMON_HEADER should set session_replication_role', () => {
expect(META_COMMON_HEADER).toContain('session_replication_role');
});
it('META_COMMON_FOOTER should reset session_replication_role', () => {
expect(META_COMMON_FOOTER).toContain('session_replication_role');
expect(META_COMMON_FOOTER).toContain('DEFAULT');
});
});
// =============================================================================
// normalizeOutdir
// =============================================================================
describe('normalizeOutdir', () => {
it('should add trailing separator if missing', () => {
const result = normalizeOutdir('/some/path');
expect(result).toBe(`/some/path${path.sep}`);
});
it('should not double-add trailing separator', () => {
const result = normalizeOutdir(`/some/path${path.sep}`);
expect(result).toBe(`/some/path${path.sep}`);
});
it('should handle empty string', () => {
const result = normalizeOutdir('');
expect(result).toBe(path.sep);
});
});
// =============================================================================
// makeReplacer
// =============================================================================
describe('makeReplacer', () => {
it('should replace schema names with snake_case prefixed versions', () => {
const { replacer } = makeReplacer({
schemas: [
{ name: 'public', schema_name: 'pets_public' },
{ name: 'private', schema_name: 'pets_private' }
],
name: 'my-extension'
});
expect(replacer('pets_public')).toBe('my_extension_public');
expect(replacer('pets_private')).toBe('my_extension_private');
});
it('should replace the extension name placeholder', () => {
const { replacer } = makeReplacer({
schemas: [],
name: 'my-extension'
});
expect(replacer('constructive-extension-name')).toBe('my-extension');
});
it('should handle multiple replacements in one string', () => {
const { replacer } = makeReplacer({
schemas: [
{ name: 'public', schema_name: 'pets_public' }
],
name: 'my-ext'
});
const input = 'SELECT * FROM pets_public.users WHERE constructive-extension-name = true';
const result = replacer(input);
expect(result).toContain('my_ext_public');
expect(result).toContain('my-ext');
expect(result).not.toContain('pets_public');
expect(result).not.toContain('constructive-extension-name');
});
it('should return empty string for empty/falsy input', () => {
const { replacer } = makeReplacer({ schemas: [], name: 'test' });
expect(replacer('')).toBe('');
});
it('should use schemaPrefix when provided instead of name for schema replacement', () => {
const { replacer } = makeReplacer({
schemas: [
{ name: 'auth_public', schema_name: 'pets_auth_public' }
],
name: 'pets-services',
schemaPrefix: 'pets'
});
// Schema replacement should use the schemaPrefix, not name
expect(replacer('pets_auth_public')).toBe('pets_auth_public');
});
it('should return replace array with correct length', () => {
const { replace } = makeReplacer({
schemas: [
{ name: 'public', schema_name: 'pets_public' },
{ name: 'private', schema_name: 'pets_private' }
],
name: 'my-ext'
});
// 2 schema replacements + 1 extension name replacement
expect(replace).toHaveLength(3);
expect(replace[0][0]).toBeInstanceOf(RegExp);
expect(typeof replace[0][1]).toBe('string');
});
});