2022-03-31 07:45:03 +03:00
|
|
|
{-# LANGUAGE OverloadedLists #-}
|
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
|
2022-05-02 08:03:12 +03:00
|
|
|
module Hasura.Backends.DataConnector.API.V0.TableSpec (spec, genTableName, genTableInfo) where
|
2022-03-31 07:45:03 +03:00
|
|
|
|
|
|
|
import Data.Aeson.QQ.Simple (aesonQQ)
|
2022-08-29 03:20:00 +03:00
|
|
|
import Data.HashMap.Strict qualified as HashMap
|
2022-06-24 09:58:25 +03:00
|
|
|
import Hasura.Backends.DataConnector.API.V0
|
2022-07-01 15:20:07 +03:00
|
|
|
import Hasura.Backends.DataConnector.API.V0.ColumnSpec (genColumnInfo, genColumnName)
|
2022-08-29 03:20:00 +03:00
|
|
|
import Hasura.Generator.Common
|
2022-03-31 07:45:03 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import Hedgehog
|
|
|
|
import Hedgehog.Gen qualified as Gen
|
|
|
|
import Hedgehog.Range
|
|
|
|
import Test.Aeson.Utils
|
|
|
|
import Test.Hspec
|
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec = do
|
|
|
|
describe "TableName" $ do
|
2022-08-04 11:34:45 +03:00
|
|
|
testToFromJSONToSchema (TableName ["my_table_name"]) [aesonQQ|["my_table_name"]|]
|
2022-03-31 07:45:03 +03:00
|
|
|
jsonOpenApiProperties genTableName
|
|
|
|
describe "TableInfo" $ do
|
|
|
|
describe "minimal" $
|
|
|
|
testToFromJSONToSchema
|
2022-08-24 00:46:10 +03:00
|
|
|
(TableInfo (TableName ["my_table_name"]) [] Nothing Nothing Nothing)
|
2022-03-31 07:45:03 +03:00
|
|
|
[aesonQQ|
|
2022-08-04 11:34:45 +03:00
|
|
|
{ "name": ["my_table_name"],
|
2022-03-31 07:45:03 +03:00
|
|
|
"columns": []
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
describe "non-minimal" $
|
|
|
|
testToFromJSONToSchema
|
|
|
|
( TableInfo
|
2022-08-04 11:34:45 +03:00
|
|
|
(TableName ["my_table_name"])
|
2022-03-31 07:45:03 +03:00
|
|
|
[ColumnInfo (ColumnName "id") StringTy False Nothing]
|
2022-07-01 15:20:07 +03:00
|
|
|
(Just [ColumnName "id"])
|
2022-08-24 00:46:10 +03:00
|
|
|
Nothing
|
2022-09-21 08:11:53 +03:00
|
|
|
(Just "my description")
|
2022-03-31 07:45:03 +03:00
|
|
|
)
|
|
|
|
[aesonQQ|
|
2022-08-04 11:34:45 +03:00
|
|
|
{ "name": ["my_table_name"],
|
2022-03-31 07:45:03 +03:00
|
|
|
"columns": [{"name": "id", "type": "string", "nullable": false}],
|
2022-07-01 15:20:07 +03:00
|
|
|
"primary_key": ["id"],
|
2022-03-31 07:45:03 +03:00
|
|
|
"description": "my description"
|
|
|
|
}
|
|
|
|
|]
|
2022-08-29 03:20:00 +03:00
|
|
|
describe "foreign-key" $
|
|
|
|
testToFromJSONToSchema
|
|
|
|
( TableInfo
|
|
|
|
(TableName ["my_table_name"])
|
|
|
|
[ColumnInfo (ColumnName "id") StringTy False Nothing]
|
|
|
|
(Just [ColumnName "id"])
|
2022-10-06 02:23:49 +03:00
|
|
|
(Just $ ForeignKeys $ HashMap.singleton (ConstraintName "Artist") (Constraint (TableName ["artist_table"]) (HashMap.singleton (ColumnName "ArtistId") (ColumnName "ArtistId"))))
|
2022-09-21 08:11:53 +03:00
|
|
|
(Just "my description")
|
2022-08-29 03:20:00 +03:00
|
|
|
)
|
|
|
|
[aesonQQ|
|
|
|
|
{ "name": ["my_table_name"],
|
|
|
|
"columns": [{"name": "id", "type": "string", "nullable": false}],
|
|
|
|
"primary_key": ["id"],
|
|
|
|
"description": "my description",
|
|
|
|
"foreign_keys": {
|
|
|
|
"Artist": {
|
2022-09-01 03:50:54 +03:00
|
|
|
"foreign_table": ["artist_table"],
|
2022-08-29 03:20:00 +03:00
|
|
|
"column_mapping": {
|
|
|
|
"ArtistId": "ArtistId"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
2022-03-31 07:45:03 +03:00
|
|
|
jsonOpenApiProperties genTableInfo
|
|
|
|
|
|
|
|
genTableName :: MonadGen m => m TableName
|
2022-09-06 07:24:46 +03:00
|
|
|
genTableName = TableName <$> Gen.nonEmpty (linear 1 3) (genArbitraryAlphaNumText defaultRange)
|
2022-03-31 07:45:03 +03:00
|
|
|
|
2022-08-29 03:20:00 +03:00
|
|
|
genForeignKeys :: MonadGen m => m ForeignKeys
|
|
|
|
genForeignKeys = ForeignKeys <$> genHashMap genConstraintName genConstraint defaultRange
|
|
|
|
|
|
|
|
genConstraintName :: MonadGen m => m ConstraintName
|
2022-09-06 07:24:46 +03:00
|
|
|
genConstraintName = ConstraintName <$> genArbitraryAlphaNumText defaultRange
|
2022-08-29 03:20:00 +03:00
|
|
|
|
|
|
|
genConstraint :: MonadGen m => m Constraint
|
|
|
|
genConstraint =
|
2022-10-06 02:23:49 +03:00
|
|
|
let mapping = genHashMap genColumnName genColumnName defaultRange
|
2022-09-01 03:50:54 +03:00
|
|
|
in Constraint <$> genTableName <*> mapping
|
2022-08-29 03:20:00 +03:00
|
|
|
|
|
|
|
-- | Note: this generator is intended for serialization tests only and does not ensure valid Foreign Key Constraints.
|
2022-03-31 07:45:03 +03:00
|
|
|
genTableInfo :: MonadGen m => m TableInfo
|
|
|
|
genTableInfo =
|
|
|
|
TableInfo
|
|
|
|
<$> genTableName
|
2022-09-06 07:24:46 +03:00
|
|
|
<*> Gen.list defaultRange genColumnInfo
|
|
|
|
<*> Gen.maybe (Gen.list defaultRange genColumnName)
|
2022-08-29 03:20:00 +03:00
|
|
|
<*> Gen.maybe genForeignKeys
|
2022-09-21 08:11:53 +03:00
|
|
|
<*> Gen.maybe (genArbitraryAlphaNumText defaultRange)
|