import { jest } from '@jest/globals' import { Knex } from 'knex' import type { DbConfig } from '../../../../src/Database/index.js' import { KnexDatabase } from '../../../../src/Database/integrations/knex/KnexDatabase.js' const config: DbConfig<'knex'> = { type: 'knex', username: 'username', password: 'password', host: 'host', port: 1234, database: 'database', driver: 'driver', maxConnections: 1, autoCommit: true, } const expectedImplConfig = { client: config.driver, connection: { host: config.host, port: config.port, user: config.username, password: config.password, database: config.database, }, pool: { min: 1, max: config.maxConnections, }, } describe('KnexDatabase', () => { test('KnexDatabase', async () => { const mockTrans = { a: 'b' } as any const mockClient = jest.fn( () => ({ transaction: async () => mockTrans, }) as Knex ) KnexDatabase['knexProvider'] = mockClient const underTest = new KnexDatabase(config) expect(mockClient).toBeCalledWith(expectedImplConfig) const trans = await underTest.transaction() expect(trans).toBeInstanceOf(Function) expect(trans.transaction).toBe(mockTrans) }) test('KnexDatabase - convert camel to snake', async () => { const mockTrans = { a: 'b' } as any const mockClient = jest.fn( () => ({ transaction: async () => mockTrans, }) as Knex ) KnexDatabase['knexProvider'] = mockClient const underTest = new KnexDatabase({ ...config, convertCamelToSnake: true }) expect(mockClient).toBeCalledWith({ ...expectedImplConfig, postProcessResponse: expect.any(Function), wrapIdentifier: expect.any(Function), }) const trans = await underTest.transaction() expect(trans).toBeInstanceOf(Function) expect(trans.transaction).toBe(mockTrans) }) })