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

Client.test.ts

106 lines4.1 KB

1import { LibsqlClient } from "@effect/sql-libsql"
2import { assert, describe, layer } from "@effect/vitest"
3import { Effect, Layer } from "effect"
4import { LibsqlContainer } from "./util.ts"
5​
6const Migrations = Layer.effectDiscard(
7 LibsqlClient.LibsqlClient.pipe(
8 Effect.andThen((sql) =>
9 Effect.acquireRelease(
10 sql`CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)`,
11 () => sql`DROP TABLE test;`.pipe(Effect.ignore)
12 )
13 )
14 )
16​
17describe("Client", () => {
18 layer(LibsqlContainer.layerClient, { timeout: "30 seconds" })((it) => {
19 it.effect("should work", () =>
20 Effect.gen(function*() {
21 const sql = yield* LibsqlClient.LibsqlClient
22 let 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`SELECT * FROM test`
27 assert.deepStrictEqual(yield* sql`select * from test`.values, [
28 [1, "hello"]
29 ])
30 }).pipe(Effect.provide(Migrations)))
31​
32 it.effect("should work with raw", () =>
33 Effect.gen(function*() {
34 const sql = yield* LibsqlClient.LibsqlClient
35 let response: any
36 response = yield* sql`CREATE TABLE test2 (id INTEGER PRIMARY KEY, name TEXT)`.raw
37 yield* Effect.addFinalizer(() => sql`DROP TABLE test2;`.pipe(Effect.ignore))
38 assert.deepStrictEqual(response.toJSON(), {
39 columnTypes: [],
40 columns: [],
41 lastInsertRowid: null,
42 rows: [],
43 rowsAffected: 0
44 })
45 response = yield* sql`INSERT INTO test (name) VALUES ('hello')`.raw
46 assert.deepStrictEqual(response.toJSON(), {
47 columnTypes: [],
48 columns: [],
49 lastInsertRowid: "1",
50 rows: [],
51 rowsAffected: 1
52 })
53 response = yield* sql`SELECT * FROM test`.raw
54 assert.deepStrictEqual(response.toJSON(), {
55 columnTypes: ["INTEGER", "TEXT"],
56 columns: ["id", "name"],
57 lastInsertRowid: null,
58 rows: [[1, "hello"]],
59 rowsAffected: 0
60 })
61 }).pipe(Effect.provide(Migrations)))
62​
63 it.effect("withTransaction", () =>
64 Effect.gen(function*() {
65 const sql = yield* LibsqlClient.LibsqlClient
66 yield* sql.withTransaction(sql`INSERT INTO test (name) VALUES ('hello')`)
67 const rows = yield* sql`SELECT * FROM test`
68 assert.deepStrictEqual(rows, [{ id: 1, name: "hello" }])
69 }).pipe(Effect.provide(Migrations)))
70​
71 it.effect("withTransaction rollback", () =>
72 Effect.gen(function*() {
73 const sql = yield* LibsqlClient.LibsqlClient
74 yield* sql`INSERT INTO test (name) VALUES ('hello')`.pipe(
75 Effect.andThen(Effect.fail("boom")),
76 sql.withTransaction,
77 Effect.ignore
78 )
79 const rows = yield* sql`SELECT * FROM test`
80 assert.deepStrictEqual(rows, [])
81 }).pipe(Effect.provide(Migrations)))
82​
83 it.effect("withTransaction nested", () =>
84 Effect.gen(function*() {
85 const sql = yield* LibsqlClient.LibsqlClient
86 const stmt = sql`INSERT INTO test (name) VALUES ('hello')`
87​
88 yield* stmt.pipe(Effect.andThen(() => stmt.pipe(sql.withTransaction)), sql.withTransaction)
89 const rows = yield* sql<{ total_rows: number }>`select count(*) as total_rows FROM test`
90 assert.deepStrictEqual(rows.at(0)?.total_rows, 2)
91 }).pipe(Effect.provide(Migrations)))
92​
93 it.effect("withTransaction nested rollback", () =>
94 Effect.gen(function*() {
95 const sql = yield* LibsqlClient.LibsqlClient
96 const stmt = sql`INSERT INTO test (name) VALUES ('hello')`
97​
98 yield* stmt.pipe(
99 Effect.andThen(() => stmt.pipe(Effect.andThen(Effect.fail("boom")), sql.withTransaction, Effect.ignore)),
100 sql.withTransaction
101 )
102 const rows = yield* sql<{ total_rows: number }>`select count(*) as total_rows FROM test`
103 assert.deepStrictEqual(rows.at(0)?.total_rows, 1)
104 }).pipe(Effect.provide(Migrations)))
105 })
106})