forked from Effect-TS/effect
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d018291
commit 7102698
Showing
1 changed file
with
58 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,65 @@ | ||
import { FileSystem } from "@effect/platform" | ||
import { NodeFileSystem } from "@effect/platform-node" | ||
import { LibsqlClient } from "@effect/sql-libsql" | ||
import { assert, describe, it } from "@effect/vitest" | ||
import { assert, describe, layer } from "@effect/vitest" | ||
import { Effect } from "effect" | ||
|
||
const makeClient = Effect.gen(function*() { | ||
const fs = yield* FileSystem.FileSystem | ||
const dir = yield* fs.makeTempDirectoryScoped() | ||
return yield* LibsqlClient.make({ | ||
url: "file:" + dir + "/test.db" | ||
}) | ||
}).pipe(Effect.provide(NodeFileSystem.layer)) | ||
import { LibsqlContainer } from "./util.js" | ||
|
||
describe("Client", () => { | ||
it.scoped("should work", () => | ||
Effect.gen(function*() { | ||
const sql = yield* makeClient | ||
let response | ||
response = yield* sql`CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)` | ||
assert.deepStrictEqual(response, []) | ||
response = yield* sql`INSERT INTO test (name) VALUES ('hello')` | ||
assert.deepStrictEqual(response, []) | ||
response = yield* sql`SELECT * FROM test` | ||
assert.deepStrictEqual(response, [{ id: 1, name: "hello" }]) | ||
response = yield* sql.withTransaction(sql`INSERT INTO test (name) VALUES ('world')`) | ||
assert.deepStrictEqual(response, []) | ||
response = yield* sql`SELECT * FROM test` | ||
assert.deepStrictEqual(response, [ | ||
{ id: 1, name: "hello" }, | ||
{ id: 2, name: "world" } | ||
]) | ||
assert.deepStrictEqual(yield* sql`select * from test`.values, [ | ||
[1, "hello"], | ||
[2, "world"] | ||
]) | ||
})) | ||
layer(LibsqlContainer.ClientLive)((it) => { | ||
it.scoped("should work", () => | ||
Effect.gen(function*() { | ||
const sql = yield* LibsqlClient.LibsqlClient | ||
let response | ||
response = yield* sql`CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)` | ||
yield* Effect.addFinalizer(() => sql`DROP TABLE test;`.pipe(Effect.ignore)) | ||
assert.deepStrictEqual(response, []) | ||
response = yield* sql`INSERT INTO test (name) VALUES ('hello')` | ||
assert.deepStrictEqual(response, []) | ||
response = yield* sql`SELECT * FROM test` | ||
assert.deepStrictEqual(response, [{ id: 1, name: "hello" }]) | ||
response = yield* sql`SELECT * FROM test` | ||
assert.deepStrictEqual(yield* sql`select * from test`.values, [ | ||
[1, "hello"] | ||
]) | ||
})) | ||
|
||
it.scoped("should work with raw", () => | ||
Effect.gen(function*() { | ||
const sql = yield* makeClient | ||
let response: any | ||
response = yield* sql`CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)`.raw | ||
assert.deepStrictEqual(response.toJSON(), { | ||
columnTypes: [], | ||
columns: [], | ||
lastInsertRowid: "0", | ||
rows: [], | ||
rowsAffected: 0 | ||
}) | ||
response = yield* sql`INSERT INTO test (name) VALUES ('hello')`.raw | ||
assert.deepStrictEqual(response.toJSON(), { | ||
columnTypes: [], | ||
columns: [], | ||
lastInsertRowid: "1", | ||
rows: [], | ||
rowsAffected: 1 | ||
}) | ||
response = yield* sql`SELECT * FROM test`.raw | ||
assert.deepStrictEqual(response.toJSON(), { | ||
columnTypes: ["INTEGER", "TEXT"], | ||
columns: ["id", "name"], | ||
lastInsertRowid: null, | ||
rows: [[1, "hello"]], | ||
rowsAffected: 0 | ||
}) | ||
})) | ||
it.scoped("should work with raw", () => | ||
Effect.gen(function*() { | ||
const sql = yield* LibsqlClient.LibsqlClient | ||
let response: any | ||
response = yield* sql`CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)`.raw | ||
yield* Effect.addFinalizer(() => sql`DROP TABLE test;`.pipe(Effect.ignore)) | ||
assert.deepStrictEqual(response.toJSON(), { | ||
columnTypes: [], | ||
columns: [], | ||
lastInsertRowid: null, | ||
rows: [], | ||
rowsAffected: 0 | ||
}) | ||
response = yield* sql`INSERT INTO test (name) VALUES ('hello')`.raw | ||
assert.deepStrictEqual(response.toJSON(), { | ||
columnTypes: [], | ||
columns: [], | ||
lastInsertRowid: "1", | ||
rows: [], | ||
rowsAffected: 1 | ||
}) | ||
response = yield* sql`SELECT * FROM test`.raw | ||
assert.deepStrictEqual(response.toJSON(), { | ||
columnTypes: ["INTEGER", "TEXT"], | ||
columns: ["id", "name"], | ||
lastInsertRowid: null, | ||
rows: [[1, "hello"]], | ||
rowsAffected: 0 | ||
}) | ||
})) | ||
|
||
it.scoped("withTransaction", () => | ||
Effect.gen(function*() { | ||
const sql = yield* makeClient | ||
yield* sql`CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)` | ||
yield* sql.withTransaction(sql`INSERT INTO test (name) VALUES ('hello')`) | ||
const rows = yield* sql`SELECT * FROM test` | ||
assert.deepStrictEqual(rows, [{ id: 1, name: "hello" }]) | ||
})) | ||
|
||
it.scoped("withTransaction rollback", () => | ||
Effect.gen(function*() { | ||
const sql = yield* makeClient | ||
yield* sql`CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)` | ||
yield* sql`INSERT INTO test (name) VALUES ('hello')`.pipe( | ||
Effect.andThen(Effect.fail("boom")), | ||
sql.withTransaction, | ||
Effect.ignore | ||
) | ||
const rows = yield* sql`SELECT * FROM test` | ||
assert.deepStrictEqual(rows, []) | ||
})) | ||
it.scoped("withTransaction", () => | ||
Effect.gen(function*() { | ||
const sql = yield* LibsqlClient.LibsqlClient | ||
yield* sql`CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)` | ||
yield* sql.withTransaction(sql`INSERT INTO test (name) VALUES ('hello')`) | ||
const rows = yield* sql`SELECT * FROM test` | ||
assert.deepStrictEqual(rows, [{ id: 1, name: "hello" }]) | ||
})) | ||
}) | ||
}) |