UNPKG

@magic-mustard/sqlsync

Version:

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

169 lines (168 loc) 8.01 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; }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const State = __importStar(require("./state.processor")); let mockFileIO; let mockStateFileContents; let processor; const latestMigrationFileName = 'migration_001'; const testNamespace = 'testNamespace'; // Simple mock function to replace jest.fn() const mockFn = () => () => { }; beforeEach(() => { mockFileIO = { write: mockFn(), read: mockFn(), exists: mockFn() }; mockStateFileContents = { fileExists: false, contents: '' }; processor = new State.StateProcessor(mockStateFileContents, mockFileIO, latestMigrationFileName); }); describe('StateProcessor', () => { describe('getMigrationState', () => { test('should return empty state if no state exists for namespace', () => { const state = processor.getMigrationState(testNamespace); expect(state).toEqual({}); }); test('should return state for specific namespace', () => { const initialState = { migrations: { 'migration_001': { [testNamespace]: { data: 'testData' } } } }; mockStateFileContents.fileExists = true; mockStateFileContents.contents = JSON.stringify(initialState); processor = new State.StateProcessor(mockStateFileContents, mockFileIO, latestMigrationFileName); const state = processor.getMigrationState(testNamespace); expect(state).toEqual({ 'migration_001': { data: 'testData' } }); }); }); describe('updateMigrationState', () => { test('should update state for latest migration', () => { const newState = { testNamespace: { data: 'updatedData' } }; processor.updateMigrationState(newState); const state = processor.getMigrationState('testNamespace'); expect(state).toEqual({ [latestMigrationFileName]: { data: 'updatedData' } }); }); test('should merge new state with existing state for latest migration', () => { const initialState = { testNamespace: { existingData: 'initial' } }; processor.updateMigrationState(initialState); const updatedState = { testNamespace: { newData: 'updated' } }; processor.updateMigrationState(updatedState); const state = processor.getMigrationState('testNamespace'); expect(state).toEqual({ [latestMigrationFileName]: { existingData: 'initial', newData: 'updated' } }); }); test('should merge arrays in migration state by overwriting', () => { const initialState = { createTables: { tables: ['public.users'] } }; processor.updateMigrationState(initialState); const updatedState = { createTables: { tables: ['public.profiles'] } }; processor.updateMigrationState(updatedState); const state = processor.getMigrationState('createTables'); expect(state).toEqual({ [latestMigrationFileName]: { tables: ['public.profiles'] } }); }); }); describe('getNamespaceState', () => { test('should return state for namespace', () => { const newState = { testNamespace: { data: 'namespaceData' } }; processor.updateNamespaceState('testNamespace', newState); const namespaceState = processor.getNamespaceState('testNamespace'); expect(namespaceState).toEqual({ data: 'namespaceData' }); }); test('should return empty object if no state exists for namespace', () => { const namespaceState = processor.getNamespaceState('nonexistent'); expect(namespaceState).toEqual({}); }); }); describe('updateNamespaceState', () => { test('should update state for namespace', () => { const newState = { testNamespace: { data: 'updatedNamespaceData' } }; processor.updateNamespaceState('testNamespace', newState); const namespaceState = processor.getNamespaceState('testNamespace'); expect(namespaceState).toEqual({ data: 'updatedNamespaceData' }); }); test('should merge new state with existing state for namespace', () => { const initialState = { testNamespace: { existingData: 'initial' } }; processor.updateNamespaceState('testNamespace', initialState); const updatedState = { testNamespace: { newData: 'updated' } }; processor.updateNamespaceState('testNamespace', updatedState); const namespaceState = processor.getNamespaceState('testNamespace'); expect(namespaceState).toEqual({ existingData: 'initial', newData: 'updated' }); }); test('should merge arrays in namespace state by overwriting', () => { const initialState = { createTables: { tables: ['public.users'] } }; processor.updateNamespaceState('createTables', initialState); const updatedState = { createTables: { tables: ['public.profiles'] } }; processor.updateNamespaceState('createTables', updatedState); const namespaceState = processor.getNamespaceState('createTables'); expect(namespaceState).toEqual({ tables: ['public.profiles'] }); }); }); describe('write', () => { test('should write state to file', () => __awaiter(void 0, void 0, void 0, function* () { const filePath = 'test/path/state.json'; yield processor.write(filePath); // Cannot test mock function call without Jest, just checking if it runs expect(true).toBe(true); })); }); });