2022-06-24 09:58:25 +03:00
|
|
|
{-# LANGUAGE OverloadedLists #-}
|
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
|
|
|
|
module Hasura.Backends.DataConnector.API.V0.RelationshipsSpec
|
|
|
|
( spec,
|
|
|
|
genRelationshipName,
|
2022-07-20 08:20:49 +03:00
|
|
|
genTableRelationships,
|
2022-06-24 09:58:25 +03:00
|
|
|
)
|
|
|
|
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)
|
2022-09-06 07:24:46 +03:00
|
|
|
import Hasura.Generator.Common (defaultRange, genArbitraryAlphaNumText)
|
2022-06-24 09:58:25 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import Hedgehog
|
|
|
|
import Hedgehog.Gen qualified as Gen
|
|
|
|
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
|
2022-08-04 11:34:45 +03:00
|
|
|
{ _rTargetTable = TableName ["target_table_name"],
|
2022-06-24 09:58:25 +03:00
|
|
|
_rRelationshipType = ObjectRelationship,
|
|
|
|
_rColumnMapping = [(ColumnName "outer_column", ColumnName "inner_column")]
|
|
|
|
}
|
|
|
|
testToFromJSONToSchema
|
|
|
|
relationship
|
|
|
|
[aesonQQ|
|
2022-08-04 11:34:45 +03:00
|
|
|
{ "target_table": ["target_table_name"],
|
2022-06-24 09:58:25 +03:00
|
|
|
"relationship_type": "object",
|
|
|
|
"column_mapping": {
|
|
|
|
"outer_column": "inner_column"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
jsonOpenApiProperties genRelationship
|
|
|
|
describe "TableRelationships" $ do
|
|
|
|
let relationshipA =
|
|
|
|
Relationship
|
2022-08-04 11:34:45 +03:00
|
|
|
{ _rTargetTable = TableName ["target_table_name_a"],
|
2022-06-24 09:58:25 +03:00
|
|
|
_rRelationshipType = ObjectRelationship,
|
|
|
|
_rColumnMapping = [(ColumnName "outer_column_a", ColumnName "inner_column_a")]
|
|
|
|
}
|
|
|
|
let relationshipB =
|
|
|
|
Relationship
|
2022-08-04 11:34:45 +03:00
|
|
|
{ _rTargetTable = TableName ["target_table_name_b"],
|
2022-06-24 09:58:25 +03:00
|
|
|
_rRelationshipType = ArrayRelationship,
|
|
|
|
_rColumnMapping = [(ColumnName "outer_column_b", ColumnName "inner_column_b")]
|
|
|
|
}
|
|
|
|
let tableRelationships =
|
|
|
|
TableRelationships
|
2022-08-04 11:34:45 +03:00
|
|
|
{ _trSourceTable = TableName ["source_table_name"],
|
2022-06-24 09:58:25 +03:00
|
|
|
_trRelationships =
|
|
|
|
[ (RelationshipName "relationship_a", relationshipA),
|
|
|
|
(RelationshipName "relationship_b", relationshipB)
|
|
|
|
]
|
|
|
|
}
|
|
|
|
testToFromJSONToSchema
|
|
|
|
tableRelationships
|
|
|
|
[aesonQQ|
|
2022-08-04 11:34:45 +03:00
|
|
|
{ "source_table": ["source_table_name"],
|
2022-06-24 09:58:25 +03:00
|
|
|
"relationships": {
|
|
|
|
"relationship_a": {
|
2022-08-04 11:34:45 +03:00
|
|
|
"target_table": ["target_table_name_a"],
|
2022-06-24 09:58:25 +03:00
|
|
|
"relationship_type": "object",
|
|
|
|
"column_mapping": {
|
|
|
|
"outer_column_a": "inner_column_a"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"relationship_b": {
|
2022-08-04 11:34:45 +03:00
|
|
|
"target_table": ["target_table_name_b"],
|
2022-06-24 09:58:25 +03:00
|
|
|
"relationship_type": "array",
|
|
|
|
"column_mapping": {
|
|
|
|
"outer_column_b": "inner_column_b"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
2022-07-20 08:20:49 +03:00
|
|
|
jsonOpenApiProperties genTableRelationships
|
2022-06-24 09:58:25 +03:00
|
|
|
|
|
|
|
genRelationshipName :: MonadGen m => m RelationshipName
|
|
|
|
genRelationshipName =
|
2022-09-06 07:24:46 +03:00
|
|
|
RelationshipName <$> genArbitraryAlphaNumText defaultRange
|
2022-06-24 09:58:25 +03:00
|
|
|
|
|
|
|
genRelationshipType :: MonadGen m => m RelationshipType
|
|
|
|
genRelationshipType = Gen.enumBounded
|
|
|
|
|
|
|
|
genRelationship :: MonadGen m => m Relationship
|
|
|
|
genRelationship =
|
|
|
|
Relationship
|
|
|
|
<$> genTableName
|
|
|
|
<*> genRelationshipType
|
2022-09-06 07:24:46 +03:00
|
|
|
<*> (HashMap.fromList <$> Gen.list defaultRange ((,) <$> genColumnName <*> genColumnName))
|
2022-07-20 08:20:49 +03:00
|
|
|
|
|
|
|
genTableRelationships :: MonadGen m => m TableRelationships
|
|
|
|
genTableRelationships =
|
|
|
|
TableRelationships
|
|
|
|
<$> genTableName
|
2022-09-06 07:24:46 +03:00
|
|
|
<*> fmap HashMap.fromList (Gen.list defaultRange ((,) <$> genRelationshipName <*> genRelationship))
|