Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 88198d6

Browse files
Michael Hueschenintricate
Michael Hueschen
authored andcommitted
[CDEC-512] Fix roundTripsCanonicalJSONShow
Luke hit some weird type errors and I got them to go away by adding a type signature. This commit uses `roundTripsCanonicalJSONShow` to test `GenesisData`.
1 parent 57fc810 commit 88198d6

File tree

5 files changed

+47
-24
lines changed

5 files changed

+47
-24
lines changed

core/test/Test/Pos/Core/Gen.hs

+17-4
Original file line numberDiff line numberDiff line change
@@ -416,13 +416,17 @@ genGenesisData pm =
416416
GenesisData
417417
<$> genGenesisWStakeholders
418418
<*> genGenesisDelegation pm
419-
<*> genTimestamp
419+
<*> genTimestampRoundedToSecond
420420
<*> genGenesisVssCertificatesMap pm
421421
<*> genGenesisNonAvvmBalances
422-
<*> genBlockVersionData
422+
<*> genBlockVersionDataByTxFP genLinearTxFP
423423
<*> genGenesisProtocolConstants
424424
<*> genGenesisAvvmBalances
425425
<*> genSharedSeed
426+
where
427+
-- @TxFeePolicy@s ToJSON instance crashes if we have a
428+
-- TxFeePolicyUnknown value.
429+
genLinearTxFP = TxFeePolicyTxSizeLinear <$> genTxSizeLinear
426430

427431
genGenesisWStakeholders :: Gen GenesisWStakeholders
428432
genGenesisWStakeholders = do
@@ -558,6 +562,12 @@ genTimeDiff = TimeDiff <$> genMicrosecond
558562
genTimestamp :: Gen Timestamp
559563
genTimestamp = Timestamp <$> genMicrosecond
560564

565+
-- Microseconds are rounded to the nearest second when enc/decoded to/from
566+
-- JSON. So here we round to the nearest 10^6.
567+
genTimestampRoundedToSecond :: Gen Timestamp
568+
genTimestampRoundedToSecond =
569+
Timestamp . (* 1000000) . (`rem` 1000000) <$> genMicrosecond
570+
561571
----------------------------------------------------------------------------
562572
-- Pos.Core.Ssc Generators
563573
----------------------------------------------------------------------------
@@ -756,7 +766,10 @@ genBlockVersion =
756766
<*> Gen.word8 Range.constantBounded
757767

758768
genBlockVersionData :: Gen BlockVersionData
759-
genBlockVersionData =
769+
genBlockVersionData = genBlockVersionDataByTxFP genTxFeePolicy
770+
771+
genBlockVersionDataByTxFP :: Gen TxFeePolicy -> Gen BlockVersionData
772+
genBlockVersionDataByTxFP genTxFP =
760773
BlockVersionData
761774
<$> genScriptVersion
762775
<*> genMillisecond
@@ -770,7 +783,7 @@ genBlockVersionData =
770783
<*> genCoinPortion
771784
<*> genFlatSlotId
772785
<*> genSoftforkRule
773-
<*> genTxFeePolicy
786+
<*> genTxFP
774787
<*> genEpochIndex
775788

776789

core/test/Test/Pos/Core/Json.hs

+8-6
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ import Test.Pos.Core.ExampleHelpers (exampleAddress, exampleAddress1,
1010
exampleGenesisConfiguration_GCSpec0,
1111
exampleGenesisConfiguration_GCSpec1,
1212
exampleGenesisConfiguration_GCSpec2,
13-
exampleGenesisConfiguration_GCSrc, exampleGenesisData0, exampleGenesisData1, exampleGenesisData2,
13+
exampleGenesisConfiguration_GCSrc, exampleGenesisData0,
14+
exampleGenesisData1, exampleGenesisData2,
1415
exampleGenesisProtocolConstants0,
1516
exampleGenesisProtocolConstants1,
1617
exampleGenesisProtocolConstants2, feedPM)
1718
import Test.Pos.Core.Gen (genAddress, genBlockVersionData, genByte,
1819
genCoin, genCoinPortion, genEpochIndex, genFlatSlotId,
1920
genGenesisAvvmBalances, genGenesisConfiguration,
20-
genGenesisDelegation,
21+
genGenesisData, genGenesisDelegation,
2122
genGenesisInitializer, genGenesisProtocolConstants,
2223
genInvReqDataFlowLog, genSharedSeed, genSoftforkRule,
2324
genTxFeePolicy)
@@ -27,7 +28,8 @@ import Test.Pos.Util.Golden (discoverGolden, eachOf,
2728
goldenTestCanonicalJSONDec, goldenTestJSON,
2829
goldenTestJSONDec)
2930
import Test.Pos.Util.Tripping (discoverRoundTrip,
30-
roundTripsAesonBuildable, roundTripsAesonShow)
31+
roundTripsAesonBuildable, roundTripsAesonShow,
32+
roundTripsCanonicalJSONShow)
3133
import Universum
3234

