Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

Client.test.ts

75 lines3.0 KB

1import { NodeFileSystem } from "@effect/platform-node"
2import { SqliteClient } from "@effect/sql-sqlite-node"
3import { assert, describe, it } from "@effect/vitest"
4import { Effect, FileSystem } from "effect"
5import { Reactivity } from "effect/unstable/reactivity"
6​
7const makeClient = Effect.gen(function*() {
8 const fs = yield* FileSystem.FileSystem
9 const dir = yield* fs.makeTempDirectoryScoped()
10 return yield* SqliteClient.make({
11 filename: dir + "/test.db"
12 })
13}).pipe(Effect.provide([NodeFileSystem.layer, Reactivity.layer]))
14​
15describe("Client", () => {
16 it.effect("should work", () =>
17 Effect.gen(function*() {
18 const sql = yield* makeClient
19 let response
20 response = yield* sql`CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)`
21 assert.deepStrictEqual(response, [])
22 response = yield* sql`INSERT INTO test (name) VALUES ('hello')`
23 assert.deepStrictEqual(response, [])
24 response = yield* sql`SELECT * FROM test`
25 assert.deepStrictEqual(response, [{ id: 1, name: "hello" }])
26 response = yield* sql`INSERT INTO test (name) VALUES ('world')`.pipe(sql.withTransaction)
27 assert.deepStrictEqual(response, [])
28 response = yield* sql`SELECT * FROM test`
29 assert.deepStrictEqual(response, [
30 { id: 1, name: "hello" },
31 { id: 2, name: "world" }
32 ])
33 }))
34​
35 it.effect("should work with raw", () =>
36 Effect.gen(function*() {
37 const sql = yield* makeClient
38 let response
39 response = yield* sql`CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)`.raw
40 assert.deepStrictEqual(response, { changes: 0, lastInsertRowid: 0 })
41 response = yield* sql`INSERT INTO test (name) VALUES ('hello')`.raw
42 assert.deepStrictEqual(response, { changes: 1, lastInsertRowid: 1 })
43 response = yield* sql`SELECT * FROM test`.raw
44 assert.deepStrictEqual(response, [{ id: 1, name: "hello" }])
45 response = yield* sql`INSERT INTO test (name) VALUES ('world')`.raw.pipe(sql.withTransaction)
46 assert.deepStrictEqual(response, { changes: 1, lastInsertRowid: 2 })
47 response = yield* sql`SELECT * FROM test`
48 assert.deepStrictEqual(response, [
49 { id: 1, name: "hello" },
50 { id: 2, name: "world" }
51 ])
52 }))
53​
54 it.effect("withTransaction", () =>
55 Effect.gen(function*() {
56 const sql = yield* makeClient
57 yield* sql`CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)`
58 yield* sql.withTransaction(sql`INSERT INTO test (name) VALUES ('hello')`)
59 const rows = yield* sql`SELECT * FROM test`
60 assert.deepStrictEqual(rows, [{ id: 1, name: "hello" }])
61 }))
62​
63 it.effect("withTransaction rollback", () =>
64 Effect.gen(function*() {
65 const sql = yield* makeClient
66 yield* sql`CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)`
67 yield* sql`INSERT INTO test (name) VALUES ('hello')`.pipe(
68 Effect.andThen(Effect.fail("boom")),
69 sql.withTransaction,
70 Effect.ignore
71 )
72 const rows = yield* sql`SELECT * FROM test`
73 assert.deepStrictEqual(rows, [])
74 }))
75})