Skip to content

Commit 12358b2

Browse files
committed
Fix test
1 parent 93b5b8f commit 12358b2

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

metadata-lib/metadata-lib.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ test-suite unit-tests
103103
, QuickCheck
104104
, aeson
105105
, aeson-pretty
106+
, aeson-qq
106107
, base >=4.12 && <5
107108
, base64-bytestring
108109
, bytestring

metadata-lib/test/Test/Cardano/Metadata/Validation.hs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@ module Test.Cardano.Metadata.Validation
99
) where
1010

1111
import qualified Data.Aeson as Aeson
12+
import qualified Data.Aeson.Encode.Pretty as Aeson
13+
import Data.Aeson.QQ
14+
( aesonQQ )
15+
import qualified Data.ByteString.Lazy as BSL
1216
import Data.Foldable
1317
( forM_, traverse_ )
1418
import Data.Int
1519
( Int32, Int64, Int8 )
1620
import qualified Data.List.NonEmpty as NE
1721
import qualified Data.Map.Strict as M
18-
import Data.Maybe
19-
( fromJust )
2022
import qualified Data.Text as T
2123
import Data.Validation
2224
( Validation (Failure) )
@@ -59,6 +61,7 @@ import Cardano.Metadata.Types.Common
5961
, seqSucc
6062
, seqZero
6163
, unSequenceNumber
64+
, unSubject
6265
)
6366
import Cardano.Metadata.Validation.Rules
6467
( ValidationError (ErrorMetadataFileBaseNameLengthBounds, ErrorMetadataFileExpectedExtension, ErrorMetadataFileNameDoesntMatchSubject, ErrorMetadataFileTooBig, ErrorMetadataPropertySequenceNumberMustBeLarger)
@@ -78,6 +81,7 @@ import Cardano.Metadata.Validation.Types
7881
, Metadata (Metadata)
7982
, invalid
8083
, metaAttestedProperties
84+
, metaSubject
8185
, metaVerifiableProperties
8286
, onMatchingAttestedProperties
8387
, valid
@@ -102,6 +106,21 @@ tests = testGroup "Validation tests"
102106
]
103107
]
104108

109+
-- | From a JSON value, parse some Metadata and use the length of the
110+
-- pretty encoded JSON value and parsed Subject to make a well-formed
111+
-- File.
112+
asMetadataFile :: Aeson.Value -> File Metadata
113+
asMetadataFile json =
114+
let
115+
-- Prettily print the JSON to get a realistic and consistent file size
116+
fileSize = BSL.length (Aeson.encodePretty json)
117+
metadata = case Aeson.fromJSON json of
118+
Aeson.Error err -> error err
119+
Aeson.Success a -> a
120+
fileName = unSubject (metaSubject metadata) <> ".json"
121+
in
122+
File metadata (fromIntegral fileSize) (T.unpack fileName)
123+
105124
prop_rules_isJSONFile :: H.Property
106125
prop_rules_isJSONFile = property $ do
107126
-- All files with the ".json" extension should pass
@@ -241,8 +260,7 @@ prop_rules_subjectMatchesFileName = property $ do
241260
unit_sequence_number_of_unknown_property_validated :: Assertion
242261
unit_sequence_number_of_unknown_property_validated = do
243262
let
244-
before :: Metadata
245-
before = fromJust $ Aeson.decode' [r|
263+
before = [aesonQQ|
246264
{
247265
"subject": "1234",
248266
"prop": {
@@ -251,8 +269,7 @@ unit_sequence_number_of_unknown_property_validated = do
251269
}
252270
}
253271
|]
254-
after :: Metadata
255-
after = fromJust $ Aeson.decode' [r|
272+
after = [aesonQQ|
256273
{
257274
"subject": "1234",
258275
"prop": {
@@ -261,7 +278,7 @@ unit_sequence_number_of_unknown_property_validated = do
261278
}
262279
}
263280
|]
264-
diff = Changed (File before undefined undefined) (File after undefined undefined)
281+
diff = Changed (asMetadataFile before) (asMetadataFile after)
265282

266283
defaultRules `apply` diff
267284
@?= (Failure (ErrorMetadataPropertySequenceNumberMustBeLarger (AttestedProperty {attestedValue = Aeson.String "hello", attestedSignatures = [], attestedSequenceNumber = seqFromNatural 0}) (AttestedProperty {attestedValue = Aeson.String "goodbye", attestedSignatures = [], attestedSequenceNumber = seqFromNatural 0}) (seqFromNatural 0) (seqFromNatural 0) NE.:| []) :: Validation (NE.NonEmpty (ValidationError ())) ())

0 commit comments

Comments
 (0)