-
-
Notifications
You must be signed in to change notification settings - Fork 257
/
Resolver.test.ts
180 lines (172 loc) · 6.37 KB
/
Resolver.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { FileSystem } from "@effect/platform"
import { NodeFileSystem } from "@effect/platform-node"
import * as Schema from "@effect/schema/Schema"
import * as Sql from "@effect/sql"
import * as Sqlite from "@effect/sql-sqlite-node"
import { assert, describe, it } from "@effect/vitest"
import { Array, Effect, Option } from "effect"
const makeClient = Effect.gen(function*(_) {
const fs = yield* _(FileSystem.FileSystem)
const dir = yield* _(fs.makeTempDirectoryScoped())
return yield* _(Sqlite.client.make({
filename: dir + "/test.db"
}))
}).pipe(Effect.provide(NodeFileSystem.layer))
const seededClient = Effect.gen(function*(_) {
const sql = yield* _(makeClient)
yield* _(sql`CREATE TABLE test (id INTEGER PRIMARY KEY, name TEXT)`)
yield* _(
Effect.forEach(Array.range(1, 100), (id) => sql`INSERT INTO test ${sql.insert({ id, name: `name${id}` })}`)
)
return sql
})
describe("Resolver", () => {
describe("ordered", () => {
it.scoped("insert", () =>
Effect.gen(function*(_) {
const batches: Array<Array<string>> = []
const sql = yield* _(seededClient)
const Insert = yield* _(Sql.resolver.ordered("Insert", {
Request: Schema.String,
Result: Schema.Struct({ id: Schema.Number, name: Schema.String }),
execute: (names) => {
batches.push(names)
return sql`INSERT INTO test ${sql.insert(names.map((name) => ({ name })))} RETURNING *`
}
}))
assert.deepStrictEqual(
yield* _(Effect.all({
one: Insert.execute("one"),
two: Insert.execute("two")
}, { batching: true })),
{
one: { id: 101, name: "one" },
two: { id: 102, name: "two" }
}
)
assert.deepStrictEqual(batches, [["one", "two"]])
}))
it.scoped("result length mismatch", () =>
Effect.gen(function*(_) {
const batches: Array<Array<number>> = []
const sql = yield* _(seededClient)
const Select = yield* _(Sql.resolver.ordered("Select", {
Request: Schema.Number,
Result: Schema.Struct({ id: Schema.Number, name: Schema.String }),
execute: (ids) => {
batches.push(ids)
return sql`SELECT * FROM test WHERE id IN ${sql.in(ids)}`
}
}))
const error = yield* _(
Effect.all([
Select.execute(1),
Select.execute(2),
Select.execute(3),
Select.execute(101)
], { batching: true }),
Effect.flip
)
assert(error instanceof Sql.error.ResultLengthMismatch)
assert.strictEqual(error.actual, 3)
assert.strictEqual(error.expected, 4)
assert.deepStrictEqual(batches, [[1, 2, 3, 101]])
}))
})
describe("grouped", () => {
it.scoped("find by name", () =>
Effect.gen(function*(_) {
const sql = yield* _(seededClient)
const FindByName = yield* _(Sql.resolver.grouped("FindByName", {
Request: Schema.String,
RequestGroupKey: (name) => name,
Result: Schema.Struct({ id: Schema.Number, name: Schema.String }),
ResultGroupKey: (result) => result.name,
execute: (names) => sql`SELECT * FROM test WHERE name IN ${sql.in(names)}`
}))
yield* _(sql`INSERT INTO test ${sql.insert({ name: "name1" })}`)
assert.deepStrictEqual(
yield* _(Effect.all({
one: FindByName.execute("name1"),
two: FindByName.execute("name2"),
three: FindByName.execute("name0")
}, { batching: true })),
{
one: [{ id: 1, name: "name1" }, { id: 101, name: "name1" }],
two: [{ id: 2, name: "name2" }],
three: []
}
)
}))
it.scoped("using raw rows", () =>
Effect.gen(function*(_) {
const sql = yield* _(seededClient)
const FindByName = yield* _(Sql.resolver.grouped("FindByName", {
Request: Schema.String,
RequestGroupKey: (name) => name,
Result: Schema.Struct({ id: Schema.Number, name: Schema.String }),
ResultGroupKey: (_, result: any) => result.name,
execute: (names) => sql`SELECT * FROM test WHERE name IN ${sql.in(names)}`
}))
yield* _(sql`INSERT INTO test ${sql.insert({ name: "name1" })}`)
assert.deepStrictEqual(
yield* _(Effect.all({
one: FindByName.execute("name1"),
two: FindByName.execute("name2"),
three: FindByName.execute("name0")
}, { batching: true })),
{
one: [{ id: 1, name: "name1" }, { id: 101, name: "name1" }],
two: [{ id: 2, name: "name2" }],
three: []
}
)
}))
})
describe("findById", () => {
it.scoped("find by id", () =>
Effect.gen(function*(_) {
const sql = yield* _(seededClient)
const FindById = yield* _(Sql.resolver.findById("FindById", {
Id: Schema.Number,
Result: Schema.Struct({ id: Schema.Number, name: Schema.String }),
ResultId: (result) => result.id,
execute: (ids) => sql`SELECT * FROM test WHERE id IN ${sql.in(ids)}`
}))
assert.deepStrictEqual(
yield* _(Effect.all({
one: FindById.execute(1),
two: FindById.execute(2),
three: FindById.execute(101)
}, { batching: true })),
{
one: Option.some({ id: 1, name: "name1" }),
two: Option.some({ id: 2, name: "name2" }),
three: Option.none()
}
)
}))
it.scoped("using raw rows", () =>
Effect.gen(function*(_) {
const sql = yield* _(seededClient)
const FindById = yield* _(Sql.resolver.findById("FindById", {
Id: Schema.Number,
Result: Schema.Struct({ id: Schema.Number, name: Schema.String }),
ResultId: (_, result: any) => result.id,
execute: (ids) => sql`SELECT * FROM test WHERE id IN ${sql.in(ids)}`
}))
assert.deepStrictEqual(
yield* _(Effect.all({
one: FindById.execute(1),
two: FindById.execute(2),
three: FindById.execute(101)
}, { batching: true })),
{
one: Option.some({ id: 1, name: "name1" }),
two: Option.some({ id: 2, name: "name2" }),
three: Option.none()
}
)
}))
})
})