Skip to content

Commit 19a7e04

Browse files
committed
add more generic jsonld CIP-100 test files
1 parent ac2b5f0 commit 19a7e04

File tree

6 files changed

+205
-124
lines changed

6 files changed

+205
-124
lines changed

cardano-db-sync/test/Cardano/DbSync/OffChain/VoteTest.hs

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
3167
prop_parseInvalidCIPFormat :: Property
3268
prop_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
6096
prop_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
79114
prop_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

cardano-db-sync/test/testfiles/invalid-vote-doNotList.jsonld

Lines changed: 0 additions & 110 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"givenName": "Test DRep",
3+
"objectives": "Missing closing quote and brace
4+
"motivations": "This JSON is malformed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"@context": {
3+
"CIP100": "https://github.com/cardano-foundation/CIPs/blob/master/CIP-0100/README.md#",
4+
"CIP119": "https://github.com/cardano-foundation/CIPs/blob/master/CIP-0119/README.md#",
5+
"hashAlgorithm": "CIP100:hashAlgorithm",
6+
"body": {
7+
"@id": "CIP119:body",
8+
"@context": {
9+
"references": {
10+
"@id": "CIP119:references",
11+
"@container": "@set",
12+
"@context": {
13+
"GovernanceMetadata": "CIP100:GovernanceMetadataReference",
14+
"Identity": "CIP119:IdentityReference",
15+
"Link": "CIP119:LinkReference",
16+
"Other": "CIP100:OtherReference",
17+
"label": "CIP100:reference-label",
18+
"uri": "CIP100:reference-uri"
19+
}
20+
},
21+
"paymentAddress": "CIP119:paymentAddress",
22+
"givenName": "CIP119:givenName",
23+
"image": "CIP119:image",
24+
"objectives": "CIP119:objectives",
25+
"motivations": "CIP119:motivations",
26+
"qualifications": "CIP119:qualifications",
27+
"doNotList": "CIP119:doNotList"
28+
}
29+
},
30+
"authors": {
31+
"@id": "CIP100:authors",
32+
"@container": "@set",
33+
"@context": {
34+
"name": "http://xmlns.com/foaf/0.1/name",
35+
"witness": {
36+
"@id": "CIP100:witness",
37+
"@context": {
38+
"witnessAlgorithm": "CIP100:witnessAlgorithm",
39+
"publicKey": "CIP100:publicKey",
40+
"signature": "CIP100:signature"
41+
}
42+
}
43+
}
44+
}
45+
},
46+
"authors": [],
47+
"hashAlgorithm": "blake2b-256",
48+
"body": {
49+
"givenName": "Test DRep",
50+
"doNotList": "false",
51+
"motivations": "Test motivations for DRep registration",
52+
"objectives": "Test objectives for governance participation",
53+
"qualifications": "Test qualifications and experience",
54+
"paymentAddress": "addr1test000000000000000000000000000000000000000000000000000000",
55+
"references": [
56+
{
57+
"@type": "Link",
58+
"label": "Test Website",
59+
"uri": "https://example.com/test"
60+
},
61+
{
62+
"@type": "Identity",
63+
"label": "Test Identity",
64+
"uri": "https://example.com/identity"
65+
}
66+
]
67+
}
68+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"title": "Some Random Document",
3+
"description": "This is valid JSON but doesn't match CIP-100/119 schema at all",
4+
"version": "1.0",
5+
"data": {
6+
"field1": "value1",
7+
"field2": 42,
8+
"field3": true,
9+
"nested": {
10+
"items": [1, 2, 3],
11+
"metadata": {
12+
"timestamp": "2025-01-01T00:00:00Z"
13+
}
14+
}
15+
},
16+
"tags": ["test", "example", "invalid-schema"]
17+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
{
2+
"@context": {
3+
"CIP100": "https://github.com/cardano-foundation/CIPs/blob/master/CIP-0100/README.md#",
4+
"CIP119": "https://github.com/cardano-foundation/CIPs/blob/master/CIP-0119/README.md#",
5+
"hashAlgorithm": "CIP100:hashAlgorithm",
6+
"body": {
7+
"@id": "CIP119:body",
8+
"@context": {
9+
"references": {
10+
"@id": "CIP119:references",
11+
"@container": "@set",
12+
"@context": {
13+
"GovernanceMetadata": "CIP100:GovernanceMetadataReference",
14+
"Identity": "CIP119:IdentityReference",
15+
"Link": "CIP119:LinkReference",
16+
"Other": "CIP100:OtherReference",
17+
"label": "CIP100:reference-label",
18+
"uri": "CIP100:reference-uri"
19+
}
20+
},
21+
"paymentAddress": "CIP119:paymentAddress",
22+
"givenName": "CIP119:givenName",
23+
"image": "CIP119:image",
24+
"objectives": "CIP119:objectives",
25+
"motivations": "CIP119:motivations",
26+
"qualifications": "CIP119:qualifications",
27+
"doNotList": "CIP119:doNotList"
28+
}
29+
},
30+
"authors": {
31+
"@id": "CIP100:authors",
32+
"@container": "@set",
33+
"@context": {
34+
"name": "http://xmlns.com/foaf/0.1/name",
35+
"witness": {
36+
"@id": "CIP100:witness",
37+
"@context": {
38+
"witnessAlgorithm": "CIP100:witnessAlgorithm",
39+
"publicKey": "CIP100:publicKey",
40+
"signature": "CIP100:signature"
41+
}
42+
}
43+
}
44+
}
45+
},
46+
"authors": [],
47+
"hashAlgorithm": "blake2b-256",
48+
"body": {
49+
"givenName": "Test DRep",
50+
"doNotList": false,
51+
"motivations": "Test motivations for DRep registration",
52+
"objectives": "Test objectives for governance participation",
53+
"qualifications": "Test qualifications and experience",
54+
"paymentAddress": "addr1test000000000000000000000000000000000000000000000000000000",
55+
"references": [
56+
{
57+
"@type": "Link",
58+
"label": "Test Website",
59+
"uri": "https://example.com/test"
60+
},
61+
{
62+
"@type": "Identity",
63+
"label": "Test Identity",
64+
"uri": "https://example.com/identity"
65+
}
66+
]
67+
}
68+
}

0 commit comments

Comments
 (0)