diff --git a/bun.lockb b/bun.lockb index 912089b..e234d1e 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 667e517..b754d7f 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "@types/glob": "^8.1.0", "@types/pg": "^8.11.8", "@types/sql.js": "^1.4.9", - "better-sqlite3": "^11.2.1", + "better-sqlite3": "^11.3.0", "esbuild": "^0.23.1", "glob": "^11.0.0", "madge": "^8.0.0", diff --git a/test/TestDriver.ts b/test/TestDriver.ts deleted file mode 100644 index 8a30b30..0000000 --- a/test/TestDriver.ts +++ /dev/null @@ -1,35 +0,0 @@ -import {suite} from '@alinea/suite' -import type {Database} from '../src/core/Database.ts' -import {testBasic} from './integration/TestBasic.ts' -import {testCTE} from './integration/TestCTE.ts' -import {testColumns} from './integration/TestColumns.ts' -import {testConstraints} from './integration/TestConstraints.ts' -import {testInclude} from './integration/TestInclude.ts' -import {testJoins} from './integration/TestJoins.ts' -import {testJson} from './integration/TestJson.ts' -import {testMigration} from './integration/TestMigration.ts' -import {testPreparedQuery} from './integration/TestPreparedQuery.ts' -import {testSubquery} from './integration/TestSubquery.ts' -import {testTransactions} from './integration/TestTransactions.ts' - -export async function testDriver( - meta: ImportMeta, - createDb: () => Promise, - supportsDiff = true -) { - const db = await createDb() - suite(meta, test => { - testBasic(db, test) - testColumns(db, test) - testSubquery(db, test) - testPreparedQuery(db, test) - testJoins(db, test) - testJson(db, test) - testTransactions(db, test) - testConstraints(db, test) - testCTE(db, test) - testInclude(db, test) - - if (supportsDiff) testMigration(db, test) - }) -} diff --git a/test/driver.test.ts b/test/driver.test.ts new file mode 100644 index 0000000..db85378 --- /dev/null +++ b/test/driver.test.ts @@ -0,0 +1,108 @@ +import * as driver from '@/driver.ts' +import {type DefineTest, type Describe, suite} from '@alinea/suite' +import {isBun, isCi, isNode} from './TestRuntime.ts' +import {testBasic} from './integration/TestBasic.ts' +import {testCTE} from './integration/TestCTE.ts' +import {testColumns} from './integration/TestColumns.ts' +import {testConstraints} from './integration/TestConstraints.ts' +import {testInclude} from './integration/TestInclude.ts' +import {testJoins} from './integration/TestJoins.ts' +import {testJson} from './integration/TestJson.ts' +import {testMigration} from './integration/TestMigration.ts' +import {testPreparedQuery} from './integration/TestPreparedQuery.ts' +import {testSubquery} from './integration/TestSubquery.ts' +import {testTransactions} from './integration/TestTransactions.ts' + +const init = { + 'better-sqlite3': { + condition: isNode, + supportsDiff: true, + async client() { + const {default: Database} = await import('better-sqlite3') + return new Database(':memory:') + } + }, + 'bun:sqlite': { + condition: isBun, + supportsDiff: true, + async client() { + const {Database} = await import('bun:sqlite') + return new Database(':memory:') + } + }, + mysql2: { + condition: isCi, + supportsDiff: false, + async client() { + const {default: mysql2} = await import('mysql2') + const client = mysql2.createConnection( + 'mysql://root:mysql@0.0.0.0:3306/mysql' + ) + return client + } + }, + '@electric-sql/pglite': { + condition: true, + supportsDiff: true, + async client() { + const {PGlite} = await import('@electric-sql/pglite') + return new PGlite() + } + }, + pg: { + condition: isCi, + supportsDiff: true, + async client() { + const {default: pg} = await import('pg') + const client = new pg.Client({ + connectionString: 'postgres://postgres:postgres@0.0.0.0:5432/postgres' + }) + await client.connect() + return client + } + }, + 'sql.js': { + condition: true, + supportsDiff: true, + async client() { + const {default: init} = await import('sql.js') + const {Database} = await init() + return new Database() + } + } +} + +async function createTests() { + const clients = await Promise.all( + Object.entries(init) + .filter(([name, meta]) => meta.condition) + .map( + async ([name, meta]) => + [name, await meta.client()] as [keyof typeof init, any] + ) + ) + return (test: DefineTest) => { + for (const [name, client] of clients) { + const {supportsDiff} = init[name] + const db = driver[name](client) + const prefixed: Describe = (description, fn) => + test(`${name}: ${description}`, fn) + const withName = Object.assign(prefixed, test) + + testBasic(db, withName) + testColumns(db, withName) + testSubquery(db, withName) + testPreparedQuery(db, withName) + testJoins(db, withName) + testJson(db, withName) + testTransactions(db, withName) + testConstraints(db, withName) + testCTE(db, withName) + testInclude(db, withName) + + if (supportsDiff) testMigration(db, withName) + } + } +} + +suite(import.meta, await createTests()) diff --git a/test/driver/@db_sqlite.test.ts b/test/driver/@db_sqlite.test.ts deleted file mode 100644 index 724af8a..0000000 --- a/test/driver/@db_sqlite.test.ts +++ /dev/null @@ -1,10 +0,0 @@ -import {testDriver} from '../TestDriver.ts' -import {isDeno} from '../TestRuntime.ts' - -async function createDb() { - const {Database} = await import('@db/sqlite') - const driver = await import('../../src/driver.ts') - return driver['@db/sqlite'](new Database(':memory:')) -} - -if (isDeno) await testDriver(import.meta, createDb) diff --git a/test/driver/@electric-sql_pglite.test.ts b/test/driver/@electric-sql_pglite.test.ts deleted file mode 100644 index 6074a73..0000000 --- a/test/driver/@electric-sql_pglite.test.ts +++ /dev/null @@ -1,7 +0,0 @@ -import {testDriver} from '../TestDriver.ts' - -await testDriver(import.meta, async () => { - const driver = await import('../../src/driver.ts') - const {PGlite} = await import('@electric-sql/pglite') - return driver['@electric-sql/pglite'](new PGlite()) -}) diff --git a/test/driver/better-sqlite3.test.ts b/test/driver/better-sqlite3.test.ts deleted file mode 100644 index 54b7f31..0000000 --- a/test/driver/better-sqlite3.test.ts +++ /dev/null @@ -1,9 +0,0 @@ -import {testDriver} from '../TestDriver.ts' -import {isNode} from '../TestRuntime.ts' - -if (isNode) - await testDriver(import.meta, async () => { - const {default: Database} = await import('better-sqlite3') - const driver = await import('../../src/driver.ts') - return driver['better-sqlite3'](new Database(':memory:')) - }) diff --git a/test/driver/bun-sqlite.test.ts b/test/driver/bun-sqlite.test.ts deleted file mode 100644 index bb7e737..0000000 --- a/test/driver/bun-sqlite.test.ts +++ /dev/null @@ -1,10 +0,0 @@ -import {testDriver} from '../TestDriver.ts' -import {isBun} from '../TestRuntime.ts' - -async function createDb() { - const {Database} = await import('bun:sqlite') - const driver = await import('../../src/driver.ts') - return driver['bun:sqlite'](new Database(':memory:')) -} - -if (isBun) await testDriver(import.meta, createDb) diff --git a/test/driver/mysql2.test.ts b/test/driver/mysql2.test.ts deleted file mode 100644 index e4f469b..0000000 --- a/test/driver/mysql2.test.ts +++ /dev/null @@ -1,16 +0,0 @@ -import {testDriver} from '../TestDriver.ts' -import {isCi} from '../TestRuntime.ts' - -if (isCi) - await testDriver( - import.meta, - async () => { - const driver = await import('../../src/driver.ts') - const {default: mysql2} = await import('mysql2') - const client = mysql2.createConnection( - 'mysql://root:mysql@0.0.0.0:3306/mysql' - ) - return driver.mysql2(client) - }, - false - ) diff --git a/test/driver/pg.test.ts b/test/driver/pg.test.ts deleted file mode 100644 index f0418bc..0000000 --- a/test/driver/pg.test.ts +++ /dev/null @@ -1,13 +0,0 @@ -import {testDriver} from '../TestDriver.ts' -import {isCi} from '../TestRuntime.ts' - -if (isCi) - await testDriver(import.meta, async () => { - const driver = await import('../../src/driver.ts') - const {default: pg} = await import('pg') - const client = new pg.Client({ - connectionString: 'postgres://postgres:postgres@0.0.0.0:5432/postgres' - }) - await client.connect() - return driver.pg(client) - }) diff --git a/test/driver/sql.js.test.ts b/test/driver/sql.js.test.ts deleted file mode 100644 index 87e415a..0000000 --- a/test/driver/sql.js.test.ts +++ /dev/null @@ -1,8 +0,0 @@ -import {testDriver} from '../TestDriver.ts' - -await testDriver(import.meta, async () => { - const driver = await import('../../src/driver.ts') - const {default: init} = await import('sql.js') - const {Database} = await init() - return driver['sql.js'](new Database()) -})