2022-03-31 07:45:03 +03:00
|
|
|
{-# LANGUAGE OverloadedLists #-}
|
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
|
2022-05-02 08:03:12 +03:00
|
|
|
module Hasura.Backends.DataConnector.API.V0.QuerySpec (spec) where
|
2022-03-31 07:45:03 +03:00
|
|
|
|
|
|
|
import Autodocodec.Extended
|
|
|
|
import Data.Aeson.QQ.Simple (aesonQQ)
|
|
|
|
import Data.HashMap.Strict qualified as Map
|
2022-05-02 08:03:12 +03:00
|
|
|
import Hasura.Backends.DataConnector.API.V0.API
|
|
|
|
import Hasura.Backends.DataConnector.API.V0.ColumnSpec (genColumnName)
|
2022-03-31 07:45:03 +03:00
|
|
|
import Hasura.Prelude
|
|
|
|
import Hedgehog
|
2022-04-01 04:20:23 +03:00
|
|
|
import Hedgehog.Gen qualified as Gen
|
|
|
|
import Hedgehog.Range
|
2022-03-31 07:45:03 +03:00
|
|
|
import Test.Aeson.Utils
|
|
|
|
import Test.Hspec
|
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec = do
|
|
|
|
describe "PrimaryKey" $ do
|
|
|
|
testToFromJSONToSchema (PrimaryKey $ ColumnName "my_primary_key") [aesonQQ|"my_primary_key"|]
|
|
|
|
jsonOpenApiProperties genPrimaryKey
|
|
|
|
describe "ForeignKey" $
|
|
|
|
testToFromJSONToSchema (ForeignKey $ ColumnName "my_foreign_key") [aesonQQ|"my_foreign_key"|]
|
|
|
|
describe "Field" $ do
|
|
|
|
describe "ColumnField" $
|
|
|
|
testToFromJSONToSchema
|
|
|
|
(ColumnField (ValueWrapper $ ColumnName "my_column_name"))
|
|
|
|
[aesonQQ|
|
|
|
|
{ "type": "column",
|
|
|
|
"column": "my_column_name"
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
describe "RelationshipField" $ do
|
|
|
|
let fieldMapping = Map.fromList [(PrimaryKey $ ColumnName "id", ForeignKey $ ColumnName "my_foreign_id")]
|
|
|
|
query = Query mempty (TableName "my_table_name") Nothing Nothing Nothing Nothing
|
|
|
|
testToFromJSONToSchema
|
2022-06-07 04:34:37 +03:00
|
|
|
(RelationshipField $ RelField fieldMapping ArrayRelationship query)
|
2022-03-31 07:45:03 +03:00
|
|
|
[aesonQQ|
|
|
|
|
{ "type": "relationship",
|
2022-06-07 04:34:37 +03:00
|
|
|
"relation_type": "array",
|
2022-04-10 07:47:15 +03:00
|
|
|
"column_mapping": {"id": "my_foreign_id"},
|
2022-03-31 07:45:03 +03:00
|
|
|
"query": {"fields": {}, "from": "my_table_name"}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
describe "Query" $ do
|
|
|
|
let query =
|
|
|
|
Query
|
|
|
|
{ fields = Map.fromList [("my_field_alias", ColumnField $ ValueWrapper $ ColumnName "my_field_name")],
|
|
|
|
from = TableName "my_table_name",
|
|
|
|
limit = Just 10,
|
|
|
|
offset = Just 20,
|
2022-06-02 05:06:45 +03:00
|
|
|
where_ = Just . And $ ValueWrapper [],
|
2022-03-31 07:45:03 +03:00
|
|
|
orderBy = Just [OrderBy (ColumnName "my_column_name") Ascending]
|
|
|
|
}
|
|
|
|
testToFromJSONToSchema
|
|
|
|
query
|
|
|
|
[aesonQQ|
|
|
|
|
{ "fields": {"my_field_alias": {"type": "column", "column": "my_field_name"}},
|
|
|
|
"from": "my_table_name",
|
|
|
|
"limit": 10,
|
|
|
|
"offset": 20,
|
2022-06-02 05:06:45 +03:00
|
|
|
"where": {"type": "and", "expressions": []},
|
2022-03-31 07:45:03 +03:00
|
|
|
"order_by": [{"column": "my_column_name", "ordering": "asc"}]
|
|
|
|
}
|
|
|
|
|]
|
2022-04-01 04:20:23 +03:00
|
|
|
describe "QueryResponse" $ do
|
|
|
|
testToFromJSONToSchema (QueryResponse []) [aesonQQ|[]|]
|
|
|
|
jsonOpenApiProperties genQueryResponse
|
2022-03-31 07:45:03 +03:00
|
|
|
|
|
|
|
genPrimaryKey :: MonadGen m => m PrimaryKey
|
|
|
|
genPrimaryKey = PrimaryKey <$> genColumnName
|
2022-04-01 04:20:23 +03:00
|
|
|
|
|
|
|
genQueryResponse :: MonadGen m => m QueryResponse
|
|
|
|
genQueryResponse =
|
|
|
|
QueryResponse <$> Gen.list (linear 0 5) genObject
|