2022-07-19 04:51:42 +03:00
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
|
|
|
|
-- | Configuration Transformation Tests for Data Connector Backend using a Mock Agent
|
|
|
|
module Test.DataConnector.MockAgent.TransformedConfigurationSpec
|
|
|
|
( spec,
|
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
import Data.Aeson qualified as Aeson
|
|
|
|
import Data.Aeson.KeyMap qualified as KM
|
2022-08-11 18:03:04 +03:00
|
|
|
import Data.List.NonEmpty qualified as NE
|
2022-07-19 04:51:42 +03:00
|
|
|
import Harness.Backend.DataConnector (TestCase (..))
|
|
|
|
import Harness.Backend.DataConnector qualified as DataConnector
|
|
|
|
import Harness.Quoter.Graphql (graphql)
|
2022-07-20 08:20:49 +03:00
|
|
|
import Harness.Quoter.Yaml (yaml)
|
2022-07-19 04:51:42 +03:00
|
|
|
import Harness.Test.BackendType (BackendType (..), defaultBackendTypeString, defaultSource)
|
2022-08-10 10:52:57 +03:00
|
|
|
import Harness.Test.Fixture qualified as Fixture
|
2022-07-19 04:51:42 +03:00
|
|
|
import Harness.TestEnvironment (TestEnvironment)
|
|
|
|
import Hasura.Backends.DataConnector.API qualified as API
|
2022-08-03 17:18:43 +03:00
|
|
|
import Hasura.Prelude
|
2022-07-19 04:51:42 +03:00
|
|
|
import Test.Hspec (SpecWith, describe, it)
|
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
spec :: SpecWith TestEnvironment
|
|
|
|
spec =
|
2022-08-10 10:52:57 +03:00
|
|
|
Fixture.runWithLocalTestEnvironment
|
2022-08-11 18:03:04 +03:00
|
|
|
( NE.fromList
|
|
|
|
[ (Fixture.fixture $ Fixture.Backend Fixture.DataConnector)
|
|
|
|
{ Fixture.mkLocalTestEnvironment = DataConnector.mkLocalTestEnvironmentMock,
|
|
|
|
Fixture.setupTeardown = \(testEnv, mockEnv) ->
|
|
|
|
[ DataConnector.setupMockAction
|
|
|
|
sourceMetadata
|
|
|
|
DataConnector.mockBackendConfig
|
|
|
|
(testEnv, mockEnv)
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
2022-08-03 17:18:43 +03:00
|
|
|
)
|
2022-07-19 04:51:42 +03:00
|
|
|
tests
|
|
|
|
|
|
|
|
sourceMetadata :: Aeson.Value
|
|
|
|
sourceMetadata =
|
|
|
|
let source = defaultSource DataConnector
|
|
|
|
backendType = defaultBackendTypeString DataConnector
|
|
|
|
in [yaml|
|
2022-07-20 08:20:49 +03:00
|
|
|
name : *source
|
|
|
|
kind: *backendType
|
|
|
|
tables:
|
2022-08-04 11:34:45 +03:00
|
|
|
- table: [Album]
|
2022-07-20 08:20:49 +03:00
|
|
|
configuration:
|
|
|
|
custom_root_fields:
|
|
|
|
select: albums
|
|
|
|
select_by_pk: albums_by_pk
|
|
|
|
column_config:
|
|
|
|
AlbumId:
|
|
|
|
custom_name: id
|
|
|
|
Title:
|
|
|
|
custom_name: title
|
|
|
|
ArtistId:
|
|
|
|
custom_name: artist_id
|
|
|
|
object_relationships:
|
|
|
|
- name: artist
|
|
|
|
using:
|
|
|
|
manual_configuration:
|
2022-08-04 11:34:45 +03:00
|
|
|
remote_table: [Artist]
|
2022-07-20 08:20:49 +03:00
|
|
|
column_mapping:
|
|
|
|
ArtistId: ArtistId
|
2022-08-04 11:34:45 +03:00
|
|
|
- table: [Artist]
|
2022-07-20 08:20:49 +03:00
|
|
|
configuration:
|
|
|
|
custom_root_fields:
|
|
|
|
select: artists
|
|
|
|
select_by_pk: artists_by_pk
|
|
|
|
column_config:
|
|
|
|
ArtistId:
|
|
|
|
custom_name: id
|
|
|
|
Name:
|
|
|
|
custom_name: name
|
|
|
|
array_relationships:
|
|
|
|
- name: albums
|
|
|
|
using:
|
|
|
|
manual_configuration:
|
2022-08-04 11:34:45 +03:00
|
|
|
remote_table: [Album]
|
2022-07-20 08:20:49 +03:00
|
|
|
column_mapping:
|
|
|
|
ArtistId: ArtistId
|
|
|
|
configuration:
|
|
|
|
value: {}
|
|
|
|
template: |
|
|
|
|
{
|
|
|
|
"DEBUG": {
|
|
|
|
"session": {{ $session?.foo ?? "foo session default" }},
|
|
|
|
"env": {{ $env?.bar ?? "bar env default" }},
|
|
|
|
"config": {{ $config?.baz ?? "baz config default" }}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
2022-07-19 04:51:42 +03:00
|
|
|
|
|
|
|
--------------------------------------------------------------------------------
|
|
|
|
|
2022-08-10 10:52:57 +03:00
|
|
|
tests :: Fixture.Options -> SpecWith (TestEnvironment, DataConnector.MockAgentEnvironment)
|
2022-07-19 04:51:42 +03:00
|
|
|
tests opts = do
|
|
|
|
describe "Basic Tests" $ do
|
|
|
|
it "works with configuration transformation Kriti template" $
|
|
|
|
DataConnector.runMockedTest opts $
|
|
|
|
let required =
|
|
|
|
DataConnector.TestCaseRequired
|
|
|
|
{ _givenRequired =
|
|
|
|
let albums =
|
2022-07-26 05:28:57 +03:00
|
|
|
[ [ ("id", API.mkColumnFieldValue $ Aeson.Number 1),
|
|
|
|
("title", API.mkColumnFieldValue $ Aeson.String "For Those About To Rock We Salute You")
|
2022-07-20 08:20:49 +03:00
|
|
|
]
|
|
|
|
]
|
|
|
|
in DataConnector.chinookMock {DataConnector._queryResponse = \_ -> rowsResponse albums},
|
2022-07-19 04:51:42 +03:00
|
|
|
_whenRequestRequired =
|
|
|
|
[graphql|
|
2022-07-20 08:20:49 +03:00
|
|
|
query getAlbum {
|
|
|
|
albums(limit: 1) {
|
|
|
|
id
|
|
|
|
title
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|],
|
2022-07-19 04:51:42 +03:00
|
|
|
_thenRequired =
|
|
|
|
[yaml|
|
2022-07-20 08:20:49 +03:00
|
|
|
data:
|
|
|
|
albums:
|
|
|
|
- id: 1
|
|
|
|
title: For Those About To Rock We Salute You
|
|
|
|
|]
|
2022-07-19 04:51:42 +03:00
|
|
|
}
|
|
|
|
in (DataConnector.defaultTestCase required)
|
|
|
|
{ _whenQuery =
|
|
|
|
Just
|
|
|
|
( API.QueryRequest
|
2022-08-04 11:34:45 +03:00
|
|
|
{ _qrTable = API.TableName ("Album" :| []),
|
2022-07-19 04:51:42 +03:00
|
|
|
_qrTableRelationships = [],
|
|
|
|
_qrQuery =
|
|
|
|
API.Query
|
|
|
|
{ _qFields =
|
2022-07-20 08:20:49 +03:00
|
|
|
Just $
|
|
|
|
KM.fromList
|
2022-07-27 08:27:34 +03:00
|
|
|
[ ("id", API.ColumnField (API.ColumnName "AlbumId")),
|
|
|
|
("title", API.ColumnField (API.ColumnName "Title"))
|
2022-07-20 08:20:49 +03:00
|
|
|
],
|
|
|
|
_qAggregates = Nothing,
|
2022-07-19 04:51:42 +03:00
|
|
|
_qLimit = Just 1,
|
|
|
|
_qOffset = Nothing,
|
2022-08-22 07:22:07 +03:00
|
|
|
_qWhere = Nothing,
|
2022-07-19 04:51:42 +03:00
|
|
|
_qOrderBy = Nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
),
|
|
|
|
_whenConfig =
|
|
|
|
-- TODO: Create a QQ for this purpose.
|
|
|
|
let conf =
|
|
|
|
Aeson.fromJSON
|
|
|
|
[yaml|
|
2022-07-20 08:20:49 +03:00
|
|
|
DEBUG:
|
|
|
|
config: "baz config default"
|
|
|
|
env: "bar env default"
|
|
|
|
session: "foo session default"
|
|
|
|
|]
|
2022-07-19 04:51:42 +03:00
|
|
|
in case conf of
|
|
|
|
Aeson.Success r -> Just r
|
|
|
|
_ -> error "Should parse."
|
|
|
|
}
|
2022-07-20 08:20:49 +03:00
|
|
|
|
|
|
|
rowsResponse :: [[(Aeson.Key, API.FieldValue)]] -> API.QueryResponse
|
|
|
|
rowsResponse rows = API.QueryResponse (Just $ KM.fromList <$> rows) Nothing
|