2020-04-16 09:45:21 +03:00
|
|
|
module Data.Parser.JSONPathSpec (spec) where
|
|
|
|
|
|
|
|
import Hasura.Prelude
|
2021-05-11 18:18:31 +03:00
|
|
|
|
|
|
|
import qualified Data.Text as T
|
2020-04-16 09:45:21 +03:00
|
|
|
|
|
|
|
import Data.Parser.JSONPath
|
|
|
|
import Test.Hspec
|
|
|
|
import Test.QuickCheck
|
|
|
|
|
2021-05-11 18:18:31 +03:00
|
|
|
import Hasura.Base.Error (encodeJSONPath)
|
|
|
|
|
2020-04-16 09:45:21 +03:00
|
|
|
|
|
|
|
spec :: Spec
|
2020-04-20 11:55:09 +03:00
|
|
|
spec = describe "encode and parse JSONPath" $ do
|
|
|
|
it "JSONPath encoder" $
|
|
|
|
forM_ generateTestEncodeJSONPath $ \(jsonPath, result) ->
|
|
|
|
encodeJSONPath jsonPath `shouldBe` result
|
|
|
|
|
2020-04-21 11:06:11 +03:00
|
|
|
describe "JSONPath parser" $ do
|
|
|
|
|
|
|
|
it "Single $" $
|
|
|
|
parseJSONPath "$" `shouldBe` (Right [] :: Either String JSONPath)
|
|
|
|
|
|
|
|
it "Random json paths" $
|
|
|
|
withMaxSuccess 1000 $
|
|
|
|
forAll (resize 20 generateJSONPath) $ \jsonPath ->
|
|
|
|
let encPath = encodeJSONPath jsonPath
|
|
|
|
parsedJSONPathE = parseJSONPath $ T.pack encPath
|
|
|
|
in case parsedJSONPathE of
|
|
|
|
Left err -> counterexample (err <> ": " <> encPath) False
|
|
|
|
Right parsedJSONPath -> property $ parsedJSONPath == jsonPath
|
|
|
|
|
2020-04-16 09:45:21 +03:00
|
|
|
|
2020-04-20 11:55:09 +03:00
|
|
|
|
|
|
|
generateTestEncodeJSONPath :: [(JSONPath, String)]
|
|
|
|
generateTestEncodeJSONPath =
|
|
|
|
[ ([Key "7seven", Index 0, Key "@!^@*#(!("], "$['7seven'][0]['@!^@*#(!(']")
|
|
|
|
, ([Key "ABCD"], "$.ABCD")
|
|
|
|
]
|
|
|
|
|
2020-04-16 09:45:21 +03:00
|
|
|
generateJSONPath :: Gen JSONPath
|
|
|
|
generateJSONPath = map (either id id) <$> listOf1 genPathElementEither
|
|
|
|
where
|
|
|
|
genPathElementEither = do
|
|
|
|
indexLeft <- Left <$> genIndex
|
|
|
|
keyRight <- Right <$> genKey
|
|
|
|
elements [indexLeft, keyRight]
|
|
|
|
genIndex = Index <$> choose (0, 100)
|
2020-10-28 19:40:33 +03:00
|
|
|
genKey = Key . T.pack <$> listOf1 (elements $ alphaNumerics ++ ".,!@#$%^&*_-?:;|/\"")
|