chore(server/tests): use more Logical Model test helpers

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8348
GitOrigin-RevId: c9e168125696993a39ea816459c588108a880710
This commit is contained in:
Daniel Harvey 2023-03-16 15:00:11 +00:00 committed by hasura-bot
parent 97d655240c
commit 01636a9cfb
2 changed files with 126 additions and 171 deletions

View File

@ -65,25 +65,25 @@ testAdminAccess opts = do
query = "SELECT thing / {{denominator}} AS divided FROM stuff WHERE date = {{target_date}}"
describe "Admin access" do
let dividedStuffLogicalModel :: Schema.LogicalModel
dividedStuffLogicalModel =
(Schema.logicalModel "divided_stuff" query)
{ Schema.logicalModelColumns =
[ (Schema.logicalModelColumn "divided" "integer")
{ Schema.logicalModelColumnDescription = Just "a divided thing"
}
],
Schema.logicalModelArguments =
[ Schema.logicalModelColumn "denominator" "integer",
Schema.logicalModelColumn "target_date" "date"
]
}
it "Fails to track a Logical Model without admin access" $
\testEnvironment -> do
let backendTypeMetadata = fromMaybe (error "Unknown backend") $ getBackendTypeConfig testEnvironment
sourceName = BackendType.backendSourceName backendTypeMetadata
dividedStuffLogicalModel :: Schema.LogicalModel
dividedStuffLogicalModel =
(Schema.logicalModel "divided_stuff" query)
{ Schema.logicalModelColumns =
[ (Schema.logicalModelColumn "divided" "integer")
{ Schema.logicalModelColumnDescription = Just "a divided thing"
}
],
Schema.logicalModelArguments =
[ Schema.logicalModelColumn "denominator" "integer",
Schema.logicalModelColumn "target_date" "date"
]
}
shouldReturnYaml
opts
( GraphqlEngine.postMetadataWithStatusAndHeaders
@ -91,7 +91,7 @@ testAdminAccess opts = do
testEnvironment
[ ("X-Hasura-Role", "not-admin")
]
(Schema.logicalModelToJson sourceName dividedStuffLogicalModel)
(Schema.trackLogicalModelCommand sourceName backendTypeMetadata dividedStuffLogicalModel)
)
[yaml|
code: access-denied
@ -111,12 +111,7 @@ testAdminAccess opts = do
testEnvironment
[ ("X-Hasura-Role", "not-admin")
]
[yaml|
type: pg_untrack_logical_model
args:
root_field_name: divided_stuff
source: *sourceName
|]
(Schema.untrackLogicalModelCommand sourceName backendTypeMetadata dividedStuffLogicalModel)
)
[yaml|
code: access-denied
@ -201,7 +196,7 @@ testImplementation opts = do
( GraphqlEngine.postMetadataWithStatus
400
testEnvironment
(Schema.logicalModelToJson sourceName brokenQueryLogicalModel)
(Schema.trackLogicalModelCommand sourceName backendTypeMetadata brokenQueryLogicalModel)
)
[yaml|
code: parse-failed
@ -280,20 +275,7 @@ testImplementation opts = do
Schema.trackLogicalModel sourceName dividedStuffLogicalModel testEnvironment
shouldReturnYaml
opts
( GraphqlEngine.postMetadata
testEnvironment
[yaml|
type: pg_untrack_logical_model
args:
source: *sourceName
root_field_name: divided_stuff
|]
)
[yaml|
message: success
|]
Schema.untrackLogicalModel sourceName dividedStuffLogicalModel testEnvironment
it "Checks the logical model of a function can be deleted" $ \testEnvironment -> do
let backendTypeMetadata = fromMaybe (error "Unknown backend") $ getBackendTypeConfig testEnvironment
@ -315,15 +297,7 @@ testImplementation opts = do
Schema.trackLogicalModel sourceName dividedStuffLogicalModel testEnvironment
_ <-
GraphqlEngine.postMetadata
testEnvironment
[yaml|
type: pg_untrack_logical_model
args:
root_field_name: divided_stuff
source: *sourceName
|]
Schema.untrackLogicalModel sourceName dividedStuffLogicalModel testEnvironment
shouldReturnYaml
opts
@ -440,17 +414,15 @@ testValidation opts = do
let backendTypeMetadata = fromMaybe (error "Unknown backend") $ getBackendTypeConfig testEnvironment
sourceName = BackendType.backendSourceName backendTypeMetadata
nonExistentLogicalModel :: Schema.LogicalModel
nonExistentLogicalModel = Schema.logicalModel "some_logical_model" ""
shouldReturnYaml
opts
( GraphqlEngine.postMetadataWithStatus
400
testEnvironment
[yaml|
type: pg_untrack_logical_model
args:
root_field_name: some_logical_model
source: *sourceName
|]
(Schema.untrackLogicalModelCommand sourceName backendTypeMetadata nonExistentLogicalModel)
)
[yaml|
code: not-found
@ -492,7 +464,7 @@ testValidation opts = do
GraphqlEngine.postMetadataWithStatus
400
testEnvironment
(Schema.logicalModelToJson sourceName syntaxErrorLogicalModel)
(Schema.trackLogicalModelCommand sourceName backendTypeMetadata syntaxErrorLogicalModel)
actual `shouldAtLeastBe` expected
@ -532,7 +504,7 @@ testValidation opts = do
GraphqlEngine.postMetadataWithStatus
400
testEnvironment
(Schema.logicalModelToJson sourceName brokenLogicalModel)
(Schema.trackLogicalModelCommand sourceName backendTypeMetadata brokenLogicalModel)
actual `shouldAtLeastBe` expected
@ -563,7 +535,7 @@ testValidation opts = do
( GraphqlEngine.postMetadataWithStatus
500
testEnv
(Schema.logicalModelToJson source conflictingLogicalModel)
(Schema.trackLogicalModelCommand source backendTypeMetadata conflictingLogicalModel)
)
[yaml|
code: unexpected
@ -597,7 +569,7 @@ testValidation opts = do
opts
( GraphqlEngine.postMetadata
testEnv
(Schema.logicalModelToJson source conflictingLogicalModel)
(Schema.trackLogicalModelCommand source backendTypeMetadata conflictingLogicalModel)
)
[yaml|
message: success
@ -608,7 +580,7 @@ testValidation opts = do
( GraphqlEngine.postMetadataWithStatus
400
testEnv
(Schema.logicalModelToJson source conflictingLogicalModel)
(Schema.trackLogicalModelCommand source backendTypeMetadata conflictingLogicalModel)
)
[yaml|
code: already-tracked
@ -621,6 +593,19 @@ testValidation opts = do
let backendTypeMetadata = fromMaybe (error "Unknown backend") $ getBackendTypeConfig testEnvironment
sourceName = BackendType.backendSourceName backendTypeMetadata
query = "SELECT 10 / {{denominator}} AS divided"
brokenTypesLogicalModel :: Schema.LogicalModel
brokenTypesLogicalModel =
(Schema.logicalModel "divided_falling" query)
{ Schema.logicalModelColumns =
[ Schema.logicalModelColumn "divided" "integer"
],
Schema.logicalModelArguments =
[ Schema.logicalModelColumn "denominator" "varchar"
]
}
let expected =
[yaml|
code: validation-failed
@ -631,22 +616,7 @@ testValidation opts = do
GraphqlEngine.postMetadataWithStatus
400
testEnvironment
[yaml|
type: pg_track_logical_model
args:
type: query
source: *sourceName
root_field_name: divided_failing
code: |
SELECT 10 / {{denominator}} AS divided
arguments:
denominator:
type: varchar
returns:
columns:
divided:
type: integer
|]
(Schema.trackLogicalModelCommand sourceName backendTypeMetadata brokenTypesLogicalModel)
actual `shouldAtLeastBe` expected
@ -688,24 +658,24 @@ testValidation opts = do
let backendTypeMetadata = fromMaybe (error "Unknown backend") $ getBackendTypeConfig testEnvironment
sourceName = BackendType.backendSourceName backendTypeMetadata
query = "SELECT 10 / {{denominator}} AS divided"
missingArgsLogicalModel :: Schema.LogicalModel
missingArgsLogicalModel =
(Schema.logicalModel "divided_falling" query)
{ Schema.logicalModelColumns =
[ Schema.logicalModelColumn "divided" "integer"
],
Schema.logicalModelArguments =
[]
}
shouldReturnYaml
opts
( GraphqlEngine.postMetadataWithStatus
400
testEnvironment
[yaml|
type: pg_track_logical_model
args:
type: query
source: *sourceName
root_field_name: divided_failing
code: |
SELECT 10 / {{denominator}} AS divided
returns:
columns:
divided:
type: integer
|]
(Schema.trackLogicalModelCommand sourceName backendTypeMetadata missingArgsLogicalModel)
)
[yaml|
code: validation-failed
@ -719,54 +689,23 @@ testValidation opts = do
let backendTypeMetadata = fromMaybe (error "Unknown backend") $ getBackendTypeConfig testEnvironment
sourceName = BackendType.backendSourceName backendTypeMetadata
shouldReturnYaml
opts
( GraphqlEngine.postMetadata
testEnvironment
[yaml|
type: bulk
args:
- type: pg_track_logical_model
args:
type: query
source: *sourceName
root_field_name: divided_stuff2
code: *simpleQuery
arguments:
denominator:
type: integer
target_date:
type: date
returns:
columns:
divided:
type: integer
- type: pg_untrack_logical_model
args:
root_field_name: divided_stuff2
source: *sourceName
- type: pg_track_logical_model
args:
type: query
source: *sourceName
root_field_name: divided_stuff2
code: *simpleQuery
arguments:
denominator:
type: integer
target_date:
type: date
returns:
columns:
divided:
type: integer
|]
)
[yaml|
- message: success
- message: success
- message: success
|]
dividedStuffLogicalModel :: Schema.LogicalModel
dividedStuffLogicalModel =
(Schema.logicalModel "divided_stuff2" simpleQuery)
{ Schema.logicalModelColumns =
[ Schema.logicalModelColumn "divided" "integer"
],
Schema.logicalModelArguments =
[ Schema.logicalModelColumn "denominator" "integer",
Schema.logicalModelColumn "target_date" "date"
]
}
Schema.trackLogicalModel sourceName dividedStuffLogicalModel testEnvironment
Schema.untrackLogicalModel sourceName dividedStuffLogicalModel testEnvironment
Schema.trackLogicalModel sourceName dividedStuffLogicalModel testEnvironment
----------------------
-- Test permissions --
@ -778,10 +717,25 @@ testPermissions opts = do
simpleQuery = "SELECT thing / 2 AS divided FROM stuff"
describe "Permissions" do
let dividedStuffLogicalModel :: Schema.LogicalModel
dividedStuffLogicalModel =
(Schema.logicalModel "divided_stuff" simpleQuery)
{ Schema.logicalModelColumns =
[ (Schema.logicalModelColumn "divided" "integer")
{ Schema.logicalModelColumnDescription = Just "a divided thing"
}
],
Schema.logicalModelArguments =
[ Schema.logicalModelColumn "unused" "integer"
]
}
it "Adds a simple logical model function with no arguments a select permission and returns a 200" $ \testEnvironment -> do
let backendTypeMetadata = fromMaybe (error "Unknown backend") $ getBackendTypeConfig testEnvironment
sourceName = BackendType.backendSourceName backendTypeMetadata
Schema.trackLogicalModel sourceName dividedStuffLogicalModel testEnvironment
shouldReturnYaml
opts
( GraphqlEngine.postMetadata
@ -789,20 +743,6 @@ testPermissions opts = do
[yaml|
type: bulk
args:
- type: pg_track_logical_model
args:
type: query
source: *sourceName
root_field_name: divided_stuff
code: *simpleQuery
arguments:
unused:
type: integer
returns:
columns:
divided:
type: integer
description: "a divided thing"
- type: pg_create_logical_model_select_permission
args:
source: *sourceName
@ -816,7 +756,6 @@ testPermissions opts = do
)
[yaml|
- message: success
- message: success
|]
shouldReturnYaml
@ -851,12 +790,14 @@ testPermissions opts = do
|]
it "Adds a logical model, removes it, and returns 200" $ \testEnvironment -> do
let rootfield :: String
rootfield = "divided_stuff1231"
let rootfield :: Text
rootfield = Schema.logicalModelName dividedStuffLogicalModel
backendTypeMetadata = fromMaybe (error "Unknown backend") $ getBackendTypeConfig testEnvironment
sourceName = BackendType.backendSourceName backendTypeMetadata
Schema.trackLogicalModel sourceName dividedStuffLogicalModel testEnvironment
shouldReturnYaml
opts
( GraphqlEngine.postMetadata
@ -864,20 +805,6 @@ testPermissions opts = do
[yaml|
type: bulk
args:
- type: pg_track_logical_model
args:
type: query
source: *sourceName
root_field_name: *rootfield
code: *simpleQuery
arguments:
unused:
type: integer
returns:
columns:
divided:
type: integer
description: "a divided thing"
- type: pg_create_logical_model_select_permission
args:
source: *sourceName
@ -897,7 +824,6 @@ testPermissions opts = do
[yaml|
- message: success
- message: success
- message: success
|]
shouldReturnYaml

View File

@ -24,7 +24,8 @@ module Harness.Test.Schema
logicalModel,
LogicalModelColumn (..),
logicalModelColumn,
logicalModelToJson,
trackLogicalModelCommand,
untrackLogicalModelCommand,
resolveTableSchema,
resolveReferenceSchema,
quotedValue,
@ -53,6 +54,7 @@ module Harness.Test.Schema
runSQL,
addSource,
trackLogicalModel,
untrackLogicalModel,
)
where
@ -739,8 +741,8 @@ addSource sourceName sourceConfig testEnvironment = do
configuration: #{ sourceConfig }
|]
logicalModelToJson :: String -> LogicalModel -> Value
logicalModelToJson sourceName (LogicalModel {logicalModelReturnTypeDescription, logicalModelName, logicalModelArguments, logicalModelQuery, logicalModelColumns}) =
trackLogicalModelCommand :: String -> BackendTypeConfig -> LogicalModel -> Value
trackLogicalModelCommand sourceName backendTypeConfig (LogicalModel {logicalModelReturnTypeDescription, logicalModelName, logicalModelArguments, logicalModelQuery, logicalModelColumns}) =
let columnsToJson =
Aeson.object
. fmap
@ -770,8 +772,12 @@ logicalModelToJson sourceName (LogicalModel {logicalModelReturnTypeDescription,
returns =
Aeson.object $
[(K.fromText "columns", columns)] <> returnTypePair
backendType = BackendType.backendTypeString backendTypeConfig
requestType = backendType <> "_track_logical_model"
in [yaml|
type: pg_track_logical_model
type: *requestType
args:
type: query
source: *sourceName
@ -783,6 +789,29 @@ logicalModelToJson sourceName (LogicalModel {logicalModelReturnTypeDescription,
trackLogicalModel :: HasCallStack => String -> LogicalModel -> TestEnvironment -> IO ()
trackLogicalModel sourceName logMod testEnvironment = do
let json = logicalModelToJson sourceName logMod
let backendTypeMetadata = fromMaybe (error "Unknown backend") $ getBackendTypeConfig testEnvironment
GraphqlEngine.postMetadata_ testEnvironment json
let command = trackLogicalModelCommand sourceName backendTypeMetadata logMod
GraphqlEngine.postMetadata_ testEnvironment command
untrackLogicalModelCommand :: String -> BackendTypeConfig -> LogicalModel -> Value
untrackLogicalModelCommand source backendTypeMetadata LogicalModel {logicalModelName} =
let backendType = BackendType.backendTypeString backendTypeMetadata
requestType = backendType <> "_untrack_logical_model"
in [yaml|
type: *requestType
args:
source: *source
root_field_name: *logicalModelName
|]
untrackLogicalModel :: HasCallStack => String -> LogicalModel -> TestEnvironment -> IO ()
untrackLogicalModel source logMod testEnvironment = do
let backendTypeMetadata = fromMaybe (error "Unknown backend") $ getBackendTypeConfig testEnvironment
let command = untrackLogicalModelCommand source backendTypeMetadata logMod
GraphqlEngine.postMetadata_
testEnvironment
command