3335
--------------------------------------------------------------------------------
@@ -132,9 +134,9 @@ golden_GenesisDataDec2 =
132134
"test/golden/json/GenesisData2_NoNetworkMagic"
133135

134136
-- TODO @intricate: roundTripsCanonicalJSONShow
135-
--roundTripGenesisData :: Property
136-
--roundTripGenesisData =
137-
-- eachOf 100 (feedPM genGenesisData) roundTripsCanonicalJSONShow
137+
roundTripGenesisData :: Property
138+
roundTripGenesisData =
139+
eachOf 100 (feedPM genGenesisData) roundTripsCanonicalJSONShow
138140

139141
--------------------------------------------------------------------------------
140142
-- GenesisAvvmBalances

pkgs/default.nix

+1
Original file line numberDiff line numberDiff line change
@@ -17463,6 +17463,7 @@ aeson
1746317463
async
1746417464
base
1746517465
bytestring
17466+
canonical-json
1746617467
directory
1746717468
file-embed
1746817469
filepath

util/cardano-sl-util.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ test-suite test
157157
, base
158158
, bytestring
159159
, async
160+
, canonical-json
160161
, cardano-sl-util
161162
, directory
162163
, file-embed

util/test/Test/Pos/Util/Tripping.hs

+20-14
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import Hedgehog.Internal.Property (Diff (..), failWith)
1313
import Hedgehog.Internal.Show (valueDiff)
1414
import Hedgehog.Internal.TH (TExpQ)
1515
import System.IO (hSetEncoding, stderr, stdout, utf8)
16-
--import qualified Text.JSON.Canonical as Canonical
16+
import qualified Text.JSON.Canonical as Canonical
1717
import Text.Show.Pretty (Value (..), parseValue)
1818

19-
--import Pos.Util.Json.Canonical (SchemaError (..))
19+
import Pos.Util.Json.Canonical (SchemaError (..))
2020

2121
discoverRoundTrip :: TExpQ Group
2222
discoverRoundTrip = discoverPrefix "roundTrip"
@@ -30,19 +30,25 @@ roundTripsAesonBuildable
3030
:: (Eq a, MonadTest m, ToJSON a, FromJSON a, Buildable a) => a -> m ()
3131
roundTripsAesonBuildable a = trippingBuildable a encode decode
3232

33+
-- We want @SchemaError@s to show up different (register failure)
34+
instance Eq SchemaError where
35+
_ == _ = False
36+
3337
-- TODO @intricate: roundTripsCanonicalJSONShow
34-
--roundTripsCanonicalJSONShow
35-
-- :: ( Eq a
36-
-- , MonadTest m
37-
-- , Canonical.ToJSON Identity a
38-
-- , Canonical.FromJSON (Either SchemaError) a
39-
-- , HasCallStack
40-
-- , Show a
41-
-- )
42-
-- => a
43-
-- -> m ()
44-
--roundTripsCanonicalJSONShow x =
45-
-- tripping x (runIdentity . Canonical.toJSON) (Canonical.fromJSON :: Canonical.JSValue -> Either SchemaError a)
38+
roundTripsCanonicalJSONShow
39+
:: forall m a
40+
. ( Eq a
41+
, MonadTest m
42+
, Canonical.ToJSON Identity a
43+
, Canonical.FromJSON (Either SchemaError) a
44+
, HasCallStack
45+
, Show a
46+
)
47+
=> a
48+
-> m ()
49+
roundTripsCanonicalJSONShow x =
50+
tripping x (runIdentity . Canonical.toJSON :: a -> Canonical.JSValue)
51+
(Canonical.fromJSON :: Canonical.JSValue -> Either SchemaError a)
4652

4753
runTests :: [IO Bool] -> IO ()
4854
runTests tests' = do

0 commit comments

Comments
 (0)