Skip to content

Commit 032e106

Browse files
committed
feat: add tests for key format
Signed-off-by: aceppaluni <113948612+aceppaluni@users.noreply.github.com> Signed-off-by: aceppaluni <aceppaluni@gmail.com>
1 parent 1a04e0b commit 032e106

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

tests/unit/test_key_format.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
"""Tests for the key_format module."""
2+
3+
import pytest
4+
5+
from hiero_sdk_python.crypto.private_key import PrivateKey
6+
from hiero_sdk_python.hapi.services import basic_types_pb2
7+
from hiero_sdk_python.utils.key_utils import Key, key_to_proto
8+
from hiero_sdk_python.utils.key_format import format_key
9+
10+
pytestmark = pytest.mark.unit
11+
12+
def test_format_key_ed25519():
13+
"""Test formatting an Ed25519 key."""
14+
private_key = PrivateKey.generate_ed25519()
15+
public_key = private_key.public_key()
16+
17+
proto_key = key_to_proto(public_key)
18+
formatted = format_key(proto_key)
19+
20+
expected = f"ed25519({public_key.to_bytes_raw().hex()})"
21+
22+
assert formatted == expected
23+
24+
def test_format_key_none():
25+
"""Test formatting a None key."""
26+
formatted = format_key(None)
27+
28+
assert formatted == "None"
29+
30+
def test_format_key_threshold_key():
31+
"""Test formatting a ThresholdKey."""
32+
33+
key = basic_types_pb2.Key()
34+
key.thresholdKey.threshold = 2
35+
36+
formatted = format_key(key)
37+
38+
assert formatted == "thresholdKey(...)"
39+
40+
def test_format_key_contract_id():
41+
"""Test formatting a ContractID key."""
42+
43+
key = basic_types_pb2.Key()
44+
key.contractID.shardNum = 0
45+
key.contractID.realmNum = 0
46+
key.contractID.contractNum = 5678
47+
48+
expected_inner = str(key.contractID)
49+
expected = f"contractID({expected_inner})"
50+
51+
formatted = format_key(key)
52+
53+
assert formatted == expected
54+
55+
def test_format_key_keylist():
56+
"""Test formatting a KeyList."""
57+
58+
key = basic_types_pb2.Key()
59+
key.keyList.keys.add()
60+
61+
formatted = format_key(key)
62+
63+
assert formatted == "keyList(...)"
64+
65+
def test_format_key_unknown():
66+
"""Test formatting an unknown key type."""
67+
key = basic_types_pb2.Key()
68+
# Intentionally not setting any known key type
69+
70+
formatted = format_key(key)
71+
expected = str(key).replace("\n", " ")
72+
73+
assert formatted == expected

0 commit comments

Comments
 (0)