graphql-engine/server/tests-hspec/Test/Schema/ViewsSpec.hs
Tom Harding d26b16106f Migrate ViewsSpec to the new structure
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/5337
Co-authored-by: Gil Mizrahi <8547573+soupi@users.noreply.github.com>
GitOrigin-RevId: c1056790b42039630d649c96ad7aa2aa5c691ecf
2022-08-09 11:16:30 +00:00

168 lines
4.5 KiB
Haskell

{-# LANGUAGE QuasiQuotes #-}
module Test.Schema.ViewsSpec (spec) where
import Data.Aeson (Value)
import Database.PG.Query.Pool (sql)
import Harness.Backend.Mysql qualified as Mysql
import Harness.Backend.Postgres qualified as Postgres
import Harness.GraphqlEngine (postGraphql, postMetadata_)
import Harness.Quoter.Graphql (graphql)
import Harness.Quoter.Yaml (yaml)
import Harness.Test.Fixture qualified as Fixture
import Harness.Test.Schema (Table (..), table)
import Harness.Test.Schema qualified as Schema
import Harness.TestEnvironment (TestEnvironment)
import Harness.Yaml (shouldReturnYaml)
import Hasura.Prelude
import Test.Hspec (SpecWith, describe, it)
spec :: SpecWith TestEnvironment
spec =
Fixture.run
[ (Fixture.fixture $ Fixture.Backend Fixture.MySQL)
{ Fixture.setupTeardown = \(testEnvironment, _) ->
[ Mysql.setupTablesAction schema testEnvironment,
setupMysql,
setupMetadata Fixture.MySQL testEnvironment
]
},
(Fixture.fixture $ Fixture.Backend Fixture.Postgres)
{ Fixture.setupTeardown = \(testEnvironment, _) ->
[ Postgres.setupTablesAction schema testEnvironment,
setupPostgres,
setupMetadata Fixture.Postgres testEnvironment
]
}
]
tests
--------------------------------------------------------------------------------
-- Schema
schema :: [Schema.Table]
schema =
[ (table "author")
{ tableColumns =
[ Schema.column "id" Schema.TInt,
Schema.column "name" Schema.TStr
],
tablePrimaryKey = ["id"],
tableData =
[ [Schema.VInt 1, Schema.VStr "Alice"],
[Schema.VInt 2, Schema.VStr "Bob"]
]
}
]
--------------------------------------------------------------------------------
-- Tests
tests :: Fixture.Options -> SpecWith TestEnvironment
tests opts = do
let shouldBe :: IO Value -> Value -> IO ()
shouldBe = shouldReturnYaml opts
describe "Queries involving views" do
it "Queries views correctly" \testEnvironment -> do
let expected :: Value
expected =
[yaml|
data:
hasura_author_view:
- id: 1
name: Alice
|]
actual :: IO Value
actual =
postGraphql
testEnvironment
[graphql|
query {
hasura_author_view(where: { id: { _eq: 1 } }) {
id
name
}
}
|]
actual `shouldBe` expected
--------------------------------------------------------------------------------
-- MySQL setup
setupMysql :: Fixture.SetupAction
setupMysql =
Fixture.SetupAction
{ Fixture.setupAction =
Mysql.run_
[sql|
CREATE OR REPLACE VIEW author_view
AS SELECT * FROM author
|],
Fixture.teardownAction = \_ ->
Mysql.run_
[sql|
DROP VIEW IF EXISTS author_view
|]
}
--------------------------------------------------------------------------------
-- Postgres setup
setupPostgres :: Fixture.SetupAction
setupPostgres =
Fixture.SetupAction
{ Fixture.setupAction =
Postgres.run_
[sql|
CREATE OR REPLACE VIEW author_view
AS SELECT * FROM author
|],
Fixture.teardownAction = \_ ->
Postgres.run_
[sql|
DROP VIEW IF EXISTS author_view
|]
}
--------------------------------------------------------------------------------
-- Metadata
setupMetadata :: Fixture.BackendType -> TestEnvironment -> Fixture.SetupAction
setupMetadata backend testEnvironment =
Fixture.SetupAction
{ Fixture.setupAction =
postMetadata_
testEnvironment
[yaml|
type: *track
args:
source: *label
table:
name: author_view
schema: hasura
|],
Fixture.teardownAction = \_ ->
postMetadata_
testEnvironment
[yaml|
type: *untrack
args:
source: *label
table:
name: author_view
schema: hasura
|]
}
where
label :: String
label = Fixture.defaultBackendTypeString backend
track :: String
track = label <> "_track_table"
untrack :: String
untrack = label <> "_untrack_table"