i think that should do it

This commit is contained in:
parsonsmatt 2021-03-29 13:12:29 -06:00
parent cf7a6e50ae
commit d2d52566bb

View File

@ -726,130 +726,155 @@ testFilterOperators =
testInclusion :: Spec testInclusion :: Spec
testInclusion = do testInclusion = do
describe "@>" $ do describe "@>" $ do
it "creates sane SQL" $ it "creates sane SQL" $ do
createSaneSQL let obj = object ["a" .= False, "b" .= True]
(jsonbVal (object ["a" .= False, "b" .= True]) @>. jsonbVal (object ["a" .= False])) encoded = BSL.toStrct $ encode obj
"SELECT (? @> ?)\nFROM \"Json\"\n" createSaneSQL
[ PersistLiteralEscaped "{\"a\":false,\"b\":true}" (jsonbVal obj @>. jsonbVal (object ["a" .= False]))
, PersistLiteralEscaped "{\"a\":false}" ] "SELECT (? @> ?)\nFROM \"Json\"\n"
it "creates sane SQL (chained)" $ do [ PersistLiteralEscaped encoded
let obj = object ["a" .= [object ["b" .= True]]] , PersistLiteralEscaped "{\"a\":false}"
createSaneSQL ]
(jsonbVal obj ->. "a" @>. jsonbVal (object ["b" .= True])) it "creates sane SQL (chained)" $ do
"SELECT ((? -> ?) @> ?)\nFROM \"Json\"\n" let obj = object ["a" .= [object ["b" .= True]]]
[ PersistLiteralEscaped "{\"a\":[{\"b\":true}]}" encoded = BSL.toStrct $ encode obj
, PersistText "a" createSaneSQL
, PersistLiteralEscaped "{\"b\":true}" ] (jsonbVal obj ->. "a" @>. jsonbVal (object ["b" .= True]))
it "works as expected" $ run $ do "SELECT ((? -> ?) @> ?)\nFROM \"Json\"\n"
x <- selectJSONwhere $ \v -> v @>. jsonbVal (Number 1) [ PersistLiteralEscaped encoded
y <- selectJSONwhere $ \v -> v @>. jsonbVal (toJSON [object ["a" .= Number 3.14]]) , PersistText "a"
z <- selectJSONwhere $ \v -> v ->. 1 @>. jsonbVal (object ["a" .= Number 3.14]) , PersistLiteralEscaped "{\"b\":true}"
liftIO $ length x `shouldBe` 2 ]
liftIO $ length y `shouldBe` 1 it "works as expected" $ run $ do
liftIO $ length z `shouldBe` 1 x <- selectJSONwhere $ \v -> v @>. jsonbVal (Number 1)
describe "<@" $ do y <- selectJSONwhere $ \v -> v @>. jsonbVal (toJSON [object ["a" .= Number 3.14]])
it "creates sane SQL" $ z <- selectJSONwhere $ \v -> v ->. 1 @>. jsonbVal (object ["a" .= Number 3.14])
createSaneSQL liftIO $ length x `shouldBe` 2
(jsonbVal (object ["a" .= False]) <@. jsonbVal (object ["a" .= False, "b" .= True])) liftIO $ length y `shouldBe` 1
"SELECT (? <@ ?)\nFROM \"Json\"\n" liftIO $ length z `shouldBe` 1
[ PersistLiteralEscaped "{\"a\":false}" describe "<@" $ do
, PersistLiteralEscaped "{\"a\":false,\"b\":true}" ] it "creates sane SQL" $
it "creates sane SQL (chained)" $ do let obj = object ["a" .= False, "b" .= True]
let obj = object ["a" .= [object ["b" .= True]]] encoded = BSL.toStrct $ encode obj
createSaneSQL createSaneSQL
(jsonbVal obj ->. "a" <@. jsonbVal (object ["b" .= True, "c" .= Null])) (jsonbVal (object ["a" .= False]) <@. jsonbVal obj )
"SELECT ((? -> ?) <@ ?)\nFROM \"Json\"\n" "SELECT (? <@ ?)\nFROM \"Json\"\n"
[ PersistLiteralEscaped "{\"a\":[{\"b\":true}]}" [ PersistLiteralEscaped "{\"a\":false}"
, PersistText "a" , PersistLiteralEscaped encoded
, PersistLiteralEscaped "{\"b\":true,\"c\":null}" ] ]
it "works as expected" $ run $ do it "creates sane SQL (chained)" $ do
x <- selectJSONwhere $ \v -> v <@. jsonbVal (toJSON [Number 1]) let obj = object ["a" .= [object ["b" .= True]]]
y <- selectJSONwhere $ \v -> v <@. jsonbVal (object ["a" .= (1 :: Int), "b" .= False, "c" .= Null]) obj' = object ["b" .= True, "c" .= Null]
z <- selectJSONwhere $ \v -> v #>. ["a","b"] <@. jsonbVal (object ["b" .= False, "c" .= String "message"]) encoded = encode obj'
liftIO $ length x `shouldBe` 2 createSaneSQL
liftIO $ length y `shouldBe` 1 (jsonbVal obj ->. "a" <@. jsonbVal )
liftIO $ length z `shouldBe` 1 "SELECT ((? -> ?) <@ ?)\nFROM \"Json\"\n"
[ PersistLiteralEscaped "{\"a\":[{\"b\":true}]}"
, PersistText "a"
, PersistLiteralEscaped encoded'
]
it "works as expected" $ run $ do
x <- selectJSONwhere $ \v -> v <@. jsonbVal (toJSON [Number 1])
y <- selectJSONwhere $ \v -> v <@. jsonbVal (object ["a" .= (1 :: Int), "b" .= False, "c" .= Null])
z <- selectJSONwhere $ \v -> v #>. ["a","b"] <@. jsonbVal (object ["b" .= False, "c" .= String "message"])
liftIO $ length x `shouldBe` 2
liftIO $ length y `shouldBe` 1
liftIO $ length z `shouldBe` 1
testQMark :: Spec testQMark :: Spec
testQMark = testQMark = do
describe "Question Mark" $ do describe "Question Mark" $ do
it "creates sane SQL" $ it "creates sane SQL" $ do
createSaneSQL let obj = object ["a" .= False, "b" .= True]
(jsonbVal (object ["a" .= False, "b" .= True]) JSON.?. "a") encoded = BSL.toStrict $ encode obj
"SELECT (? ?? ?)\nFROM \"Json\"\n" createSaneSQL
[ PersistLiteralEscaped "{\"a\":false,\"b\":true}" (jsonbVal obj JSON.?. "a")
, PersistText "a" ] "SELECT (? ?? ?)\nFROM \"Json\"\n"
it "creates sane SQL (chained)" $ do [ PersistLiteralEscaped encoded
let obj = object ["a" .= [object ["b" .= True]]] , PersistText "a"
createSaneSQL ]
(jsonbVal obj #>. ["a","0"] JSON.?. "b") it "creates sane SQL (chained)" $ do
"SELECT ((? #> ?) ?? ?)\nFROM \"Json\"\n" let obj = object ["a" .= [object ["b" .= True]]]
[ PersistLiteralEscaped "{\"a\":[{\"b\":true}]}" encoded = BSL.toStrict $ encode obj
, persistTextArray ["a","0"] createSaneSQL
, PersistText "b" ] (jsonbVal obj #>. ["a","0"] JSON.?. "b")
it "works as expected" $ run $ do "SELECT ((? #> ?) ?? ?)\nFROM \"Json\"\n"
x <- selectJSONwhere (JSON.?. "a") [ PersistLiteralEscaped encoded
y <- selectJSONwhere (JSON.?. "test") , persistTextArray ["a","0"]
z <- selectJSONwhere $ \v -> v ->. "a" JSON.?. "b" , PersistText "b"
liftIO $ length x `shouldBe` 2 ]
liftIO $ length y `shouldBe` 2 it "works as expected" $ run $ do
liftIO $ length z `shouldBe` 1 x <- selectJSONwhere (JSON.?. "a")
y <- selectJSONwhere (JSON.?. "test")
z <- selectJSONwhere $ \v -> v ->. "a" JSON.?. "b"
liftIO $ length x `shouldBe` 2
liftIO $ length y `shouldBe` 2
liftIO $ length z `shouldBe` 1
testQMarkAny :: Spec testQMarkAny :: Spec
testQMarkAny = testQMarkAny = do
describe "Question Mark (Any)" $ do describe "Question Mark (Any)" $ do
it "creates sane SQL" $ it "creates sane SQL" $ do
createSaneSQL let obj = (object ["a" .= False, "b" .= True])
(jsonbVal (object ["a" .= False, "b" .= True]) ?|. ["a","c"]) encoded = BSL.toStrict $ encode obj
"SELECT (? ??| ?)\nFROM \"Json\"\n" createSaneSQL
[ PersistLiteralEscaped "{\"a\":false,\"b\":true}" (jsonbVal obj ?|. ["a","c"])
, persistTextArray ["a","c"] ] "SELECT (? ??| ?)\nFROM \"Json\"\n"
it "creates sane SQL (chained)" $ do [ PersistLiteralEscaped encoded
let obj = object ["a" .= [object ["b" .= True]]] , persistTextArray ["a","c"]
createSaneSQL ]
(jsonbVal obj #>. ["a","0"] ?|. ["b","c"]) it "creates sane SQL (chained)" $ do
"SELECT ((? #> ?) ??| ?)\nFROM \"Json\"\n" let obj = object ["a" .= [object ["b" .= True]]]
[ PersistLiteralEscaped "{\"a\":[{\"b\":true}]}" encoded = BSL.toStrct $ encode obj
, persistTextArray ["a","0"] createSaneSQL
, persistTextArray ["b","c"] ] (jsonbVal obj #>. ["a","0"] ?|. ["b","c"])
it "works as expected" $ run $ do "SELECT ((? #> ?) ??| ?)\nFROM \"Json\"\n"
x <- selectJSONwhere (?|. ["b","test"]) [ PersistLiteralEscaped encoded
y <- selectJSONwhere (?|. ["a"]) , persistTextArray ["a","0"]
z <- selectJSONwhere $ \v -> v ->. (-3) ?|. ["a"] , persistTextArray ["b","c"]
w <- selectJSONwhere (?|. []) ]
liftIO $ length x `shouldBe` 3 it "works as expected" $ run $ do
liftIO $ length y `shouldBe` 2 x <- selectJSONwhere (?|. ["b","test"])
liftIO $ length z `shouldBe` 1 y <- selectJSONwhere (?|. ["a"])
liftIO $ length w `shouldBe` 0 z <- selectJSONwhere $ \v -> v ->. (-3) ?|. ["a"]
w <- selectJSONwhere (?|. [])
liftIO $ length x `shouldBe` 3
liftIO $ length y `shouldBe` 2
liftIO $ length z `shouldBe` 1
liftIO $ length w `shouldBe` 0
testQMarkAll :: Spec testQMarkAll :: Spec
testQMarkAll = testQMarkAll = do
describe "Question Mark (All)" $ do describe "Question Mark (All)" $ do
it "creates sane SQL" $ it "creates sane SQL" $ do
createSaneSQL let obj = object ["a" .= False, "b" .= True]
(jsonbVal (object ["a" .= False, "b" .= True]) ?&. ["a","c"]) encoded = BSL.toStrct $ encode obj
"SELECT (? ??& ?)\nFROM \"Json\"\n" createSaneSQL
[ PersistLiteralEscaped "{\"a\":false,\"b\":true}" (jsonbVal obj ?&. ["a","c"])
, persistTextArray ["a","c"] ] "SELECT (? ??& ?)\nFROM \"Json\"\n"
it "creates sane SQL (chained)" $ do [ PersistLiteralEscaped encoded
let obj = object ["a" .= [object ["b" .= True]]] , persistTextArray ["a","c"]
createSaneSQL ]
(jsonbVal obj #>. ["a","0"] ?&. ["b","c"]) it "creates sane SQL (chained)" $ do
"SELECT ((? #> ?) ??& ?)\nFROM \"Json\"\n" let obj = object ["a" .= [object ["b" .= True]]]
[ PersistLiteralEscaped "{\"a\":[{\"b\":true}]}" encoded = BSL.toStrct $ encode obj
, persistTextArray ["a","0"] createSaneSQL
, persistTextArray ["b","c"] ] (jsonbVal obj #>. ["a","0"] ?&. ["b","c"])
it "works as expected" $ run $ do "SELECT ((? #> ?) ??& ?)\nFROM \"Json\"\n"
x <- selectJSONwhere (?&. ["test"]) [ PersistLiteralEscaped encoded
y <- selectJSONwhere (?&. ["a","b"]) , persistTextArray ["a","0"]
z <- selectJSONwhere $ \v -> v ->. "a" ?&. ["b"] , persistTextArray ["b","c"]
w <- selectJSONwhere (?&. []) ]
liftIO $ length x `shouldBe` 2 it "works as expected" $ run $ do
liftIO $ length y `shouldBe` 1 x <- selectJSONwhere (?&. ["test"])
liftIO $ length z `shouldBe` 1 y <- selectJSONwhere (?&. ["a","b"])
liftIO $ length w `shouldBe` 9 z <- selectJSONwhere $ \v -> v ->. "a" ?&. ["b"]
w <- selectJSONwhere (?&. [])
liftIO $ length x `shouldBe` 2
liftIO $ length y `shouldBe` 1
liftIO $ length z `shouldBe` 1
liftIO $ length w `shouldBe` 9
testConcatDeleteOperators :: Spec testConcatDeleteOperators :: Spec
testConcatDeleteOperators = do testConcatDeleteOperators = do
@ -860,41 +885,46 @@ testConcatDeleteOperators = do
testHashMinusOperator testHashMinusOperator
testConcatenationOperator :: Spec testConcatenationOperator :: Spec
testConcatenationOperator = testConcatenationOperator = do
describe "Concatenation" $ do describe "Concatenation" $ do
it "creates sane SQL" $ it "creates sane SQL" $ do
createSaneSQL @JSONValue let objAB = object ["a" .= False, "b" .= True]
(jsonbVal (object ["a" .= False, "b" .= True]) objC = object ["c" .= Null]
JSON.||. jsonbVal (object ["c" .= Null])) createSaneSQL @JSONValue
"SELECT (? || ?)\nFROM \"Json\"\n" (jsonbVal objAB
[ PersistLiteralEscaped "{\"a\":false,\"b\":true}" JSON.||. jsonbVal objC)
, PersistLiteralEscaped "{\"c\":null}" ] "SELECT (? || ?)\nFROM \"Json\"\n"
it "creates sane SQL (chained)" $ do [ PersistLiteralEscaped $ BSL.toStrict $ encode objAB
let obj = object ["a" .= [object ["b" .= True]]] , PersistLiteralEscaped $ BSL.toStrict $ encode objC
createSaneSQL @JSONValue ]
(jsonbVal obj ->. "a" JSON.||. jsonbVal (toJSON [Null])) it "creates sane SQL (chained)" $ do
"SELECT ((? -> ?) || ?)\nFROM \"Json\"\n" let obj = object ["a" .= [object ["b" .= True]]]
[ PersistLiteralEscaped "{\"a\":[{\"b\":true}]}" encoded = BSL.toStrct $ encode obj
, PersistText "a" createSaneSQL @JSONValue
, PersistLiteralEscaped "[null]" ] (jsonbVal obj ->. "a" JSON.||. jsonbVal (toJSON [Null]))
it "works as expected" $ run $ do "SELECT ((? -> ?) || ?)\nFROM \"Json\"\n"
x <- selectJSON $ \v -> do [ PersistLiteralEscaped encoded
where_ $ v @>. jsonbVal (object []) , PersistText "a"
where_ $ v JSON.||. jsonbVal (object ["x" .= True]) , PersistLiteralEscaped "[null]"
@>. jsonbVal (object ["x" .= True]) ]
y <- selectJSONwhere $ \v -> it "works as expected" $ run $ do
v JSON.||. jsonbVal (toJSON [String "a", String "b"]) x <- selectJSON $ \v -> do
->>. 4 ==. just (val "b") where_ $ v @>. jsonbVal (object [])
z <- selectJSONwhere $ \v -> where_ $ v JSON.||. jsonbVal (object ["x" .= True])
v JSON.||. jsonbVal (toJSON [Bool False]) @>. jsonbVal (object ["x" .= True])
->. 0 JSON.@>. jsonbVal (Number 1) y <- selectJSONwhere $ \v ->
w <- selectJSON $ \v -> do v JSON.||. jsonbVal (toJSON [String "a", String "b"])
where_ . not_ $ v @>. jsonbVal (object []) ->>. 4 ==. just (val "b")
where_ $ jsonbVal (String "test1") JSON.||. v ->>. 0 ==. just (val "test1") z <- selectJSONwhere $ \v ->
liftIO $ length x `shouldBe` 2 v JSON.||. jsonbVal (toJSON [Bool False])
liftIO $ length y `shouldBe` 1 ->. 0 JSON.@>. jsonbVal (Number 1)
liftIO $ length z `shouldBe` 2 w <- selectJSON $ \v -> do
liftIO $ length w `shouldBe` 7 where_ . not_ $ v @>. jsonbVal (object [])
where_ $ jsonbVal (String "test1") JSON.||. v ->>. 0 ==. just (val "test1")
liftIO $ length x `shouldBe` 2
liftIO $ length y `shouldBe` 1
liftIO $ length z `shouldBe` 2
liftIO $ length w `shouldBe` 7
testMinusOperator :: Spec testMinusOperator :: Spec
testMinusOperator = testMinusOperator =
@ -942,42 +972,48 @@ testMinusOperator =
where_ $ f v where_ $ f v
testMinusOperatorV10 :: Spec testMinusOperatorV10 :: Spec
testMinusOperatorV10 = testMinusOperatorV10 = do
describe "Minus Operator (PSQL >= v10)" $ do describe "Minus Operator (PSQL >= v10)" $ do
it "creates sane SQL" $ it "creates sane SQL" $ do
createSaneSQL @JSONValue let obj = object ["a" .= False, "b" .= True]
(jsonbVal (object ["a" .= False, "b" .= True]) --. ["a","b"]) encoded = BSL.toStrct $ encode obj
"SELECT (? - ?)\nFROM \"Json\"\n" createSaneSQL @JSONValue
[ PersistLiteralEscaped "{\"a\":false,\"b\":true}" (jsonbVal obj --. ["a","b"])
, persistTextArray ["a","b"] ] "SELECT (? - ?)\nFROM \"Json\"\n"
it "creates sane SQL (chained)" $ do [ PersistLiteralEscaped encoded
let obj = object ["a" .= [object ["b" .= True]]] , persistTextArray ["a","b"]
createSaneSQL @JSONValue ]
(jsonbVal obj #>. ["a","0"] --. ["b"]) it "creates sane SQL (chained)" $ do
"SELECT ((? #> ?) - ?)\nFROM \"Json\"\n" let obj = object ["a" .= [object ["b" .= True]]]
[ PersistLiteralEscaped "{\"a\":[{\"b\":true}]}" encoded = BSL.toStrct $ encode obj
, persistTextArray ["a","0"] createSaneSQL @JSONValue
, persistTextArray ["b"] ] (jsonbVal obj #>. ["a","0"] --. ["b"])
it "works as expected" $ run $ do "SELECT ((? #> ?) - ?)\nFROM \"Json\"\n"
x <- selectJSON $ \v -> do [ PersistLiteralEscaped encoded
where_ $ v @>. jsonbVal (toJSON ([] :: [Int])) , persistTextArray ["a","0"]
where_ $ v --. ["test","a"] @>. jsonbVal (toJSON [String "test"]) , persistTextArray ["b"]
y <- selectJSON $ \v -> do ]
where_ $ v @>. jsonbVal (object []) it "works as expected" $ run $ do
where_ $ v --. ["a","b"] <@. jsonbVal (object []) x <- selectJSON $ \v -> do
z <- selectJSON_ $ \v -> v --. ["b"] <@. jsonbVal (object ["a" .= (1 :: Int)]) where_ $ v @>. jsonbVal (toJSON ([] :: [Int]))
w <- selectJSON_ $ \v -> do where_ $ v --. ["test","a"] @>. jsonbVal (toJSON [String "test"])
v --. ["test"] @>. jsonbVal (toJSON [String "test"]) y <- selectJSON $ \v -> do
liftIO $ length x `shouldBe` 0 where_ $ v @>. jsonbVal (object [])
liftIO $ length y `shouldBe` 2 where_ $ v --. ["a","b"] <@. jsonbVal (object [])
liftIO $ length z `shouldBe` 1 z <- selectJSON_ $ \v -> v --. ["b"] <@. jsonbVal (object ["a" .= (1 :: Int)])
liftIO $ length w `shouldBe` 0 w <- selectJSON_ $ \v -> do
sqlFailWith "22023" $ selectJSONwhere $ \v -> v --. ["test"] @>. jsonbVal (toJSON [String "test"])
v --. ["a"] @>. jsonbVal (toJSON ([] :: [Int])) liftIO $ length x `shouldBe` 0
where selectJSON_ f = selectJSON $ \v -> do liftIO $ length y `shouldBe` 2
where_ $ v @>. jsonbVal (object []) liftIO $ length z `shouldBe` 1
liftIO $ length w `shouldBe` 0
sqlFailWith "22023" $ selectJSONwhere $ \v ->
v --. ["a"] @>. jsonbVal (toJSON ([] :: [Int]))
where
selectJSON_ f = selectJSON $ \v -> do
where_ $ v @>. jsonbVal (object [])
||. v @>. jsonbVal (toJSON ([] :: [Int])) ||. v @>. jsonbVal (toJSON ([] :: [Int]))
where_ $ f v where_ $ f v
testHashMinusOperator :: Spec testHashMinusOperator :: Spec
testHashMinusOperator = testHashMinusOperator =