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)
|
2022-08-30 02:51:34 +03:00
|
|
|
import Hasura.Backends.DataConnector.API qualified as API
|
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-08-30 02:51:34 +03:00
|
|
|
removeDescriptionFromColumn :: API.ColumnInfo -> API.ColumnInfo
|
2022-09-21 08:11:53 +03:00
|
|
|
removeDescriptionFromColumn c = c {API._ciDescription = Nothing}
|
2022-07-19 03:37:04 +03:00
|
|
|
|
2022-08-30 02:51:34 +03:00
|
|
|
removeDescription :: API.TableInfo -> API.TableInfo
|
2022-09-21 08:11:53 +03:00
|
|
|
removeDescription t@API.TableInfo {API._tiColumns} = t {API._tiDescription = Nothing, API._tiColumns = newColumns}
|
2022-07-19 03:37:04 +03:00
|
|
|
where
|
2022-09-21 08:11:53 +03:00
|
|
|
newColumns = map removeDescriptionFromColumn _tiColumns
|
2022-07-19 03:37:04 +03:00
|
|
|
|
2022-08-30 02:51:34 +03:00
|
|
|
removeForeignKeys :: API.TableInfo -> API.TableInfo
|
2022-09-21 08:11:53 +03:00
|
|
|
removeForeignKeys t = t {API._tiForeignKeys = Nothing}
|
2022-08-29 03:20:00 +03:00
|
|
|
|
2022-08-30 02:51:34 +03:00
|
|
|
extractForeignKeys :: API.TableInfo -> [API.Constraint]
|
2022-09-21 08:11:53 +03:00
|
|
|
extractForeignKeys = foldMap (HashMap.elems . API.unConstraints) . API._tiForeignKeys
|
2022-08-29 03:20:00 +03:00
|
|
|
|
2022-08-30 02:51:34 +03:00
|
|
|
spec :: Client IO (NamedRoutes API.Routes) -> API.SourceName -> API.Config -> Spec
|
2022-06-03 11:06:31 +03:00
|
|
|
spec api sourceName config = describe "schema API" $ do
|
2022-04-10 07:47:15 +03:00
|
|
|
it "returns Chinook schema" $ do
|
2022-09-21 08:11:53 +03:00
|
|
|
tables <- (map removeDescription . sortOn API._tiName . API._srTables) <$> (api // 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
|