2021-11-10 14:12:01 +03:00
|
|
|
module Hasura.Generator.Common
|
|
|
|
( genHashMap,
|
2023-02-07 01:01:31 +03:00
|
|
|
genInt,
|
|
|
|
genText,
|
2021-11-10 14:12:01 +03:00
|
|
|
genNonEmptyText,
|
|
|
|
genArbitraryUnicodeText,
|
|
|
|
genArbitraryAlphaNumText,
|
2022-11-17 03:55:59 +03:00
|
|
|
genArbitraryAlphaNumTextExcluding,
|
2021-11-10 14:12:01 +03:00
|
|
|
genFieldName,
|
|
|
|
genGName,
|
|
|
|
genDescription,
|
2022-04-06 01:08:43 +03:00
|
|
|
defaultRange,
|
2023-02-07 01:01:31 +03:00
|
|
|
jsonRoundTrip,
|
2021-11-10 14:12:01 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
2023-02-07 01:01:31 +03:00
|
|
|
import Data.Aeson (FromJSON, ToJSON)
|
2023-04-26 20:28:48 +03:00
|
|
|
import Data.Aeson qualified as J
|
2023-04-26 18:42:13 +03:00
|
|
|
import Data.HashMap.Strict qualified as HashMap
|
2021-11-10 14:12:01 +03:00
|
|
|
import Data.Text.NonEmpty (NonEmptyText, mkNonEmptyText)
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hasura.RQL.Types.Common (FieldName (FieldName))
|
|
|
|
import Hedgehog
|
2022-04-06 01:08:43 +03:00
|
|
|
import Hedgehog.Gen qualified as Gen
|
|
|
|
import Hedgehog.Range qualified as Range
|
2021-11-10 14:12:01 +03:00
|
|
|
import Language.GraphQL.Draft.Syntax qualified as G
|
2023-02-07 01:01:31 +03:00
|
|
|
import Test.Hspec
|
|
|
|
import Test.Hspec.Hedgehog
|
2021-11-10 14:12:01 +03:00
|
|
|
|
|
|
|
genHashMap ::
|
2023-05-24 16:51:56 +03:00
|
|
|
(MonadGen m) =>
|
|
|
|
(Hashable a) =>
|
2021-11-10 14:12:01 +03:00
|
|
|
m a ->
|
|
|
|
m b ->
|
|
|
|
Range Int ->
|
|
|
|
m (HashMap a b)
|
2023-04-26 18:42:13 +03:00
|
|
|
genHashMap genA genB range = fmap HashMap.fromList . Gen.list range $ (,) <$> genA <*> genB
|
2021-11-10 14:12:01 +03:00
|
|
|
|
2023-02-07 01:01:31 +03:00
|
|
|
genInt :: Gen Int
|
|
|
|
genInt = fromIntegral <$> Gen.int32 (Range.linear 1 99999)
|
|
|
|
|
|
|
|
genText :: Gen Text
|
|
|
|
genText = Gen.text (Range.linear 0 11) Gen.unicode
|
|
|
|
|
2023-05-24 16:51:56 +03:00
|
|
|
genNonEmptyText :: (MonadGen m) => Range Int -> m NonEmptyText
|
2022-04-06 01:08:43 +03:00
|
|
|
genNonEmptyText range = mkNonEmptyText `Gen.mapMaybeT` genArbitraryUnicodeText range
|
2021-11-10 14:12:01 +03:00
|
|
|
|
2023-05-24 16:51:56 +03:00
|
|
|
genArbitraryUnicodeText :: (MonadGen m) => Range Int -> m Text
|
2022-04-06 01:08:43 +03:00
|
|
|
genArbitraryUnicodeText range = Gen.text range Gen.unicode
|
2021-11-10 14:12:01 +03:00
|
|
|
|
2023-05-24 16:51:56 +03:00
|
|
|
genArbitraryAlphaNumText :: (MonadGen m) => Range Int -> m Text
|
2022-04-06 01:08:43 +03:00
|
|
|
genArbitraryAlphaNumText range = Gen.text range Gen.alphaNum
|
2021-11-10 14:12:01 +03:00
|
|
|
|
2022-11-17 03:55:59 +03:00
|
|
|
genArbitraryAlphaNumTextExcluding :: (MonadGen m, GenBase m ~ Identity) => [Text] -> Range Int -> m Text
|
|
|
|
genArbitraryAlphaNumTextExcluding excluded = Gen.filter (`notElem` excluded) . genArbitraryAlphaNumText
|
|
|
|
|
2023-05-24 16:51:56 +03:00
|
|
|
genFieldName :: (MonadGen m) => Range Int -> m FieldName
|
2021-11-10 14:12:01 +03:00
|
|
|
genFieldName range = FieldName <$> genArbitraryUnicodeText range
|
|
|
|
|
2023-05-24 16:51:56 +03:00
|
|
|
genGName :: (MonadGen m) => Range Int -> m G.Name
|
2022-04-06 01:08:43 +03:00
|
|
|
genGName range = G.mkName `Gen.mapMaybeT` genArbitraryAlphaNumText range
|
2021-11-10 14:12:01 +03:00
|
|
|
|
2023-05-24 16:51:56 +03:00
|
|
|
genDescription :: (MonadGen m) => Range Int -> m G.Description
|
2021-11-10 14:12:01 +03:00
|
|
|
genDescription range = G.Description <$> genArbitraryUnicodeText range
|
2022-04-06 01:08:43 +03:00
|
|
|
|
|
|
|
-- | A reasonable range size to generate data on dev machines without
|
|
|
|
-- blowing up.
|
2023-05-24 16:51:56 +03:00
|
|
|
defaultRange :: (Integral a) => Range a
|
2022-04-06 01:08:43 +03:00
|
|
|
defaultRange = Range.linear 0 8
|
2023-02-07 01:01:31 +03:00
|
|
|
|
|
|
|
-- | Given 'Gen' @a@, assert that @a@'s Aeson instances are isomorphic.
|
|
|
|
jsonRoundTrip :: forall a. (FromJSON a, ToJSON a, Eq a, Show a) => Gen a -> String -> Spec
|
|
|
|
jsonRoundTrip gen ty = do
|
|
|
|
it ty $ hedgehog $ do
|
|
|
|
term <- forAll gen
|
2023-04-26 20:28:48 +03:00
|
|
|
tripping term J.toJSON J.fromJSON
|