[server/tests] use fresh HGE for stringify numeric types tests

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/7582
GitOrigin-RevId: 7c7832d322024b01987f1568a87793a7c0a969db
This commit is contained in:
Daniel Harvey 2023-01-20 10:38:33 +00:00 committed by hasura-bot
parent a5769645fa
commit 0270dbf4b4
3 changed files with 65 additions and 49 deletions

View File

@ -29,7 +29,12 @@ import Test.Schema.RemoteRelationships.MetadataAPI.Common qualified as Common
-- Preamble
spec :: SpecWith GlobalTestEnvironment
spec = Fixture.runWithLocalTestEnvironment contexts tests
spec = do
Fixture.hgeWithEnv [("HASURA_GRAPHQL_STRINGIFY_NUMERIC_TYPES", "true")] $
Fixture.runWithLocalTestEnvironment contexts testsWithFeatureOn
Fixture.hgeWithEnv [("HASURA_GRAPHQL_STRINGIFY_NUMERIC_TYPES", "false")] $
Fixture.runWithLocalTestEnvironment contexts testsWithFeatureOff
where
lhsFixtures = [lhsPostgres, lhsRemoteServer]
rhsFixtures = [rhsPostgres]
@ -44,12 +49,7 @@ lhsPostgres tableName =
{ Fixture.mkLocalTestEnvironment = \_ -> pure Nothing,
Fixture.setupTeardown = \testEnv ->
[ SetupAction.noTeardown (lhsPostgresSetup tableName testEnv)
],
Fixture.customOptions =
Just $
Fixture.defaultOptions
{ Fixture.stringifyNumbers = True
}
]
}
lhsRemoteServer :: Fixture.LHSFixture
@ -61,12 +61,7 @@ lhsRemoteServer tableName =
{ Fixture.setupAction = lhsRemoteServerSetup tableName testEnv,
Fixture.teardownAction = \_ -> lhsRemoteServerTeardown testEnv
}
],
Fixture.customOptions =
Just $
Fixture.defaultOptions
{ Fixture.stringifyNumbers = True
}
]
}
--------------------------------------------------------------------------------
@ -327,36 +322,63 @@ rhsPostgresSetup (testEnvironment, _) = do
--------------------------------------------------------------------------------
-- Tests
tests :: Fixture.Options -> SpecWith (TestEnvironment, Maybe Server)
tests opts = describe "object-relationship" $ do
executionTests opts
-- | Basic queries using *-to-DB joins
executionTests :: Fixture.Options -> SpecWith (TestEnvironment, Maybe Server)
executionTests opts = describe "execution" $ do
testsWithFeatureOn :: Fixture.Options -> SpecWith (TestEnvironment, Maybe Server)
testsWithFeatureOn opts = describe "object-relationship (stringified numeric types)" $ do
-- fetches the relationship data
it "related-data" $ \(testEnvironment, _) -> do
let query =
[graphql|
query {
track: hasura_track {
album {
title
play_count
version
query {
track: hasura_track {
album {
title
play_count
version
}
}
}
}
|]
|]
expectedResponse =
[interpolateYaml|
data:
track:
- album:
title: "album1"
play_count: "1000000000000"
version: "1.075"
|]
data:
track:
- album:
title: "album1"
play_count: "1000000000000"
version: "1.075"
|]
shouldReturnYaml
opts
(GraphqlEngine.postGraphql testEnvironment query)
expectedResponse
-- | Expected behaviour when HASURA_GRAPHQL_STRINGIFY_NUMERIC_TYPES is false
testsWithFeatureOff :: Fixture.Options -> SpecWith (TestEnvironment, Maybe Server)
testsWithFeatureOff opts = describe "object-relationship (no stringified numeric types)" $ do
-- fetches the relationship data
it "related-data" $ \(testEnvironment, _) -> do
let query =
[graphql|
query {
track: hasura_track {
album {
title
play_count
version
}
}
}
|]
expectedResponse =
[interpolateYaml|
data:
track:
- album:
title: "album1"
play_count: 1.0e12
version: 1.075
|]
shouldReturnYaml
opts
(GraphqlEngine.postGraphql testEnvironment query)

View File

@ -19,7 +19,6 @@ import Data.Aeson
import Data.Aeson qualified as Aeson
import Data.Aeson.KeyMap qualified as KM
import Data.List (permutations)
import Data.Scientific (FPFormat (Fixed), formatScientific, toBoundedInteger)
import Data.Set (Set)
import Data.Set qualified as Set
import Data.Text qualified as T
@ -96,26 +95,22 @@ shouldReturnYaml = shouldReturnYamlF pure
-- If the zipping doesn't line up, we assume this is probably a bad result and
-- consequently should result in a failing test. In these cases, we leave the
-- actual output exactly as-is, and wait for the test to fail.
tryToMatch :: Value -> Value -> Value
tryToMatch (Array expected) (Array actual) =
Array (Vector.zipWith tryToMatch expected actual)
tryToMatch (Number _) (String text) =
parseToMatch :: Value -> Value -> Value
parseToMatch (Array expected) (Array actual) =
Array (Vector.zipWith parseToMatch expected actual)
parseToMatch (Number _) (String text) =
case readMaybe (T.unpack text) of
Just actual -> Number actual
Nothing -> String text
tryToMatch (String _) (Number actual) = do
-- format floats with decimal places and ints without, as we do in production
let decimalPlaces = 0 <$ (toBoundedInteger actual :: Maybe Int)
String $ T.pack $ formatScientific Fixed decimalPlaces actual
tryToMatch (Object expected) (Object actual) = do
parseToMatch (Object expected) (Object actual) = do
let walk :: KM.KeyMap Value -> Aeson.Key -> Value -> Value
walk reference key current =
case KM.lookup key reference of
Just this -> tryToMatch this current
Just this -> parseToMatch this current
Nothing -> current
Object (KM.mapWithKey (walk expected) actual)
tryToMatch _ actual = actual
parseToMatch _ actual = actual
-- | The function @transform@ converts the returned YAML
-- prior to comparison. It exists in IO in order to be able
@ -132,7 +127,7 @@ shouldReturnYamlF transform options actualIO expected = do
actualIO >>= transform >>= \actual ->
pure
if Fixture.stringifyNumbers options
then tryToMatch expected actual
then parseToMatch expected actual
else actual
actual `shouldBe` expected
@ -151,7 +146,7 @@ shouldReturnOneOfYaml Fixture.Options {stringifyNumbers} actualIO candidates = d
actuals :: Set Value
actuals
| stringifyNumbers = Set.map (`tryToMatch` actual) expecteds
| stringifyNumbers = Set.map (`parseToMatch` actual) expecteds
| otherwise = Set.singleton actual
case Set.lookupMin (Set.intersection expecteds actuals) of

View File

@ -49,7 +49,6 @@ library
, resourcet
, safe
, safe-exceptions
, scientific
, servant-server
, sop-core
, stm