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

Commit fe893f9

Browse files
authored
Merge pull request #3415 from input-output-hk/mhuesch/CDEC-506
[CDEC-506] Make JSON golden tests check `FromJSON` decoding in addition to `ToJSON` encoding
2 parents 3d36b64 + 1d9316b commit fe893f9

File tree

3 files changed

+46
-24
lines changed

3 files changed

+46
-24
lines changed

Diff for: core/test/Test/Pos/Core/ExampleHelpers.hs

+36-18
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ import Data.Time.Units (Millisecond)
9494
import qualified Data.Vector as V
9595
import qualified Hedgehog as H
9696
import Serokell.Data.Memory.Units (Byte)
97+
import qualified Serokell.Util.Base16 as B16
9798

9899
import qualified Cardano.Crypto.Wallet as CC
99100
import Pos.Binary.Class (Raw (..), asBinary)
@@ -636,31 +637,43 @@ exampleGenesisAvvmBalances =
636637
,Coin {getCoin = 37343863242999412})
637638
]) }
638639
where
639-
fstRedKey = "\254\156\235\217{]\130W\183LfJ\240"
640-
sndRedKey =
641-
"\254\156\235\217{]\130W\183LfJ\240\RS\224"
640+
fstRedKey = hexToBS "e2a1773a2a82d10c30890cbf84eccbdc1aaaee9204\
641+
\96424d36e868039d9cb519"
642+
sndRedKey = hexToBS "9cdabcec332abbc6fdf883ca5bf3a8afddca69bfea\
643+
\c14c013304da88ac032fe6"
642644

643645
exampleSharedSeed :: SharedSeed
644646
exampleSharedSeed = SharedSeed (getBytes 8 32)
645647

