@magic-mustard/sqlsync
Version:
SQLSync simplifies database schema evolution by allowing a declarative approach to table management
73 lines (72 loc) • 3.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const create_enum_processor_1 = require("./create.enum.processor");
const namespace_1 = require("./namespace");
describe('CreateEnumsProcessor', () => {
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 TYPE/ENUM statements and update state correctly', () => {
const statement = 'CREATE TYPE public.user_role AS ENUM (\'admin\', \'user\');';
const file = { path: 'test.sql', contents: statement };
const processor = new create_enum_processor_1.CreateEnumsProcessor(file, mockMigrations, mockState, mockContentBetweenFlags);
processor.process();
expect(mockMigrations.append).toHaveBeenCalledWith({
token: statement,
filePath: file.path
});
expect(mockState.updateNamespaceState).toHaveBeenCalledWith(expect.objectContaining({
[namespace_1.Enums.CreateEnumStateIndex]: expect.objectContaining({
public: expect.arrayContaining(['public.user_role'])
})
}));
});
test('should not process if no CREATE TYPE/ENUM statements are found', () => {
const file = { path: 'test.sql', contents: 'SELECT * FROM table;' };
const processor = new create_enum_processor_1.CreateEnumsProcessor(file, mockMigrations, mockState, mockContentBetweenFlags);
processor.process();
expect(mockMigrations.append).not.toHaveBeenCalled();
expect(mockState.updateNamespaceState).not.toHaveBeenCalled();
});
test('should not process CREATE TYPE/ENUM statement without schema', () => {
const statement = 'CREATE TYPE user_role AS ENUM (\'admin\', \'user\');';
const file = { path: 'test.sql', contents: statement };
const processor = new create_enum_processor_1.CreateEnumsProcessor(file, mockMigrations, mockState, mockContentBetweenFlags);
processor.process();
expect(mockMigrations.append).not.toHaveBeenCalled();
expect(mockState.updateNamespaceState).not.toHaveBeenCalled();
});
test('extracts multiple CREATE TYPE/ENUM statements directly from content', () => {
const statements = [
'CREATE TYPE public.role_type AS ENUM (\'admin\', \'user\');',
'CREATE TYPE public.status_type AS ENUM (\'active\', \'inactive\');'
];
const file = { path: 'test.sql', contents: statements.join('\n') };
const processor = new create_enum_processor_1.CreateEnumsProcessor(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
});
});
});