-
Notifications
You must be signed in to change notification settings - Fork 302
Expand file tree
/
Copy pathJSONTest.hs
More file actions
679 lines (581 loc) · 30.2 KB
/
JSONTest.hs
File metadata and controls
679 lines (581 loc) · 30.2 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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-} -- FIXME
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module JSONTest where
import Control.Monad.IO.Class (MonadIO)
import Data.Aeson
import qualified Data.Vector as V (fromList)
import Test.HUnit (assertBool)
import Test.Hspec.Expectations ()
import Database.Persist
import Database.Persist.Postgresql.JSON
import PgInit
share [mkPersist persistSettings, mkMigrate "jsonTestMigrate"] [persistLowerCase|
TestValue
json Value
deriving Show
|]
cleanDB :: (BaseBackend backend ~ SqlBackend, PersistQueryWrite backend, MonadIO m)
=> ReaderT backend m ()
cleanDB = deleteWhere ([] :: [Filter TestValue])
emptyArr :: Value
emptyArr = toJSON ([] :: [Value])
insert' :: (MonadIO m, PersistStoreWrite backend, BaseBackend backend ~ SqlBackend)
=> Value -> ReaderT backend m (Key TestValue)
insert' = insert . TestValue
matchKeys :: (Show record, Show (Key record), MonadIO m, Eq (Key record))
=> [Key record] -> [Entity record] -> m ()
matchKeys ys xs = do
msg1 `assertBoolIO` (xLen == yLen)
forM_ ys $ \y -> msg2 y `assertBoolIO` (y `elem` ks)
where ks = entityKey <$> xs
xLen = length xs
yLen = length ys
msg1 = mconcat
[ "\nexpected: ", show yLen
, "\n but got: ", show xLen
, "\n[xs: ", show xs, "]"
, "\n[ys: ", show ys, "]"
]
msg2 y = mconcat
[ "key \"", show y
, "\" not in result:\n ", show ks
]
setup :: IO TestKeys
setup = asIO $ runConn_ $ do
void $ runMigrationSilent jsonTestMigrate
testKeys
teardown :: IO ()
teardown = asIO $ runConn_ $ do
cleanDB
shouldBeIO :: (Show a, Eq a, MonadIO m) => a -> a -> m ()
shouldBeIO x y = liftIO $ shouldBe x y
assertBoolIO :: MonadIO m => String -> Bool -> m ()
assertBoolIO s b = liftIO $ assertBool s b
testKeys :: (Monad m, MonadIO m) => ReaderT SqlBackend m TestKeys
testKeys = do
nullK <- insert' Null
boolTK <- insert' $ Bool True
boolFK <- insert' $ toJSON False
num0K <- insert' $ Number 0
num1K <- insert' $ Number 1
numBigK <- insert' $ toJSON (1234567890 :: Int)
numFloatK <- insert' $ Number 0.0
numSmallK <- insert' $ Number 0.0000000000000000123
numFloat2K <- insert' $ Number 1.5
-- numBigFloatK will turn into 9876543210.123457 because JSON
numBigFloatK <- insert' $ toJSON (9876543210.123456789 :: Double)
strNullK <- insert' $ String ""
strObjK <- insert' $ String "{}"
strArrK <- insert' $ String "[]"
strAK <- insert' $ String "a"
strTestK <- insert' $ toJSON ("testing" :: Text)
str2K <- insert' $ String "2"
strFloatK <- insert' $ String "0.45876"
arrNullK <- insert' $ Array $ V.fromList []
arrListK <- insert' $ toJSON [emptyArr,emptyArr,toJSON [emptyArr,emptyArr]]
arrList2K <- insert' $ toJSON [emptyArr,toJSON [Number 3,Bool False]
,toJSON [emptyArr,toJSON [Object mempty]]
]
arrFilledK <- insert' $ toJSON [Null, Number 4, String "b"
,Object mempty, emptyArr
,object [ "test" .= [Null], "test2" .= String "yes"]
]
arrList3K <- insert' $ toJSON [toJSON [String "a"], Number 1]
arrList4K <- insert' $ toJSON [String "a", String "b", String "c", String "d"]
objNullK <- insert' $ Object mempty
objTestK <- insert' $ object ["test" .= Null, "test1" .= String "no"]
objDeepK <- insert' $ object ["c" .= Number 24.986, "foo" .= object ["deep1" .= Bool True]]
objEmptyK <- insert' $ object ["" .= Number 9001]
objFullK <- insert' $ object ["a" .= Number 1, "b" .= Number 2
,"c" .= Number 3, "d" .= Number 4
]
return TestKeys{..}
data TestKeys =
TestKeys { nullK :: Key TestValue
, boolTK :: Key TestValue
, boolFK :: Key TestValue
, num0K :: Key TestValue
, num1K :: Key TestValue
, numBigK :: Key TestValue
, numFloatK :: Key TestValue
, numSmallK :: Key TestValue
, numFloat2K :: Key TestValue
, numBigFloatK :: Key TestValue
, strNullK :: Key TestValue
, strObjK :: Key TestValue
, strArrK :: Key TestValue
, strAK :: Key TestValue
, strTestK :: Key TestValue
, str2K :: Key TestValue
, strFloatK :: Key TestValue
, arrNullK :: Key TestValue
, arrListK :: Key TestValue
, arrList2K :: Key TestValue
, arrFilledK :: Key TestValue
, objNullK :: Key TestValue
, objTestK :: Key TestValue
, objDeepK :: Key TestValue
, arrList3K :: Key TestValue
, arrList4K :: Key TestValue
, objEmptyK :: Key TestValue
, objFullK :: Key TestValue
} deriving (Eq, Ord, Show)
specs :: Spec
specs = afterAll_ teardown $ do
beforeAll setup $ do
describe "Testing JSON operators" $ do
describe "@>. object queries" $ do
it "matches an empty Object with any object" $
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. Object mempty] []
[objNullK, objTestK, objDeepK, objEmptyK, objFullK] `matchKeys` vals
it "matches a subset of object properties" $
-- {test: null, test1: no} @>. {test: null} == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. object ["test" .= Null]] []
[objTestK] `matchKeys` vals
it "matches a nested object against an empty object at the same key" $
-- {c: 24.986, foo: {deep1: true}} @>. {foo: {}} == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. object ["foo" .= object []]] []
[objDeepK] `matchKeys` vals
it "doesn't match a nested object against a string at the same key" $
-- {c: 24.986, foo: {deep1: true}} @>. {foo: nope} == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. object ["foo" .= String "nope"]] []
[] `matchKeys` vals
it "matches a nested object when the query object is identical" $
-- {c: 24.986, foo: {deep1: true}} @>. {foo: {deep1: true}} == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. (object ["foo" .= object ["deep1" .= True]])] []
[objDeepK] `matchKeys` vals
it "doesn't match a nested object when queried with that exact object" $
-- {c: 24.986, foo: {deep1: true}} @>. {deep1: true} == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. object ["deep1" .= True]] []
[] `matchKeys` vals
describe "@>. array queries" $ do
it "matches an empty Array with any list" $
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. emptyArr] []
[arrNullK, arrListK, arrList2K, arrFilledK, arrList3K, arrList4K] `matchKeys` vals
it "matches list when queried with subset (1 item)" $
-- [null, 4, 'b', {}, [], {test: [null], test2: 'yes'}] @>. [4] == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. toJSON [4 :: Int]] []
[arrFilledK] `matchKeys` vals
it "matches list when queried with subset (2 items)" $
-- [null, 4, 'b', {}, [], {test: [null], test2: 'yes'}] @>. [null,'b'] == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. toJSON [Null, String "b"]] []
[arrFilledK] `matchKeys` vals
it "doesn't match list when queried with intersecting list (1 match, 1 diff)" $
-- [null, 4, 'b', {}, [], {test: [null], test2: 'yes'}] @>. [null,'d'] == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. toJSON [emptyArr, String "d"]] []
[] `matchKeys` vals
it "matches list when queried with same list in different order" $
-- [null, 4, 'b', {}, [], {test: [null], test2: 'yes'}] @>.
-- [[],'b',{test: [null],test2: 'yes'},4,null,{}] == True
\TestKeys {..} -> runConnAssert $ do
let queryList =
toJSON [ emptyArr, String "b"
, object [ "test" .= [Null], "test2" .= String "yes"]
, Number 4, Null, Object mempty ]
vals <- selectList [TestValueJson @>. queryList ] []
[arrFilledK] `matchKeys` vals
it "doesn't match list when queried with same list + 1 item" $
-- [null,4,'b',{},[],{test:[null],test2:'yes'}] @>.
-- [null,4,'b',{},[],{test:[null],test2: 'yes'}, false] == False
\TestKeys {..} -> runConnAssert $ do
let testList =
toJSON [ Null, Number 4, String "b", Object mempty, emptyArr
, object [ "test" .= [Null], "test2" .= String "yes"]
, Bool False ]
vals <- selectList [TestValueJson @>. testList] []
[] `matchKeys` vals
it "matches list when it shares an empty object with the query list" $
-- [null,4,'b',{},[],{test: [null],test2: 'yes'}] @>. [{}] == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. toJSON [Object mempty]] []
[arrFilledK] `matchKeys` vals
it "matches list with nested list, when queried with an empty nested list" $
-- [null,4,'b',{},[],{test:[null],test2:'yes'}] @>. [{test:[]}] == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. toJSON [object ["test" .= emptyArr]]] []
[arrFilledK] `matchKeys` vals
it "doesn't match list with nested list, when queried with a diff. nested list" $
-- [null,4,"b",{},[],{"test":[null],"test2":"yes"}] @>.
-- [{"test1":[null]}] == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. toJSON [object ["test1" .= [Null]]]] []
[] `matchKeys` vals
it "matches many nested lists when queried with empty nested list" $
-- [[],[],[[],[]]] @>. [[]] == True
-- [[],[3,false],[[],[{}]]] @>. [[]] == True
-- [null,4,"b",{},[],{"test":[null],"test2":"yes"}] @>. [[]] == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. toJSON [emptyArr]] []
[arrListK,arrList2K,arrFilledK, arrList3K] `matchKeys` vals
it "matches nested list when queried with a subset of that list" $
-- [[],[3,false],[[],[{}]]] @>. [[3]] == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. toJSON [[3 :: Int]]] []
[arrList2K] `matchKeys` vals
it "doesn't match nested list againts a partial intersection of that list" $
-- [[],[3,false],[[],[{}]]] @>. [[true,3]] == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. toJSON [[Bool True, Number 3]]] []
[] `matchKeys` vals
it "matches list when queried with raw number contained in the list" $
-- [null,4,"b",{},[],{"test":[null],"test2":"yes"}] @>. 4 == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. Number 4] []
[arrFilledK] `matchKeys` vals
it "doesn't match list when queried with raw value not contained in the list" $
-- [null,4,"b",{},[],{"test":[null],"test2":"yes"}] @>. 99 == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. Number 99] []
[] `matchKeys` vals
it "matches list when queried with raw string contained in the list" $
-- [null,4,"b",{},[],{"test":[null],"test2":"yes"}] @>. "b" == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. String "b"] []
[arrFilledK, arrList4K] `matchKeys` vals
it "doesn't match list with empty object when queried with \"{}\" " $
-- [null,4,"b",{},[],{"test":[null],"test2":"yes"}] @>. "{}" == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. String "{}"] []
[strObjK] `matchKeys` vals
it "doesnt match list with nested object when queried with object (not in list)" $
-- [null,4,"b",{},[],{"test":[null],"test2":"yes"}] @>.
-- {"test":[null],"test2":"yes"} == False
\TestKeys {..} -> runConnAssert $ do
let queryObject = object [ "test" .= [Null], "test2" .= String "yes"]
vals <- selectList [TestValueJson @>. queryObject ] []
[] `matchKeys` vals
describe "@>. string queries" $ do
it "matches identical strings" $
-- "testing" @>. "testing" == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. String "testing"] []
[strTestK] `matchKeys` vals
it "doesnt match case insensitive" $
-- "testing" @>. "Testing" == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. String "Testing"] []
[] `matchKeys` vals
it "doesn't match substrings" $
-- "testing" @>. "test" == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. String "test"] []
[] `matchKeys` vals
it "doesn't match strings with object keys" $
-- "testing" @>. {"testing":1} == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. object ["testing" .= Number 1]] []
[] `matchKeys` vals
describe "@>. number queries" $ do
it "matches identical numbers" $
-- 1 @>. 1 == True
-- [1] @>. 1 == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. toJSON (1 :: Int)] []
[num1K, arrList3K] `matchKeys` vals
it "matches numbers when queried with float" $
-- 0 @>. 0.0 == True
-- 0.0 @>. 0.0 == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. toJSON (0.0 :: Double)] []
[num0K,numFloatK] `matchKeys` vals
it "does not match numbers when queried with a substring of that number" $
-- 1234567890 @>. 123456789 == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. toJSON (123456789 :: Int)] []
[] `matchKeys` vals
it "does not match number when queried with different number" $
-- 1234567890 @>. 234567890 == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. toJSON (234567890 :: Int)] []
[] `matchKeys` vals
it "does not match number when queried with string of that number" $
-- 1 @>. "1" == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. String "1"] []
[] `matchKeys` vals
it "does not match number when queried with list of digits" $
-- 1234567890 @>. [1,2,3,4,5,6,7,8,9,0] == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. toJSON ([1,2,3,4,5,6,7,8,9,0] :: [Int])] []
[] `matchKeys` vals
describe "@>. boolean queries" $ do
it "matches identical booleans (True)" $
-- true @>. true == True
-- false @>. true == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. toJSON True] []
[boolTK] `matchKeys` vals
it "matches identical booleans (False)" $
-- false @>. false == True
-- true @>. false == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. Bool False] []
[boolFK] `matchKeys` vals
it "does not match boolean with string of boolean" $
-- true @>. "true" == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. String "true"] []
[] `matchKeys` vals
describe "@>. null queries" $ do
it "matches nulls" $
-- null @>. null == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. Null] []
[nullK,arrFilledK] `matchKeys` vals
it "does not match null with string of null" $
-- null @>. "null" == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson @>. String "null"] []
[] `matchKeys` vals
describe "<@. queries" $ do
it "matches subobject when queried with superobject" $
-- {} <@. {"test":null,"test1":"no","blabla":[]} == True
-- {"test":null,"test1":"no"} <@. {"test":null,"test1":"no","blabla":[]} == True
\TestKeys {..} -> runConnAssert $ do
let queryObject = object ["test" .= Null
, "test1" .= String "no"
, "blabla" .= emptyArr
]
vals <- selectList [TestValueJson <@. queryObject] []
[objNullK,objTestK] `matchKeys` vals
it "matches raw values and sublists when queried with superlist" $
-- [] <@. [null,4,"b",{},[],{"test":[null],"test2":"yes"},false] == True
-- null <@. [null,4,"b",{},[],{"test":[null],"test2":"yes"},false] == True
-- false <@. [null,4,"b",{},[],{"test":[null],"test2":"yes"},false] == True
-- [null,4,"b",{},[],{"test":[null],"test2":"yes"}] <@.
-- [null,4,"b",{},[],{"test":[null],"test2":"yes"},false] == True
\TestKeys {..} -> runConnAssert $ do
let queryList =
toJSON [ Null, Number 4, String "b", Object mempty, emptyArr
, object [ "test" .= [Null], "test2" .= String "yes"]
, Bool False ]
vals <- selectList [TestValueJson <@. queryList ] []
[arrNullK,arrFilledK,boolFK,nullK] `matchKeys` vals
it "matches identical strings" $
-- "a" <@. "a" == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson <@. String "a"] []
[strAK] `matchKeys` vals
it "matches identical big floats" $
-- 9876543210.123457 <@ 9876543210.123457 == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson <@. Number 9876543210.123457] []
[numBigFloatK] `matchKeys` vals
it "doesn't match different big floats" $
-- 9876543210.123457 <@. 9876543210.123456789 == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson <@. Number 9876543210.123456789] []
[] `matchKeys` vals
it "matches nulls" $
-- null <@. null == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson <@. Null] []
[nullK] `matchKeys` vals
describe "?. queries" $ do
it "matches top level keys and not the keys of nested objects" $
-- {"test":null,"test1":"no"} ?. "test" == True
-- [null,4,"b",{},[],{"test":[null],"test2":"yes"}] ?. "test" == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?. "test"] []
[objTestK] `matchKeys` vals
it "doesn't match nested key" $
-- {"c":24.986,"foo":{"deep1":true"}} ?. "deep1" == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?. "deep1"] []
[] `matchKeys` vals
it "matches \"{}\" but not empty object when queried with \"{}\"" $
-- "{}" ?. "{}" == True
-- {} ?. "{}" == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?. "{}"] []
[strObjK] `matchKeys` vals
it "matches raw empty str and empty str key when queried with \"\"" $
---- {} ?. "" == False
---- "" ?. "" == True
---- {"":9001} ?. "" == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?. ""] []
[strNullK,objEmptyK] `matchKeys` vals
it "matches lists containing string value when queried with raw string value" $
-- [null,4,"b",{},[],{"test":[null],"test2":"yes"}] ?. "b" == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?. "b"] []
[arrFilledK,arrList4K,objFullK] `matchKeys` vals
it "matches lists, objects, and raw values correctly when queried with string" $
-- [["a"]] ?. "a" == False
-- "a" ?. "a" == True
-- ["a","b","c","d"] ?. "a" == True
-- {"a":1,"b":2,"c":3,"d":4} ?. "a" == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?. "a"] []
[strAK,arrList4K,objFullK] `matchKeys` vals
it "matches string list but not real list when queried with \"[]\"" $
-- "[]" ?. "[]" == True
-- [] ?. "[]" == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?. "[]"] []
[strArrK] `matchKeys` vals
it "does not match null when queried with string null" $
-- null ?. "null" == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?. "null"] []
[] `matchKeys` vals
it "does not match bool whe nqueried with string bool" $
-- true ?. "true" == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?. "true"] []
[] `matchKeys` vals
describe "?|. queries" $ do
it "matches raw vals, lists, objects, and nested objects" $
-- "a" ?|. ["a","b","c"] == True
-- [["a"],1] ?|. ["a","b","c"] == False
-- [null,4,"b",{},[],{"test":[null],"test2":"yes"}] ?|. ["a","b","c"] == True
-- ["a","b","c","d"] ?|. ["a","b","c"] == True
-- {"a":1,"b":2,"c":3,"d":4} ?|. ["a","b","c"] == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?|. ["a","b","c"]] []
[strAK,arrFilledK,objDeepK,arrList4K,objFullK] `matchKeys` vals
it "matches str object but not object when queried with \"{}\"" $
-- "{}" ?|. ["{}"] == True
-- {} ?|. ["{}"] == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?|. ["{}"]] []
[strObjK] `matchKeys` vals
it "doesn't match superstrings when queried with substring" $
-- [null,4,"b",{},[],{"test":[null],"test2":"yes"}] ?|. ["test"] == False
-- "testing" ?|. ["test"] == False
-- {"test":null,"test1":"no"} ?|. ["test"] == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?|. ["test"]] []
[objTestK] `matchKeys` vals
it "doesn't match nested keys" $
-- {"c":24.986,"foo":{"deep1":true"}} ?|. ["deep1"] == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?|. ["deep1"]] []
[] `matchKeys` vals
it "doesn't match anything when queried with empty list" $
-- ANYTHING ?|. [] == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?|. []] []
[] `matchKeys` vals
it "doesn't match raw, non-string, values when queried with strings" $
-- true ?|. ["true","null","1"] == False
-- null ?|. ["true","null","1"] == False
-- 1 ?|. ["true","null","1"] == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?|. ["true","null","1"]] []
[] `matchKeys` vals
it "matches string array when queried with \"[]\"" $
-- [] ?|. ["[]"] == False
-- "[]" ?|. ["[]"] == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?|. ["[]"]] []
[strArrK] `matchKeys` vals
describe "?&. queries" $ do
it "matches anything when queried with an empty list" $
-- ANYTHING ?&. [] == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?&. []] []
flip matchKeys vals [ nullK
, boolTK, boolFK
, num0K, num1K, numBigK, numFloatK
, numSmallK, numFloat2K, numBigFloatK
, strNullK, strObjK, strArrK, strAK
, strTestK, str2K, strFloatK
, arrNullK, arrListK, arrList2K
, arrFilledK, arrList3K, arrList4K
, objNullK, objTestK, objDeepK
, objEmptyK, objFullK
]
it "matches raw values, lists, and objects when queried with string" $
-- "a" ?&. ["a"] == True
-- [["a"],1] ?&. ["a"] == False
-- ["a","b","c","d"] ?&. ["a"] == True
-- {"a":1,"b":2,"c":3,"d":4} ?&. ["a"] == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?&. ["a"]] []
[strAK,arrList4K,objFullK] `matchKeys` vals
it "matches raw values, lists, and objects when queried with multiple string" $
-- [null,4,"b",{},[],{"test":[null],"test2":"yes"}] ?&. ["b","c"] == False
-- {"c":24.986,"foo":{"deep1":true"}} ?&. ["b","c"] == False
-- ["a","b","c","d"] ?&. ["b","c"] == True
-- {"a":1,"b":2,"c":3,"d":4} ?&. ["b","c"] == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?&. ["b","c"]] []
[arrList4K,objFullK] `matchKeys` vals
it "matches object string when queried with \"{}\"" $
-- {} ?&. ["{}"] == False
-- "{}" ?&. ["{}"] == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?&. ["{}"]] []
[strObjK] `matchKeys` vals
it "doesn't match superstrings when queried with substring" $
-- [null,4,"b",{},[],{"test":[null],"test2":"yes"}] ?&. ["test"] == False
-- "testing" ?&. ["test"] == False
-- {"test":null,"test1":"no"} ?&. ["test"] == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?&. ["test"]] []
[objTestK] `matchKeys` vals
it "doesn't match nested keys" $
-- {"c":24.986,"foo":{"deep1":true"}} ?&. ["deep1"] == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?&. ["deep1"]] []
[] `matchKeys` vals
it "doesn't match anything when there is a partial match" $
-- "a" ?&. ["a","e"] == False
-- ["a","b","c","d"] ?&. ["a","e"] == False
-- {"a":1,"b":2,"c":3,"d":4} ?&. ["a","e"] == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?&. ["a","e"]] []
[] `matchKeys` vals
it "matches string array when queried with \"[]\"" $
-- [] ?&. ["[]"] == False
-- "[]" ?&. ["[]"] == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?&. ["[]"]] []
[strArrK] `matchKeys` vals
it "doesn't match null when queried with string null" $
-- THIS WILL FAIL IF THE IMPLEMENTATION USES
-- @ '{null}' @
-- INSTEAD OF
-- @ ARRAY['null'] @
-- null ?&. ["null"] == False
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?&. ["null"]] []
[] `matchKeys` vals
it "doesn't match number when queried with str of that number" $
-- [["a"],1] ?&. ["1"] == False
-- "1" ?&. ["1"] == True
\TestKeys {..} -> runConnAssert $ do
str1 <- insert' $ toJSON $ String "1"
vals <- selectList [TestValueJson ?&. ["1"]] []
[str1] `matchKeys` vals
it "doesn't match empty objs or list when queried with empty string" $
-- {} ?&. [""] == False
-- [] ?&. [""] == False
-- "" ?&. [""] == True
-- {"":9001} ?&. [""] == True
\TestKeys {..} -> runConnAssert $ do
vals <- selectList [TestValueJson ?&. [""]] []
[strNullK,objEmptyK] `matchKeys` vals