@magic-mustard/sqlsync
Version:
SQLSync simplifies database schema evolution by allowing a declarative approach to table management
74 lines (73 loc) • 3.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const create_table_processor_1 = require("./create.table.processor");
const namespace_1 = require("./namespace");
// Mock dependencies
describe('CreateTablesProcessor', () => {
let mockMigrations;
let mockState;
let mockContentBetweenFlags;
beforeEach(() => {
mockMigrations = {
append: jest.fn(),
getLatestMigrationName: jest.fn().mockReturnValue('migration_1')
};
mockState = {
getMigrationState: jest.fn().mockReturnValue({}),
updateMigrationState: jest.fn(),
getNamespaceState: jest.fn().mockReturnValue({}),
updateNamespaceState: jest.fn()
};
mockContentBetweenFlags = {
extractContentBetweenFlags: jest.fn()
};
});
test('should process CREATE TABLE statements and update state correctly', () => {
const statement = 'CREATE TABLE public.test_table (id INT PRIMARY KEY);';
const file = { path: 'test.sql', contents: statement };
const processor = new create_table_processor_1.CreateTablesProcessor(file, mockMigrations, mockState, mockContentBetweenFlags);
processor.process();
expect(mockMigrations.append).toHaveBeenCalledWith({
token: statement,
filePath: file.path
});
expect(mockState.updateNamespaceState).toHaveBeenCalledWith(expect.objectContaining({
[namespace_1.Tables.CreateTableStateIndex]: expect.objectContaining({
public: expect.arrayContaining(['public.test_table'])
})
}));
});
test('should not process if no CREATE TABLE statements are found', () => {
const file = { path: 'test.sql', contents: 'SELECT * FROM table;' };
const processor = new create_table_processor_1.CreateTablesProcessor(file, mockMigrations, mockState, mockContentBetweenFlags);
processor.process();
expect(mockMigrations.append).not.toHaveBeenCalled();
expect(mockState.updateNamespaceState).not.toHaveBeenCalled();
});
test('should not process CREATE TABLE statement without schema', () => {
const statement = 'CREATE TABLE test_table (id INT PRIMARY KEY);';
const file = { path: 'test.sql', contents: statement };
const processor = new create_table_processor_1.CreateTablesProcessor(file, mockMigrations, mockState, mockContentBetweenFlags);
processor.process();
expect(mockMigrations.append).not.toHaveBeenCalled();
expect(mockState.updateNamespaceState).not.toHaveBeenCalled();
});
test('extracts CREATE TABLE statements directly from content', () => {
const statements = [
'CREATE TABLE public.table1 (id INT PRIMARY KEY);',
'CREATE TABLE public.table2 (name TEXT);'
];
const file = { path: 'test.sql', contents: statements.join('\n') };
const processor = new create_table_processor_1.CreateTablesProcessor(file, mockMigrations, mockState, mockContentBetweenFlags);
processor.process();
expect(mockMigrations.append).toHaveBeenCalledTimes(2);
expect(mockMigrations.append).toHaveBeenCalledWith({
token: statements[0],
filePath: file.path
});
expect(mockMigrations.append).toHaveBeenCalledWith({
token: statements[1],
filePath: file.path
});
});
});