-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresult_test.ts
More file actions
230 lines (192 loc) · 6.43 KB
/
result_test.ts
File metadata and controls
230 lines (192 loc) · 6.43 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import {
assert,
assertEquals,
assertFalse,
assertInstanceOf,
} from "@std/assert";
import {
type SqlQueryResult,
SqlQueryResultErrorImpl,
SqlQueryResultFailureImpl,
SqlQueryResultSuccessImpl,
} from "./result.ts";
import { QuerySyntaxError, SqlConnectionError } from "./errors.ts";
Deno.test("SqlQueryResultSuccessImpl", async (t) => {
await t.step("creates with all properties", () => {
const result = new SqlQueryResultSuccessImpl({
rows: [{ id: 1, name: "Alice" }],
rowCount: 1,
duration: 10,
lastInsertId: 1n,
});
assert(result.ok);
assertEquals(result.processed, true);
assertEquals(result.error, null);
assertEquals(result.rowCount, 1);
assertEquals(result.duration, 10);
assertEquals(result.lastInsertId, 1n);
assertEquals(result.kind, "sql");
});
await t.step("rows is readonly array", () => {
const result = new SqlQueryResultSuccessImpl({
rows: [{ id: 1 }, { id: 2 }],
rowCount: 0,
duration: 5,
});
assertEquals(Array.isArray(result.rows), true);
assertEquals(result.rows.length, 2);
});
await t.step("map() transforms rows", () => {
const result = new SqlQueryResultSuccessImpl({
rows: [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }],
rowCount: 0,
duration: 5,
});
const names = result.map((row) => row.name);
assertEquals(names, ["Alice", "Bob"]);
});
await t.step("as() creates class instances", () => {
class User {
readonly id: number;
readonly displayName: string;
constructor(row: { id: number; name: string }) {
this.id = row.id;
this.displayName = `User: ${row.name}`;
}
}
const result = new SqlQueryResultSuccessImpl({
rows: [{ id: 1, name: "Alice" }],
rowCount: 0,
duration: 5,
});
const users = result.as(User);
assertEquals(users.length, 1);
assertInstanceOf(users[0], User);
assertEquals(users[0].id, 1);
assertEquals(users[0].displayName, "User: Alice");
});
await t.step("optional properties default to null", () => {
const result = new SqlQueryResultSuccessImpl({
rows: [],
rowCount: 0,
duration: 0,
});
assertEquals(result.lastInsertId, null);
assertEquals(result.warnings, null);
});
await t.step("warnings property", () => {
const result = new SqlQueryResultSuccessImpl({
rows: [],
rowCount: 0,
duration: 0,
warnings: ["truncation occurred"],
});
assertEquals(result.warnings, ["truncation occurred"]);
});
});
Deno.test("SqlQueryResultErrorImpl", async (t) => {
await t.step("creates with error", () => {
const error = new QuerySyntaxError("Syntax error near SELECT");
const result = new SqlQueryResultErrorImpl(error, 10);
assertFalse(result.ok);
assertEquals(result.processed, true);
assertEquals(result.error, error);
assertEquals(result.rowCount, 0);
assertEquals(result.duration, 10);
assertEquals(result.kind, "sql");
});
await t.step("rows is empty array", () => {
const error = new QuerySyntaxError("Syntax error");
const result = new SqlQueryResultErrorImpl(error, 5);
assertEquals(Array.isArray(result.rows), true);
assertEquals(result.rows.length, 0);
});
await t.step("map() returns empty array", () => {
const error = new QuerySyntaxError("Syntax error");
const result = new SqlQueryResultErrorImpl<{ id: number }>(error, 5);
const mapped = result.map((row) => row.id);
assertEquals(mapped, []);
});
await t.step("as() returns empty array", () => {
class User {
constructor(_row: { id: number }) {}
}
const error = new QuerySyntaxError("Syntax error");
const result = new SqlQueryResultErrorImpl<{ id: number }>(error, 5);
const users = result.as(User);
assertEquals(users, []);
});
});
Deno.test("SqlQueryResultFailureImpl", async (t) => {
await t.step("creates with connection error", () => {
const error = new SqlConnectionError("Connection refused");
const result = new SqlQueryResultFailureImpl(error, 100);
assertFalse(result.ok);
assertEquals(result.processed, false);
assertEquals(result.error, error);
assertEquals(result.rows, null);
assertEquals(result.rowCount, null);
assertEquals(result.duration, 100);
assertEquals(result.kind, "sql");
});
await t.step("map() returns empty array", () => {
const error = new SqlConnectionError("Connection refused");
const result = new SqlQueryResultFailureImpl<{ id: number }>(error, 100);
const mapped = result.map((row) => row.id);
assertEquals(mapped, []);
});
await t.step("as() returns empty array", () => {
class User {
constructor(_row: { id: number }) {}
}
const error = new SqlConnectionError("Connection refused");
const result = new SqlQueryResultFailureImpl<{ id: number }>(error, 100);
const users = result.as(User);
assertEquals(users, []);
});
});
Deno.test("SqlQueryResult type narrowing", async (t) => {
await t.step("narrows by ok property", () => {
const successResult: SqlQueryResult<{ id: number }> =
new SqlQueryResultSuccessImpl({
rows: [{ id: 1 }],
rowCount: 1,
duration: 10,
});
if (successResult.ok) {
// TypeScript knows this is SqlQueryResultSuccess
assertEquals(successResult.rows[0], { id: 1 });
}
const errorResult: SqlQueryResult<{ id: number }> =
new SqlQueryResultErrorImpl(
new QuerySyntaxError("Syntax error"),
10,
);
if (!errorResult.ok) {
// TypeScript knows this is SqlQueryResultError | SqlQueryResultFailure
assertEquals(errorResult.error.message, "Syntax error");
}
});
await t.step("narrows by processed property", () => {
const failureResult: SqlQueryResult<{ id: number }> =
new SqlQueryResultFailureImpl(
new SqlConnectionError("Connection refused"),
100,
);
if (!failureResult.processed) {
// TypeScript knows this is SqlQueryResultFailure
assertEquals(failureResult.rows, null);
assertEquals(failureResult.rowCount, null);
}
const successResult: SqlQueryResult<{ id: number }> =
new SqlQueryResultSuccessImpl({
rows: [{ id: 1 }],
rowCount: 1,
duration: 10,
});
if (successResult.processed) {
// TypeScript knows this is SqlQueryResultSuccess | SqlQueryResultError
assertEquals(typeof successResult.rowCount, "number");
}
});
});