这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion server/graphql-engine.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ test-suite graphql-engine-test
, aeson
, aeson-casing
, bytestring
--, directory
-- , directory
--, fgl
, filepath
, hspec
Expand Down
7 changes: 3 additions & 4 deletions server/test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import Options.Applicative
import System.Environment (withArgs)
import System.Exit (exitFailure)
import Test.Hspec.Core.Runner
import Test.Hspec.Formatters
import Test.Hspec.Wai

import qualified Data.Aeson as J
Expand Down Expand Up @@ -43,7 +42,7 @@ resetStateTx = do

ravenApp :: L.LoggerCtx -> PGQ.PGPool -> IO Application
ravenApp loggerCtx pool = do
let corsCfg = CorsConfigG "*" True -- cors is disabled
let corsCfg = CorsConfigG "*" False -- cors is enabled
-- spockAsApp $ spockT id $ app Q.Serializable Nothing rlogger pool AMNoAuth corsCfg True -- no access key and no webhook
mkWaiApp Q.Serializable Nothing loggerCtx pool AMNoAuth corsCfg True -- no access key and no webhook

Expand All @@ -60,11 +59,11 @@ main = do
void $ liftIO $ runExceptT $ Q.runTx pool defTxMode resetStateTx
-- intialize state for graphql-engine in the database
liftIO $ initialise pool
-- generate the test specs
specs <- mkSpecs
loggerCtx <- L.mkLoggerCtx L.defaultLoggerSettings
-- run the tests
withArgs [] $ hspecWith defaultConfig {configFormatter = Just progress} $
with (ravenApp loggerCtx pool) specs
withArgs [] $ hspecWith defaultConfig $ with (ravenApp loggerCtx pool) specs

where
initialise :: Q.PGPool -> IO ()
Expand Down
18 changes: 17 additions & 1 deletion server/test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ data TestCase
, tcQuery :: !J.Value
, tcUrl :: !T.Text
, tcStatus :: !Int
-- , tcDependsOn :: !(Maybe TestCase)
} deriving (Show)

$(J.deriveJSON (J.aesonDrop 2 J.snakeCase) ''TestCase)


querySpecFiles :: [FilePath]
querySpecFiles =
[ "create_tables.yaml"
, "track_tables.yaml"
, "create_author_article_relationship.yaml"
, "create_author_article_permissions.yaml"
]

gqlSpecFiles :: [FilePath]
Expand All @@ -46,6 +49,8 @@ gqlSpecFiles =
, "insert_mutation_article_on_conflict_error_02.yaml"
, "insert_mutation_article_on_conflict_error_03.yaml"
, "nested_select_query_article.yaml"
, "update_mutation_author.yaml"
, "delete_mutation_article.yaml"
]

readTestCase :: FilePath -> IO TestCase
Expand All @@ -63,9 +68,10 @@ mkSpec tc = do
url = tcUrl tc
q = tcQuery tc
respStatus = (fromIntegral $ tcStatus tc) :: ResponseMatcher
it (T.unpack desc) $ do
it (T.unpack desc) $
post (T.encodeUtf8 url) (J.encode q) `shouldRespondWith` respStatus


mkSpecs :: IO (SpecWith Application)
mkSpecs = do
ddlTc <- mapM readTestCase querySpecFiles
Expand All @@ -75,6 +81,16 @@ mkSpecs = do
it "responds with version" $
get "/v1/version" `shouldRespondWith` 200

describe "console endpoint" $
it "responds with 200" $
get "/console" `shouldRespondWith` 200

describe "CORS test" $
it "should respond with correct CORS headers" $
request "OPTIONS" "/v1/version" [("Origin", "example.com")] ""
`shouldRespondWith` 204
{matchHeaders = ["Access-Control-Allow-Origin" <:> "example.com"]}

describe "Query API" $ mapM_ mkSpec ddlTc

describe "GraphQL API" $ mapM_ mkSpec gqlTc
14 changes: 14 additions & 0 deletions server/test/testcases/create_author_article_permissions.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
description: Create relevant permissions
url: /v1/query
status: 200
query:
type: create_select_permission
args:
table: article
role: user
permission:
columns: '*'
filter:
$or:
- author_id: X-HASURA-USER-ID
- is_published: true
20 changes: 15 additions & 5 deletions server/test/testcases/create_author_article_relationship.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@ description: Creates relationships
url: /v1/query
status: 200
query:
type: create_object_relationship
type: bulk
args:
table: article
name: author
using:
foreign_key_constraint_on: author_id
- type: create_object_relationship
args:
table: article
name: author
using:
foreign_key_constraint_on: author_id
- type: create_array_relationship
args:
table: author
name: articles
using:
foreign_key_constraint_on:
table: article
column: author_id
4 changes: 3 additions & 1 deletion server/test/testcases/create_tables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ query:
id SERIAL PRIMARY KEY,
title TEXT,
content TEXT,
author_id INTEGER REFERENCES author(id)
author_id INTEGER REFERENCES author(id),
is_published BOOLEAN,
published_on TIMESTAMP
)
12 changes: 12 additions & 0 deletions server/test/testcases/delete_mutation_article.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
description: Delete mutation on article
url: /v1alpha1/graphql
status: 200
query:
query: |
mutation delete_article {
delete_article (
where: {id: {_eq: 5}}
) {
affected_rows
}
}
21 changes: 18 additions & 3 deletions server/test/testcases/insert_mutation_article.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,32 @@ query:
{
title: "Article 1",
content: "Sample article content",
author_id: 1
author_id: 1,
is_published: true
},
{
title: "Article 2",
content: "Sample article content",
author_id: 1
author_id: 1,
is_published: true
},
{
title: "Article 3",
content: "Sample article content",
author_id: 2
author_id: 2,
is_published: true
},
{
title: "Article 4",
content: "Sample article content",
author_id: 1,
is_published: false
},
{
title: "Article 5",
content: "Sample article content",
author_id: 2,
is_published: false
}
]
) {
Expand Down
19 changes: 19 additions & 0 deletions server/test/testcases/nested_select_where_query_author.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
description: Select author and their articles
url: /v1alpha1/graphql
status: 200
query:
query: |
query {
author (where: {name: {_eq: "Author 1"}}) {
id
name
articles (
where: {is_published: {_eq: true}}
limit: 10,
offset: 1
) {
id
title
}
}
}
13 changes: 13 additions & 0 deletions server/test/testcases/update_mutation_author.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
description: Update mutation on author
url: /v1alpha1/graphql
status: 200
query:
query: |
mutation update_author {
update_author(
where: {id: {_eq: 1}},
_set: {name: "Jane"}
) {
affected_rows
}
}