graphql-engine/server/src-test/Hasura/Backends/DataConnector/API/V0/RelationshipsSpec.hs
Tom Harding e0c0043e76 Upgrade Ormolu to 0.7.0.0
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/9284
GitOrigin-RevId: 2f2cf2ad01900a54e4bdb970205ac0ef313c7e00
2023-05-24 13:53:53 +00:00

131 lines
4.6 KiB
Haskell

{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE QuasiQuotes #-}
module Hasura.Backends.DataConnector.API.V0.RelationshipsSpec
( spec,
genRelationshipName,
genRelationships,
genTableRelationships,
)
where
import Data.Aeson.QQ.Simple (aesonQQ)
import Data.HashMap.Strict qualified as HashMap
import Hasura.Backends.DataConnector.API.V0
import Hasura.Backends.DataConnector.API.V0.ColumnSpec (genColumnName)
import Hasura.Backends.DataConnector.API.V0.TableSpec (genTableName)
import Hasura.Generator.Common (defaultRange, genArbitraryAlphaNumText)
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 "RelationshipName" $ do
testToFromJSONToSchema (RelationshipName "relationship_name") [aesonQQ|"relationship_name"|]
jsonOpenApiProperties genRelationshipName
describe "RelationshipType" $ do
describe "ObjectRelationship"
$ testToFromJSONToSchema ObjectRelationship [aesonQQ|"object"|]
describe "ArrayRelationship"
$ testToFromJSONToSchema ArrayRelationship [aesonQQ|"array"|]
jsonOpenApiProperties genRelationshipType
describe "Relationship" $ do
let relationship =
Relationship
{ _rTargetTable = TableName ["target_table_name"],
_rRelationshipType = ObjectRelationship,
_rColumnMapping = [(ColumnName "outer_column", ColumnName "inner_column")]
}
testToFromJSONToSchema
relationship
[aesonQQ|
{ "target_table": ["target_table_name"],
"relationship_type": "object",
"column_mapping": {
"outer_column": "inner_column"
}
}
|]
jsonOpenApiProperties genRelationship
describe "TableRelationships" $ do
let relationshipA =
Relationship
{ _rTargetTable = TableName ["target_table_name_a"],
_rRelationshipType = ObjectRelationship,
_rColumnMapping = [(ColumnName "outer_column_a", ColumnName "inner_column_a")]
}
let relationshipB =
Relationship
{ _rTargetTable = TableName ["target_table_name_b"],
_rRelationshipType = ArrayRelationship,
_rColumnMapping = [(ColumnName "outer_column_b", ColumnName "inner_column_b")]
}
let tableRelationships =
TableRelationships
{ _trelSourceTable = TableName ["source_table_name"],
_trelRelationships =
[ (RelationshipName "relationship_a", relationshipA),
(RelationshipName "relationship_b", relationshipB)
]
}
testToFromJSONToSchema
tableRelationships
[aesonQQ|
{ "source_table": ["source_table_name"],
"type": "table",
"relationships": {
"relationship_a": {
"target_table": ["target_table_name_a"],
"relationship_type": "object",
"column_mapping": {
"outer_column_a": "inner_column_a"
}
},
"relationship_b": {
"target_table": ["target_table_name_b"],
"relationship_type": "array",
"column_mapping": {
"outer_column_b": "inner_column_b"
}
}
}
}
|]
jsonOpenApiProperties genTableRelationships
genRelationshipName :: (MonadGen m) => m RelationshipName
genRelationshipName =
RelationshipName <$> genArbitraryAlphaNumText defaultRange
genRelationshipType :: (MonadGen m) => m RelationshipType
genRelationshipType = Gen.enumBounded
genRelationship :: (MonadGen m) => m Relationship
genRelationship =
Relationship
<$> genTableName
<*> genRelationshipType
<*> (HashMap.fromList <$> Gen.list defaultRange ((,) <$> genColumnName <*> genColumnName))
genRelationships :: Gen Relationships
genRelationships = (RTable <$> genTableRelationships) <|> (RFunction <$> genFunctionRelationships)
genTableRelationships :: (MonadGen m) => m TableRelationships
genTableRelationships =
TableRelationships
<$> genTableName
<*> fmap HashMap.fromList (Gen.list defaultRange ((,) <$> genRelationshipName <*> genRelationship))
genFunctionRelationships :: (MonadGen m) => m FunctionRelationships
genFunctionRelationships =
FunctionRelationships
<$> genFunctionName
<*> fmap HashMap.fromList (Gen.list defaultRange ((,) <$> genRelationshipName <*> genRelationship))
genFunctionName :: (MonadGen m) => m FunctionName
genFunctionName = FunctionName <$> Gen.nonEmpty (linear 1 3) (genArbitraryAlphaNumText defaultRange)