|
1
|
-
import{afterAll,afterEach,expect,mock,setDefaultTimeout,setSystemTime}from"bun:test";import{PostgreSqlContainer}from"@testcontainers/postgresql";import{Wait}from"testcontainers";import{migrate}from"@kunk/database";import{DatabaseConfig}from"@kunk/server";async function dockerExec(e,t){let n=new(await(import(`dockerode`).then(e=>e.default)));try{let r=await(await n.getContainer(e.getId()??``).exec({Cmd:t,AttachStdout:!0,AttachStderr:!0})).start({abortSignal:AbortSignal.timeout(2e3)}),i=``;r.on(`data`,e=>{i+=e.toString()}),await new Promise(e=>r.on(`end`,e)),console.log(`Output:`,i)}catch{}}var PostgresContainer=class e{container=null;static CLEAR_TEST_DATA;constructor(e){this.options=e}static async start(t={}){let n=new e(t);return await n.start(),n}async start(){let e=!!process.env.LOGGING,t=new PostgreSqlContainer(`postgres:18`).withWaitStrategy(Wait.forLogMessage(`database system is ready to accept connections`,1)).withName(`postgres-${this.database}`).withDatabase(this.database).withUsername(this.username).withPassword(this.password).withResourcesQuota({memory:.5,cpu:1}).withLogConsumer(t=>{t.on(`data`,t=>{e&&console.debug(`PostgreSQL: `,t.toString())})});this.options.port&&(t=t.withReuse().withExposedPorts({container:5432,host:Number(this.options.port)})),this.container=await t.start(),await new Promise(e=>setTimeout(e,1e3)),await migrate({adapter:`postgres`,hostname:this.hostname,port:this.port,username:this.username,password:this.password,database:this.database,max:1}),console.log(`DATABASE CONFIG FINISHED`)}get hostname(){return this.container?.getHost()}get port(){return this.container?.getMappedPort(5432)}get username(){return this.options.username??`kunk`}get password(){return this.options.password??`kunk`}get database(){return this.options.database??`kunk`}async stop(){await this.container?.stop(),this.container=null}async clear(){this.container&&await dockerExec(this.container,[`psql`,`--username`,`kunk`,`--password`,`kunk`,`--host`,this.container.getHost(),`--port`,this.container.getMappedPort(5432).toString(),`--database`,`kunk`,`--command`,`DROP DATABASE kunk`])}};setSystemTime(new Date(`2025-03-01T09:00:00.000Z`)),setDefaultTimeout(1e4);const db=await PostgresContainer.start();DatabaseConfig.set({uri:`postgresql://app_user:${db.password}@${db.hostname}:${db.port}/${db.database}`,maxPoolSize:3,idleTimeout:5,connectionTimeout:30}),afterAll(async()=>{await db.stop()}),afterEach(async()=>{mock.clearAllMocks(),mock.restore()});function expectToMatchObject(e,t){return expect(e).toMatchObject(t)}async function CLEAR_TEST_DATA(){await db.clear()}export{CLEAR_TEST_DATA,expectToMatchObject};
|
|
1
|
+
import{afterAll,afterEach,expect,mock,setDefaultTimeout,setSystemTime}from"bun:test";import{PostgresContainer}from"@kunk/docker";import{DatabaseConfig}from"@kunk/server";setSystemTime(new Date(`2025-03-01T09:00:00.000Z`)),setDefaultTimeout(1e4);const db=await PostgresContainer.start();DatabaseConfig.set({uri:`postgresql://app_user:${db.password}@${db.hostname}:${db.port}/${db.database}`,maxPoolSize:3,idleTimeout:5,connectionTimeout:30}),afterAll(async()=>{await db.stop()}),afterEach(async()=>{mock.clearAllMocks(),mock.restore()});function expectToMatchObject(e,t){return expect(e).toMatchObject(t)}async function CLEAR_TEST_DATA(){await db.clear()}export{CLEAR_TEST_DATA,expectToMatchObject};
|