UNPKG

@magic-mustard/sqlsync

Version:

SQLSync simplifies database schema evolution by allowing a declarative approach to table management

130 lines (129 loc) 5.79 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); const fs = __importStar(require("fs")); const sql_files_1 = require("./sql-files"); jest.mock('fs'); describe('SqlFiles', () => { let sqlFiles; const mockSchemaStructure = { tables: { order: ['/path/to/tables/table1.sql', '/path/to/tables/subfolder'], orderedSubdirectoryFileOrder: ['schema.sql', 'data.sql'], }, seeds: { order: ['/path/to/seeds/seed1.sql'], orderedSubdirectoryFileOrder: [], }, }; beforeEach(() => { jest.spyOn(fs, 'statSync').mockImplementation((path) => { if (typeof path === 'string' && path.endsWith('/subfolder')) { return { isFile: () => false, isDirectory: () => true }; } else { return { isFile: () => true, isDirectory: () => false }; } }); jest.spyOn(fs, 'existsSync').mockImplementation((path) => { return (typeof path === 'string' && (path.includes('schema.sql') || path.includes('data.sql'))); }); jest .spyOn(fs, 'readFileSync') .mockImplementation((path) => { return typeof path === 'string' ? `Contents of ${path}` : 'Contents of unknown path'; }); sqlFiles = new sql_files_1.SqlFiles(mockSchemaStructure); }); afterEach(() => { jest.clearAllMocks(); }); it('should return the first SQL file in the order', () => { const result = sqlFiles.next(); expect(result).not.toBeNull(); expect(result === null || result === void 0 ? void 0 : result.location).toBe('/path/to/tables/table1.sql'); expect(result === null || result === void 0 ? void 0 : result.contents).toBe('Contents of /path/to/tables/table1.sql'); }); it('should handle directories and return subdirectory files if they exist', () => { // First call should return table1.sql sqlFiles.next(); // Second call should process the subfolder and return schema.sql const result = sqlFiles.next(); expect(result).not.toBeNull(); expect(result === null || result === void 0 ? void 0 : result.location).toBe('/path/to/tables/subfolder/schema.sql'); expect(result === null || result === void 0 ? void 0 : result.contents).toBe('Contents of /path/to/tables/subfolder/schema.sql'); }); it('should move to the next category after exhausting the current one', () => { // Process tables category sqlFiles.next(); // table1.sql sqlFiles.next(); // subfolder/schema.sql sqlFiles.next(); // subfolder/data.sql // Should move to seeds category const result = sqlFiles.next(); expect(result).not.toBeNull(); expect(result === null || result === void 0 ? void 0 : result.location).toBe('/path/to/seeds/seed1.sql'); expect(result === null || result === void 0 ? void 0 : result.contents).toBe('Contents of /path/to/seeds/seed1.sql'); }); it('should return null when there are no more files', () => { // Process all files sqlFiles.next(); // table1.sql sqlFiles.next(); // subfolder/schema.sql sqlFiles.next(); // subfolder/data.sql sqlFiles.next(); // seed1.sql const result = sqlFiles.next(); expect(result).toBeNull(); }); it('should skip subdirectory files that do not exist', () => { jest.spyOn(fs, 'existsSync').mockImplementation((path) => { return (typeof path === 'string' && path.includes('schema.sql') && !path.includes('data.sql')); }); sqlFiles = new sql_files_1.SqlFiles(mockSchemaStructure); // Process table1.sql sqlFiles.next(); // Should return schema.sql, skip data.sql as it doesn't exist const result = sqlFiles.next(); expect(result).not.toBeNull(); expect(result === null || result === void 0 ? void 0 : result.location).toBe('/path/to/tables/subfolder/schema.sql'); // Next should be seeds as data.sql is skipped const nextResult = sqlFiles.next(); expect(nextResult).not.toBeNull(); expect(nextResult === null || nextResult === void 0 ? void 0 : nextResult.location).toBe('/path/to/seeds/seed1.sql'); }); });