/
DatabaseTests.elm
364 lines (329 loc) · 14.7 KB
/
DatabaseTests.elm
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
module DatabaseTests exposing (..)
import Array exposing (Array)
import Database exposing (..)
import Expect
import Test exposing (..)
insertTests : Test
insertTests =
describe "insert"
[ test "you can insert a row" <|
\_ ->
empty
|> insert "human" [ String "Socrates" ]
|> Expect.ok
, test "you can't insert a row with a different number of columns" <|
\_ ->
empty
|> insert "human" [ String "Socrates" ]
|> Result.andThen (insert "human" [ String "Socrates", String "Greek" ])
|> Expect.equal
(Err
(SchemaMismatch
{ wanted = Array.fromList [ StringType ]
, got = Array.fromList [ StringType, StringType ]
}
)
)
, test "you can't insert a row with a different field type" <|
\_ ->
empty
|> insert "human" [ String "Socrates" ]
|> Result.andThen (insert "human" [ Int 0 ])
|> Expect.equal
(Err
(SchemaMismatch
{ wanted = Array.fromList [ StringType ]
, got = Array.fromList [ IntType ]
}
)
)
]
runPlanTests : Test
runPlanTests =
describe "runPlan"
[ describe "read"
[ test "can read a relation" <|
\_ ->
empty
|> insert "human" [ String "Socrates" ]
|> Result.andThen (runPlan (Read "human"))
|> Result.map .rows
|> Expect.equal (Ok [ Array.fromList [ String "Socrates" ] ])
, test "cannot read a relation that doesn't exist" <|
\_ ->
empty
|> runPlan (Read "human")
|> Expect.equal (Err (RelationNotFound "human"))
]
, describe "select"
[ test "doesn't let you select a field that doesn't exist on the left-hand side" <|
\_ ->
toysDb
|> Result.andThen
(runPlan (Select (Predicate 5 Eq (Constant (Int 0))) (Read "toys")))
|> Expect.equal (Err (UnknownFields [ 5 ]))
, test "doesn't let you select a field that doesn't exist on the right-hand side" <|
\_ ->
toysDb
|> Result.andThen
(runPlan (Select (Predicate 0 Eq (Field 5)) (Read "toys")))
|> Expect.equal (Err (UnknownFields [ 5 ]))
, test "doesn't let you make comparisons against unlike types" <|
\_ ->
toysDb
|> Result.andThen
(runPlan (Select (Predicate 0 Eq (Constant (Int 0))) (Read "toys")))
|> Expect.equal (Err (IncompatibleComparison StringType IntType))
, test "can select on string equality" <|
\_ ->
toysDb
|> Result.andThen
(runPlan (Select (Predicate 1 Eq (Constant (String "Bear"))) (Read "toys")))
|> Result.map .rows
|> Expect.equal (Ok [ cloudBear ])
, test "can select on integer equality" <|
\_ ->
toysDb
|> Result.andThen
(runPlan (Select (Predicate 3 Eq (Constant (Int 16))) (Read "toys")))
|> Result.map .rows
|> Expect.equal (Ok [ axel ])
, test "can select on string greater-than" <|
\_ ->
toysDb
|> Result.andThen
(runPlan (Select (Predicate 0 Gt (Constant (String "Cloud Bear"))) (Read "toys")))
|> Result.map .rows
|> Expect.equal (Ok [ hampton, humphrey ])
, test "can select on integer greater-than" <|
\_ ->
toysDb
|> Result.andThen
(runPlan (Select (Predicate 3 Gt (Constant (Int 20))) (Read "toys")))
|> Result.map .rows
|> Expect.equal (Ok [ humphrey, cloudBear ])
, test "can select on string less-than" <|
\_ ->
toysDb
|> Result.andThen
(runPlan (Select (Predicate 0 Lt (Constant (String "Cloud Bear"))) (Read "toys")))
|> Result.map .rows
|> Expect.equal (Ok [ axel ])
, test "can select on integer less-than" <|
\_ ->
toysDb
|> Result.andThen
(runPlan (Select (Predicate 3 Lt (Constant (Int 20))) (Read "toys")))
|> Result.map .rows
|> Expect.equal (Ok [ hampton, axel ])
, test "can not-ify a selection" <|
\_ ->
toysDb
|> Result.andThen
(runPlan (Select (Not (Predicate 0 Eq (Constant (String "Humphrey")))) (Read "toys")))
|> Result.map .rows
|> Expect.equal (Ok [ hampton, cloudBear, axel ])
, test "can and-ify two selections" <|
\_ ->
toysDb
|> Result.andThen
(runPlan
(Select
(And
(Predicate 2 Eq (Constant (String "USA")))
(Predicate 3 Gt (Constant (Int 30)))
)
(Read "toys")
)
)
|> Result.map .rows
|> Expect.equal (Ok [ humphrey ])
, test "can or-ify two selections" <|
\_ ->
toysDb
|> Result.andThen
(runPlan
(Select
(Or
(Predicate 3 Lt (Constant (Int 16)))
(Predicate 3 Eq (Constant (Int 16)))
)
(Read "toys")
)
)
|> Result.map .rows
|> Expect.equal (Ok [ hampton, axel ])
]
, describe "project"
[ test "does not allow selecting non-existent fields" <|
\_ ->
toysDb
|> Result.andThen
(runPlan (Project [ 5 ] (Read "toys")))
|> Expect.equal (Err (UnknownFields [ 5 ]))
, test "can project a set of fields" <|
\_ ->
toysDb
|> Result.andThen
(runPlan (Project [ 0, 2 ] (Read "toys")))
|> Expect.equal
(Ok
{ schema = Array.fromList [ StringType, StringType ]
, rows =
[ Array.fromList [ String "Hampton", String "USA" ]
, Array.fromList [ String "Humphrey", String "USA" ]
, Array.fromList [ String "Cloud Bear", String "USA" ]
, Array.fromList [ String "Axel", String "Iceland" ]
]
}
)
]
, describe "join"
[ test "it's an error if you choose fields that don't exist on the left" <|
\_ ->
mascotsDb
|> Result.andThen
(runPlan
(Join
{ left = Read "mascots"
, right = Read "teams"
, fields = [ ( 3, 0 ) ]
}
)
)
|> Expect.equal (Err (UnknownFields [ 3 ]))
, test "it's an error if you choose fields that don't exist on the right" <|
\_ ->
mascotsDb
|> Result.andThen
(runPlan
(Join
{ left = Read "mascots"
, right = Read "teams"
, fields = [ ( 0, 4 ) ]
}
)
)
|> Expect.equal (Err (UnknownFields [ 4 ]))
, test "it's an error if you join on incompatible types" <|
\_ ->
mascotsDb
|> Result.andThen
(runPlan
(Join
{ left = Read "mascots"
, right = Read "teams"
, fields = [ ( 0, 3 ) ]
}
)
)
|> Expect.equal
(Err
(SchemaMismatch
{ wanted = Array.fromList [ StringType ]
, got = Array.fromList [ IntType ]
}
)
)
, test "joins on fields in order" <|
\_ ->
mascotsDb
|> Result.andThen
(runPlan
(Join
{ left = Read "mascots"
, right = Read "teams"
, fields = [ ( 1, 0 ) ]
}
)
)
|> Expect.equal
(Ok
{ schema = Array.fromList [ StringType, StringType, StringType, StringType, StringType, IntType ]
, rows =
[ Array.append gritty flyers
, Array.append louie blues
, Array.append fredbird cardinals
]
}
)
, test "if you specify no columns, it works the same as a cross-product" <|
\_ ->
mascotsDb
|> Result.andThen
(runPlan
(Join
{ left = Read "mascots"
, right = Read "teams"
, fields = []
}
)
)
|> Expect.equal
(Ok
{ schema = Array.fromList [ StringType, StringType, StringType, StringType, StringType, IntType ]
, rows =
[ Array.append fredbird flyers
, Array.append louie flyers
, Array.append gritty flyers
, Array.append fredbird blues
, Array.append louie blues
, Array.append gritty blues
, Array.append fredbird cardinals
, Array.append louie cardinals
, Array.append gritty cardinals
]
}
)
]
]
-- Example: toys
-- Used for simple selection
toysDb : Result Problem Database
toysDb =
Ok empty
|> Result.andThen (insert "toys" (Array.toList axel))
|> Result.andThen (insert "toys" (Array.toList cloudBear))
|> Result.andThen (insert "toys" (Array.toList humphrey))
|> Result.andThen (insert "toys" (Array.toList hampton))
axel : Array Constant
axel =
Array.fromList [ String "Axel", String "Puffin", String "Iceland", Int 16 ]
cloudBear : Array Constant
cloudBear =
Array.fromList [ String "Cloud Bear", String "Bear", String "USA", Int 24 ]
humphrey : Array Constant
humphrey =
Array.fromList [ String "Humphrey", String "Elephant", String "USA", Int 32 ]
hampton : Array Constant
hampton =
Array.fromList [ String "Hampton", String "Rabbit", String "USA", Int 8 ]
-- Example: Mascots
-- used for joining
mascotsDb : Result Problem Database
mascotsDb =
Ok empty
|> Result.andThen (insert "mascots" (Array.toList fredbird))
|> Result.andThen (insert "mascots" (Array.toList louie))
|> Result.andThen (insert "mascots" (Array.toList gritty))
|> Result.andThen (insert "teams" (Array.toList cardinals))
|> Result.andThen (insert "teams" (Array.toList blues))
|> Result.andThen (insert "teams" (Array.toList flyers))
fredbird : Array Constant
fredbird =
Array.fromList [ String "Fredbird", String "Cardinals" ]
louie : Array Constant
louie =
Array.fromList [ String "Louie", String "Blues" ]
gritty : Array Constant
gritty =
Array.fromList [ String "Gritty", String "Flyers" ]
cardinals : Array Constant
cardinals =
Array.fromList [ String "Cardinals", String "MLB", String "St. Louis, MO", Int 1882 ]
blues : Array Constant
blues =
Array.fromList [ String "Blues", String "NHL", String "St. Louis, MO", Int 1967 ]
flyers : Array Constant
flyers =
Array.fromList [ String "Flyers", String "NHL", String "Philadelphia, PA", Int 1967 ]