From 5388f1e104e5d23f2928bbc739612d4aa8514d49 Mon Sep 17 00:00:00 2001 From: Neil Mayhew Date: Tue, 9 Apr 2024 17:32:01 -0600 Subject: [PATCH 1/3] Add golden tests for Cardano.Tracing.OrphanInstances.Shelley --- cardano-node/cardano-node.cabal | 3 + .../Tracing/OrphanInstances/Shelley.hs | 107 ++++++++++++++++++ .../data/failureDescription.json | 1 + .../Tracing/OrphanInstances/data/isValid.json | 1 + .../data/tagMismatchDescription.json | 1 + .../data/validityInterval.json | 1 + cardano-node/test/cardano-node-test.hs | 2 + 7 files changed, 116 insertions(+) create mode 100644 cardano-node/test/Test/Cardano/Tracing/OrphanInstances/Shelley.hs create mode 100644 cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/failureDescription.json create mode 100644 cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/isValid.json create mode 100644 cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/tagMismatchDescription.json create mode 100644 cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/validityInterval.json diff --git a/cardano-node/cardano-node.cabal b/cardano-node/cardano-node.cabal index 6d49261dae5..f203103964f 100644 --- a/cardano-node/cardano-node.cabal +++ b/cardano-node/cardano-node.cabal @@ -248,6 +248,8 @@ test-suite cardano-node-test , cardano-crypto-wrapper , cardano-api:{cardano-api, internal} , cardano-ledger-core + , cardano-ledger-allegra + , cardano-ledger-alonzo , cardano-node , cardano-slotting , directory @@ -276,5 +278,6 @@ test-suite cardano-node-test Test.Cardano.Node.POM Test.Cardano.Tracing.NewTracing.Consistency Test.Cardano.Tracing.OrphanInstances.HardFork + Test.Cardano.Tracing.OrphanInstances.Shelley ghc-options: -threaded -rtsopts "-with-rtsopts=-N -T" diff --git a/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/Shelley.hs b/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/Shelley.hs new file mode 100644 index 00000000000..ea965ac8891 --- /dev/null +++ b/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/Shelley.hs @@ -0,0 +1,107 @@ +{-# LANGUAGE OverloadedStrings #-} + +-- | Test the JSON encoding of instances in Cardano.Tracing.OrphanInstances.Shelley +-- +-- The golden files are stored in the path given by 'addPrefix'. +-- +-- If a new test is added and no golden file exists for it it will be created. +-- This new file needs to be commited. +-- +-- For now we added a couple of representative examples, however the tests are +-- not exhaustive. +-- +-- The examples can be best viewed using a tool like 'jq'. +module Test.Cardano.Tracing.OrphanInstances.Shelley (tests) where + +import Cardano.Ledger.Allegra.Scripts (ValidityInterval (..)) +import Cardano.Ledger.Alonzo.Rules (FailureDescription (..), TagMismatchDescription (..)) +import Cardano.Ledger.Alonzo.Tx (IsValid (..)) +import Cardano.Ledger.BaseTypes (SlotNo (..), StrictMaybe (..)) +import Cardano.Tracing.OrphanInstances.Shelley () + +import qualified Data.Aeson as Aeson +import Data.ByteString.Lazy.Char8 (unpack) +import qualified Data.List.NonEmpty as NE + +import Hedgehog (Property) +import qualified Hedgehog as H +import qualified Hedgehog.Extras.Test.Base as H.Base +import qualified Hedgehog.Extras.Test.Golden as H.Golden +import Hedgehog.Internal.Property (PropertyName (PropertyName)) + +tests :: IO Bool +tests = H.checkSequential + $ H.Group "Shelley JSON instances" + [ test + ( validityInterval + , "validityInterval.json") + , test + ( isValid + , "isValid.json") + , test + ( failureDescription + , "failureDescription.json") + , test + ( tagMismatchDescription + , "tagMismatchDescription.json") + ] + where + test (actualValue, goldenBaseName) = + (PropertyName goldenBaseName, goldenTestJSON actualValue goldenBaseName) + +-------------------------------------------------------------------------------- +-- Examples +-------------------------------------------------------------------------------- + +validityInterval :: [ValidityInterval] +validityInterval = + [ ValidityInterval + { invalidBefore = SNothing + , invalidHereafter = SNothing + } + , ValidityInterval + { invalidBefore = SJust (SlotNo 12345) + , invalidHereafter = SNothing + } + , ValidityInterval + { invalidBefore = SNothing + , invalidHereafter = SJust (SlotNo 12354) + } + , ValidityInterval + { invalidBefore = SJust (SlotNo 12345) + , invalidHereafter = SJust (SlotNo 12354) + } + ] + +isValid :: [IsValid] +isValid = + [ IsValid True + , IsValid False + ] + +failureDescription :: [FailureDescription] +failureDescription = + [ PlutusFailure "A description" "A reconstruction" + ] + +tagMismatchDescription :: [TagMismatchDescription] +tagMismatchDescription = + [ PassedUnexpectedly + , FailedUnexpectedly (NE.fromList failureDescription) + ] + +-------------------------------------------------------------------------------- +-- Helper functions +-------------------------------------------------------------------------------- + +goldenTestJSON :: Aeson.ToJSON a => a -> FilePath -> Property +goldenTestJSON valueToEncode goldenFileBaseName = + H.withTests 1 $ H.withShrinks 0 $ H.property $ do + goldenFp <- H.Base.note $ addPrefix goldenFileBaseName + let actualValue = unpack $ Aeson.encode valueToEncode + H.Golden.diffVsGoldenFile actualValue goldenFp + +-- | NB: this function is only used in 'goldenTestJSON' but it is defined at the +-- top level so that we can refer to it in the documentation of this module. +addPrefix :: FilePath -> FilePath +addPrefix fname = "test/Test/Cardano/Tracing/OrphanInstances/data/" <> fname diff --git a/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/failureDescription.json b/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/failureDescription.json new file mode 100644 index 00000000000..d2e46571a2c --- /dev/null +++ b/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/failureDescription.json @@ -0,0 +1 @@ +[{"description":"A description","error":"PlutusFailure","kind":"FailureDescription"}] \ No newline at end of file diff --git a/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/isValid.json b/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/isValid.json new file mode 100644 index 00000000000..b123175a26c --- /dev/null +++ b/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/isValid.json @@ -0,0 +1 @@ +[true,false] \ No newline at end of file diff --git a/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/tagMismatchDescription.json b/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/tagMismatchDescription.json new file mode 100644 index 00000000000..d534474d83d --- /dev/null +++ b/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/tagMismatchDescription.json @@ -0,0 +1 @@ +[{"error":"PassedUnexpectedly","kind":"TagMismatchDescription"},{"error":"FailedUnexpectedly","kind":"TagMismatchDescription","reconstruction":[{"description":"A description","error":"PlutusFailure","kind":"FailureDescription"}]}] \ No newline at end of file diff --git a/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/validityInterval.json b/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/validityInterval.json new file mode 100644 index 00000000000..d789731fe73 --- /dev/null +++ b/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/validityInterval.json @@ -0,0 +1 @@ +[{},{"invalidBefore":12345},{"invalidHereafter":12354},{"invalidBefore":12345,"invalidHereafter":12354}] \ No newline at end of file diff --git a/cardano-node/test/cardano-node-test.hs b/cardano-node/test/cardano-node-test.hs index 330681215f5..b907eb493ba 100644 --- a/cardano-node/test/cardano-node-test.hs +++ b/cardano-node/test/cardano-node-test.hs @@ -14,6 +14,7 @@ import qualified Test.Cardano.Node.FilePermissions import qualified Test.Cardano.Node.Json import qualified Test.Cardano.Node.POM import qualified Test.Cardano.Tracing.OrphanInstances.HardFork +import qualified Test.Cardano.Tracing.OrphanInstances.Shelley import qualified Test.Cardano.Tracing.NewTracing.Consistency import qualified Cardano.Crypto.Init as Crypto @@ -35,5 +36,6 @@ main = do , Test.Cardano.Node.Json.tests , Test.Cardano.Node.POM.tests , Test.Cardano.Tracing.OrphanInstances.HardFork.tests + , Test.Cardano.Tracing.OrphanInstances.Shelley.tests , Test.Cardano.Tracing.NewTracing.Consistency.tests ] From 7f697b6889ab6863457a1dfa1aa286428ce68efc Mon Sep 17 00:00:00 2001 From: Neil Mayhew Date: Tue, 9 Apr 2024 16:16:03 -0600 Subject: [PATCH 2/3] Remove orphan ToJSON instances that were moved to ledger --- cardano-node/cardano-node.cabal | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cardano-node/cardano-node.cabal b/cardano-node/cardano-node.cabal index f203103964f..084ac925fa4 100644 --- a/cardano-node/cardano-node.cabal +++ b/cardano-node/cardano-node.cabal @@ -149,8 +149,8 @@ library , cardano-crypto-class , cardano-crypto-wrapper , cardano-git-rev ^>=0.2.2 - , cardano-ledger-alonzo - , cardano-ledger-allegra + , cardano-ledger-alonzo >=1.7.1 + , cardano-ledger-allegra >=1.4.1 , cardano-ledger-api , cardano-ledger-babbage , cardano-ledger-byron From 61ee43ce9c916b93c67cc0d8eb9444bfa1452203 Mon Sep 17 00:00:00 2001 From: Neil Mayhew Date: Wed, 10 Apr 2024 10:49:58 -0600 Subject: [PATCH 3/3] Revert "Add golden tests for Cardano.Tracing.OrphanInstances.Shelley" This reverts commit 40df9b877472deac16d4399df9aa12b48a4bda28. The tests aren't needed now that the instances have been moved to ledger. --- cardano-node/cardano-node.cabal | 3 - .../Tracing/OrphanInstances/Shelley.hs | 107 ------------------ .../data/failureDescription.json | 1 - .../Tracing/OrphanInstances/data/isValid.json | 1 - .../data/tagMismatchDescription.json | 1 - .../data/validityInterval.json | 1 - cardano-node/test/cardano-node-test.hs | 2 - 7 files changed, 116 deletions(-) delete mode 100644 cardano-node/test/Test/Cardano/Tracing/OrphanInstances/Shelley.hs delete mode 100644 cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/failureDescription.json delete mode 100644 cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/isValid.json delete mode 100644 cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/tagMismatchDescription.json delete mode 100644 cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/validityInterval.json diff --git a/cardano-node/cardano-node.cabal b/cardano-node/cardano-node.cabal index 084ac925fa4..488311a4f84 100644 --- a/cardano-node/cardano-node.cabal +++ b/cardano-node/cardano-node.cabal @@ -248,8 +248,6 @@ test-suite cardano-node-test , cardano-crypto-wrapper , cardano-api:{cardano-api, internal} , cardano-ledger-core - , cardano-ledger-allegra - , cardano-ledger-alonzo , cardano-node , cardano-slotting , directory @@ -278,6 +276,5 @@ test-suite cardano-node-test Test.Cardano.Node.POM Test.Cardano.Tracing.NewTracing.Consistency Test.Cardano.Tracing.OrphanInstances.HardFork - Test.Cardano.Tracing.OrphanInstances.Shelley ghc-options: -threaded -rtsopts "-with-rtsopts=-N -T" diff --git a/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/Shelley.hs b/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/Shelley.hs deleted file mode 100644 index ea965ac8891..00000000000 --- a/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/Shelley.hs +++ /dev/null @@ -1,107 +0,0 @@ -{-# LANGUAGE OverloadedStrings #-} - --- | Test the JSON encoding of instances in Cardano.Tracing.OrphanInstances.Shelley --- --- The golden files are stored in the path given by 'addPrefix'. --- --- If a new test is added and no golden file exists for it it will be created. --- This new file needs to be commited. --- --- For now we added a couple of representative examples, however the tests are --- not exhaustive. --- --- The examples can be best viewed using a tool like 'jq'. -module Test.Cardano.Tracing.OrphanInstances.Shelley (tests) where - -import Cardano.Ledger.Allegra.Scripts (ValidityInterval (..)) -import Cardano.Ledger.Alonzo.Rules (FailureDescription (..), TagMismatchDescription (..)) -import Cardano.Ledger.Alonzo.Tx (IsValid (..)) -import Cardano.Ledger.BaseTypes (SlotNo (..), StrictMaybe (..)) -import Cardano.Tracing.OrphanInstances.Shelley () - -import qualified Data.Aeson as Aeson -import Data.ByteString.Lazy.Char8 (unpack) -import qualified Data.List.NonEmpty as NE - -import Hedgehog (Property) -import qualified Hedgehog as H -import qualified Hedgehog.Extras.Test.Base as H.Base -import qualified Hedgehog.Extras.Test.Golden as H.Golden -import Hedgehog.Internal.Property (PropertyName (PropertyName)) - -tests :: IO Bool -tests = H.checkSequential - $ H.Group "Shelley JSON instances" - [ test - ( validityInterval - , "validityInterval.json") - , test - ( isValid - , "isValid.json") - , test - ( failureDescription - , "failureDescription.json") - , test - ( tagMismatchDescription - , "tagMismatchDescription.json") - ] - where - test (actualValue, goldenBaseName) = - (PropertyName goldenBaseName, goldenTestJSON actualValue goldenBaseName) - --------------------------------------------------------------------------------- --- Examples --------------------------------------------------------------------------------- - -validityInterval :: [ValidityInterval] -validityInterval = - [ ValidityInterval - { invalidBefore = SNothing - , invalidHereafter = SNothing - } - , ValidityInterval - { invalidBefore = SJust (SlotNo 12345) - , invalidHereafter = SNothing - } - , ValidityInterval - { invalidBefore = SNothing - , invalidHereafter = SJust (SlotNo 12354) - } - , ValidityInterval - { invalidBefore = SJust (SlotNo 12345) - , invalidHereafter = SJust (SlotNo 12354) - } - ] - -isValid :: [IsValid] -isValid = - [ IsValid True - , IsValid False - ] - -failureDescription :: [FailureDescription] -failureDescription = - [ PlutusFailure "A description" "A reconstruction" - ] - -tagMismatchDescription :: [TagMismatchDescription] -tagMismatchDescription = - [ PassedUnexpectedly - , FailedUnexpectedly (NE.fromList failureDescription) - ] - --------------------------------------------------------------------------------- --- Helper functions --------------------------------------------------------------------------------- - -goldenTestJSON :: Aeson.ToJSON a => a -> FilePath -> Property -goldenTestJSON valueToEncode goldenFileBaseName = - H.withTests 1 $ H.withShrinks 0 $ H.property $ do - goldenFp <- H.Base.note $ addPrefix goldenFileBaseName - let actualValue = unpack $ Aeson.encode valueToEncode - H.Golden.diffVsGoldenFile actualValue goldenFp - --- | NB: this function is only used in 'goldenTestJSON' but it is defined at the --- top level so that we can refer to it in the documentation of this module. -addPrefix :: FilePath -> FilePath -addPrefix fname = "test/Test/Cardano/Tracing/OrphanInstances/data/" <> fname diff --git a/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/failureDescription.json b/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/failureDescription.json deleted file mode 100644 index d2e46571a2c..00000000000 --- a/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/failureDescription.json +++ /dev/null @@ -1 +0,0 @@ -[{"description":"A description","error":"PlutusFailure","kind":"FailureDescription"}] \ No newline at end of file diff --git a/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/isValid.json b/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/isValid.json deleted file mode 100644 index b123175a26c..00000000000 --- a/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/isValid.json +++ /dev/null @@ -1 +0,0 @@ -[true,false] \ No newline at end of file diff --git a/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/tagMismatchDescription.json b/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/tagMismatchDescription.json deleted file mode 100644 index d534474d83d..00000000000 --- a/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/tagMismatchDescription.json +++ /dev/null @@ -1 +0,0 @@ -[{"error":"PassedUnexpectedly","kind":"TagMismatchDescription"},{"error":"FailedUnexpectedly","kind":"TagMismatchDescription","reconstruction":[{"description":"A description","error":"PlutusFailure","kind":"FailureDescription"}]}] \ No newline at end of file diff --git a/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/validityInterval.json b/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/validityInterval.json deleted file mode 100644 index d789731fe73..00000000000 --- a/cardano-node/test/Test/Cardano/Tracing/OrphanInstances/data/validityInterval.json +++ /dev/null @@ -1 +0,0 @@ -[{},{"invalidBefore":12345},{"invalidHereafter":12354},{"invalidBefore":12345,"invalidHereafter":12354}] \ No newline at end of file diff --git a/cardano-node/test/cardano-node-test.hs b/cardano-node/test/cardano-node-test.hs index b907eb493ba..330681215f5 100644 --- a/cardano-node/test/cardano-node-test.hs +++ b/cardano-node/test/cardano-node-test.hs @@ -14,7 +14,6 @@ import qualified Test.Cardano.Node.FilePermissions import qualified Test.Cardano.Node.Json import qualified Test.Cardano.Node.POM import qualified Test.Cardano.Tracing.OrphanInstances.HardFork -import qualified Test.Cardano.Tracing.OrphanInstances.Shelley import qualified Test.Cardano.Tracing.NewTracing.Consistency import qualified Cardano.Crypto.Init as Crypto @@ -36,6 +35,5 @@ main = do , Test.Cardano.Node.Json.tests , Test.Cardano.Node.POM.tests , Test.Cardano.Tracing.OrphanInstances.HardFork.tests - , Test.Cardano.Tracing.OrphanInstances.Shelley.tests , Test.Cardano.Tracing.NewTracing.Consistency.tests ]