@magic-mustard/sqlsync
Version:
SQLSync simplifies database schema evolution by allowing a declarative approach to table management
58 lines (57 loc) • 2.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const table_processor_1 = require("./table.processor");
// Mock dependencies
describe('TableProcessor', () => {
let processor;
let mockFile;
let mockMigrations;
let mockState;
let mockContentBetweenFlags;
beforeEach(() => {
mockFile = { path: 'test.sql', contents: '' };
mockMigrations = {
append: jest.fn(),
getLatestMigrationName: jest.fn().mockReturnValue('test-migration')
};
mockState = {
getMigrationState: jest.fn().mockReturnValue({}),
updateMigrationState: jest.fn(),
getNamespaceState: jest.fn().mockReturnValue({}),
updateNamespaceState: jest.fn()
};
mockContentBetweenFlags = {
extractContentBetweenFlags: jest.fn().mockReturnValue([])
};
processor = new table_processor_1.TableProcessor(mockFile, mockMigrations, mockState, mockContentBetweenFlags);
});
describe('process', () => {
test('should process CREATE TABLE statements', () => {
// Mock content to simulate a CREATE TABLE statement with the correct flags and schema
mockFile.contents = '-- sqlsync: tables\n-- sqlsync: startCreateTable\nCREATE TABLE public.test_table (id INT PRIMARY KEY);\n-- sqlsync: endCreateTable';
jest.spyOn(mockContentBetweenFlags, 'extractContentBetweenFlags').mockImplementation(() => [
'CREATE TABLE public.test_table (id INT PRIMARY KEY);'
]);
processor.process();
expect(mockMigrations.append).toHaveBeenCalled();
});
test('should handle empty or irrelevant content', () => {
mockFile.contents = '-- sqlsync: other\n-- Some unrelated content';
jest.spyOn(mockContentBetweenFlags, 'extractContentBetweenFlags').mockImplementation(() => []);
processor.process();
expect(mockMigrations.append).not.toHaveBeenCalled();
});
test('should process ALTER TABLE statements if present', () => {
// Mock content to simulate an ALTER TABLE statement
mockFile.contents = '-- sqlsync: tables\n-- sqlsync: startAlterTable\nALTER TABLE public.test_table ADD COLUMN new_column INT;\n-- sqlsync: endAlterTable';
jest.spyOn(mockContentBetweenFlags, 'extractContentBetweenFlags').mockImplementation((content, filePath, startFlag, endFlag) => {
if (startFlag === 'startAlterTable') {
return ['ALTER TABLE public.test_table ADD COLUMN new_column INT;'];
}
return [];
});
processor.process();
expect(mockMigrations.append).toHaveBeenCalled();
});
});
});