2021-11-19 18:13:33 +03:00
|
|
|
-- | Test views.
|
2022-01-21 10:48:27 +03:00
|
|
|
module Test.ViewsSpec (spec) where
|
2021-11-19 18:13:33 +03:00
|
|
|
|
2022-01-21 10:48:27 +03:00
|
|
|
import Harness.Backend.Mysql as Mysql
|
2021-11-19 18:13:33 +03:00
|
|
|
import Harness.GraphqlEngine qualified as GraphqlEngine
|
2022-01-21 10:48:27 +03:00
|
|
|
import Harness.Quoter.Graphql
|
|
|
|
import Harness.Quoter.Sql
|
|
|
|
import Harness.Quoter.Yaml
|
2021-11-23 21:15:17 +03:00
|
|
|
import Harness.State (State)
|
2022-02-21 20:05:09 +03:00
|
|
|
import Harness.Test.Context qualified as Context
|
2021-11-19 18:13:33 +03:00
|
|
|
import Test.Hspec
|
|
|
|
import Prelude
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Preamble
|
|
|
|
|
2021-11-23 21:15:17 +03:00
|
|
|
spec :: SpecWith State
|
2021-11-19 18:13:33 +03:00
|
|
|
spec =
|
2022-02-21 20:05:09 +03:00
|
|
|
Context.run
|
|
|
|
[ Context.Context
|
|
|
|
{ name = Context.MySQL,
|
|
|
|
mkLocalState = Context.noLocalState,
|
2022-02-14 20:24:24 +03:00
|
|
|
setup = mysqlSetup,
|
|
|
|
teardown = mysqlTeardown,
|
2022-02-18 16:35:32 +03:00
|
|
|
customOptions = Nothing
|
2022-02-14 20:24:24 +03:00
|
|
|
}
|
|
|
|
]
|
|
|
|
tests
|
2021-11-19 18:13:33 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- MySQL backend
|
|
|
|
|
2022-02-21 20:05:09 +03:00
|
|
|
mysqlSetup :: (State, ()) -> IO ()
|
|
|
|
mysqlSetup (state, _) = do
|
2021-11-19 18:13:33 +03:00
|
|
|
-- Clear and reconfigure the metadata
|
2022-01-25 19:34:29 +03:00
|
|
|
GraphqlEngine.setSource state Mysql.defaultSourceMetadata
|
2021-11-19 18:13:33 +03:00
|
|
|
|
|
|
|
-- Setup tables
|
|
|
|
Mysql.run_
|
|
|
|
[sql|
|
|
|
|
CREATE TABLE author
|
|
|
|
(
|
|
|
|
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
|
|
|
name VARCHAR(45) UNIQUE KEY,
|
|
|
|
createdAt DATETIME
|
|
|
|
);
|
|
|
|
|]
|
|
|
|
Mysql.run_
|
|
|
|
[sql|
|
|
|
|
INSERT INTO author
|
|
|
|
(name, createdAt)
|
|
|
|
VALUES
|
|
|
|
( 'Author 1', '2017-09-21 09:39:44' ),
|
|
|
|
( 'Author 2', '2017-09-21 09:50:44' );
|
|
|
|
|]
|
|
|
|
|
|
|
|
-- Setup views
|
|
|
|
Mysql.run_
|
|
|
|
[sql|
|
|
|
|
CREATE OR REPLACE VIEW search_author_view AS
|
|
|
|
SELECT * FROM author;
|
|
|
|
|]
|
|
|
|
|
|
|
|
-- Track the tables
|
2022-02-14 20:24:24 +03:00
|
|
|
GraphqlEngine.postMetadata_
|
2021-11-23 21:15:17 +03:00
|
|
|
state
|
2021-11-19 18:13:33 +03:00
|
|
|
[yaml|
|
|
|
|
type: mysql_track_table
|
|
|
|
args:
|
|
|
|
source: mysql
|
|
|
|
table:
|
|
|
|
schema: hasura
|
|
|
|
name: author
|
|
|
|
|]
|
|
|
|
-- Track the views
|
|
|
|
|
2022-02-14 20:24:24 +03:00
|
|
|
GraphqlEngine.postMetadata_
|
2021-11-23 21:15:17 +03:00
|
|
|
state
|
2021-11-19 18:13:33 +03:00
|
|
|
[yaml|
|
|
|
|
type: mysql_track_table
|
|
|
|
args:
|
|
|
|
source: mysql
|
|
|
|
table:
|
|
|
|
name: search_author_view
|
|
|
|
schema: hasura
|
|
|
|
|]
|
|
|
|
|
2022-02-14 20:24:24 +03:00
|
|
|
mysqlTeardown :: (State, ()) -> IO ()
|
2021-11-23 21:15:17 +03:00
|
|
|
mysqlTeardown _ = do
|
2021-11-19 18:13:33 +03:00
|
|
|
Mysql.run_
|
|
|
|
[sql|
|
|
|
|
DROP VIEW IF EXISTS search_author_view;
|
|
|
|
|]
|
|
|
|
Mysql.run_
|
|
|
|
[sql|
|
|
|
|
DROP TABLE author;
|
|
|
|
|]
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
-- Tests
|
|
|
|
|
2022-02-21 20:05:09 +03:00
|
|
|
tests :: Context.Options -> SpecWith State
|
2022-02-09 18:26:14 +03:00
|
|
|
tests opts = do
|
2021-11-23 21:15:17 +03:00
|
|
|
it "Query that a view works properly" \state ->
|
2021-11-19 18:13:33 +03:00
|
|
|
shouldReturnYaml
|
2022-02-09 18:26:14 +03:00
|
|
|
opts
|
2021-11-19 18:13:33 +03:00
|
|
|
( GraphqlEngine.postGraphql
|
2021-11-23 21:15:17 +03:00
|
|
|
state
|
2021-11-19 18:13:33 +03:00
|
|
|
[graphql|
|
|
|
|
query {
|
|
|
|
hasura_search_author_view(where: {id: {_eq: 1}}) {
|
|
|
|
id
|
|
|
|
name
|
|
|
|
createdAt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
)
|
|
|
|
[yaml|
|
|
|
|
data:
|
|
|
|
hasura_search_author_view:
|
|
|
|
- id: 1
|
|
|
|
name: Author 1
|
|
|
|
createdAt: "2017-09-21 09:39:44"
|
|
|
|
|]
|