2022-04-10 07:47:15 +03:00
|
|
|
module Test.SchemaSpec (spec) where
|
|
|
|
|
2022-08-29 03:20:00 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
import Data.HashMap.Strict qualified as HashMap
|
|
|
|
import Data.List (sort, sortOn)
|
|
|
|
import Hasura.Backends.DataConnector.API (Config, Constraint (..), ForeignKeys (..), Routes (..), SchemaResponse (..), SourceName, TableInfo (..))
|
2022-07-19 03:37:04 +03:00
|
|
|
import Hasura.Backends.DataConnector.API.V0.Column (ColumnInfo (..))
|
2022-04-10 07:47:15 +03:00
|
|
|
import Servant.API (NamedRoutes)
|
|
|
|
import Servant.Client (Client, (//))
|
|
|
|
import Test.Data qualified as Data
|
2022-08-04 04:00:48 +03:00
|
|
|
import Test.Expectations (jsonShouldBe)
|
2022-04-10 07:47:15 +03:00
|
|
|
import Test.Hspec (Spec, describe, it)
|
|
|
|
import Prelude
|
|
|
|
|
2022-08-29 03:20:00 +03:00
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2022-07-19 03:37:04 +03:00
|
|
|
removeDescriptionFromColumn :: ColumnInfo -> ColumnInfo
|
|
|
|
removeDescriptionFromColumn c = c {dciDescription = Nothing}
|
|
|
|
|
|
|
|
removeDescription :: TableInfo -> TableInfo
|
|
|
|
removeDescription t@TableInfo {dtiColumns} = t {dtiDescription = Nothing, dtiColumns = newColumns}
|
|
|
|
where
|
|
|
|
newColumns = map removeDescriptionFromColumn dtiColumns
|
|
|
|
|
2022-08-29 03:20:00 +03:00
|
|
|
removeForeignKeys :: TableInfo -> TableInfo
|
|
|
|
removeForeignKeys t = t {dtiForeignKeys = Nothing}
|
|
|
|
|
|
|
|
extractForeignKeys :: TableInfo -> [Constraint]
|
|
|
|
extractForeignKeys = foldMap (HashMap.elems . unConstraints) . dtiForeignKeys
|
|
|
|
|
2022-06-03 11:06:31 +03:00
|
|
|
spec :: Client IO (NamedRoutes Routes) -> SourceName -> Config -> Spec
|
|
|
|
spec api sourceName config = describe "schema API" $ do
|
2022-04-10 07:47:15 +03:00
|
|
|
it "returns Chinook schema" $ do
|
2022-07-19 03:37:04 +03:00
|
|
|
tables <- (map removeDescription . sortOn dtiName . srTables) <$> (api // _schema) sourceName config
|
2022-08-29 03:20:00 +03:00
|
|
|
|
|
|
|
-- NOTE: Constraint names arent guaranteed to be the same across
|
|
|
|
-- Chinook backends so we compare Constraints without their names
|
|
|
|
-- independently from the rest of the schema.
|
|
|
|
(map removeForeignKeys tables) `jsonShouldBe` map (removeForeignKeys . removeDescription) Data.schemaTables
|
|
|
|
(map (sort . extractForeignKeys) tables) `jsonShouldBe` map (sort . extractForeignKeys) Data.schemaTables
|