Skip to content

Commit

Permalink
Interim
Browse files Browse the repository at this point in the history
  • Loading branch information
benmerckx committed Apr 23, 2024
1 parent 0907501 commit 5db1b5b
Showing 1 changed file with 55 additions and 6 deletions.
61 changes: 55 additions & 6 deletions test/TestDriver.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import {Assert, Test} from '@sinclair/carbon'
import type {Database} from '../src/core/Database.ts'
import {table} from '../src/core/Table.ts'
import {eq, sql} from '../src/index.ts'
import {boolean, id, int, json, text} from '../src/universal.ts'
import { Assert, Test } from '@sinclair/carbon'
import {
AsyncDatabase,
type Database,
type SyncDatabase
} from '../src/core/Database.ts'
import { table } from '../src/core/Table.ts'
import { eq, sql } from '../src/index.ts'
import { boolean, id, int, json, text } from '../src/universal.ts'

const Node = table('Node', {
id: id(),
Expand All @@ -26,8 +30,9 @@ export async function testDriver(
createDb: () => Promise<Database>
) {
const db = await createDb()
const isAsync = db instanceof AsyncDatabase

Test.describe(name, async () => {
Test.describe(`Driver: ${name}`, async () => {
Test.it('create table', async () => {
try {
await db.create(Node)
Expand Down Expand Up @@ -149,5 +154,49 @@ export async function testDriver(
await db.drop(WithJson)
}
})

Test.it('transactions', async () => {
if (isAsync) {
const asyncDb = db as AsyncDatabase<'universal'>
try {
await asyncDb.create(Node)
await asyncDb.transaction(async tx => {
await tx.insert(Node).values({
textField: 'hello',
bool: true
})
const nodes = await tx.select().from(Node)
Assert.isEqual(nodes, [{id: 1, textField: 'hello', bool: true}])
tx.rollback()
})
} catch {
const nodes = await asyncDb.select().from(Node)
Assert.isEqual(nodes, [])
} finally {
await asyncDb.drop(Node)
}
} else {
const syncDb = db as SyncDatabase<'universal'>
try {
syncDb.create(Node).run()
syncDb.transaction((tx): void => {
tx.insert(Node).values({
textField: 'hello',
bool: true
})
const nodes = tx.select().from(Node).all()
Assert.isEqual(nodes, [{id: 1, textField: 'hello', bool: true}])
tx.rollback()
})
const nodes = syncDb.select().from(Node).all()
Assert.isEqual(nodes, [])
} catch {
const nodes = syncDb.select().from(Node).all()
Assert.isEqual(nodes, [])
} finally {
syncDb.drop(Node).run()
}
}
})
})
}

0 comments on commit 5db1b5b

Please sign in to comment.