add more tests (#170)

This commit is contained in:
Anon Ray 2018-07-20 08:19:06 +00:00 committed by Vamshi Surabhi
parent e3f960da96
commit 07ac9fe345
10 changed files with 115 additions and 15 deletions

View File

@ -231,7 +231,7 @@ test-suite graphql-engine-test
, aeson
, aeson-casing
, bytestring
--, directory
-- , directory
--, fgl
, filepath
, hspec

View File

@ -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
@ -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
@ -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 ()

View File

@ -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]
@ -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
@ -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
@ -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

View File

@ -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

View File

@ -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

View File

@ -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
)

View File

@ -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
}
}

View File

@ -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
}
]
) {

View File

@ -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
}
}
}

View File

@ -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
}
}