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

Client.integration.test.ts

139 lines5.1 KB

1import { LibsqlClient } from "@effect/sql-libsql"
2import { assert, describe, it, layer } from "@effect/vitest"
3import { Effect, Exit, Layer } from "effect"
4import * as Reactivity from "effect/unstable/reactivity/Reactivity"
5import { LibsqlContainer } from "./util.ts"
6​
7const Migrations = Layer.effectDiscard(
8 LibsqlClient.LibsqlClient.pipe(
9 Effect.andThen((sql) =>
10 Effect.acquireRelease(
11 sql`CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)`,
12 () => sql`DROP TABLE test;`.pipe(Effect.ignore)
13 )
14 )
15 )
17​
18describe("Client", () => {
19 it.effect("releases transaction serialization after begin fails", () => {
20 let transactionCalls = 0
21 const transaction = {
22 execute: () => Promise.resolve({ rows: [] }),
23 commit: () => Promise.resolve(),
24 rollback: () => Promise.resolve()
25 }
26 const liveClient = {
27 execute: () => Promise.resolve({ rows: [] }),
28 transaction: () => {
29 transactionCalls++
30 return transactionCalls === 1
31 ? Promise.reject(new Error("transient begin failure"))
32 : Promise.resolve(transaction)
33 }
34 }
35​
36 return Effect.gen(function*() {
37 const client = yield* LibsqlClient.make({ liveClient: liveClient as any })
38 const first = yield* Effect.exit(client.withTransaction(Effect.void))
39 assert.isTrue(Exit.isFailure(first))
40​
41 yield* Effect.forkChild(client.withTransaction(Effect.void))
42 yield* Effect.yieldNow
43​
44 assert.strictEqual(transactionCalls, 2)
45 }).pipe(
46 Effect.scoped,
47 Effect.provide(Reactivity.layer)
48 )
49 })
50​
51 layer(LibsqlContainer.layerClient, { timeout: "30 seconds" })((it) => {
52 it.effect("should work", () =>
53 Effect.gen(function*() {
54 const sql = yield* LibsqlClient.LibsqlClient
55 let response = yield* sql`INSERT INTO test (name) VALUES ('hello')`
56 assert.deepStrictEqual(response, [])
57 response = yield* sql`SELECT * FROM test`
58 assert.deepStrictEqual(response, [{ id: 1, name: "hello" }])
59 response = yield* sql`SELECT * FROM test`
60 assert.deepStrictEqual(yield* sql`select * from test`.values, [
61 [1, "hello"]
62 ])
63 }).pipe(Effect.provide(Migrations)))
64​
65 it.effect("should work with raw", () =>
66 Effect.gen(function*() {
67 const sql = yield* LibsqlClient.LibsqlClient
68 let response: any
69 response = yield* sql`CREATE TABLE test2 (id INTEGER PRIMARY KEY, name TEXT)`.raw
70 yield* Effect.addFinalizer(() => sql`DROP TABLE test2;`.pipe(Effect.ignore))
71 assert.deepStrictEqual(response.toJSON(), {
72 columnTypes: [],
73 columns: [],
74 lastInsertRowid: null,
75 rows: [],
76 rowsAffected: 0
77 })
78 response = yield* sql`INSERT INTO test (name) VALUES ('hello')`.raw
79 assert.deepStrictEqual(response.toJSON(), {
80 columnTypes: [],
81 columns: [],
82 lastInsertRowid: "1",
83 rows: [],
84 rowsAffected: 1
85 })
86 response = yield* sql`SELECT * FROM test`.raw
87 assert.deepStrictEqual(response.toJSON(), {
88 columnTypes: ["INTEGER", "TEXT"],
89 columns: ["id", "name"],
90 lastInsertRowid: null,
91 rows: [[1, "hello"]],
92 rowsAffected: 0
93 })
94 }).pipe(Effect.provide(Migrations)))
95​
96 it.effect("withTransaction", () =>
97 Effect.gen(function*() {
98 const sql = yield* LibsqlClient.LibsqlClient
99 yield* sql.withTransaction(sql`INSERT INTO test (name) VALUES ('hello')`)
100 const rows = yield* sql`SELECT * FROM test`
101 assert.deepStrictEqual(rows, [{ id: 1, name: "hello" }])
102 }).pipe(Effect.provide(Migrations)))
103​
104 it.effect("withTransaction rollback", () =>
105 Effect.gen(function*() {
106 const sql = yield* LibsqlClient.LibsqlClient
107 yield* sql`INSERT INTO test (name) VALUES ('hello')`.pipe(
108 Effect.andThen(Effect.fail("boom")),
109 sql.withTransaction,
110 Effect.ignore
111 )
112 const rows = yield* sql`SELECT * FROM test`
113 assert.deepStrictEqual(rows, [])
114 }).pipe(Effect.provide(Migrations)))
115​
116 it.effect("withTransaction nested", () =>
117 Effect.gen(function*() {
118 const sql = yield* LibsqlClient.LibsqlClient
119 const stmt = sql`INSERT INTO test (name) VALUES ('hello')`
120​
121 yield* stmt.pipe(Effect.andThen(() => stmt.pipe(sql.withTransaction)), sql.withTransaction)
122 const rows = yield* sql<{ total_rows: number }>`select count(*) as total_rows FROM test`
123 assert.deepStrictEqual(rows.at(0)?.total_rows, 2)
124 }).pipe(Effect.provide(Migrations)))
125​
126 it.effect("withTransaction nested rollback", () =>
127 Effect.gen(function*() {
128 const sql = yield* LibsqlClient.LibsqlClient
129 const stmt = sql`INSERT INTO test (name) VALUES ('hello')`
130​
131 yield* stmt.pipe(
132 Effect.andThen(() => stmt.pipe(Effect.andThen(Effect.fail("boom")), sql.withTransaction, Effect.ignore)),
133 sql.withTransaction
134 )
135 const rows = yield* sql<{ total_rows: number }>`select count(*) as total_rows FROM test`
136 assert.deepStrictEqual(rows.at(0)?.total_rows, 1)
137 }).pipe(Effect.provide(Migrations)))
138 })
139})