-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTestSqlCommonRaw.ml
More file actions
195 lines (181 loc) · 6.72 KB
/
TestSqlCommonRaw.ml
File metadata and controls
195 lines (181 loc) · 6.72 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
open Jest
type simple = {
id: int;
code: string;
}
module Sql = SqlCommon.Make_sql(MySql2)
let () =
describe "Raw SQL Query Test" (fun () ->
let conn = TestUtil.connect () in
let _ = afterAll (fun _ -> Sql.close conn) in
testAsync "Expect a test database to be listed" (fun finish ->
Sql.query conn ~sql:"SHOW DATABASES" (fun res ->
match res with
| `Error e -> raise e
| `Select (rows, _) ->
Belt_Array.map rows (Json.Decode.dict Json.Decode.string)
|> Js.Array.map (fun x -> Js.Dict.unsafeGet x "Database")
|> Expect.expect
|> Expect.toContain @@ "test"
|> finish
)
)
);
describe "Raw SQL Query Test Sequence" (fun () ->
let conn = TestUtil.connect () in
let table_sql = {|
CREATE TABLE IF NOT EXISTS test.simple (
`id` bigint(20) NOT NULL AUTO_INCREMENT
, `code` varchar(32) NOT NULL
, PRIMARY KEY(`id`)
)
|}
in
let drop next =
let _ = Sql.mutate conn ~sql:"DROP TABLE IF EXISTS test.simple" (fun resp ->
match resp with
| `Error e -> let _ = Js.log2 "DROP FAILED: " e in raise e
| `Mutation _ -> next ()
) in ()
in
let create next =
let _ = Sql.mutate conn ~sql:table_sql (fun resp ->
match resp with
| `Error e -> let _ = Js.log2 "CREATE FAILED: " e in raise e
| `Mutation _ -> next ()
) in ()
in
let _ = beforeAllAsync (fun finish ->
drop (fun _ -> create finish))
in
testAsync "Expect a mutation result for an INSERT query" (fun finish ->
Sql.mutate conn ~sql:"INSERT INTO test.simple (code) VALUES ('foo')" (fun resp ->
match resp with
| `Error e -> let _ = Js.log e in finish (fail "see log")
| `Mutation (count, id) ->
let affected_rows = count == 1 in
let insert_id = id > 0 in
Expect.expect [|affected_rows; insert_id|]
|> Expect.toBeSupersetOf [|true; true|]
|> finish
)
);
testAsync "Expect an error result for an SELECT query called via mutate" (fun finish ->
Sql.mutate conn ~sql:"SELECT * FROM test.simple" (fun resp ->
match resp with
| `Mutation _ ->
fail "This should have returned an InvalidResponse exception" |> finish
| `Error e ->
match e with
| SqlCommon.InvalidResponse s -> Expect.expect s |> Expect.toContainString "ERR_UNEXPECTED_SELECT" |> finish
| _ -> fail "Unexpected failure mode" |> finish
)
);
testAsync "Expect a mutation result for an INSERT query" (fun finish ->
Sql.mutate conn ~sql:"INSERT INTO test.simple (code) VALUES ('bar'), ('baz')" (fun resp ->
match resp with
| `Error e -> let _ = Js.log e in finish (fail "see log")
| `Mutation (count, id) ->
let affected_rows = count == 2 in
let insert_id = id > 0 in
Expect.expect [|affected_rows; insert_id|]
|> Expect.toBeSupersetOf [|true; true|]
|> finish
)
);
testAsync "Expect a SELECT NULL to return an empty array" (fun finish ->
let decoder = Json.Decode.dict (Json.Decode.nullable Json.Decode.string) in
Sql.query conn ~sql:"SELECT NULL FROM test.simple WHERE false" (fun res ->
match res with
| `Error e -> let _ = Js.log e in finish (fail "see log")
| `Select (rows, _) ->
Belt_Array.map rows decoder
|> Expect.expect
|> Expect.toHaveLength 0
|> finish
)
);
testAsync "Expect an error result for an INSERT called via query" (fun finish ->
Sql.query conn ~sql:"INSERT INTO test.simple (code) VALUES ('failure')" (fun res ->
match res with
| `Select _ ->
fail "This should have returned an InvalidResponse exception" |> finish
| `Error e ->
match e with
| SqlCommon.InvalidResponse s -> Expect.expect s |> Expect.toContainString "ERR_UNEXPECTED_MUTATION" |> finish
| _ -> fail "Unexpected failure mode" |> finish
)
);
testAsync "Expect a SELECT with one parameter to respond with one column" (fun finish ->
let decoder json = Json.Decode.({
id = json |> field "id" int;
code = json |> field "code" string;
}) in
let pick = function
| [| {id; code } |] -> [| (id == 1); (code == "foo") |]
| [||] -> failwith "empty"
| _ -> failwith "unknown"
in
let params = Some(`Positional (Json.Encode.array Json.Encode.int [|1|])) in
Sql.query conn ~sql:"SELECT * FROM test.simple WHERE test.simple.id = ?" ?params (fun res ->
match res with
| `Error e -> let _ = Js.log e in finish (fail "see log")
| `Select (rows, _) ->
Belt_Array.map rows decoder
|> pick
|> Expect.expect
|> Expect.toBeSupersetOf [|true; true|]
|> finish
)
);
testAsync "Expect a SELECT * to respond with 4 rows" (fun finish ->
let decoder json = Json.Decode.({
id = json |> field "id" int;
code = json |> field "code" string;
}) in
Sql.query conn ~sql:"SELECT * FROM test.simple" (fun res ->
match res with
| `Error e -> let _ = Js.log e in finish (fail "see log")
| `Select (rows, _) ->
Belt_Array.map rows decoder
|> Expect.expect
|> Expect.toHaveLength 4
|> finish
)
);
testAsync "Rollback batch insert on duplicate key" (fun finish ->
let id_0 = string_of_int (Js.Math.random_int 0 (Js.Int.max - 1)) in
let code_0 = {j|unique-value-$id_0|j} in
let sql = "INSERT INTO test.simple (id, code) VALUES (?, ?)" in
let params = `Positional (Json.Encode.array Json.Encode.string [| id_0; code_0 |]) in
Sql.mutate conn ~sql ~params (fun res ->
match res with
| `Error e -> let _ = Js.log e in finish (fail "see log")
| `Mutation (_, _) ->
let id_1 = string_of_int (Js.Math.random_int 0 (Js.Int.max - 1)) in
let code_1 = {j|unique-value-$id_1|j} in
let batch_size = 1 in
let table = "test.simple" in
let columns = Belt.Array.map [|"id"; "code"|] Json.Encode.string in
(* order is important here *)
let rows = Belt.Array.map [|
Json.Encode.array Json.Encode.string [| id_1; code_1 |];
Json.Encode.array Json.Encode.string [| id_0; code_0 |];
|] (fun a -> a) in
Sql.mutate_batch conn ~batch_size ~table ~columns ~rows (fun res ->
match res with
| `Mutation (rows, id) -> let _ = Js.log3 "mutation should have failed" rows id in
finish (fail "see log")
| `Error _ -> Sql.query conn ~sql:{j|SELECT * FROM test.simple WHERE code='$(code_1)'|j} (fun res ->
match res with
| `Error e -> let _ = Js.log e in finish (fail "see log")
| `Select (rows, _) ->
rows
|> Expect.expect
|> Expect.toHaveLength 0
|> finish
)
)
);
);
)