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
|
|
|
|
2022-07-26 05:28:57 +03:00
|
|
|
import Data.Aeson qualified as J
|
2022-06-08 18:31:28 +03:00
|
|
|
import Data.Aeson.KeyMap qualified as KM
|
2022-03-31 07:45:03 +03:00
|
|
|
import Data.Aeson.QQ.Simple (aesonQQ)
|
2022-06-24 09:58:25 +03:00
|
|
|
import Hasura.Backends.DataConnector.API.V0
|
2022-07-20 08:20:49 +03:00
|
|
|
import Hasura.Backends.DataConnector.API.V0.AggregateSpec (genAggregate)
|
|
|
|
import Hasura.Backends.DataConnector.API.V0.ColumnSpec (genColumnName)
|
|
|
|
import Hasura.Backends.DataConnector.API.V0.ExpressionSpec (genExpression)
|
|
|
|
import Hasura.Backends.DataConnector.API.V0.OrderBySpec (genOrderBy)
|
|
|
|
import Hasura.Backends.DataConnector.API.V0.RelationshipsSpec (genRelationshipName, genTableRelationships)
|
|
|
|
import Hasura.Backends.DataConnector.API.V0.TableSpec (genTableName)
|
2022-09-06 07:24:46 +03:00
|
|
|
import Hasura.Generator.Common (defaultRange)
|
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
|
2022-07-26 05:28:57 +03:00
|
|
|
import Test.Aeson.Utils (genKeyMap, genValue, jsonOpenApiProperties, testToFromJSONToSchema)
|
2022-03-31 07:45:03 +03:00
|
|
|
import Test.Hspec
|
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec = do
|
|
|
|
describe "Field" $ do
|
|
|
|
describe "ColumnField" $
|
|
|
|
testToFromJSONToSchema
|
2022-07-27 08:27:34 +03:00
|
|
|
(ColumnField $ ColumnName "my_column_name")
|
2022-03-31 07:45:03 +03:00
|
|
|
[aesonQQ|
|
|
|
|
{ "type": "column",
|
|
|
|
"column": "my_column_name"
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
describe "RelationshipField" $ do
|
2022-07-20 08:20:49 +03:00
|
|
|
let query = Query (Just mempty) Nothing Nothing Nothing Nothing Nothing
|
2022-03-31 07:45:03 +03:00
|
|
|
testToFromJSONToSchema
|
2022-06-24 09:58:25 +03:00
|
|
|
(RelField $ RelationshipField (RelationshipName "a_relationship") query)
|
2022-03-31 07:45:03 +03:00
|
|
|
[aesonQQ|
|
|
|
|
{ "type": "relationship",
|
2022-06-24 09:58:25 +03:00
|
|
|
"relationship": "a_relationship",
|
|
|
|
"query": {"fields": {}}
|
2022-03-31 07:45:03 +03:00
|
|
|
}
|
|
|
|
|]
|
2022-07-20 08:20:49 +03:00
|
|
|
jsonOpenApiProperties genField
|
|
|
|
|
2022-03-31 07:45:03 +03:00
|
|
|
describe "Query" $ do
|
|
|
|
let query =
|
|
|
|
Query
|
2022-07-27 08:27:34 +03:00
|
|
|
{ _qFields = Just $ KM.fromList [("my_field_alias", ColumnField $ ColumnName "my_field_name")],
|
2022-07-20 08:20:49 +03:00
|
|
|
_qAggregates = Just $ KM.fromList [("my_aggregate", StarCount)],
|
2022-06-24 09:58:25 +03:00
|
|
|
_qLimit = Just 10,
|
|
|
|
_qOffset = Just 20,
|
2022-07-27 08:27:34 +03:00
|
|
|
_qWhere = Just $ And [],
|
2022-08-19 10:00:46 +03:00
|
|
|
_qOrderBy = Just $ OrderBy [] (OrderByElement [] (OrderByColumn (ColumnName "my_column_name")) Ascending :| [])
|
2022-03-31 07:45:03 +03:00
|
|
|
}
|
|
|
|
testToFromJSONToSchema
|
|
|
|
query
|
|
|
|
[aesonQQ|
|
|
|
|
{ "fields": {"my_field_alias": {"type": "column", "column": "my_field_name"}},
|
2022-07-20 08:20:49 +03:00
|
|
|
"aggregates": { "my_aggregate": { "type": "star_count" } },
|
2022-03-31 07:45:03 +03:00
|
|
|
"limit": 10,
|
|
|
|
"offset": 20,
|
2022-06-02 05:06:45 +03:00
|
|
|
"where": {"type": "and", "expressions": []},
|
2022-08-19 10:00:46 +03:00
|
|
|
"order_by": {
|
|
|
|
"relations": {},
|
|
|
|
"elements": [
|
|
|
|
{ "target_path": [],
|
|
|
|
"target": {
|
|
|
|
"type": "column",
|
|
|
|
"column": "my_column_name"
|
|
|
|
},
|
|
|
|
"order_direction": "asc"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2022-03-31 07:45:03 +03:00
|
|
|
}
|
|
|
|
|]
|
2022-07-20 08:20:49 +03:00
|
|
|
jsonOpenApiProperties genQuery
|
|
|
|
|
2022-06-24 09:58:25 +03:00
|
|
|
describe "QueryRequest" $ do
|
|
|
|
let queryRequest =
|
|
|
|
QueryRequest
|
2022-08-04 11:34:45 +03:00
|
|
|
{ _qrTable = TableName ["my_table"],
|
2022-06-24 09:58:25 +03:00
|
|
|
_qrTableRelationships = [],
|
2022-07-20 08:20:49 +03:00
|
|
|
_qrQuery = Query (Just mempty) Nothing Nothing Nothing Nothing Nothing
|
2022-06-24 09:58:25 +03:00
|
|
|
}
|
|
|
|
testToFromJSONToSchema
|
|
|
|
queryRequest
|
|
|
|
[aesonQQ|
|
2022-08-04 11:34:45 +03:00
|
|
|
{ "table": ["my_table"],
|
2022-06-24 09:58:25 +03:00
|
|
|
"table_relationships": [],
|
|
|
|
"query": { "fields": {} } }
|
|
|
|
|]
|
2022-07-20 08:20:49 +03:00
|
|
|
jsonOpenApiProperties genQueryRequest
|
2022-06-24 09:58:25 +03:00
|
|
|
|
2022-04-01 04:20:23 +03:00
|
|
|
describe "QueryResponse" $ do
|
2022-07-20 08:20:49 +03:00
|
|
|
testToFromJSONToSchema
|
|
|
|
(QueryResponse (Just []) (Just mempty))
|
|
|
|
[aesonQQ|
|
|
|
|
{ "rows": [],
|
|
|
|
"aggregates": {} }
|
|
|
|
|]
|
2022-04-01 04:20:23 +03:00
|
|
|
jsonOpenApiProperties genQueryResponse
|
2022-03-31 07:45:03 +03:00
|
|
|
|
2022-07-26 05:28:57 +03:00
|
|
|
describe "FieldValue" $ do
|
|
|
|
describe "ColumnFieldValue - Object" $ do
|
|
|
|
testToFromJSONToSchema
|
|
|
|
(mkColumnFieldValue $ J.Object [("property", "Wow")])
|
|
|
|
[aesonQQ|
|
|
|
|
{ "property": "Wow" }
|
|
|
|
|]
|
|
|
|
describe "ColumnFieldValue - String" $ do
|
|
|
|
testToFromJSONToSchema
|
|
|
|
(mkColumnFieldValue $ J.String "Test")
|
|
|
|
[aesonQQ|
|
|
|
|
"Test"
|
|
|
|
|]
|
|
|
|
describe "ColumnFieldValue - Number" $ do
|
|
|
|
testToFromJSONToSchema
|
|
|
|
(mkColumnFieldValue $ J.Number 123)
|
|
|
|
[aesonQQ|
|
|
|
|
123
|
|
|
|
|]
|
|
|
|
describe "ColumnFieldValue - Bool" $ do
|
|
|
|
testToFromJSONToSchema
|
|
|
|
(mkColumnFieldValue $ J.Bool True)
|
|
|
|
[aesonQQ|
|
|
|
|
true
|
|
|
|
|]
|
|
|
|
describe "ColumnFieldValue - Null" $ do
|
|
|
|
testToFromJSONToSchema
|
|
|
|
(mkColumnFieldValue J.Null)
|
|
|
|
[aesonQQ|
|
|
|
|
null
|
|
|
|
|]
|
|
|
|
describe "RelationshipFieldValue" $ do
|
|
|
|
testToFromJSONToSchema
|
|
|
|
(mkRelationshipFieldValue (QueryResponse (Just []) (Just mempty)))
|
|
|
|
[aesonQQ|
|
|
|
|
{ "rows": [],
|
|
|
|
"aggregates": {} }
|
|
|
|
|]
|
|
|
|
|
2022-07-20 08:20:49 +03:00
|
|
|
genField :: MonadGen m => m Field
|
|
|
|
genField =
|
|
|
|
Gen.recursive
|
|
|
|
Gen.choice
|
2022-07-27 08:27:34 +03:00
|
|
|
[ColumnField <$> genColumnName]
|
2022-07-20 08:20:49 +03:00
|
|
|
[RelField <$> genRelationshipField]
|
|
|
|
|
|
|
|
genRelationshipField :: MonadGen m => m RelationshipField
|
|
|
|
genRelationshipField =
|
|
|
|
RelationshipField
|
|
|
|
<$> genRelationshipName
|
|
|
|
<*> genQuery
|
|
|
|
|
|
|
|
genQuery :: MonadGen m => m Query
|
|
|
|
genQuery =
|
|
|
|
Query
|
|
|
|
<$> Gen.maybe (genKeyMap genField)
|
|
|
|
<*> Gen.maybe (genKeyMap genAggregate)
|
2022-09-06 07:24:46 +03:00
|
|
|
<*> Gen.maybe (Gen.int defaultRange)
|
|
|
|
<*> Gen.maybe (Gen.int defaultRange)
|
2022-07-20 08:20:49 +03:00
|
|
|
<*> Gen.maybe genExpression
|
2022-08-19 10:00:46 +03:00
|
|
|
<*> Gen.maybe genOrderBy
|
2022-07-20 08:20:49 +03:00
|
|
|
|
|
|
|
genQueryRequest :: MonadGen m => m QueryRequest
|
|
|
|
genQueryRequest =
|
|
|
|
QueryRequest
|
|
|
|
<$> genTableName
|
2022-09-06 07:24:46 +03:00
|
|
|
<*> Gen.list defaultRange genTableRelationships
|
2022-07-20 08:20:49 +03:00
|
|
|
<*> genQuery
|
|
|
|
|
|
|
|
genFieldValue :: MonadGen m => m FieldValue
|
|
|
|
genFieldValue =
|
|
|
|
Gen.recursive
|
|
|
|
Gen.choice
|
2022-07-26 05:28:57 +03:00
|
|
|
[mkColumnFieldValue <$> genValue]
|
|
|
|
[mkRelationshipFieldValue <$> genQueryResponse]
|
2022-07-20 08:20:49 +03:00
|
|
|
|
2022-04-01 04:20:23 +03:00
|
|
|
genQueryResponse :: MonadGen m => m QueryResponse
|
|
|
|
genQueryResponse =
|
2022-07-20 08:20:49 +03:00
|
|
|
QueryResponse
|
2022-09-06 07:24:46 +03:00
|
|
|
<$> Gen.maybe (Gen.list defaultRange (genKeyMap genFieldValue))
|
|
|
|
<*> Gen.maybe (genKeyMap genValue)
|