UNPKG

@magic-mustard/sqlsync

Version:

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

165 lines (164 loc) 7.66 kB
"use strict"; // config/index.test.ts 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; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); /** * Unit tests for ConfigLoader class. */ const index_1 = require("./index"); const fs = __importStar(require("fs")); const path = __importStar(require("path")); const yaml = __importStar(require("js-yaml")); jest.mock('fs'); jest.mock('path'); jest.mock('js-yaml'); jest.mock('../utils/logger', () => ({ logger: { success: jest.fn(), }, })); describe('ConfigLoader', () => { let configLoader; const mockCwd = '/home/user/project'; const defaultConfigPath = path.join(mockCwd, 'sqlsync.yaml'); beforeEach(() => { configLoader = new index_1.Config(); jest.spyOn(process, 'cwd').mockReturnValue(mockCwd); jest.spyOn(path, 'join').mockImplementation((...args) => args.join('/')); jest.spyOn(path, 'resolve').mockImplementation((p) => `/resolved/${p}`); jest.spyOn(fs, 'existsSync').mockReturnValue(true); jest.spyOn(fs, 'readFileSync').mockReturnValue('mocked content'); jest.spyOn(yaml, 'load').mockReturnValue({ config: { migrations: { outputDir: 'migrations', }, }, schema: { tables: { order: ['create_table.sql'] }, }, }); }); afterEach(() => { jest.clearAllMocks(); }); it('should load configuration from default path if none provided', () => { const result = configLoader.load(); expect(fs.readFileSync).toHaveBeenCalledWith('/resolved//home/user/project/sqlsync.yaml', 'utf8'); expect(result).toBeDefined(); expect(result.config).toBeDefined(); expect(result.schema).toBeDefined(); }); it('should load configuration from specified path', () => { const customPath = '/path/to/custom.yaml'; const result = configLoader.load(customPath); expect(fs.readFileSync).toHaveBeenCalledWith(`/resolved/${customPath}`, 'utf8'); expect(result).toBeDefined(); expect(result.config).toBeDefined(); expect(result.schema).toBeDefined(); }); it('should throw error if configuration file does not exist', () => { jest.spyOn(fs, 'existsSync').mockReturnValue(false); expect(() => configLoader.load('/nonexistent.yaml')).toThrow('Configuration file not found at /resolved//nonexistent.yaml'); }); it('should throw error if YAML parsing fails', () => { jest.spyOn(yaml, 'load').mockImplementation(() => { throw new Error('YAML parse error'); }); expect(() => configLoader.load('/invalid.yaml')).toThrow('Error parsing YAML file /resolved//invalid.yaml: YAML parse error'); }); it('should throw error if configuration is not an object', () => { jest.spyOn(yaml, 'load').mockReturnValue('not an object'); expect(() => configLoader.load('/notobject.yaml')).toThrow('Invalid configuration format in /resolved//notobject.yaml: Expected an object.'); }); it('should throw error if config object is missing', () => { jest.spyOn(yaml, 'load').mockReturnValue({ schema: {} }); expect(() => configLoader.load('/noconfig.yaml')).toThrow('Invalid configuration in /resolved//noconfig.yaml: "config" object is required.'); }); it('should throw error if migrations object is missing', () => { jest.spyOn(yaml, 'load').mockReturnValue({ config: {}, schema: {} }); expect(() => configLoader.load('/nomigrations.yaml')).toThrow('Invalid configuration in /resolved//nomigrations.yaml: "config.migrations" object is required.'); }); it('should throw error if migrations outputDir is missing', () => { jest .spyOn(yaml, 'load') .mockReturnValue({ config: { migrations: {} }, schema: {} }); expect(() => configLoader.load('/nooutputdir.yaml')).toThrow('Invalid configuration in /resolved//nooutputdir.yaml: "config.migrations.outputDir" is required.'); }); it('should throw error if migrations outputDir is not a string', () => { jest.spyOn(yaml, 'load').mockReturnValue({ config: { migrations: { outputDir: 123 } }, schema: {}, }); expect(() => configLoader.load('/invalidoutputdir.yaml')).toThrow('Invalid configuration in /resolved//invalidoutputdir.yaml: "config.migrations.outputDir" should be a string.'); }); it('should throw error if maxRollbacks is not a number', () => { jest.spyOn(yaml, 'load').mockReturnValue({ config: { migrations: { outputDir: 'migrations', maxRollbacks: 'not a number' }, }, schema: {}, }); expect(() => configLoader.load('/invalidmaxrollbacks.yaml')).toThrow('Invalid configuration in /resolved//invalidmaxrollbacks.yaml: "config.migrations.maxRollbacks" should be a number.'); }); it('should throw error if schema object is missing', () => { jest .spyOn(yaml, 'load') .mockReturnValue({ config: { migrations: { outputDir: 'migrations' } } }); expect(() => configLoader.load('/noschema.yaml')).toThrow('Invalid configuration in /resolved//noschema.yaml: "schema" object is required.'); }); it('should successfully validate and return a minimal valid configuration', () => { jest.spyOn(yaml, 'load').mockReturnValue({ config: { migrations: { outputDir: 'migrations' } }, schema: {}, }); const result = configLoader.load('/minimal.yaml'); expect(result).toEqual({ config: { migrations: { outputDir: 'migrations' } }, schema: {}, }); }); it('should successfully validate and return a configuration with maxRollbacks', () => { jest.spyOn(yaml, 'load').mockReturnValue({ config: { migrations: { outputDir: 'migrations', maxRollbacks: 5 } }, schema: { tables: { order: ['create.sql'] } }, }); const result = configLoader.load('/maxrollbacks.yaml'); expect(result).toEqual({ config: { migrations: { outputDir: 'migrations', maxRollbacks: 5 } }, schema: { tables: { order: ['create.sql'] } }, }); }); });