|
8 | 8 | */
|
9 | 9 |
|
10 | 10 | import assert from 'node:assert';
|
11 |
| -import { autoCompare, deepSort } from './compare-database.js'; |
| 11 | +import { autoCompare, deepSort, normalizeDefinition } from './compare-database.js'; |
12 | 12 |
|
13 | 13 | // Test helper function
|
14 | 14 | const runTest = (testName, testFn) => {
|
@@ -212,3 +212,101 @@ runTest('deepSort - mixed types in array', () => {
|
212 | 212 |
|
213 | 213 | assert.deepStrictEqual(result, expected);
|
214 | 214 | });
|
| 215 | + |
| 216 | +// Test cases for normalizeDefinition function |
| 217 | +runTest('normalizeDefinition - single line without leading/trailing spaces', () => { |
| 218 | + const input = 'CREATE FUNCTION test() RETURNS void'; |
| 219 | + const expected = 'CREATE FUNCTION test() RETURNS void'; |
| 220 | + const result = normalizeDefinition(input); |
| 221 | + |
| 222 | + assert.strictEqual(result, expected); |
| 223 | +}); |
| 224 | + |
| 225 | +runTest('normalizeDefinition - single line with leading/trailing spaces', () => { |
| 226 | + const input = ' CREATE FUNCTION test() RETURNS void '; |
| 227 | + const expected = 'CREATE FUNCTION test() RETURNS void'; |
| 228 | + const result = normalizeDefinition(input); |
| 229 | + |
| 230 | + assert.strictEqual(result, expected); |
| 231 | +}); |
| 232 | + |
| 233 | +runTest('normalizeDefinition - multi-line with various indentation', () => { |
| 234 | + const input = `CREATE OR REPLACE FUNCTION test_function() |
| 235 | + RETURNS trigger |
| 236 | + LANGUAGE plpgsql |
| 237 | + AS $function$ |
| 238 | + BEGIN |
| 239 | + IF NEW.status IS NULL THEN |
| 240 | + NEW.status := 'active'; |
| 241 | + END IF; |
| 242 | + RETURN NEW; |
| 243 | + END; |
| 244 | + $function$`; |
| 245 | + |
| 246 | + const expected = `CREATE OR REPLACE FUNCTION test_function() |
| 247 | +RETURNS trigger |
| 248 | +LANGUAGE plpgsql |
| 249 | +AS $function$ |
| 250 | +BEGIN |
| 251 | +IF NEW.status IS NULL THEN |
| 252 | +NEW.status := 'active'; |
| 253 | +END IF; |
| 254 | +RETURN NEW; |
| 255 | +END; |
| 256 | +$function$`; |
| 257 | + |
| 258 | + const result = normalizeDefinition(input); |
| 259 | + assert.strictEqual(result, expected); |
| 260 | +}); |
| 261 | + |
| 262 | +runTest('normalizeDefinition - trigger action statement', () => { |
| 263 | + const input = ` BEGIN |
| 264 | + UPDATE table1 SET updated_at = NOW() WHERE id = NEW.id; |
| 265 | + INSERT INTO audit_log (table_name, action) VALUES ('table1', 'UPDATE'); |
| 266 | + RETURN NEW; |
| 267 | + END `; |
| 268 | + |
| 269 | + const expected = `BEGIN |
| 270 | +UPDATE table1 SET updated_at = NOW() WHERE id = NEW.id; |
| 271 | +INSERT INTO audit_log (table_name, action) VALUES ('table1', 'UPDATE'); |
| 272 | +RETURN NEW; |
| 273 | +END`; |
| 274 | + |
| 275 | + const result = normalizeDefinition(input); |
| 276 | + assert.strictEqual(result, expected); |
| 277 | +}); |
| 278 | + |
| 279 | +runTest('normalizeDefinition - empty lines and mixed spacing', () => { |
| 280 | + const input = ` CREATE FUNCTION complex_func() |
| 281 | +
|
| 282 | + RETURNS TABLE(id integer, name text) |
| 283 | + AS $$ |
| 284 | + SELECT id, name |
| 285 | + FROM users |
| 286 | + WHERE active = true; |
| 287 | + $$`; |
| 288 | + |
| 289 | + const expected = `CREATE FUNCTION complex_func() |
| 290 | +
|
| 291 | +RETURNS TABLE(id integer, name text) |
| 292 | +AS $$ |
| 293 | +SELECT id, name |
| 294 | +FROM users |
| 295 | +WHERE active = true; |
| 296 | +$$`; |
| 297 | + |
| 298 | + const result = normalizeDefinition(input); |
| 299 | + assert.strictEqual(result, expected); |
| 300 | +}); |
| 301 | + |
| 302 | +runTest('normalizeDefinition - non-string input', () => { |
| 303 | + assert.strictEqual(normalizeDefinition(null), null); |
| 304 | + assert.strictEqual(normalizeDefinition(undefined), undefined); |
| 305 | + assert.strictEqual(normalizeDefinition(123), 123); |
| 306 | + assert.deepStrictEqual(normalizeDefinition({ key: 'value' }), { key: 'value' }); |
| 307 | +}); |
| 308 | + |
| 309 | +runTest('normalizeDefinition - empty string', () => { |
| 310 | + assert.strictEqual(normalizeDefinition(''), ''); |
| 311 | + assert.strictEqual(normalizeDefinition(' '), ''); |
| 312 | +}); |
0 commit comments