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

@ -727,20 +727,25 @@ testFilterOperators =
testInclusion :: Spec testInclusion :: Spec
testInclusion = do testInclusion = do
describe "@>" $ do describe "@>" $ do
it "creates sane SQL" $ it "creates sane SQL" $ do
let obj = object ["a" .= False, "b" .= True]
encoded = BSL.toStrct $ encode obj
createSaneSQL createSaneSQL
(jsonbVal (object ["a" .= False, "b" .= True]) @>. jsonbVal (object ["a" .= False])) (jsonbVal obj @>. jsonbVal (object ["a" .= False]))
"SELECT (? @> ?)\nFROM \"Json\"\n" "SELECT (? @> ?)\nFROM \"Json\"\n"
[ PersistLiteralEscaped "{\"a\":false,\"b\":true}" [ PersistLiteralEscaped encoded
, PersistLiteralEscaped "{\"a\":false}" ] , PersistLiteralEscaped "{\"a\":false}"
]
it "creates sane SQL (chained)" $ do it "creates sane SQL (chained)" $ do
let obj = object ["a" .= [object ["b" .= True]]] let obj = object ["a" .= [object ["b" .= True]]]
encoded = BSL.toStrct $ encode obj
createSaneSQL createSaneSQL
(jsonbVal obj ->. "a" @>. jsonbVal (object ["b" .= True])) (jsonbVal obj ->. "a" @>. jsonbVal (object ["b" .= True]))
"SELECT ((? -> ?) @> ?)\nFROM \"Json\"\n" "SELECT ((? -> ?) @> ?)\nFROM \"Json\"\n"
[ PersistLiteralEscaped "{\"a\":[{\"b\":true}]}" [ PersistLiteralEscaped encoded
, PersistText "a" , PersistText "a"
, PersistLiteralEscaped "{\"b\":true}" ] , PersistLiteralEscaped "{\"b\":true}"
]
it "works as expected" $ run $ do it "works as expected" $ run $ do
x <- selectJSONwhere $ \v -> v @>. jsonbVal (Number 1) x <- selectJSONwhere $ \v -> v @>. jsonbVal (Number 1)
y <- selectJSONwhere $ \v -> v @>. jsonbVal (toJSON [object ["a" .= Number 3.14]]) y <- selectJSONwhere $ \v -> v @>. jsonbVal (toJSON [object ["a" .= Number 3.14]])
@ -750,19 +755,25 @@ testInclusion = do
liftIO $ length z `shouldBe` 1 liftIO $ length z `shouldBe` 1
describe "<@" $ do describe "<@" $ do
it "creates sane SQL" $ it "creates sane SQL" $
let obj = object ["a" .= False, "b" .= True]
encoded = BSL.toStrct $ encode obj
createSaneSQL createSaneSQL
(jsonbVal (object ["a" .= False]) <@. jsonbVal (object ["a" .= False, "b" .= True])) (jsonbVal (object ["a" .= False]) <@. jsonbVal obj )
"SELECT (? <@ ?)\nFROM \"Json\"\n" "SELECT (? <@ ?)\nFROM \"Json\"\n"
[ PersistLiteralEscaped "{\"a\":false}" [ PersistLiteralEscaped "{\"a\":false}"
, PersistLiteralEscaped "{\"a\":false,\"b\":true}" ] , PersistLiteralEscaped encoded
]
it "creates sane SQL (chained)" $ do it "creates sane SQL (chained)" $ do
let obj = object ["a" .= [object ["b" .= True]]] let obj = object ["a" .= [object ["b" .= True]]]
obj' = object ["b" .= True, "c" .= Null]
encoded = encode obj'
createSaneSQL createSaneSQL
(jsonbVal obj ->. "a" <@. jsonbVal (object ["b" .= True, "c" .= Null])) (jsonbVal obj ->. "a" <@. jsonbVal )
"SELECT ((? -> ?) <@ ?)\nFROM \"Json\"\n" "SELECT ((? -> ?) <@ ?)\nFROM \"Json\"\n"
[ PersistLiteralEscaped "{\"a\":[{\"b\":true}]}" [ PersistLiteralEscaped "{\"a\":[{\"b\":true}]}"
, PersistText "a" , PersistText "a"
, PersistLiteralEscaped "{\"b\":true,\"c\":null}" ] , PersistLiteralEscaped encoded'
]
it "works as expected" $ run $ do it "works as expected" $ run $ do
x <- selectJSONwhere $ \v -> v <@. jsonbVal (toJSON [Number 1]) x <- selectJSONwhere $ \v -> v <@. jsonbVal (toJSON [Number 1])
y <- selectJSONwhere $ \v -> v <@. jsonbVal (object ["a" .= (1 :: Int), "b" .= False, "c" .= Null]) y <- selectJSONwhere $ \v -> v <@. jsonbVal (object ["a" .= (1 :: Int), "b" .= False, "c" .= Null])
@ -772,22 +783,27 @@ testInclusion = do
liftIO $ length z `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
let obj = object ["a" .= False, "b" .= True]
encoded = BSL.toStrict $ encode obj
createSaneSQL createSaneSQL
(jsonbVal (object ["a" .= False, "b" .= True]) JSON.?. "a") (jsonbVal obj JSON.?. "a")
"SELECT (? ?? ?)\nFROM \"Json\"\n" "SELECT (? ?? ?)\nFROM \"Json\"\n"
[ PersistLiteralEscaped "{\"a\":false,\"b\":true}" [ PersistLiteralEscaped encoded
, PersistText "a" ] , PersistText "a"
]
it "creates sane SQL (chained)" $ do it "creates sane SQL (chained)" $ do
let obj = object ["a" .= [object ["b" .= True]]] let obj = object ["a" .= [object ["b" .= True]]]
encoded = BSL.toStrict $ encode obj
createSaneSQL createSaneSQL
(jsonbVal obj #>. ["a","0"] JSON.?. "b") (jsonbVal obj #>. ["a","0"] JSON.?. "b")
"SELECT ((? #> ?) ?? ?)\nFROM \"Json\"\n" "SELECT ((? #> ?) ?? ?)\nFROM \"Json\"\n"
[ PersistLiteralEscaped "{\"a\":[{\"b\":true}]}" [ PersistLiteralEscaped encoded
, persistTextArray ["a","0"] , persistTextArray ["a","0"]
, PersistText "b" ] , PersistText "b"
]
it "works as expected" $ run $ do it "works as expected" $ run $ do
x <- selectJSONwhere (JSON.?. "a") x <- selectJSONwhere (JSON.?. "a")
y <- selectJSONwhere (JSON.?. "test") y <- selectJSONwhere (JSON.?. "test")
@ -797,22 +813,27 @@ testQMark =
liftIO $ length z `shouldBe` 1 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
let obj = (object ["a" .= False, "b" .= True])
encoded = BSL.toStrict $ encode obj
createSaneSQL createSaneSQL
(jsonbVal (object ["a" .= False, "b" .= True]) ?|. ["a","c"]) (jsonbVal obj ?|. ["a","c"])
"SELECT (? ??| ?)\nFROM \"Json\"\n" "SELECT (? ??| ?)\nFROM \"Json\"\n"
[ PersistLiteralEscaped "{\"a\":false,\"b\":true}" [ PersistLiteralEscaped encoded
, persistTextArray ["a","c"] ] , persistTextArray ["a","c"]
]
it "creates sane SQL (chained)" $ do it "creates sane SQL (chained)" $ do
let obj = object ["a" .= [object ["b" .= True]]] let obj = object ["a" .= [object ["b" .= True]]]
encoded = BSL.toStrct $ encode obj
createSaneSQL createSaneSQL
(jsonbVal obj #>. ["a","0"] ?|. ["b","c"]) (jsonbVal obj #>. ["a","0"] ?|. ["b","c"])
"SELECT ((? #> ?) ??| ?)\nFROM \"Json\"\n" "SELECT ((? #> ?) ??| ?)\nFROM \"Json\"\n"
[ PersistLiteralEscaped "{\"a\":[{\"b\":true}]}" [ PersistLiteralEscaped encoded
, persistTextArray ["a","0"] , persistTextArray ["a","0"]
, persistTextArray ["b","c"] ] , persistTextArray ["b","c"]
]
it "works as expected" $ run $ do it "works as expected" $ run $ do
x <- selectJSONwhere (?|. ["b","test"]) x <- selectJSONwhere (?|. ["b","test"])
y <- selectJSONwhere (?|. ["a"]) y <- selectJSONwhere (?|. ["a"])
@ -824,22 +845,27 @@ testQMarkAny =
liftIO $ length w `shouldBe` 0 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
let obj = object ["a" .= False, "b" .= True]
encoded = BSL.toStrct $ encode obj
createSaneSQL createSaneSQL
(jsonbVal (object ["a" .= False, "b" .= True]) ?&. ["a","c"]) (jsonbVal obj ?&. ["a","c"])
"SELECT (? ??& ?)\nFROM \"Json\"\n" "SELECT (? ??& ?)\nFROM \"Json\"\n"
[ PersistLiteralEscaped "{\"a\":false,\"b\":true}" [ PersistLiteralEscaped encoded
, persistTextArray ["a","c"] ] , persistTextArray ["a","c"]
]
it "creates sane SQL (chained)" $ do it "creates sane SQL (chained)" $ do
let obj = object ["a" .= [object ["b" .= True]]] let obj = object ["a" .= [object ["b" .= True]]]
encoded = BSL.toStrct $ encode obj
createSaneSQL createSaneSQL
(jsonbVal obj #>. ["a","0"] ?&. ["b","c"]) (jsonbVal obj #>. ["a","0"] ?&. ["b","c"])
"SELECT ((? #> ?) ??& ?)\nFROM \"Json\"\n" "SELECT ((? #> ?) ??& ?)\nFROM \"Json\"\n"
[ PersistLiteralEscaped "{\"a\":[{\"b\":true}]}" [ PersistLiteralEscaped encoded
, persistTextArray ["a","0"] , persistTextArray ["a","0"]
, persistTextArray ["b","c"] ] , persistTextArray ["b","c"]
]
it "works as expected" $ run $ do it "works as expected" $ run $ do
x <- selectJSONwhere (?&. ["test"]) x <- selectJSONwhere (?&. ["test"])
y <- selectJSONwhere (?&. ["a","b"]) y <- selectJSONwhere (?&. ["a","b"])
@ -850,7 +876,6 @@ testQMarkAll =
liftIO $ length z `shouldBe` 1 liftIO $ length z `shouldBe` 1
liftIO $ length w `shouldBe` 9 liftIO $ length w `shouldBe` 9
testConcatDeleteOperators :: Spec testConcatDeleteOperators :: Spec
testConcatDeleteOperators = do testConcatDeleteOperators = do
describe "Concatenation Operator" testConcatenationOperator describe "Concatenation Operator" testConcatenationOperator
@ -860,23 +885,28 @@ 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
let objAB = object ["a" .= False, "b" .= True]
objC = object ["c" .= Null]
createSaneSQL @JSONValue createSaneSQL @JSONValue
(jsonbVal (object ["a" .= False, "b" .= True]) (jsonbVal objAB
JSON.||. jsonbVal (object ["c" .= Null])) JSON.||. jsonbVal objC)
"SELECT (? || ?)\nFROM \"Json\"\n" "SELECT (? || ?)\nFROM \"Json\"\n"
[ PersistLiteralEscaped "{\"a\":false,\"b\":true}" [ PersistLiteralEscaped $ BSL.toStrict $ encode objAB
, PersistLiteralEscaped "{\"c\":null}" ] , PersistLiteralEscaped $ BSL.toStrict $ encode objC
]
it "creates sane SQL (chained)" $ do it "creates sane SQL (chained)" $ do
let obj = object ["a" .= [object ["b" .= True]]] let obj = object ["a" .= [object ["b" .= True]]]
encoded = BSL.toStrct $ encode obj
createSaneSQL @JSONValue createSaneSQL @JSONValue
(jsonbVal obj ->. "a" JSON.||. jsonbVal (toJSON [Null])) (jsonbVal obj ->. "a" JSON.||. jsonbVal (toJSON [Null]))
"SELECT ((? -> ?) || ?)\nFROM \"Json\"\n" "SELECT ((? -> ?) || ?)\nFROM \"Json\"\n"
[ PersistLiteralEscaped "{\"a\":[{\"b\":true}]}" [ PersistLiteralEscaped encoded
, PersistText "a" , PersistText "a"
, PersistLiteralEscaped "[null]" ] , PersistLiteralEscaped "[null]"
]
it "works as expected" $ run $ do it "works as expected" $ run $ do
x <- selectJSON $ \v -> do x <- selectJSON $ \v -> do
where_ $ v @>. jsonbVal (object []) where_ $ v @>. jsonbVal (object [])
@ -942,22 +972,27 @@ 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
let obj = object ["a" .= False, "b" .= True]
encoded = BSL.toStrct $ encode obj
createSaneSQL @JSONValue createSaneSQL @JSONValue
(jsonbVal (object ["a" .= False, "b" .= True]) --. ["a","b"]) (jsonbVal obj --. ["a","b"])
"SELECT (? - ?)\nFROM \"Json\"\n" "SELECT (? - ?)\nFROM \"Json\"\n"
[ PersistLiteralEscaped "{\"a\":false,\"b\":true}" [ PersistLiteralEscaped encoded
, persistTextArray ["a","b"] ] , persistTextArray ["a","b"]
]
it "creates sane SQL (chained)" $ do it "creates sane SQL (chained)" $ do
let obj = object ["a" .= [object ["b" .= True]]] let obj = object ["a" .= [object ["b" .= True]]]
encoded = BSL.toStrct $ encode obj
createSaneSQL @JSONValue createSaneSQL @JSONValue
(jsonbVal obj #>. ["a","0"] --. ["b"]) (jsonbVal obj #>. ["a","0"] --. ["b"])
"SELECT ((? #> ?) - ?)\nFROM \"Json\"\n" "SELECT ((? #> ?) - ?)\nFROM \"Json\"\n"
[ PersistLiteralEscaped "{\"a\":[{\"b\":true}]}" [ PersistLiteralEscaped encoded
, persistTextArray ["a","0"] , persistTextArray ["a","0"]
, persistTextArray ["b"] ] , persistTextArray ["b"]
]
it "works as expected" $ run $ do it "works as expected" $ run $ do
x <- selectJSON $ \v -> do x <- selectJSON $ \v -> do
where_ $ v @>. jsonbVal (toJSON ([] :: [Int])) where_ $ v @>. jsonbVal (toJSON ([] :: [Int]))
@ -974,7 +1009,8 @@ testMinusOperatorV10 =
liftIO $ length w `shouldBe` 0 liftIO $ length w `shouldBe` 0
sqlFailWith "22023" $ selectJSONwhere $ \v -> sqlFailWith "22023" $ selectJSONwhere $ \v ->
v --. ["a"] @>. jsonbVal (toJSON ([] :: [Int])) v --. ["a"] @>. jsonbVal (toJSON ([] :: [Int]))
where selectJSON_ f = selectJSON $ \v -> do where
selectJSON_ f = selectJSON $ \v -> do
where_ $ v @>. jsonbVal (object []) where_ $ v @>. jsonbVal (object [])
||. v @>. jsonbVal (toJSON ([] :: [Int])) ||. v @>. jsonbVal (toJSON ([] :: [Int]))
where_ $ f v where_ $ f v