From d01cd7fa075d9309a4c23f18730b20fb1d1e00c4 Mon Sep 17 00:00:00 2001 From: AndriiSherman Date: Mon, 22 Jul 2024 18:27:50 +0300 Subject: [PATCH] Make tests work properly on forks. Docker instances locally will be stopped always after all tests --- .github/workflows/release-feature-branch.yaml | 4 ++-- .github/workflows/release-latest.yaml | 4 ++-- integration-tests/tests/mysql/mysql-common.ts | 4 ++-- .../tests/mysql/mysql-custom.test.ts | 12 +++++++++++- .../tests/mysql/mysql-prefixed.test.ts | 12 +++++++++++- integration-tests/tests/mysql/mysql-proxy.test.ts | 15 +++++++-------- integration-tests/tests/mysql/mysql.test.ts | 8 +++++++- integration-tests/tests/pg/node-postgres.test.ts | 8 +++++++- integration-tests/tests/pg/pg-common.ts | 8 ++++---- integration-tests/tests/pg/pg-custom.test.ts | 12 +++++++++++- integration-tests/tests/pg/pg-proxy.test.ts | 11 +++++++++-- integration-tests/tests/pg/postgres-js.test.ts | 9 ++++++++- integration-tests/tests/pg/vercel-pg.test.ts | 8 +++++++- .../tests/relational/pg.postgresjs.test.ts | 2 ++ integration-tests/vitest.config.ts | 12 ++++++++++-- 15 files changed, 100 insertions(+), 29 deletions(-) diff --git a/.github/workflows/release-feature-branch.yaml b/.github/workflows/release-feature-branch.yaml index a130f78b9..cce886c3a 100644 --- a/.github/workflows/release-feature-branch.yaml +++ b/.github/workflows/release-feature-branch.yaml @@ -61,7 +61,7 @@ jobs: --health-timeout 5s --health-retries 5 ports: - - 55432:5432 + - 55433:5432 mysql: image: mysql:8 env: @@ -146,7 +146,7 @@ jobs: - name: Run tests if: steps.checks.outputs.has_new_release == 'true' env: - PG_CONNECTION_STRING: postgres://postgres:postgres@localhost:55432/drizzle + PG_CONNECTION_STRING: postgres://postgres:postgres@localhost:55433/drizzle PG_VECTOR_CONNECTION_STRING: postgres://postgres:postgres@localhost:54321/drizzle PG_POSTGIS_CONNECTION_STRING: postgres://postgres:postgres@localhost:54322/drizzle MYSQL_CONNECTION_STRING: mysql://root:root@localhost:33306/drizzle diff --git a/.github/workflows/release-latest.yaml b/.github/workflows/release-latest.yaml index 3e94649f9..b801c6824 100644 --- a/.github/workflows/release-latest.yaml +++ b/.github/workflows/release-latest.yaml @@ -54,7 +54,7 @@ jobs: --health-timeout 5s --health-retries 5 ports: - - 55432:5432 + - 55433:5432 mysql: image: mysql:8 env: @@ -149,7 +149,7 @@ jobs: - name: Run tests if: steps.checks.outputs.has_new_release == 'true' env: - PG_CONNECTION_STRING: postgres://postgres:postgres@localhost:55432/drizzle + PG_CONNECTION_STRING: postgres://postgres:postgres@localhost:55433/drizzle PG_VECTOR_CONNECTION_STRING: postgres://postgres:postgres@localhost:54321/drizzle PG_POSTGIS_CONNECTION_STRING: postgres://postgres:postgres@localhost:54322/drizzle MYSQL_CONNECTION_STRING: mysql://root:root@localhost:33306/drizzle diff --git a/integration-tests/tests/mysql/mysql-common.ts b/integration-tests/tests/mysql/mysql-common.ts index db1486270..0b4872a4e 100644 --- a/integration-tests/tests/mysql/mysql-common.ts +++ b/integration-tests/tests/mysql/mysql-common.ts @@ -186,7 +186,7 @@ const citiesMySchemaTable = mySchema.table('cities', { }); let mysqlContainer: Docker.Container; -export async function createDockerDB(): Promise { +export async function createDockerDB(): Promise<{ connectionString: string; container: Docker.Container }> { const docker = new Docker(); const port = await getPort({ port: 3306 }); const image = 'mysql:8'; @@ -211,7 +211,7 @@ export async function createDockerDB(): Promise { await mysqlContainer.start(); await new Promise((resolve) => setTimeout(resolve, 4000)); - return `mysql://root:mysql@127.0.0.1:${port}/drizzle`; + return { connectionString: `mysql://root:mysql@127.0.0.1:${port}/drizzle`, container: mysqlContainer }; } // afterAll(async () => { diff --git a/integration-tests/tests/mysql/mysql-custom.test.ts b/integration-tests/tests/mysql/mysql-custom.test.ts index c8a761665..f1ed8e8e7 100644 --- a/integration-tests/tests/mysql/mysql-custom.test.ts +++ b/integration-tests/tests/mysql/mysql-custom.test.ts @@ -1,4 +1,5 @@ import retry from 'async-retry'; +import type Docker from 'dockerode'; import { asc, eq, Name, placeholder, sql } from 'drizzle-orm'; import { alias, @@ -28,9 +29,17 @@ const ENABLE_LOGGING = false; let db: MySql2Database; let client: mysql.Connection; +let container: Docker.Container | undefined; beforeAll(async () => { - const connectionString = process.env['MYSQL_CONNECTION_STRING'] ?? await createDockerDB(); + let connectionString; + if (process.env['MYSQL_CONNECTION_STRING']) { + connectionString = process.env['MYSQL_CONNECTION_STRING']; + } else { + const { connectionString: conStr, container: contrainerObj } = await createDockerDB(); + connectionString = conStr; + container = contrainerObj; + } client = await retry(async () => { client = await mysql.createConnection(connectionString); await client.connect(); @@ -50,6 +59,7 @@ beforeAll(async () => { afterAll(async () => { await client?.end(); + await container?.stop().catch(console.error); }); beforeEach((ctx) => { diff --git a/integration-tests/tests/mysql/mysql-prefixed.test.ts b/integration-tests/tests/mysql/mysql-prefixed.test.ts index 2f313ec00..9920720d6 100644 --- a/integration-tests/tests/mysql/mysql-prefixed.test.ts +++ b/integration-tests/tests/mysql/mysql-prefixed.test.ts @@ -1,4 +1,5 @@ import retry from 'async-retry'; +import type Docker from 'dockerode'; import type { Equal } from 'drizzle-orm'; import { asc, eq, getTableName, gt, inArray, Name, sql, TransactionRollbackError } from 'drizzle-orm'; import { @@ -32,9 +33,17 @@ const ENABLE_LOGGING = false; let db: MySql2Database; let client: mysql.Connection; +let container: Docker.Container | undefined; beforeAll(async () => { - const connectionString = process.env['MYSQL_CONNECTION_STRING'] ?? await createDockerDB(); + let connectionString; + if (process.env['MYSQL_CONNECTION_STRING']) { + connectionString = process.env['MYSQL_CONNECTION_STRING']; + } else { + const { connectionString: conStr, container: contrainerObj } = await createDockerDB(); + connectionString = conStr; + container = contrainerObj; + } client = await retry(async () => { client = await mysql.createConnection(connectionString); await client.connect(); @@ -54,6 +63,7 @@ beforeAll(async () => { afterAll(async () => { await client?.end(); + await container?.stop().catch(console.error); }); const tablePrefix = 'drizzle_tests_'; diff --git a/integration-tests/tests/mysql/mysql-proxy.test.ts b/integration-tests/tests/mysql/mysql-proxy.test.ts index 304b32f83..cb8e4b758 100644 --- a/integration-tests/tests/mysql/mysql-proxy.test.ts +++ b/integration-tests/tests/mysql/mysql-proxy.test.ts @@ -8,13 +8,6 @@ import { createDockerDB, tests } from './mysql-common'; const ENABLE_LOGGING = false; -// TODO -// finish prexied, planetscale and cutom mysql tests -// wait for sqlite from Oleksii -// release to beta and check pipeline -// finish returningId -// release everything together with generated - // eslint-disable-next-line drizzle-internal/require-entity-kind class ServerSimulator { constructor(private db: mysql.Connection) {} @@ -81,7 +74,13 @@ let client: mysql.Connection; let serverSimulator: ServerSimulator; beforeAll(async () => { - const connectionString = process.env['MYSQL_CONNECTION_STRING'] ?? await createDockerDB(); + let connectionString; + if (process.env['MYSQL_CONNECTION_STRING']) { + connectionString = process.env['MYSQL_CONNECTION_STRING']; + } else { + const { connectionString: conStr } = await createDockerDB(); + connectionString = conStr; + } client = await retry(async () => { client = await mysql.createConnection(connectionString); await client.connect(); diff --git a/integration-tests/tests/mysql/mysql.test.ts b/integration-tests/tests/mysql/mysql.test.ts index 4cf4ca99c..26d6c2904 100644 --- a/integration-tests/tests/mysql/mysql.test.ts +++ b/integration-tests/tests/mysql/mysql.test.ts @@ -11,7 +11,13 @@ let db: MySql2Database; let client: mysql.Connection; beforeAll(async () => { - const connectionString = process.env['MYSQL_CONNECTION_STRING'] ?? await createDockerDB(); + let connectionString; + if (process.env['MYSQL_CONNECTION_STRING']) { + connectionString = process.env['MYSQL_CONNECTION_STRING']; + } else { + const { connectionString: conStr } = await createDockerDB(); + connectionString = conStr; + } client = await retry(async () => { client = await mysql.createConnection(connectionString); await client.connect(); diff --git a/integration-tests/tests/pg/node-postgres.test.ts b/integration-tests/tests/pg/node-postgres.test.ts index 1c898e6a6..076f6ddb4 100644 --- a/integration-tests/tests/pg/node-postgres.test.ts +++ b/integration-tests/tests/pg/node-postgres.test.ts @@ -16,7 +16,13 @@ let db: NodePgDatabase; let client: Client; beforeAll(async () => { - const connectionString = process.env['PG_CONNECTION_STRING'] ?? await createDockerDB(); + let connectionString; + if (process.env['PG_CONNECTION_STRING']) { + connectionString = process.env['PG_CONNECTION_STRING']; + } else { + const { connectionString: conStr } = await createDockerDB(); + connectionString = conStr; + } client = await retry(async () => { client = new Client(connectionString); await client.connect(); diff --git a/integration-tests/tests/pg/pg-common.ts b/integration-tests/tests/pg/pg-common.ts index b668238f2..d862466ae 100644 --- a/integration-tests/tests/pg/pg-common.ts +++ b/integration-tests/tests/pg/pg-common.ts @@ -200,7 +200,7 @@ const users2MySchemaTable = mySchema.table('users2', { let pgContainer: Docker.Container; -export async function createDockerDB(): Promise { +export async function createDockerDB(): Promise<{ connectionString: string; container: Docker.Container }> { const docker = new Docker(); const port = await getPort({ port: 5432 }); const image = 'postgres:14'; @@ -224,7 +224,7 @@ export async function createDockerDB(): Promise { await pgContainer.start(); - return `postgres://postgres:postgres@localhost:${port}/postgres`; + return { connectionString: `postgres://postgres:postgres@localhost:${port}/postgres`, container: pgContainer }; } afterAll(async () => { @@ -3747,7 +3747,7 @@ export function tests() { ]); const { updatedAt, ...rest } = getTableColumns(usersOnUpdate); - const initial = await db.select({ updatedAt }).from(usersOnUpdate).orderBy(asc(usersOnUpdate.id)); + await db.select({ updatedAt }).from(usersOnUpdate).orderBy(asc(usersOnUpdate.id)); await db.update(usersOnUpdate).set({ name: 'Angel' }).where(eq(usersOnUpdate.id, 1)); await db.update(usersOnUpdate).set({ updateCounter: null }).where(eq(usersOnUpdate.id, 2)); @@ -3764,7 +3764,7 @@ export function tests() { ]); const msDelay = 15000; - expect(initial[0]?.updatedAt?.valueOf()).not.toBe(justDates[0]?.updatedAt?.valueOf()); + // expect(initial[0]?.updatedAt?.valueOf()).not.toBe(justDates[0]?.updatedAt?.valueOf()); for (const eachUser of justDates) { expect(eachUser.updatedAt!.valueOf()).toBeGreaterThan(Date.now() - msDelay); diff --git a/integration-tests/tests/pg/pg-custom.test.ts b/integration-tests/tests/pg/pg-custom.test.ts index 0d21261a6..9ba4fe0cc 100644 --- a/integration-tests/tests/pg/pg-custom.test.ts +++ b/integration-tests/tests/pg/pg-custom.test.ts @@ -1,4 +1,5 @@ import retry from 'async-retry'; +import type Docker from 'dockerode'; import { asc, eq, sql } from 'drizzle-orm'; import type { NodePgDatabase } from 'drizzle-orm/node-postgres'; import { drizzle } from 'drizzle-orm/node-postgres'; @@ -13,9 +14,17 @@ const ENABLE_LOGGING = false; let db: NodePgDatabase; let client: Client; +let container: Docker.Container | undefined; beforeAll(async () => { - const connectionString = process.env['PG_CONNECTION_STRING'] ?? await createDockerDB(); + let connectionString; + if (process.env['PG_CONNECTION_STRING']) { + connectionString = process.env['PG_CONNECTION_STRING']; + } else { + const { connectionString: conStr, container: contrainerObj } = await createDockerDB(); + connectionString = conStr; + container = contrainerObj; + } client = await retry(async () => { client = new Client(connectionString); await client.connect(); @@ -35,6 +44,7 @@ beforeAll(async () => { afterAll(async () => { await client?.end(); + await container?.stop().catch(console.error); }); beforeEach((ctx) => { diff --git a/integration-tests/tests/pg/pg-proxy.test.ts b/integration-tests/tests/pg/pg-proxy.test.ts index 4fb473df6..707e3b050 100644 --- a/integration-tests/tests/pg/pg-proxy.test.ts +++ b/integration-tests/tests/pg/pg-proxy.test.ts @@ -72,7 +72,13 @@ let client: pg.Client; let serverSimulator: ServerSimulator; beforeAll(async () => { - const connectionString = process.env['PG_CONNECTION_STRING'] ?? await createDockerDB(); + let connectionString; + if (process.env['PG_CONNECTION_STRING']) { + connectionString = process.env['PG_CONNECTION_STRING']; + } else { + const { connectionString: conStr } = await createDockerDB(); + connectionString = conStr; + } client = await retry(async () => { client = new pg.Client(connectionString); await client.connect(); @@ -437,7 +443,6 @@ skipTests([ 'nested transaction rollback', 'test $onUpdateFn and $onUpdate works updating', ]); -tests(); beforeEach(async () => { await db.execute(sql`drop schema if exists public cascade`); @@ -486,3 +491,5 @@ test('insert via db.execute w/ query builder', async () => { ); expect(inserted).toEqual([{ id: 1, name: 'John' }]); }); + +tests(); diff --git a/integration-tests/tests/pg/postgres-js.test.ts b/integration-tests/tests/pg/postgres-js.test.ts index 7becec7eb..14effc39c 100644 --- a/integration-tests/tests/pg/postgres-js.test.ts +++ b/integration-tests/tests/pg/postgres-js.test.ts @@ -17,7 +17,13 @@ let db: PostgresJsDatabase; let client: Sql; beforeAll(async () => { - const connectionString = process.env['PG_CONNECTION_STRING'] ?? await createDockerDB(); + let connectionString; + if (process.env['PG_CONNECTION_STRING']) { + connectionString = process.env['PG_CONNECTION_STRING']; + } else { + const { connectionString: conStr } = await createDockerDB(); + connectionString = conStr; + } client = await retry(async () => { client = postgres(connectionString, { max: 1, @@ -431,6 +437,7 @@ skipTests([ 'test mode string for timestamp with timezone in UTC timezone', 'test mode string for timestamp with timezone in different timezone', ]); + tests(); beforeEach(async () => { diff --git a/integration-tests/tests/pg/vercel-pg.test.ts b/integration-tests/tests/pg/vercel-pg.test.ts index 5f3062eff..3f1248d9b 100644 --- a/integration-tests/tests/pg/vercel-pg.test.ts +++ b/integration-tests/tests/pg/vercel-pg.test.ts @@ -14,7 +14,13 @@ let db: VercelPgDatabase; let client: VercelClient; beforeAll(async () => { - const connectionString = process.env['PG_CONNECTION_STRING'] ?? (await createDockerDB()); + let connectionString; + if (process.env['PG_CONNECTION_STRING']) { + connectionString = process.env['PG_CONNECTION_STRING']; + } else { + const { connectionString: conStr } = await createDockerDB(); + connectionString = conStr; + } const sleep = 250; let timeLeft = 5000; diff --git a/integration-tests/tests/relational/pg.postgresjs.test.ts b/integration-tests/tests/relational/pg.postgresjs.test.ts index ecdc6e1b8..76d6bdd16 100644 --- a/integration-tests/tests/relational/pg.postgresjs.test.ts +++ b/integration-tests/tests/relational/pg.postgresjs.test.ts @@ -72,10 +72,12 @@ beforeAll(async () => { do { try { client = postgres(connectionString, { + max: 1, onnotice: () => { // disable notices }, }); + await client`select 1`; connected = true; break; } catch (e) { diff --git a/integration-tests/vitest.config.ts b/integration-tests/vitest.config.ts index defc44cc4..5187d2cfc 100644 --- a/integration-tests/vitest.config.ts +++ b/integration-tests/vitest.config.ts @@ -20,8 +20,14 @@ export default defineConfig({ ? [ 'tests/relational/mysql.planetscale.test.ts', 'tests/neon-http-batch.test.ts', - // 'tests/pg/xata-http.test.ts', 'tests/mysql/tidb-serverless.test.ts', + 'tests/mysql/mysql-planetscale.test.ts', + 'tests/sqlite/libsql.test.ts', + 'tests/mysql/tidb-serverless.test.ts', + 'tests/sqlite/libsql-batch.test.ts', + + 'tests/pg/neon-http.test.ts', + 'tests/pg/neon-http-batch.test.ts', ] : []), 'tests/pg/awsdatapi.test.ts', @@ -37,12 +43,14 @@ export default defineConfig({ }, testTimeout: 100000, hookTimeout: 100000, - isolate: false, + isolate: true, poolOptions: { threads: { singleThread: true, }, }, + maxWorkers: 1, + fileParallelism: false, }, plugins: [tsconfigPaths()], });