2022-07-20 08:20:49 +03:00
|
|
|
{-# LANGUAGE OverloadedLists #-}
|
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
|
|
|
|
module Hasura.Backends.DataConnector.API.V0.AggregateSpec
|
|
|
|
( spec,
|
|
|
|
genAggregate,
|
2022-08-19 10:00:46 +03:00
|
|
|
genSingleColumnAggregate,
|
2022-07-20 08:20:49 +03:00
|
|
|
)
|
|
|
|
where
|
|
|
|
|
|
|
|
import Data.Aeson.QQ.Simple (aesonQQ)
|
|
|
|
import Hasura.Backends.DataConnector.API.V0
|
|
|
|
import Hasura.Backends.DataConnector.API.V0.ColumnSpec (genColumnName)
|
|
|
|
import Hasura.Prelude
|
|
|
|
import Hedgehog
|
|
|
|
import Hedgehog.Gen qualified as Gen
|
2022-10-27 03:42:49 +03:00
|
|
|
import Language.GraphQL.Draft.Generator (genName)
|
|
|
|
import Language.GraphQL.Draft.Syntax.QQ qualified as G
|
2022-07-20 08:20:49 +03:00
|
|
|
import Test.Aeson.Utils (jsonOpenApiProperties, testToFromJSONToSchema)
|
|
|
|
import Test.Hspec
|
|
|
|
|
|
|
|
spec :: Spec
|
|
|
|
spec = do
|
|
|
|
describe "Aggregate" $ do
|
|
|
|
describe "SingleColumn" $ do
|
|
|
|
testToFromJSONToSchema
|
2022-10-27 03:42:49 +03:00
|
|
|
(SingleColumn $ SingleColumnAggregate (SingleColumnAggregateFunction [G.name|avg|]) (ColumnName "my_column_name"))
|
2022-07-20 08:20:49 +03:00
|
|
|
[aesonQQ|
|
|
|
|
{ "type": "single_column",
|
|
|
|
"function": "avg",
|
|
|
|
"column": "my_column_name"
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
describe "ColumnCount" $ do
|
|
|
|
testToFromJSONToSchema
|
2022-07-28 10:24:13 +03:00
|
|
|
(ColumnCount $ ColumnCountAggregate (ColumnName "my_column_name") True)
|
2022-07-20 08:20:49 +03:00
|
|
|
[aesonQQ|
|
|
|
|
{ "type": "column_count",
|
2022-07-28 10:24:13 +03:00
|
|
|
"column": "my_column_name",
|
2022-07-20 08:20:49 +03:00
|
|
|
"distinct": true
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
describe "StarCount" $ do
|
|
|
|
testToFromJSONToSchema
|
|
|
|
(StarCount)
|
|
|
|
[aesonQQ|
|
|
|
|
{ "type": "star_count"
|
|
|
|
}
|
|
|
|
|]
|
|
|
|
jsonOpenApiProperties genAggregate
|
|
|
|
|
|
|
|
describe "SingleColumnAggregateFunction" $ do
|
2022-10-27 03:42:49 +03:00
|
|
|
testToFromJSONToSchema (SingleColumnAggregateFunction [G.name|avg|]) [aesonQQ|"avg"|]
|
2022-07-20 08:20:49 +03:00
|
|
|
jsonOpenApiProperties genSingleColumnAggregateFunction
|
|
|
|
|
2022-10-27 03:42:49 +03:00
|
|
|
genAggregate :: Gen Aggregate
|
2022-07-20 08:20:49 +03:00
|
|
|
genAggregate =
|
|
|
|
Gen.choice
|
|
|
|
[ SingleColumn <$> genSingleColumnAggregate,
|
|
|
|
ColumnCount <$> genColumnCountAggregate,
|
|
|
|
pure StarCount
|
|
|
|
]
|
|
|
|
|
2022-10-27 03:42:49 +03:00
|
|
|
genSingleColumnAggregate :: Gen SingleColumnAggregate
|
2022-07-20 08:20:49 +03:00
|
|
|
genSingleColumnAggregate =
|
|
|
|
SingleColumnAggregate
|
|
|
|
<$> genSingleColumnAggregateFunction
|
|
|
|
<*> genColumnName
|
|
|
|
|
2022-10-27 03:42:49 +03:00
|
|
|
genColumnCountAggregate :: Gen ColumnCountAggregate
|
2022-07-20 08:20:49 +03:00
|
|
|
genColumnCountAggregate =
|
|
|
|
ColumnCountAggregate
|
2022-07-28 10:24:13 +03:00
|
|
|
<$> genColumnName
|
2022-07-20 08:20:49 +03:00
|
|
|
<*> Gen.bool
|
|
|
|
|
2022-10-27 03:42:49 +03:00
|
|
|
genSingleColumnAggregateFunction :: Gen SingleColumnAggregateFunction
|
|
|
|
genSingleColumnAggregateFunction = SingleColumnAggregateFunction <$> genName
|