646648
exampleGenesisDelegation :: GenesisDelegation
647649
exampleGenesisDelegation = UnsafeGenesisDelegation (HM.fromList
648-
[(abstractHash (PublicKey (CC.XPub {CC.xpubPublicKey = pubKey1
649-
, CC.xpubChaincode = CC.ChainCode "Test"}))
650-
, UnsafeProxySecretKey {pskOmega = HeavyDlgIndex $ EpochIndex 68300481033
651-
, pskIssuerPk = PublicKey (CC.XPub {CC.xpubPublicKey = pskPubKey
652-
, CC.xpubChaincode = CC.ChainCode "Test"})
653-
, pskDelegatePk = PublicKey (CC.XPub {CC.xpubPublicKey = pskDelPubKey
654-
, CC.xpubChaincode = CC.ChainCode "Test"})
655-
, pskCert = ProxyCert (fromRight (error "Something went wrong") $ sig)})])
650+
[( addressHash issuePubKey
651+
, UnsafeProxySecretKey
652+
{ pskOmega =
653+
HeavyDlgIndex $ EpochIndex 68300481033
654+
, pskIssuerPk = issuePubKey
655+
, pskDelegatePk =
656+
PublicKey (CC.XPub { CC.xpubPublicKey = pskDelPubKey
657+
, CC.xpubChaincode = pskDelChainCode})
658+
, pskCert =
659+
ProxyCert (fromRight (error "Something went wrong") $ sig)
660+
}
661+
)]
662+
)
656663
where
657-
sig = CC.xsignature "\186\229B*\245@^8\ETX\NAKJJ\217\134\218]\DC4\207\
658-
\bMg\SOH\197\199\138y\236sw\DELt\225\&9s\175\131\
659-
\u!\DC4\217\241\129f\bY\151\252\129\228\&\
660-
\2\202\183\254\233\154']\139\241\&8\173\EOT\225\ETX"
661-
pubKey1 = "\145\&3\131kUF\226\131\253M\174\157;w>\156k"
662-
pskPubKey = "\DC1\RS\145\&3\131kUF\226\131\253M\174\157;w>\156k"
663-
pskDelPubKey = "\132\248\DC1\RS\145\&3\131kUF\226\131\253M\174\157;w>\156k"
664+
issuePubKey = PublicKey (CC.XPub { CC.xpubPublicKey = pskPubKey
665+
, CC.xpubChaincode = pskChainCode})
666+
sig = CC.xsignature (hexToBS "bae5422af5405e3803154a4ad986da5d14cf624d670\
667+
\1c5c78a79ec73777f74e13973af83752114d9f18166\
668+
\085997fc81e432cab7fee99a275d8bf138ad04e103")
669+
pskPubKey = hexToBS "e2a1773a2a82d10c30890cbf84eccbdc1aaaee920496424d36e8\
670+
\68039d9cb519"
671+
pskChainCode = CC.ChainCode (hexToBS "21b25efe033d9b00d4f02ccd9cdabcec332\
672+
\abbc6fdf883ca5bf3a8aff4aac27e")
673+
pskDelPubKey = hexToBS "ddca69bfeac14c013304da88ac032ee63281ab036c1b1b918\
674+
\8e4b174b303f43e"
675+
pskDelChainCode = CC.ChainCode (hexToBS "55163b178e999b9fd50637b2edab8c85\
676+
\8a879ac3c4bd3e610095419a19696573")
664677

665678
exampleProtocolConstants :: GenesisProtocolConstants
666679
exampleProtocolConstants = GenesisProtocolConstants
@@ -684,3 +697,8 @@ exampleGenesisInitializer = GenesisInitializer
684697
{getCoinPortion = 366832547637728}
685698
, giUseHeavyDlg = False
686699
, giSeed = 0}
700+
701+
hexToBS :: Text -> ByteString
702+
hexToBS ts = case B16.decode ts of
703+
Left err -> error $ "decode failed: " <> show err
704+
Right bs -> bs

Diff for: core/test/golden/GenesisConfiguration_GCSpec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"spec":{"avvmDistr":{"_pzr2Xtdgle3TGZK8B7g":37343863242999412,"_pzr2Xtdgle3TGZK8A==":36524597913081152},"ftsSeed":"RTVTNGZTSDZldE5vdWlYZXpDeUVqS2MzdEc0amEwa0Y=","heavyDelegation":{"cae922c720ea93e5ead49b4652fb24caf4948e31eee378a4c4513f11":{"pskDelegatePk":"hPgRHpEzg2tVRuKD/U2unTt3PpxrVGVzdA==","pskOmega":68300481033,"pskCert":"bae5422af5405e3803154a4ad986da5d14cf624d6701c5c78a79ec73777f74e13973af83752114d9f18166085997fc81e432cab7fee99a275d8bf138ad04e103","pskIssuerPk":"ER6RM4NrVUbig/1Nrp07dz6ca1Rlc3Q="}},"blockVersionData":{"scriptVersion":999,"slotDuration":999,"maxBlockSize":999,"maxHeaderSize":999,"maxTxSize":999,"maxProposalSize":999,"mpcThd":9.9e-14,"heavyDelThd":9.9e-14,"updateVoteThd":9.9e-14,"updateProposalThd":9.9e-14,"updateImplicit":99,"softforkRule":{"initThd":9.9e-14,"minThd":9.9e-14,"thdDecrement":9.9e-14},"txFeePolicy":{"txSizeLinear":{"a":9.99e-7,"b":7.7e-8}},"unlockStakeEpoch":99},"protocolConstants":{"k":37,"protocolMagic":1783847074,"vssMaxTTL":1477558317,"vssMinTTL":744040476},"initializer":{"testBalance":{"poors":2448641325904532856,"richmen":14071205313513960321,"totalBalance":10953275486128625216,"richmenShare":4.2098713311249885,"useHDAddresses":true},"fakeAvvmBalance":{"count":17853231730478779264,"oneBalance":15087947214890024355},"avvmBalanceFactor":0.366832547637728,"useHeavyDlg":false,"seed":0}}}
1+
{"spec":{"avvmDistr":{"nNq87DMqu8b9-IPKW_Oor93Kab_qwUwBMwTaiKwDL-Y=":37343863242999412,"4qF3OiqC0QwwiQy_hOzL3Bqq7pIElkJNNuhoA52ctRk=":36524597913081152},"ftsSeed":"RTVTNGZTSDZldE5vdWlYZXpDeUVqS2MzdEc0amEwa0Y=","heavyDelegation":{"ed41bf35f4cd2109bdee27b0439942f6de3f55f0ac8d170de7136d3d":{"pskDelegatePk":"3cppv+rBTAEzBNqIrAMu5jKBqwNsGxuRiOSxdLMD9D5VFjsXjpmbn9UGN7Ltq4yFioeaw8S9PmEAlUGaGWllcw==","pskOmega":68300481033,"pskCert":"bae5422af5405e3803154a4ad986da5d14cf624d6701c5c78a79ec73777f74e13973af83752114d9f18166085997fc81e432cab7fee99a275d8bf138ad04e103","pskIssuerPk":"4qF3OiqC0QwwiQy/hOzL3Bqq7pIElkJNNuhoA52ctRkhsl7+Az2bANTwLM2c2rzsMyq7xv34g8pb86iv9KrCfg=="}},"blockVersionData":{"scriptVersion":999,"slotDuration":999,"maxBlockSize":999,"maxHeaderSize":999,"maxTxSize":999,"maxProposalSize":999,"mpcThd":9.9e-14,"heavyDelThd":9.9e-14,"updateVoteThd":9.9e-14,"updateProposalThd":9.9e-14,"updateImplicit":99,"softforkRule":{"initThd":9.9e-14,"minThd":9.9e-14,"thdDecrement":9.9e-14},"txFeePolicy":{"txSizeLinear":{"a":9.99e-7,"b":7.7e-8}},"unlockStakeEpoch":99},"protocolConstants":{"k":37,"protocolMagic":1783847074,"vssMaxTTL":1477558317,"vssMinTTL":744040476},"initializer":{"testBalance":{"poors":2448641325904532856,"richmen":14071205313513960321,"totalBalance":10953275486128625216,"richmenShare":4.2098713311249885,"useHDAddresses":true},"fakeAvvmBalance":{"count":17853231730478779264,"oneBalance":15087947214890024355},"avvmBalanceFactor":0.366832547637728,"useHeavyDlg":false,"seed":0}}}

Diff for: util/test/Test/Pos/Util/Golden.hs

+9-5
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ module Test.Pos.Util.Golden where
22

33
import Universum
44

5-
import Data.Aeson (ToJSON, encode)
6-
import qualified Data.ByteString as BS
5+
import Data.Aeson (FromJSON, ToJSON, eitherDecode, encode)
76
import qualified Data.ByteString.Lazy as LB
87
import Data.FileEmbed (embedStringFile)
98
import qualified Data.List as List
109
import Hedgehog (Gen, Group, Property, PropertyT, TestLimit,
1110
discoverPrefix, forAll, property, withTests, (===))
11+
import Hedgehog.Internal.Property (failWith)
1212
import Hedgehog.Internal.TH (TExpQ)
1313
import Language.Haskell.TH (ExpQ, Q, loc_filename, runIO)
1414
import Language.Haskell.TH.Syntax (qLocation)
@@ -28,11 +28,15 @@ embedGoldenTest :: FilePath -> ExpQ
2828
embedGoldenTest path =
2929
makeRelativeToTestDir ("golden/" <> path) >>= embedStringFile
3030

31-
goldenTestJSON :: (ToJSON a, HasCallStack) => a -> FilePath -> Property
31+
goldenTestJSON :: (Eq a, FromJSON a, HasCallStack, Show a, ToJSON a)
32+
=> a -> FilePath -> Property
3233
goldenTestJSON x path = withFrozenCallStack $ do
3334
withTests 1 . property $ do
34-
goldenJSON <- liftIO $ BS.readFile path
35-
(LB.toStrict $ encode x) === goldenJSON
35+
bs <- liftIO (LB.readFile path)
36+
encode x === bs
37+
case eitherDecode bs of
38+
Left err -> failWith Nothing $ "could not decode: " <> show err
39+
Right x' -> x === x'
3640

3741
makeRelativeToTestDir :: FilePath -> Q FilePath
3842
makeRelativeToTestDir rel = do

0 commit comments

Comments
 (0)