2022-03-31 07:45:03 +03:00
|
|
|
{-# LANGUAGE OverloadedLists #-}
|
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
|
2022-08-19 10:00:46 +03:00
|
|
|
module Hasura.Backends.DataConnector.API.V0.OrderBySpec
|
|
|
|
( spec,
|
|
|
|
genOrderBy,
|
|
|
|
genOrderDirection,
|
|
|
|
)
|
|
|
|
where
|
2022-03-31 07:45:03 +03:00
|
|
|
|
|
|
|
import Data.Aeson.QQ.Simple (aesonQQ)
|
2022-08-19 10:00:46 +03:00
|
|
|
import Data.HashMap.Strict qualified as HashMap
|
2022-06-24 09:58:25 +03:00
|
|
|
import Hasura.Backends.DataConnector.API.V0
|
2022-08-19 10:00:46 +03:00
|
|
|
import Hasura.Backends.DataConnector.API.V0.AggregateSpec (genSingleColumnAggregate)
|
2022-05-02 08:03:12 +03:00
|
|
|
import Hasura.Backends.DataConnector.API.V0.ColumnSpec (genColumnName)
|
2022-08-19 10:00:46 +03:00
|
|
|
import Hasura.Backends.DataConnector.API.V0.ExpressionSpec (genExpression)
|
|
|
|
import Hasura.Backends.DataConnector.API.V0.RelationshipsSpec (genRelationshipName)
|
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
|
|
|
|
import Hedgehog.Gen qualified as Gen
|
|
|
|
import Test.Aeson.Utils (jsonOpenApiProperties, testToFromJSONToSchema)
|
|
|
|
import Test.Hspec
|
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec = do
|
2022-08-19 10:00:46 +03:00
|
|
|
describe "OrderByTarget" $ do
|
|
|
|
describe "OrderByColumn" $
|
|
|
|
testToFromJSONToSchema
|
|
|
|
(OrderByColumn (ColumnName "test_column"))
|
|
|
|
[aesonQQ|
|
|
|
|
{ "type": "column",
|
|
|
|
"column": "test_column"
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
describe "OrderByStarCountAggregate" $
|
|
|
|
testToFromJSONToSchema
|
|
|
|
(OrderByStarCountAggregate)
|
|
|
|
[aesonQQ|
|
|
|
|
{ "type": "star_count_aggregate" }
|
|
|
|
|]
|
|
|
|
describe "OrderBySingleColumnAggregate" $
|
|
|
|
testToFromJSONToSchema
|
|
|
|
(OrderBySingleColumnAggregate (SingleColumnAggregate Sum (ColumnName "test_column")))
|
|
|
|
[aesonQQ|
|
|
|
|
{ "type": "single_column_aggregate",
|
|
|
|
"function": "sum",
|
|
|
|
"column": "test_column"
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
jsonOpenApiProperties genOrderByTarget
|
|
|
|
|
|
|
|
describe "OrderByElement" $ do
|
|
|
|
testToFromJSONToSchema
|
|
|
|
( OrderByElement
|
|
|
|
[RelationshipName "relation1", RelationshipName "relation2"]
|
|
|
|
(OrderByColumn (ColumnName "my_column_name"))
|
|
|
|
Ascending
|
|
|
|
)
|
|
|
|
[aesonQQ|
|
|
|
|
{ "target_path": ["relation1", "relation2"],
|
|
|
|
"target": {
|
|
|
|
"type": "column",
|
|
|
|
"column": "my_column_name"
|
|
|
|
},
|
|
|
|
"order_direction": "asc"
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
jsonOpenApiProperties genOrderByElement
|
|
|
|
|
|
|
|
describe "OrderByRelation" $ do
|
|
|
|
testToFromJSONToSchema
|
|
|
|
( OrderByRelation
|
|
|
|
(Just $ And [])
|
|
|
|
(HashMap.fromList [(RelationshipName "relationship_name", (OrderByRelation Nothing mempty))])
|
|
|
|
)
|
|
|
|
[aesonQQ|
|
|
|
|
{ "where": {
|
|
|
|
"type": "and",
|
|
|
|
"expressions": []
|
|
|
|
},
|
|
|
|
"subrelations": {
|
|
|
|
"relationship_name": {
|
|
|
|
"subrelations": {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
jsonOpenApiProperties genOrderByRelation
|
|
|
|
|
2022-03-31 07:45:03 +03:00
|
|
|
describe "OrderBy" $ do
|
|
|
|
testToFromJSONToSchema
|
2022-08-19 10:00:46 +03:00
|
|
|
( OrderBy
|
|
|
|
(HashMap.fromList [(RelationshipName "relationship_name", (OrderByRelation Nothing mempty))])
|
|
|
|
(OrderByElement [] OrderByStarCountAggregate Ascending :| [])
|
|
|
|
)
|
2022-03-31 07:45:03 +03:00
|
|
|
[aesonQQ|
|
2022-08-19 10:00:46 +03:00
|
|
|
{ "relations": {
|
|
|
|
"relationship_name": {
|
|
|
|
"subrelations": {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"elements": [
|
|
|
|
{
|
|
|
|
"target_path": [],
|
|
|
|
"target": {
|
|
|
|
"type": "star_count_aggregate"
|
|
|
|
},
|
|
|
|
"order_direction": "asc"
|
|
|
|
}
|
|
|
|
]
|
2022-03-31 07:45:03 +03:00
|
|
|
}
|
|
|
|
|]
|
|
|
|
jsonOpenApiProperties genOrderBy
|
2022-08-19 10:00:46 +03:00
|
|
|
|
|
|
|
describe "OrderDirection" $ do
|
2022-03-31 07:45:03 +03:00
|
|
|
describe "Ascending" $
|
|
|
|
testToFromJSONToSchema Ascending [aesonQQ|"asc"|]
|
|
|
|
describe "Descending" $
|
|
|
|
testToFromJSONToSchema Descending [aesonQQ|"desc"|]
|
2022-08-19 10:00:46 +03:00
|
|
|
jsonOpenApiProperties genOrderDirection
|
2022-03-31 07:45:03 +03:00
|
|
|
|
|
|
|
genOrderBy :: MonadGen m => m OrderBy
|
|
|
|
genOrderBy =
|
|
|
|
OrderBy
|
2022-09-06 07:24:46 +03:00
|
|
|
<$> (HashMap.fromList <$> Gen.list defaultRange ((,) <$> genRelationshipName <*> genOrderByRelation))
|
|
|
|
<*> Gen.nonEmpty defaultRange genOrderByElement
|
2022-08-19 10:00:46 +03:00
|
|
|
|
|
|
|
genOrderByRelation :: MonadGen m => m OrderByRelation
|
|
|
|
genOrderByRelation =
|
|
|
|
OrderByRelation
|
|
|
|
<$> Gen.maybe genExpression
|
|
|
|
-- Gen.small ensures the recursion will terminate as the size will shrink with each recursion
|
2022-09-06 07:24:46 +03:00
|
|
|
<*> Gen.small (HashMap.fromList <$> Gen.list defaultRange ((,) <$> genRelationshipName <*> genOrderByRelation))
|
2022-08-19 10:00:46 +03:00
|
|
|
|
|
|
|
genOrderByElement :: MonadGen m => m OrderByElement
|
|
|
|
genOrderByElement =
|
|
|
|
OrderByElement
|
2022-09-06 07:24:46 +03:00
|
|
|
<$> Gen.list defaultRange genRelationshipName
|
2022-08-19 10:00:46 +03:00
|
|
|
<*> genOrderByTarget
|
|
|
|
<*> genOrderDirection
|
|
|
|
|
|
|
|
genOrderByTarget :: MonadGen m => m OrderByTarget
|
|
|
|
genOrderByTarget =
|
|
|
|
Gen.choice
|
|
|
|
[ OrderByColumn <$> genColumnName,
|
|
|
|
pure OrderByStarCountAggregate,
|
|
|
|
OrderBySingleColumnAggregate <$> genSingleColumnAggregate
|
|
|
|
]
|
2022-03-31 07:45:03 +03:00
|
|
|
|
2022-08-19 10:00:46 +03:00
|
|
|
genOrderDirection :: MonadGen m => m OrderDirection
|
|
|
|
genOrderDirection = Gen.enumBounded
|