mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-10-06 06:48:12 +03:00
hspec: Tests for MySQL equivalent to the ones from the Python suite. close hasura/graphql-engine#7757
This fills in the gaps specified in this comment: https://github.com/hasura/graphql-engine/issues/7757#issuecomment-979948890 From the issue regarding fleshing out all MySQL tests that we already had in the python suite. I'll push commits here. Closes https://github.com/hasura/graphql-engine/issues/7757 PR-URL: https://github.com/hasura/graphql-engine-mono/pull/3271 GitOrigin-RevId: 9f68ed1c2b81cae1d479f9482c975f18a699c93a
This commit is contained in:
parent
21d1baecb4
commit
14bf8ba1be
@ -970,6 +970,7 @@ test-suite tests-hspec
|
||||
Harness.GraphqlEngine
|
||||
|
||||
ArrayRelationshipsSpec
|
||||
NestedRelationshipsSpec
|
||||
BasicFieldsSpec
|
||||
DirectivesSpec
|
||||
HelloWorldSpec
|
||||
|
@ -171,3 +171,117 @@ data:
|
||||
- name: Author 1
|
||||
- name: Author 2
|
||||
|]
|
||||
|
||||
-- These two come from <https://github.com/hasura/graphql-engine-mono/blob/ec3568c704c4c3f13ecff757c547f0d5a272307b/server/tests-py/queries/graphql_query/mysql/select_query_author_with_skip_directive.yaml#L1>
|
||||
|
||||
it "Author with skip id" \state ->
|
||||
shouldReturnYaml
|
||||
( GraphqlEngine.postGraphqlYaml
|
||||
state
|
||||
[yaml|
|
||||
query: |
|
||||
query author_with_skip($skipId: Boolean!, $skipName: Boolean!) {
|
||||
hasura_author {
|
||||
id @skip(if: $skipId)
|
||||
name @skip(if: $skipName)
|
||||
}
|
||||
}
|
||||
variables:
|
||||
skipId: true
|
||||
skipName: false
|
||||
|]
|
||||
)
|
||||
[yaml|
|
||||
data:
|
||||
hasura_author:
|
||||
- name: Author 1
|
||||
- name: Author 2
|
||||
|]
|
||||
it "Author with skip name" \state ->
|
||||
shouldReturnYaml
|
||||
( GraphqlEngine.postGraphqlYaml
|
||||
state
|
||||
[yaml|
|
||||
query: |
|
||||
query author_with_skip($skipId: Boolean!, $skipName: Boolean!) {
|
||||
hasura_author {
|
||||
id @skip(if: $skipId)
|
||||
name @skip(if: $skipName)
|
||||
}
|
||||
}
|
||||
variables:
|
||||
skipId: false
|
||||
skipName: true
|
||||
|]
|
||||
)
|
||||
[yaml|
|
||||
data:
|
||||
hasura_author:
|
||||
- id: 1
|
||||
- id: 2
|
||||
|]
|
||||
|
||||
-- These three come from <https://github.com/hasura/graphql-engine-mono/blob/5f6f862e5f6b67d82cfa59568edfc4f08b920375/server/tests-py/queries/graphql_query/mysql/select_query_author_with_wrong_directive_err.yaml#L1>
|
||||
it "Rejects unknown directives" \state ->
|
||||
shouldReturnYaml
|
||||
( GraphqlEngine.postGraphqlYaml
|
||||
state
|
||||
[yaml|
|
||||
query: |
|
||||
query {
|
||||
hasura_author {
|
||||
id @exclude(if: true)
|
||||
name
|
||||
}
|
||||
}
|
||||
|]
|
||||
)
|
||||
[yaml|
|
||||
errors:
|
||||
- extensions:
|
||||
path: $.selectionSet.hasura_author.selectionSet
|
||||
code: validation-failed
|
||||
message: directive "exclude" is not defined in the schema
|
||||
|]
|
||||
it "Rejects duplicate directives" \state ->
|
||||
shouldReturnYaml
|
||||
( GraphqlEngine.postGraphqlYaml
|
||||
state
|
||||
[yaml|
|
||||
query: |
|
||||
query {
|
||||
hasura_author {
|
||||
id @include(if: true) @include(if: true)
|
||||
name
|
||||
}
|
||||
}
|
||||
|]
|
||||
)
|
||||
[yaml|
|
||||
errors:
|
||||
- extensions:
|
||||
path: $.selectionSet.hasura_author.selectionSet
|
||||
code: validation-failed
|
||||
message: 'the following directives are used more than once: include'
|
||||
|]
|
||||
it "Rejects directives on wrong element" \state ->
|
||||
shouldReturnYaml
|
||||
( GraphqlEngine.postGraphqlYaml
|
||||
state
|
||||
[yaml|
|
||||
query: |
|
||||
query @include(if: true) {
|
||||
hasura_author {
|
||||
id
|
||||
name
|
||||
}
|
||||
}
|
||||
|]
|
||||
)
|
||||
[yaml|
|
||||
errors:
|
||||
- extensions:
|
||||
path: $
|
||||
code: validation-failed
|
||||
message: directive "include" is not allowed on a query
|
||||
|]
|
||||
|
@ -123,6 +123,37 @@ data:
|
||||
id: 1
|
||||
|]
|
||||
|
||||
-- Originally from <https://github.com/hasura/graphql-engine-mono/blob/719d03bd970def443e25d04210a37556d350d84b/server/tests-py/queries/graphql_query/mysql/select_query_author_offset.yaml>
|
||||
--
|
||||
-- Technically without an ORDER, the results are UB-ish. Keep an eye
|
||||
-- on ordering with tests like this.
|
||||
it "Basic offset query" $ \state ->
|
||||
shouldReturnYaml
|
||||
( GraphqlEngine.postGraphql
|
||||
state
|
||||
[graphql|
|
||||
query {
|
||||
hasura_author(offset: 1) {
|
||||
name
|
||||
id
|
||||
}
|
||||
}
|
||||
|]
|
||||
)
|
||||
[yaml|
|
||||
data:
|
||||
hasura_author:
|
||||
- name: Author 2
|
||||
id: 2
|
||||
- name: Author 3
|
||||
id: 3
|
||||
- name: Author 4
|
||||
id: 4
|
||||
|]
|
||||
|
||||
-- This is a more precise version of <https://github.com/hasura/graphql-engine-mono/blob/dbf32f15c25c12fba88afced311fd876111cb987/server/tests-py/queries/graphql_query/mysql/select_query_author_limit_offset.yaml#L1>
|
||||
--
|
||||
-- We use ordering here, which yields a stable result.
|
||||
it "order descending, offset 2, limit 1" $ \state ->
|
||||
shouldReturnYaml
|
||||
( GraphqlEngine.postGraphql
|
||||
|
222
server/tests-hspec/NestedRelationshipsSpec.hs
Normal file
222
server/tests-hspec/NestedRelationshipsSpec.hs
Normal file
@ -0,0 +1,222 @@
|
||||
{-# LANGUAGE QuasiQuotes #-}
|
||||
{-# LANGUAGE RecordWildCards #-}
|
||||
|
||||
-- | Testing nested relationships.
|
||||
--
|
||||
-- Original inspiration for this module is <https://github.com/hasura/graphql-engine-mono/blob/08caf7df10cad0aea0916327736147a0a8f808d1/server/tests-py/queries/graphql_query/mysql/nested_select_query_deep.yaml>
|
||||
module NestedRelationshipsSpec (spec) where
|
||||
|
||||
import Harness.Constants
|
||||
import Harness.Feature qualified as Feature
|
||||
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 :: SpecWith State
|
||||
spec =
|
||||
Feature.feature
|
||||
Feature.Feature
|
||||
{ Feature.backends =
|
||||
[ Feature.Backend
|
||||
{ name = "MySQL",
|
||||
setup = mysqlSetup,
|
||||
teardown = mysqlTeardown
|
||||
}
|
||||
],
|
||||
Feature.tests = tests
|
||||
}
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- MySQL backend
|
||||
|
||||
mysqlSetup :: State -> IO ()
|
||||
mysqlSetup state = do
|
||||
-- Clear and reconfigure the metadata
|
||||
GraphqlEngine.post_
|
||||
state
|
||||
"/v1/metadata"
|
||||
[yaml|
|
||||
type: replace_metadata
|
||||
args:
|
||||
version: 3
|
||||
sources:
|
||||
- name: mysql
|
||||
kind: mysql
|
||||
tables: []
|
||||
configuration:
|
||||
database: *mysqlDatabase
|
||||
user: *mysqlUser
|
||||
password: *mysqlPassword
|
||||
host: *mysqlHost
|
||||
port: *mysqlPort
|
||||
pool_settings: {}
|
||||
|]
|
||||
|
||||
-- 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 tables
|
||||
Mysql.run_
|
||||
[sql|
|
||||
CREATE TABLE article (
|
||||
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
||||
title TEXT,
|
||||
content TEXT,
|
||||
is_published BIT,
|
||||
published_on TIMESTAMP,
|
||||
author_id INT UNSIGNED,
|
||||
co_author_id INT UNSIGNED,
|
||||
FOREIGN KEY (author_id) REFERENCES author(id),
|
||||
FOREIGN KEY (co_author_id) REFERENCES author(id)
|
||||
);
|
||||
|]
|
||||
Mysql.run_
|
||||
[sql|
|
||||
INSERT INTO article
|
||||
(title, content, author_id, is_published)
|
||||
VALUES
|
||||
( 'Article 1', 'Sample article content 1', 1, 0 ),
|
||||
( 'Article 2', 'Sample article content 2', 1, 1 ),
|
||||
( 'Article 3', 'Sample article content 3', 2, 1 );
|
||||
|]
|
||||
|
||||
-- Track the tables
|
||||
GraphqlEngine.post_
|
||||
state
|
||||
"/v1/metadata"
|
||||
[yaml|
|
||||
type: mysql_track_table
|
||||
args:
|
||||
source: mysql
|
||||
table:
|
||||
schema: hasura
|
||||
name: author
|
||||
|]
|
||||
GraphqlEngine.post_
|
||||
state
|
||||
"/v1/metadata"
|
||||
[yaml|
|
||||
type: mysql_track_table
|
||||
args:
|
||||
source: mysql
|
||||
table:
|
||||
schema: hasura
|
||||
name: article
|
||||
|]
|
||||
|
||||
-- Setup relationships
|
||||
GraphqlEngine.post_
|
||||
state
|
||||
"/v1/metadata"
|
||||
[yaml|
|
||||
type: mysql_create_object_relationship
|
||||
args:
|
||||
source: mysql
|
||||
table:
|
||||
name: article
|
||||
schema: hasura
|
||||
name: author
|
||||
using:
|
||||
foreign_key_constraint_on: author_id
|
||||
|]
|
||||
GraphqlEngine.post_
|
||||
state
|
||||
"/v1/metadata"
|
||||
[yaml|
|
||||
type: mysql_create_array_relationship
|
||||
args:
|
||||
source: mysql
|
||||
table:
|
||||
name: author
|
||||
schema: hasura
|
||||
name: articles
|
||||
using:
|
||||
foreign_key_constraint_on:
|
||||
table:
|
||||
name: article
|
||||
schema: hasura
|
||||
column: author_id
|
||||
|]
|
||||
|
||||
mysqlTeardown :: State -> IO ()
|
||||
mysqlTeardown _ = do
|
||||
Mysql.run_
|
||||
[sql|
|
||||
DROP TABLE article;
|
||||
|]
|
||||
Mysql.run_
|
||||
[sql|
|
||||
DROP TABLE author;
|
||||
|]
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
-- Tests
|
||||
|
||||
tests :: SpecWith State
|
||||
tests = do
|
||||
it "Nested select on article" $ \state ->
|
||||
shouldReturnYaml
|
||||
( GraphqlEngine.postGraphql
|
||||
state
|
||||
[graphql|
|
||||
query {
|
||||
hasura_article(where: {id: {_eq: 1}}) {
|
||||
id
|
||||
author {
|
||||
id
|
||||
articles(where: {id: {_eq: 1}}) {
|
||||
id
|
||||
author {
|
||||
id
|
||||
articles(where: {id: {_eq: 1}}) {
|
||||
id
|
||||
author {
|
||||
id
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|]
|
||||
)
|
||||
[yaml|
|
||||
data:
|
||||
hasura_article:
|
||||
- id: 1
|
||||
author:
|
||||
id: 1
|
||||
articles:
|
||||
- id: 1
|
||||
author:
|
||||
id: 1
|
||||
articles:
|
||||
- id: 1
|
||||
author:
|
||||
id: 1
|
||||
|]
|
@ -64,16 +64,17 @@ args:
|
||||
CREATE TABLE author
|
||||
(
|
||||
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(45) UNIQUE KEY
|
||||
name VARCHAR(45) UNIQUE KEY,
|
||||
createdAt DATETIME
|
||||
);
|
||||
|]
|
||||
Mysql.run_
|
||||
[sql|
|
||||
INSERT INTO author
|
||||
(name)
|
||||
(name, createdAt)
|
||||
VALUES
|
||||
( 'Author 1'),
|
||||
( 'Author 2');
|
||||
( 'Author 1', '2017-09-21 09:39:44' ),
|
||||
( 'Author 2', '2017-09-21 09:50:44' );
|
||||
|]
|
||||
|
||||
-- Setup tables
|
||||
@ -173,3 +174,34 @@ data:
|
||||
author:
|
||||
id: 1
|
||||
|]
|
||||
|
||||
-- originally from <https://github.com/hasura/graphql-engine-mono/blob/cf64da26e818ca0e4ec39667296c67021bc03c2a/server/tests-py/queries/graphql_query/mysql/select_query_author_quoted_col.yaml>
|
||||
it "Simple GraphQL object query on author" $ \state ->
|
||||
shouldReturnYaml
|
||||
( GraphqlEngine.postGraphql
|
||||
state
|
||||
[graphql|
|
||||
query {
|
||||
hasura_author {
|
||||
createdAt
|
||||
name
|
||||
id
|
||||
}
|
||||
}
|
||||
|]
|
||||
)
|
||||
-- I believe that the order is not necessarily guaranteed, but
|
||||
-- the Python test was written like this. We can't use
|
||||
-- limit/order here without requiring that new backends
|
||||
-- implement those too. Approach: hope that ordering is never
|
||||
-- violated, and if it is, rework this test.
|
||||
[yaml|
|
||||
data:
|
||||
hasura_author:
|
||||
- createdAt: '2017-09-21 09:39:44'
|
||||
name: Author 1
|
||||
id: 1
|
||||
- createdAt: '2017-09-21 09:50:44'
|
||||
name: Author 2
|
||||
id: 2
|
||||
|]
|
||||
|
@ -99,6 +99,10 @@ DROP TABLE author;
|
||||
--------------------------------------------------------------------------------
|
||||
-- Tests
|
||||
|
||||
-- Added equivalents from <https://github.com/hasura/graphql-engine-mono/blob/ee524e94caf6405ce0ae39edfe161dadd223d60f/server/tests-py/queries/graphql_query/mysql/select_query_author_order_by.yaml#L1>
|
||||
-- That includes order by {text,id} {desc,asc}
|
||||
--
|
||||
|
||||
tests :: SpecWith State
|
||||
tests = do
|
||||
it "Order by id ascending" $ \state ->
|
||||
@ -134,6 +138,50 @@ query {
|
||||
id
|
||||
}
|
||||
}
|
||||
|]
|
||||
)
|
||||
[yaml|
|
||||
data:
|
||||
hasura_author:
|
||||
- name: Author 2
|
||||
id: 2
|
||||
- name: Author 1
|
||||
id: 1
|
||||
|]
|
||||
|
||||
it "Order by name ascending" $ \state ->
|
||||
shouldReturnYaml
|
||||
( GraphqlEngine.postGraphql
|
||||
state
|
||||
[graphql|
|
||||
query {
|
||||
hasura_author (order_by: {name: asc}) {
|
||||
name
|
||||
id
|
||||
}
|
||||
}
|
||||
|]
|
||||
)
|
||||
[yaml|
|
||||
data:
|
||||
hasura_author:
|
||||
- name: Author 1
|
||||
id: 1
|
||||
- name: Author 2
|
||||
id: 2
|
||||
|]
|
||||
|
||||
it "Order by name descending" $ \state ->
|
||||
shouldReturnYaml
|
||||
( GraphqlEngine.postGraphql
|
||||
state
|
||||
[graphql|
|
||||
query {
|
||||
hasura_author (order_by: {name: desc}) {
|
||||
name
|
||||
id
|
||||
}
|
||||
}
|
||||
|]
|
||||
)
|
||||
[yaml|
|
||||
|
@ -7,6 +7,7 @@ import DirectivesSpec qualified
|
||||
import Harness.GraphqlEngine (startServerThread, stopServer)
|
||||
import Harness.State (State (..))
|
||||
import LimitOffsetSpec qualified
|
||||
import NestedRelationshipsSpec qualified
|
||||
import ObjectRelationshipsSpec qualified
|
||||
import OrderingSpec qualified
|
||||
import ServiceLivenessSpec qualified
|
||||
@ -41,3 +42,4 @@ main =
|
||||
describe "ArrayRelationships" ArrayRelationshipsSpec.spec
|
||||
describe "Directives" DirectivesSpec.spec
|
||||
describe "Views" ViewsSpec.spec
|
||||
describe "NestedRelationships" NestedRelationshipsSpec.spec
|
||||
|
Loading…
Reference in New Issue
Block a user