/chain/test/Test/Pos/Chain/Genesis/Json.hs

https://github.com/input-output-hk/cardano-sl · Haskell · 277 lines · 205 code · 40 blank · 32 comment · 7 complexity · 7ccf2cd29cbfb189efb8139cba52d1ee MD5 · raw file

  1. {-# LANGUAGE TemplateHaskell #-}
  2. module Test.Pos.Chain.Genesis.Json
  3. ( tests
  4. ) where
  5. import Universum
  6. import Data.Aeson (eitherDecode)
  7. import qualified Data.ByteString.Lazy as LB
  8. import Hedgehog (Property, assert, withTests)
  9. import qualified Hedgehog as H
  10. import Hedgehog.Internal.Property (failWith)
  11. import Pos.Chain.Genesis (GenesisProtocolConstants, StaticConfig)
  12. import Test.Pos.Chain.Genesis.Example (exampleGenesisData0,
  13. exampleGenesisData1, exampleGenesisData2,
  14. exampleGenesisProtocolConstants0,
  15. exampleGenesisProtocolConstants1,
  16. exampleGenesisProtocolConstants2,
  17. exampleStaticConfig_GCSpec0, exampleStaticConfig_GCSpec1,
  18. exampleStaticConfig_GCSpec2, exampleStaticConfig_GCSrc)
  19. import Test.Pos.Chain.Genesis.Gen (genGenesisAvvmBalances,
  20. genGenesisData, genGenesisDelegation,
  21. genGenesisInitializer, genGenesisProtocolConstants,
  22. genStaticConfig)
  23. import Test.Pos.Core.ExampleHelpers (feedPM, feedPMWithRequiresMagic)
  24. import Test.Pos.Util.Golden (discoverGolden, eachOf,
  25. goldenFileCanonicalEquiv, goldenTestCanonicalJSONDec,
  26. goldenTestJSONDec, goldenTestJSONPretty, goldenValueEquiv)
  27. import Test.Pos.Util.Tripping (discoverRoundTrip,
  28. roundTripsAesonYamlShow, roundTripsCanonicalJSONShow)
  29. --------------------------------------------------------------------------------
  30. -- StaticConfig
  31. --------------------------------------------------------------------------------
  32. -- Decode-only golden tests for ensuring that, when decoding the legacy
  33. -- `StaticConfig` JSON format, the `RequiresNetworkMagic` field defaults to
  34. -- `RequiresMagic`.
  35. golden_StaticConfig_GCSpec0Dec :: Property
  36. golden_StaticConfig_GCSpec0Dec =
  37. goldenTestJSONDec
  38. exampleStaticConfig_GCSpec0
  39. "test/golden/json/StaticConfig_GCSpec0_Legacy_HasNetworkMagic"
  40. golden_StaticConfig_GCSpec1Dec :: Property
  41. golden_StaticConfig_GCSpec1Dec =
  42. goldenTestJSONDec
  43. exampleStaticConfig_GCSpec1
  44. "test/golden/json/StaticConfig_GCSpec1_Legacy_HasNetworkMagic"
  45. golden_StaticConfig_GCSpec2Dec :: Property
  46. golden_StaticConfig_GCSpec2Dec =
  47. goldenTestJSONDec
  48. exampleStaticConfig_GCSpec2
  49. "test/golden/json/StaticConfig_GCSpec2_Legacy_HasNetworkMagic"
  50. golden_StaticConfig_GCSrc :: Property
  51. golden_StaticConfig_GCSrc =
  52. goldenTestJSONPretty
  53. exampleStaticConfig_GCSrc
  54. "test/golden/json/StaticConfig_GCSrc"
  55. roundTripStaticConfig :: Property
  56. roundTripStaticConfig =
  57. roundTripsAesonYamlShow 100 (feedPM genStaticConfig)
  58. {-# ANN module ("HLint: ignore Reduce duplication" :: Text) #-}
  59. -- Pretty print format equivalence tests. The test reads and decodes the
  60. -- non-prettified JSON (from oldJson dir) and the prettified JSON
  61. -- (from json dir). If the decoding is successful the two values are compared.
  62. golden_prettyEquivalence_StaticConfig_GCSrc :: Property
  63. golden_prettyEquivalence_StaticConfig_GCSrc = withFrozenCallStack $ do
  64. withTests 1 . H.property $ do
  65. prettyJ <- liftIO $ LB.readFile "test/golden/json/StaticConfig_GCSrc"
  66. oldJ <- liftIO $ LB.readFile "test/golden/oldJson/StaticConfig_GCSrc"
  67. let equivTest = goldenValueEquiv
  68. (eitherDecode prettyJ :: Either String StaticConfig)
  69. (eitherDecode oldJ :: Either String StaticConfig)
  70. case equivTest of
  71. Left err -> failWith Nothing $ "could not decode: " <> show err
  72. Right bool' -> assert bool'
  73. golden_prettyEquivalence_StaticConfig_GCSrc0 :: Property
  74. golden_prettyEquivalence_StaticConfig_GCSrc0 = withFrozenCallStack $ do
  75. withTests 1 . H.property $ do
  76. prettyJ <- liftIO $ LB.readFile pFile
  77. oldJ <- liftIO $ LB.readFile oFile
  78. let equivTest = goldenValueEquiv
  79. (eitherDecode prettyJ :: Either String StaticConfig)
  80. (eitherDecode oldJ :: Either String StaticConfig)
  81. case equivTest of
  82. Left err -> failWith Nothing $ "could not decode: " <> show err
  83. Right bool' -> assert bool'
  84. where
  85. pFile = "test/golden/json/StaticConfig_GCSpec0_Legacy_HasNetworkMagic"
  86. oFile = "test/golden/oldJson/StaticConfig_GCSpec0_Legacy_HasNetworkMagic"
  87. golden_prettyEquivalence_StaticConfig_GCSrc1 :: Property
  88. golden_prettyEquivalence_StaticConfig_GCSrc1 = withFrozenCallStack $ do
  89. withTests 1 . H.property $ do
  90. prettyJ <- liftIO $ LB.readFile pFile
  91. oldJ <- liftIO $ LB.readFile oFile
  92. let equivTest = goldenValueEquiv
  93. (eitherDecode prettyJ :: Either String StaticConfig)
  94. (eitherDecode oldJ :: Either String StaticConfig)
  95. case equivTest of
  96. Left err -> failWith Nothing $ "could not decode: " <> show err
  97. Right bool' -> assert bool'
  98. where
  99. pFile = "test/golden/json/StaticConfig_GCSpec1_Legacy_HasNetworkMagic"
  100. oFile = "test/golden/oldJson/StaticConfig_GCSpec1_Legacy_HasNetworkMagic"
  101. golden_prettyEquivalence_StaticConfig_GCSrc2 :: Property
  102. golden_prettyEquivalence_StaticConfig_GCSrc2 = withFrozenCallStack $ do
  103. withTests 1 . H.property $ do
  104. prettyJ <- liftIO $ LB.readFile pFile
  105. oldJ <- liftIO $ LB.readFile oFile
  106. let equivTest = goldenValueEquiv
  107. (eitherDecode prettyJ :: Either String StaticConfig)
  108. (eitherDecode oldJ :: Either String StaticConfig)
  109. case equivTest of
  110. Left err -> failWith Nothing $ "could not decode: " <> show err
  111. Right bool' -> assert bool'
  112. where
  113. pFile = "test/golden/json/StaticConfig_GCSpec2_Legacy_HasNetworkMagic"
  114. oFile = "test/golden/oldJson/StaticConfig_GCSpec2_Legacy_HasNetworkMagic"
  115. --------------------------------------------------------------------------------
  116. -- GenesisData (Canonical JSON)
  117. --------------------------------------------------------------------------------
  118. -- Decode-only golden tests for ensuring that, when decoding the legacy
  119. -- `GenesisData` canonical JSON format, the `RequiresNetworkMagic` field
  120. -- defaults to `RequiresMagic`.
  121. golden_GenesisData0Dec :: Property
  122. golden_GenesisData0Dec =
  123. goldenTestCanonicalJSONDec
  124. exampleGenesisData0
  125. "test/golden/canonical-json/GenesisData0_Legacy_HasNetworkMagic"
  126. golden_GenesisDataDec1 :: Property
  127. golden_GenesisDataDec1 =
  128. goldenTestCanonicalJSONDec
  129. exampleGenesisData1
  130. "test/golden/canonical-json/GenesisData1_Legacy_HasNetworkMagic"
  131. golden_GenesisDataDec2 :: Property
  132. golden_GenesisDataDec2 =
  133. goldenTestCanonicalJSONDec
  134. exampleGenesisData2
  135. "test/golden/canonical-json/GenesisData2_Legacy_HasNetworkMagic"
  136. roundTripGenesisData :: Property
  137. roundTripGenesisData =
  138. eachOf 100 (feedPMWithRequiresMagic genGenesisData) roundTripsCanonicalJSONShow
  139. golden_prettyEquivalence_canonical_GenesisData_0 :: Property
  140. golden_prettyEquivalence_canonical_GenesisData_0 =
  141. goldenFileCanonicalEquiv
  142. "test/golden/canonical-json/GenesisData0_Legacy_HasNetworkMagic"
  143. "test/golden/oldCanonical-json/GenesisData0_Legacy_HasNetworkMagic"
  144. golden_prettyEquivalence_canonical_GenesisData_1 :: Property
  145. golden_prettyEquivalence_canonical_GenesisData_1 =
  146. goldenFileCanonicalEquiv
  147. "test/golden/canonical-json/GenesisData1_Legacy_HasNetworkMagic"
  148. "test/golden/oldCanonical-json/GenesisData1_Legacy_HasNetworkMagic"
  149. golden_prettyEquivalence_canonical_GenesisData_2 :: Property
  150. golden_prettyEquivalence_canonical_GenesisData_2 =
  151. goldenFileCanonicalEquiv
  152. "test/golden/canonical-json/GenesisData2_Legacy_HasNetworkMagic"
  153. "test/golden/oldCanonical-json/GenesisData2_Legacy_HasNetworkMagic"
  154. --------------------------------------------------------------------------------
  155. -- GenesisAvvmBalances
  156. --------------------------------------------------------------------------------
  157. roundTripGenesisAvvmBalances :: Property
  158. roundTripGenesisAvvmBalances =
  159. roundTripsAesonYamlShow 100 genGenesisAvvmBalances
  160. --------------------------------------------------------------------------------
  161. -- GenesisDelegation
  162. --------------------------------------------------------------------------------
  163. roundTripGenesisDelegation :: Property
  164. roundTripGenesisDelegation =
  165. roundTripsAesonYamlShow 100 (feedPM genGenesisDelegation)
  166. --------------------------------------------------------------------------------
  167. -- GenesisProtocolConstants
  168. --------------------------------------------------------------------------------
  169. -- Decode-only golden tests for ensuring that, when decoding the legacy
  170. -- `GenesisProtocolConstants` JSON format, the `RequiresNetworkMagic` field
  171. -- defaults to `RequiresMagic`.
  172. golden_GenesisProtocolConstants0Dec :: Property
  173. golden_GenesisProtocolConstants0Dec =
  174. goldenTestJSONDec exampleGenesisProtocolConstants0
  175. "test/golden/json/GenesisProtocolConstants0_Legacy_HasNetworkMagic"
  176. golden_GenesisProtocolConstants1Dec :: Property
  177. golden_GenesisProtocolConstants1Dec =
  178. goldenTestJSONDec exampleGenesisProtocolConstants1
  179. "test/golden/json/GenesisProtocolConstants1_Legacy_HasNetworkMagic"
  180. golden_GenesisProtocolConstants2Dec :: Property
  181. golden_GenesisProtocolConstants2Dec =
  182. goldenTestJSONDec exampleGenesisProtocolConstants2
  183. "test/golden/json/GenesisProtocolConstants2_Legacy_HasNetworkMagic"
  184. roundTripGenesisProtocolConstants :: Property
  185. roundTripGenesisProtocolConstants =
  186. roundTripsAesonYamlShow 1000 (feedPM genGenesisProtocolConstants)
  187. golden_prettyEquivalence_GenesisProtocolConstants0 :: Property
  188. golden_prettyEquivalence_GenesisProtocolConstants0 = withFrozenCallStack $ do
  189. withTests 1 . H.property $ do
  190. prettyJ <- liftIO $ LB.readFile pFile
  191. oldJ <- liftIO $ LB.readFile oFile
  192. let equivTest = goldenValueEquiv
  193. (eitherDecode prettyJ :: Either String GenesisProtocolConstants)
  194. (eitherDecode oldJ :: Either String GenesisProtocolConstants)
  195. case equivTest of
  196. Left err -> failWith Nothing $ "could not decode: " <> show err
  197. Right bool' -> assert bool'
  198. where
  199. pFile = "test/golden/json/GenesisProtocolConstants0_Legacy_HasNetworkMagic"
  200. oFile = "test/golden/oldJson/GenesisProtocolConstants0_Legacy_HasNetworkMagic"
  201. golden_prettyEquivalence_GenesisProtocolConstants1 :: Property
  202. golden_prettyEquivalence_GenesisProtocolConstants1 = withFrozenCallStack $ do
  203. withTests 1 . H.property $ do
  204. prettyJ <- liftIO $ LB.readFile pFile
  205. oldJ <- liftIO $ LB.readFile oFile
  206. let equivTest = goldenValueEquiv
  207. (eitherDecode prettyJ :: Either String GenesisProtocolConstants)
  208. (eitherDecode oldJ :: Either String GenesisProtocolConstants)
  209. case equivTest of
  210. Left err -> failWith Nothing $ "could not decode: " <> show err
  211. Right bool' -> assert bool'
  212. where
  213. pFile = "test/golden/json/GenesisProtocolConstants1_Legacy_HasNetworkMagic"
  214. oFile = "test/golden/oldJson/GenesisProtocolConstants1_Legacy_HasNetworkMagic"
  215. golden_prettyEquivalence_GenesisProtocolConstants2 :: Property
  216. golden_prettyEquivalence_GenesisProtocolConstants2 = withFrozenCallStack $ do
  217. withTests 1 . H.property $ do
  218. prettyJ <- liftIO $ LB.readFile pFile
  219. oldJ <- liftIO $ LB.readFile oFile
  220. let equivTest = goldenValueEquiv
  221. (eitherDecode prettyJ :: Either String GenesisProtocolConstants)
  222. (eitherDecode oldJ :: Either String GenesisProtocolConstants)
  223. case equivTest of
  224. Left err -> failWith Nothing $ "could not decode: " <> show err
  225. Right bool' -> assert bool'
  226. where
  227. pFile = "test/golden/json/GenesisProtocolConstants2_Legacy_HasNetworkMagic"
  228. oFile = "test/golden/oldJson/GenesisProtocolConstants2_Legacy_HasNetworkMagic"
  229. --------------------------------------------------------------------------------
  230. -- GenesisInitializer
  231. --------------------------------------------------------------------------------
  232. roundTripGenesisInitializer :: Property
  233. roundTripGenesisInitializer =
  234. roundTripsAesonYamlShow 1000 genGenesisInitializer
  235. tests :: IO Bool
  236. tests = (&&) <$> H.checkSequential $$discoverGolden
  237. <*> H.checkParallel $$discoverRoundTrip