@@ -20,18 +20,54 @@ tests =
2020 checkParallel $
2121 Group
2222 " Cardano.DbSync.OffChain.Vote"
23- [ (" parseAndValidateVoteData handles invalid CIP format" , prop_parseInvalidCIPFormat)
23+ [ (" parseAndValidateVoteData handles valid CIP-119 format" , prop_parseValidCIPFormat)
24+ , (" parseAndValidateVoteData handles invalid CIP format (type error)" , prop_parseInvalidCIPFormat)
2425 , (" parseAndValidateVoteData handles valid JSON but invalid structure" , prop_parseValidJsonInvalidStructure)
2526 , (" parseAndValidateVoteData handles unparseable JSON" , prop_parseUnparseableJson)
2627 ]
2728
29+ -- | Test that we can parse valid CIP-119 format correctly
30+ -- Scenario: Valid JSON + Valid CIP schema -> is_valid = true
31+ prop_parseValidCIPFormat :: Property
32+ prop_parseValidCIPFormat = withTests 1 $ property $ do
33+ -- Read the test file with valid CIP-119 format
34+ fileContent <- liftIO $ BS. readFile " test/testfiles/valid-vote-minimal.jsonld"
35+ let lbsContent = LBS. fromStrict fileContent
36+
37+ -- Run the parser
38+ result <- liftIO $ runOrThrowIO $ runExceptT $ parseAndValidateVoteData fileContent lbsContent Nothing DB. DrepAnchor Nothing
39+
40+ let (mocvd, val, _hash, _warning, isValidJson) = result
41+
42+ -- Should succeed in parsing generic JSON
43+ annotate " Successfully parsed as generic JSON"
44+ assert isValidJson
45+
46+ -- Should successfully parse into strongly-typed OffChainVoteData
47+ case mocvd of
48+ Just _ocvd -> do
49+ annotate " Successfully parsed into OffChainVoteData"
50+ success
51+ Nothing -> do
52+ annotate " Failed to parse into OffChainVoteData"
53+ failure
54+
55+ -- Should have valid Aeson.Value
56+ case Aeson. toJSON val of
57+ Aeson. Object _obj -> do
58+ annotate " Has valid JSON object"
59+ success
60+ _ -> do
61+ annotate " Expected JSON object"
62+ failure
63+
2864-- | Test that we can parse JSON with incorrect field types (e.g., doNotList as string instead of bool)
2965-- This is based on the issue https://github.com/IntersectMBO/cardano-db-sync/issues/1995
3066-- Scenario: Valid JSON but invalid CIP schema -> is_valid = false
3167prop_parseInvalidCIPFormat :: Property
3268prop_parseInvalidCIPFormat = withTests 1 $ property $ do
3369 -- Read the test file with invalid doNotList field (string instead of bool)
34- fileContent <- liftIO $ BS. readFile " test/testfiles/invalid-vote-doNotList .jsonld"
70+ fileContent <- liftIO $ BS. readFile " test/testfiles/invalid-vote-type-error .jsonld"
3571 let lbsContent = LBS. fromStrict fileContent
3672
3773 -- Run the parser
@@ -58,14 +94,13 @@ prop_parseInvalidCIPFormat = withTests 1 $ property $ do
5894-- | Test with completely valid JSON but not matching the CIP schema
5995-- Scenario: Valid JSON but invalid CIP schema -> is_valid = false
6096prop_parseValidJsonInvalidStructure :: Property
61- prop_parseValidJsonInvalidStructure = property $ do
62- -- Create a valid JSON that doesn't match CIP schema at all
63- let invalidJson = " {\" randomField\" : \" value\" , \" number\" : 42}"
64- bs = encodeUtf8 invalidJson
65- lbs = LBS. fromStrict bs
97+ prop_parseValidJsonInvalidStructure = withTests 1 $ property $ do
98+ -- Read the test file with valid JSON but wrong structure
99+ fileContent <- liftIO $ BS. readFile " test/testfiles/invalid-vote-wrong-structure.jsonld"
100+ let lbsContent = LBS. fromStrict fileContent
66101
67102 -- This should succeed because it's valid JSON, just not matching the schema
68- result <- liftIO $ runOrThrowIO $ runExceptT $ parseAndValidateVoteData bs lbs Nothing DB. DrepAnchor Nothing
103+ result <- liftIO $ runOrThrowIO $ runExceptT $ parseAndValidateVoteData fileContent lbsContent Nothing DB. DrepAnchor Nothing
69104
70105 let (mocvd, _val, _hash, _warning, isValidJson) = result
71106
@@ -77,14 +112,13 @@ prop_parseValidJsonInvalidStructure = property $ do
77112-- | Test with completely unparseable content (not valid JSON at all)
78113-- Scenario: Invalid JSON but hash matches -> is_valid = NULL
79114prop_parseUnparseableJson :: Property
80- prop_parseUnparseableJson = property $ do
81- -- Create content that is not valid JSON
82- let notJson = " This is just plain text, not JSON at all!"
83- bs = encodeUtf8 notJson
84- lbs = LBS. fromStrict bs
115+ prop_parseUnparseableJson = withTests 1 $ property $ do
116+ -- Read the test file with malformed JSON
117+ fileContent <- liftIO $ BS. readFile " test/testfiles/invalid-vote-malformed-json.jsonld"
118+ let lbsContent = LBS. fromStrict fileContent
85119
86120 -- This should not fail, but instead return an error message in the JSON field
87- result <- liftIO $ runOrThrowIO $ runExceptT $ parseAndValidateVoteData bs lbs Nothing DB. DrepAnchor Nothing
121+ result <- liftIO $ runOrThrowIO $ runExceptT $ parseAndValidateVoteData fileContent lbsContent Nothing DB. DrepAnchor Nothing
88122
89123 let (mocvd, val, _hash, _warning, isValidJson) = result
90124
0 commit comments