Files
graphql-engine/server/src-test/Data/Parser/JSONPathSpec.hs
Toan Nguyen 15c0ebf1ef allow special characters in json path's property name (close #3890) (#3892)
* allow underscore prefix and special characters in json path

* server: Rewrite/refactor JSONPath parser

The JSONPath parser is also rewritten, the previous implementation
was written in a very explicitly “recursive descent” style, but the whole
point of using attoparsec is to be able to backtrack! Taking advantage
of the combinators makes for a much simpler parser.

Co-authored-by: Vamshi Surabhi <0x777@users.noreply.github.com>
Co-authored-by: Alexis King <lexi.lambda@gmail.com>
Co-authored-by: Aleksandra Sikora <ola.zxcvbnm@gmail.com>
Co-authored-by: Shahidh K Muhammed <shahidh@hasura.io>
2020-04-20 14:25:09 +05:30

44 lines
1.5 KiB
Haskell

module Data.Parser.JSONPathSpec (spec) where
import Data.Aeson.Internal (JSONPath, JSONPathElement (..))
import Hasura.Prelude
import Hasura.RQL.Types (encodeJSONPath)
import Data.Parser.JSONPath
import Test.Hspec
import Test.QuickCheck
import qualified Data.Text as T
spec :: Spec
spec = describe "encode and parse JSONPath" $ do
it "JSONPath encoder" $
forM_ generateTestEncodeJSONPath $ \(jsonPath, result) ->
encodeJSONPath jsonPath `shouldBe` result
it "JSONPath parser" $
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
generateTestEncodeJSONPath :: [(JSONPath, String)]
generateTestEncodeJSONPath =
[ ([Key "7seven", Index 0, Key "@!^@*#(!("], "$['7seven'][0]['@!^@*#(!(']")
, ([Key "ABCD"], "$.ABCD")
]
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)
genKey = (Key . T.pack) <$> listOf1 (elements $ alphaNumerics ++ ".,!@#$%^&*_-?:;|/\"")