|
7 | 7 | from pycardano.certificate import (
|
8 | 8 | Anchor,
|
9 | 9 | AuthCommitteeHotCertificate,
|
| 10 | + DRep, |
10 | 11 | DRepCredential,
|
| 12 | + DRepKind, |
11 | 13 | PoolRegistration,
|
12 | 14 | PoolRetirement,
|
13 | 15 | ResignCommitteeColdCertificate,
|
|
22 | 24 | from pycardano.hash import ( # plutus_script_hash,
|
23 | 25 | POOL_KEY_HASH_SIZE,
|
24 | 26 | SCRIPT_HASH_SIZE,
|
| 27 | + VERIFICATION_KEY_HASH_SIZE, |
25 | 28 | PoolKeyHash,
|
26 | 29 | ScriptHash,
|
| 30 | + VerificationKeyHash, |
27 | 31 | )
|
28 | 32 |
|
29 | 33 | TEST_ADDR = Address.from_primitive(
|
@@ -197,6 +201,104 @@ def test_drep_credential():
|
197 | 201 | assert DRepCredential.from_cbor(drep_credential_cbor_hex) == drep_credential
|
198 | 202 |
|
199 | 203 |
|
| 204 | +@pytest.mark.parametrize( |
| 205 | + "input_values,expected_kind,expected_credential,expected_exception,case_id", |
| 206 | + [ |
| 207 | + # Happy path: VERIFICATION_KEY_HASH |
| 208 | + ( |
| 209 | + [DRepKind.VERIFICATION_KEY_HASH.value, b"1" * VERIFICATION_KEY_HASH_SIZE], |
| 210 | + DRepKind.VERIFICATION_KEY_HASH, |
| 211 | + VerificationKeyHash(b"1" * VERIFICATION_KEY_HASH_SIZE), |
| 212 | + None, |
| 213 | + "verification_key_hash", |
| 214 | + ), |
| 215 | + # Happy path: SCRIPT_HASH |
| 216 | + ( |
| 217 | + [DRepKind.SCRIPT_HASH.value, b"1" * SCRIPT_HASH_SIZE], |
| 218 | + DRepKind.SCRIPT_HASH, |
| 219 | + ScriptHash(b"1" * SCRIPT_HASH_SIZE), |
| 220 | + None, |
| 221 | + "script_hash", |
| 222 | + ), |
| 223 | + # Happy path: ALWAYS_ABSTAIN |
| 224 | + ( |
| 225 | + [DRepKind.ALWAYS_ABSTAIN.value], |
| 226 | + DRepKind.ALWAYS_ABSTAIN, |
| 227 | + None, |
| 228 | + None, |
| 229 | + "always_abstain", |
| 230 | + ), |
| 231 | + # Happy path: ALWAYS_NO_CONFIDENCE |
| 232 | + ( |
| 233 | + [DRepKind.ALWAYS_NO_CONFIDENCE.value], |
| 234 | + DRepKind.ALWAYS_NO_CONFIDENCE, |
| 235 | + None, |
| 236 | + None, |
| 237 | + "always_no_confidence", |
| 238 | + ), |
| 239 | + # Error: invalid DRepKind value (not in enum) |
| 240 | + ([99], None, None, DeserializeException, "invalid_drep_kind"), |
| 241 | + # Error: valid kind but missing credential for VERIFICATION_KEY_HASH |
| 242 | + ( |
| 243 | + [DRepKind.VERIFICATION_KEY_HASH.value], |
| 244 | + None, |
| 245 | + None, |
| 246 | + IndexError, |
| 247 | + "missing_credential_verification_key_hash", |
| 248 | + ), |
| 249 | + # Error: valid kind but missing credential for SCRIPT_HASH |
| 250 | + ( |
| 251 | + [DRepKind.SCRIPT_HASH.value], |
| 252 | + None, |
| 253 | + None, |
| 254 | + IndexError, |
| 255 | + "missing_credential_script_hash", |
| 256 | + ), |
| 257 | + # Error: valid kind but extra credential for ALWAYS_ABSTAIN |
| 258 | + ( |
| 259 | + [DRepKind.ALWAYS_ABSTAIN.value, b"extra"], |
| 260 | + DRepKind.ALWAYS_ABSTAIN, |
| 261 | + None, |
| 262 | + None, |
| 263 | + "abstain_with_extra", |
| 264 | + ), |
| 265 | + # Error: valid kind but extra credential for ALWAYS_NO_CONFIDENCE |
| 266 | + ( |
| 267 | + [DRepKind.ALWAYS_NO_CONFIDENCE.value, b"extra"], |
| 268 | + DRepKind.ALWAYS_NO_CONFIDENCE, |
| 269 | + None, |
| 270 | + None, |
| 271 | + "no_confidence_with_extra", |
| 272 | + ), |
| 273 | + # Error: input is empty |
| 274 | + ([], None, None, IndexError, "empty_input"), |
| 275 | + # Error: input is not a list or tuple |
| 276 | + ("notalist", None, None, DeserializeException, "input_not_list"), |
| 277 | + ], |
| 278 | + ids=lambda p: p if isinstance(p, str) else None, |
| 279 | +) |
| 280 | +def test_drep_from_primitive( |
| 281 | + input_values, expected_kind, expected_credential, expected_exception, case_id |
| 282 | +): |
| 283 | + |
| 284 | + # Arrange |
| 285 | + # (All input values are provided via test parameters) |
| 286 | + |
| 287 | + # Act / Assert |
| 288 | + if expected_exception: |
| 289 | + with pytest.raises(expected_exception): |
| 290 | + DRep.from_primitive(input_values) |
| 291 | + else: |
| 292 | + result = DRep.from_primitive(input_values) |
| 293 | + # Assert |
| 294 | + assert result.kind == expected_kind |
| 295 | + if expected_credential is not None: |
| 296 | + assert isinstance(result.credential, type(expected_credential)) |
| 297 | + assert result.credential.payload == expected_credential.payload |
| 298 | + else: |
| 299 | + assert result.credential is None |
| 300 | + |
| 301 | + |
200 | 302 | def test_unreg_drep_certificate():
|
201 | 303 | staking_key = StakeSigningKey.from_cbor(
|
202 | 304 | "5820ff3a330df8859e4e5f42a97fcaee73f6a00d0cf864f4bca902bd106d423f02c0"
|
|
0 commit comments