Skip to content

Commit

Permalink
Auto-launch graphql-engine (close #7801, #7827 )
Browse files Browse the repository at this point in the history
Dupe of hasura/graphql-engine-mono#2853 with branch renamed so it doesn't break a tool.

prev pr: hasura/graphql-engine-mono#2911

next pr: hasura/graphql-engine-mono#2922

This implements #7801

Some points to keep in mind for review:

* How state is passed to the tests. Do we like how this works?
   * I quite like it, with the opaque type [`State`](https://github.com/hasura/graphql-engine-mono/blob/68f33051ca6373c42988e78d069eea2a135dc190/server/tests-hspec/Harness/State.hs#L17), we can avoid churn when adding things.
   * The [setup/teardown](https://github.com/hasura/graphql-engine-mono/blob/68f33051ca6373c42988e78d069eea2a135dc190/server/tests-hspec/Spec.hs#L19-L31) seems clean.
   * By using hspec's own means to pass and denote context, we avoid "getting new ideas" for how to structure the tests -- we use a standard. Hopefully, that means the tests' structure rarely change.
* The various flags passed in the [ServeOptions](https://github.com/hasura/graphql-engine-mono/blob/68f33051ca6373c42988e78d069eea2a135dc190/server/tests-hspec/Harness/Constants.hs#L123) - if there are any causes for concern, raise them here. My thinking is that, there are lots. I've picked some "sane" defaults (mostly empty). The tests pass. I think as we add more complex tests, these flags will be scrutinised and updated as needed. I think it's valuable that all flags are explicitly listed here, though.

PR-URL: hasura/graphql-engine-mono#2921
GitOrigin-RevId: 2c2e70b
  • Loading branch information
chrisdone authored and hasura-bot committed Nov 23, 2021
1 parent a79886b commit 926da74
Show file tree
Hide file tree
Showing 19 changed files with 449 additions and 111 deletions.
53 changes: 53 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# This file starts up the necessary database services to test
# graphql-engine.
#
# Run the following to get started:
#
# docker-compose up -d
#
# That will start up services in the background. To take them down,
# you have to run
#
# docker-compose down
#
# If you changed DB init scripts, then you should also run:
#
# docker-compose down --volumes
#
# That'll delete the volumes. Otherwise e.g. PostgreSQL will skip
# initializing if a DB already exists.
#
# If you omit -d, it'll run them all in the foreground, then you can
# stop them with your usual control-C terminal command.
#
# Facts:
#
# * The SERVICE PORTS numbering start at 65001, 65002, etc. to avoid
# bother existing instances of databases.
#
# * The login credentials are, where possible, all "hasura" to avoid
# unnecessary mental overhead.

version: "3.3"
services:

mariadb:
image: mariadb:10.6.4-focal@sha256:c014ba1efc5dbd711d0520c7762d57807f35549de3414eb31e942a420c8a2ed2
ports:
- "65001:3306"
environment:
MARIADB_USER: "hasura"
MARIADB_PASSWORD: "hasura"
MARIADB_DATABASE: "hasura"
MARIADB_ALLOW_EMPTY_ROOT_PASSWORD: "true"

postgres:
image: postgres:14-alpine@sha256:1cf551de75fd602ebc94ccda93305a4e411f941bf9465f59d850bdcfd474ca52
ports:
- "65002:5432"
environment:
POSTGRES_USER: "hasura"
POSTGRES_PASSWORD: "hasura"
POSTGRES_DB: "hasura"
volumes:
- ./docker-compose/postgres:/docker-entrypoint-initdb.d
6 changes: 6 additions & 0 deletions docker-compose/postgres/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash
set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
create schema hasura;
EOSQL
1 change: 1 addition & 0 deletions server/graphql-engine.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,7 @@ test-suite tests-hspec
Harness.Feature
Harness.Yaml
Harness.Sql
Harness.State
Harness.Graphql
Harness.GraphqlEngine

Expand Down
20 changes: 13 additions & 7 deletions server/tests-hspec/ArrayRelationshipsSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import Harness.Graphql
import Harness.GraphqlEngine qualified as GraphqlEngine
import Harness.Mysql as Mysql
import Harness.Sql
import Harness.State (State)
import Harness.Yaml
import Test.Hspec
import Prelude

--------------------------------------------------------------------------------
-- Preamble

spec :: Spec
spec :: SpecWith State
spec =
Feature.feature
Feature.Feature
Expand All @@ -34,10 +35,11 @@ spec =
--------------------------------------------------------------------------------
-- MySQL backend

mysqlSetup :: IO ()
mysqlSetup = do
mysqlSetup :: State -> IO ()
mysqlSetup state = do
-- Clear and reconfigure the metadata
GraphqlEngine.post_
state
"/v1/metadata"
[yaml|
type: replace_metadata
Expand Down Expand Up @@ -102,6 +104,7 @@ VALUES

-- Track the tables
GraphqlEngine.post_
state
"/v1/metadata"
[yaml|
type: mysql_track_table
Expand All @@ -112,6 +115,7 @@ args:
name: author
|]
GraphqlEngine.post_
state
"/v1/metadata"
[yaml|
type: mysql_track_table
Expand All @@ -124,6 +128,7 @@ args:

-- Setup relationships
GraphqlEngine.post_
state
"/v1/metadata"
[yaml|
type: mysql_create_array_relationship
Expand All @@ -141,8 +146,8 @@ args:
column: author_id
|]

mysqlTeardown :: IO ()
mysqlTeardown = do
mysqlTeardown :: State -> IO ()
mysqlTeardown _ = do
Mysql.run_
[sql|
DROP TABLE article;
Expand All @@ -155,11 +160,12 @@ DROP TABLE author;
--------------------------------------------------------------------------------
-- Tests

tests :: Spec
tests :: SpecWith State
tests = do
it "Select an author and one of their articles" $
it "Select an author and one of their articles" $ \state ->
shouldReturnYaml
( GraphqlEngine.postGraphql
state
[graphql|
query {
# we put id=1 restrictions here because we don't assume ordering support
Expand Down
37 changes: 23 additions & 14 deletions server/tests-hspec/BasicFieldsSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import Harness.GraphqlEngine qualified as GraphqlEngine
import Harness.Mysql as Mysql
import Harness.Postgres as Postgres
import Harness.Sql
import Harness.State (State)
import Harness.Yaml
import Test.Hspec
import Prelude

--------------------------------------------------------------------------------
-- Preamble

spec :: Spec
spec :: SpecWith State
spec =
Feature.feature
Feature.Feature
Expand All @@ -40,10 +41,11 @@ spec =
--------------------------------------------------------------------------------
-- MySQL backend

mysqlSetup :: IO ()
mysqlSetup = do
mysqlSetup :: State -> IO ()
mysqlSetup state = do
-- Clear and reconfigure the metadata
GraphqlEngine.post_
state
"/v1/metadata"
[yaml|
type: replace_metadata
Expand Down Expand Up @@ -82,6 +84,7 @@ VALUES

-- Track the tables
GraphqlEngine.post_
state
"/v1/metadata"
[yaml|
type: mysql_track_table
Expand All @@ -92,8 +95,8 @@ args:
name: author
|]

mysqlTeardown :: IO ()
mysqlTeardown = do
mysqlTeardown :: State -> IO ()
mysqlTeardown _ = do
Mysql.run_
[sql|
DROP TABLE hasura.author;
Expand All @@ -102,10 +105,11 @@ DROP TABLE hasura.author;
--------------------------------------------------------------------------------
-- PostgreSQL backend

postgresSetup :: IO ()
postgresSetup = do
postgresSetup :: State -> IO ()
postgresSetup state = do
-- Clear and reconfigure the metadata
GraphqlEngine.post_
state
"/v1/metadata"
[yaml|
type: replace_metadata
Expand Down Expand Up @@ -141,6 +145,7 @@ VALUES

-- Track the tables
GraphqlEngine.post_
state
"/v1/metadata"
[yaml|
type: postgres_track_table
Expand All @@ -151,8 +156,8 @@ args:
name: author
|]

postgresTeardown :: IO ()
postgresTeardown = do
postgresTeardown :: State -> IO ()
postgresTeardown _ = do
Postgres.run_
[sql|
DROP TABLE hasura.author;
Expand All @@ -161,11 +166,12 @@ DROP TABLE hasura.author;
--------------------------------------------------------------------------------
-- Tests

tests :: Spec
tests :: SpecWith State
tests = do
it "Author fields" $
it "Author fields" $ \state ->
shouldReturnYaml
( GraphqlEngine.postGraphql
state
[graphql|
query {
hasura_author {
Expand All @@ -183,9 +189,10 @@ data:
- name: Author 2
id: 2
|]
it "Use operationName" $
it "Use operationName" $ \state ->
shouldReturnYaml
( GraphqlEngine.postGraphqlYaml
state
[yaml|
operationName: chooseThisOne
query: |
Expand All @@ -210,9 +217,10 @@ data:
- name: Author 2
id: 2
|]
it "Missing field" $ do
it "Missing field" $ \state -> do
shouldReturnYaml
( GraphqlEngine.postGraphql
state
[graphql|
query {
hasura_author {
Expand All @@ -231,9 +239,10 @@ errors:
message: |-
field "notPresentCol" not found in type: 'hasura_author'
|]
it "Missing table" $ do
it "Missing table" $ \state ->
shouldReturnYaml
( GraphqlEngine.postGraphql
state
[graphql|
query {
random {
Expand Down
27 changes: 17 additions & 10 deletions server/tests-hspec/DirectivesSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ import Harness.Feature qualified as Feature
import Harness.GraphqlEngine qualified as GraphqlEngine
import Harness.Mysql as Mysql
import Harness.Sql
import Harness.State (State)
import Harness.Yaml
import Test.Hspec
import Prelude

--------------------------------------------------------------------------------
-- Preamble

spec :: Spec
spec :: SpecWith State
spec =
Feature.feature
Feature.Feature
Expand All @@ -33,10 +34,11 @@ spec =
--------------------------------------------------------------------------------
-- MySQL backend

mysqlSetup :: IO ()
mysqlSetup = do
mysqlSetup :: State -> IO ()
mysqlSetup state = do
-- Clear and reconfigure the metadata
GraphqlEngine.post_
state
"/v1/metadata"
[yaml|
type: replace_metadata
Expand Down Expand Up @@ -75,6 +77,7 @@ VALUES

-- Track the tables
GraphqlEngine.post_
state
"/v1/metadata"
[yaml|
type: mysql_track_table
Expand All @@ -85,8 +88,8 @@ args:
name: author
|]

mysqlTeardown :: IO ()
mysqlTeardown = do
mysqlTeardown :: State -> IO ()
mysqlTeardown _ = do
Mysql.run_
[sql|
DROP TABLE author;
Expand All @@ -95,11 +98,12 @@ DROP TABLE author;
--------------------------------------------------------------------------------
-- Tests

tests :: Spec
tests :: SpecWith State
tests = do
it "Skip id field conditionally" $
it "Skip id field conditionally" \state ->
shouldReturnYaml
( GraphqlEngine.postGraphqlYaml
state
[yaml|
query: |
query author_with_both($includeId: Boolean!, $skipId: Boolean!) {
Expand All @@ -120,9 +124,10 @@ data:
- name: Author 2
|]

it "Skip id field conditionally, includeId=true" $
it "Skip id field conditionally, includeId=true" \state ->
shouldReturnYaml
( GraphqlEngine.postGraphqlYaml
state
[yaml|
query: |
query author_with_both($includeId: Boolean!, $skipId: Boolean!) {
Expand All @@ -145,9 +150,10 @@ data:
name: Author 2
|]

it "Skip id field conditionally, skipId=true" $
it "Skip id field conditionally, skipId=true" \state ->
shouldReturnYaml
( GraphqlEngine.postGraphqlYaml
state
[yaml|
query: |
query author_with_both($includeId: Boolean!, $skipId: Boolean!) {
Expand All @@ -168,9 +174,10 @@ data:
- name: Author 2
|]

it "Skip id field conditionally, skipId=true, includeId=true" $
it "Skip id field conditionally, skipId=true, includeId=true" \state ->
shouldReturnYaml
( GraphqlEngine.postGraphqlYaml
state
[yaml|
query: |
query author_with_both($includeId: Boolean!, $skipId: Boolean!) {
Expand Down
Loading

0 comments on commit 926da74

Please sign in to comment.