@magic-mustard/sqlsync
Version:
SQLSync simplifies database schema evolution by allowing a declarative approach to table management
76 lines (75 loc) • 3.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const alter_enum_processor_1 = require("./alter.enum.processor");
const namespace_1 = require("./namespace");
describe('AlterEnumsProcessor', () => {
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 ALTER TYPE/ENUM statements and update state correctly', () => {
const statement = 'ALTER TYPE public.user_role ADD VALUE \'superuser\';';
const file = { path: 'test.sql', contents: statement };
const processor = new alter_enum_processor_1.AlterEnumsProcessor(file, mockMigrations, mockState, mockContentBetweenFlags);
processor.process();
expect(mockMigrations.append).toHaveBeenCalledWith({
token: statement,
filePath: file.path,
checksum: expect.any(String)
});
expect(mockState.updateMigrationState).toHaveBeenCalledWith(namespace_1.Enums.AlterEnumStateIndex, expect.objectContaining({
alterEnum: expect.objectContaining({
'public.user_role': expect.arrayContaining([expect.any(String)])
})
}));
});
test('should not process if no ALTER TYPE/ENUM statements are found', () => {
const file = { path: 'test.sql', contents: 'SELECT * FROM table;' };
const processor = new alter_enum_processor_1.AlterEnumsProcessor(file, mockMigrations, mockState, mockContentBetweenFlags);
processor.process();
expect(mockMigrations.append).not.toHaveBeenCalled();
expect(mockState.updateMigrationState).not.toHaveBeenCalled();
});
test('should not process ALTER TYPE/ENUM statement without schema', () => {
const statement = 'ALTER TYPE user_role ADD VALUE \'superuser\';';
const file = { path: 'test.sql', contents: statement };
const processor = new alter_enum_processor_1.AlterEnumsProcessor(file, mockMigrations, mockState, mockContentBetweenFlags);
processor.process();
expect(mockMigrations.append).not.toHaveBeenCalled();
expect(mockState.updateMigrationState).not.toHaveBeenCalled();
});
test('extracts multiple ALTER TYPE/ENUM statements directly from content', () => {
const statements = [
'ALTER TYPE public.role_type ADD VALUE \'manager\';',
'ALTER TYPE public.status_type ADD VALUE \'pending\';'
];
const file = { path: 'test.sql', contents: statements.join('\n') };
const processor = new alter_enum_processor_1.AlterEnumsProcessor(file, mockMigrations, mockState, mockContentBetweenFlags);
processor.process();
expect(mockMigrations.append).toHaveBeenCalledTimes(2);
expect(mockMigrations.append).toHaveBeenCalledWith({
token: statements[0],
filePath: file.path,
checksum: expect.any(String)
});
expect(mockMigrations.append).toHaveBeenCalledWith({
token: statements[1],
filePath: file.path,
checksum: expect.any(String)
});
});
});