1
+ """
2
+ Unit tests for the ContractId class.
3
+ """
4
+
1
5
import pytest
6
+
2
7
from hiero_sdk_python .contract .contract_id import ContractId
3
8
from hiero_sdk_python .hapi .services import basic_types_pb2
4
9
5
10
pytestmark = pytest .mark .unit
6
11
12
+
7
13
def test_default_initialization ():
8
14
"""Test ContractId initialization with default values."""
9
15
contract_id = ContractId ()
10
-
16
+
11
17
assert contract_id .shard == 0
12
18
assert contract_id .realm == 0
13
19
assert contract_id .contract == 0
14
20
21
+
15
22
def test_custom_initialization ():
16
23
"""Test ContractId initialization with custom values."""
17
24
contract_id = ContractId (shard = 1 , realm = 2 , contract = 3 )
18
-
25
+
19
26
assert contract_id .shard == 1
20
27
assert contract_id .realm == 2
21
28
assert contract_id .contract == 3
22
29
30
+
23
31
def test_str_representation ():
24
32
"""Test string representation of ContractId."""
25
33
contract_id = ContractId (shard = 1 , realm = 2 , contract = 3 )
26
-
34
+
27
35
assert str (contract_id ) == "1.2.3"
28
36
37
+
29
38
def test_str_representation_default ():
30
39
"""Test string representation of ContractId with default values."""
31
40
contract_id = ContractId ()
32
-
41
+
33
42
assert str (contract_id ) == "0.0.0"
34
43
44
+
35
45
def test_from_string_valid ():
36
46
"""Test creating ContractId from valid string format."""
37
47
contract_id = ContractId .from_string ("1.2.3" )
38
-
48
+
39
49
assert contract_id .shard == 1
40
50
assert contract_id .realm == 2
41
51
assert contract_id .contract == 3
42
52
53
+
43
54
def test_from_string_with_spaces ():
44
55
"""Test creating ContractId from string with leading/trailing spaces."""
45
56
contract_id = ContractId .from_string (" 1.2.3 " )
46
-
57
+
47
58
assert contract_id .shard == 1
48
59
assert contract_id .realm == 2
49
60
assert contract_id .contract == 3
50
61
62
+
51
63
def test_from_string_zeros ():
52
64
"""Test creating ContractId from string with zero values."""
53
65
contract_id = ContractId .from_string ("0.0.0" )
54
-
66
+
55
67
assert contract_id .shard == 0
56
68
assert contract_id .realm == 0
57
69
assert contract_id .contract == 0
58
70
59
- def test_from_string_large_numbers ():
60
- """Test creating ContractId from string with large numbers."""
61
- contract_id = ContractId .from_string ("999.888.777" )
62
-
63
- assert contract_id .shard == 999
64
- assert contract_id .realm == 888
65
- assert contract_id .contract == 777
66
71
67
72
def test_from_string_invalid_format_too_few_parts ():
68
73
"""Test creating ContractId from invalid string format with too few parts."""
69
- with pytest .raises (ValueError , match = "Invalid ContractId format. Expected 'shard.realm.contract'" ):
74
+ with pytest .raises (
75
+ ValueError , match = "Invalid ContractId format. Expected 'shard.realm.contract'"
76
+ ):
70
77
ContractId .from_string ("1.2" )
71
78
79
+
72
80
def test_from_string_invalid_format_too_many_parts ():
73
81
"""Test creating ContractId from invalid string format with too many parts."""
74
- with pytest .raises (ValueError , match = "Invalid ContractId format. Expected 'shard.realm.contract'" ):
82
+ with pytest .raises (
83
+ ValueError , match = "Invalid ContractId format. Expected 'shard.realm.contract'"
84
+ ):
75
85
ContractId .from_string ("1.2.3.4" )
76
86
87
+
77
88
def test_from_string_invalid_format_non_numeric ():
78
89
"""Test creating ContractId from invalid string format with non-numeric parts."""
79
90
with pytest .raises (ValueError ):
80
91
ContractId .from_string ("a.b.c" )
81
92
93
+
82
94
def test_from_string_invalid_format_empty ():
83
95
"""Test creating ContractId from empty string."""
84
- with pytest .raises (ValueError , match = "Invalid ContractId format. Expected 'shard.realm.contract'" ):
96
+ with pytest .raises (
97
+ ValueError , match = "Invalid ContractId format. Expected 'shard.realm.contract'"
98
+ ):
85
99
ContractId .from_string ("" )
86
100
101
+
87
102
def test_from_string_invalid_format_partial_numeric ():
88
103
"""Test creating ContractId from string with some non-numeric parts."""
89
104
with pytest .raises (ValueError ):
90
105
ContractId .from_string ("1.a.3" )
91
106
107
+
92
108
def test_to_proto ():
93
109
"""Test converting ContractId to protobuf format."""
94
110
contract_id = ContractId (shard = 1 , realm = 2 , contract = 3 )
95
111
proto = contract_id ._to_proto ()
96
-
112
+
97
113
assert isinstance (proto , basic_types_pb2 .ContractID )
98
114
assert proto .shardNum == 1
99
115
assert proto .realmNum == 2
100
116
assert proto .contractNum == 3
101
117
118
+
102
119
def test_to_proto_default_values ():
103
120
"""Test converting ContractId with default values to protobuf format."""
104
121
contract_id = ContractId ()
105
122
proto = contract_id ._to_proto ()
106
-
123
+
107
124
assert isinstance (proto , basic_types_pb2 .ContractID )
108
125
assert proto .shardNum == 0
109
126
assert proto .realmNum == 0
110
127
assert proto .contractNum == 0
111
128
129
+
112
130
def test_from_proto ():
113
131
"""Test creating ContractId from protobuf format."""
114
- proto = basic_types_pb2 .ContractID (
115
- shardNum = 1 ,
116
- realmNum = 2 ,
117
- contractNum = 3
118
- )
119
-
132
+ proto = basic_types_pb2 .ContractID (shardNum = 1 , realmNum = 2 , contractNum = 3 )
133
+
120
134
contract_id = ContractId ._from_proto (proto )
121
-
135
+
122
136
assert contract_id .shard == 1
123
137
assert contract_id .realm == 2
124
138
assert contract_id .contract == 3
125
139
140
+
126
141
def test_from_proto_zero_values ():
127
142
"""Test creating ContractId from protobuf format with zero values."""
128
- proto = basic_types_pb2 .ContractID (
129
- shardNum = 0 ,
130
- realmNum = 0 ,
131
- contractNum = 0
132
- )
133
-
143
+ proto = basic_types_pb2 .ContractID (shardNum = 0 , realmNum = 0 , contractNum = 0 )
144
+
134
145
contract_id = ContractId ._from_proto (proto )
135
-
146
+
136
147
assert contract_id .shard == 0
137
148
assert contract_id .realm == 0
138
149
assert contract_id .contract == 0
139
150
151
+
140
152
def test_roundtrip_proto_conversion ():
141
153
"""Test that converting to proto and back preserves values."""
142
154
original = ContractId (shard = 5 , realm = 10 , contract = 15 )
143
155
proto = original ._to_proto ()
144
156
reconstructed = ContractId ._from_proto (proto )
145
-
157
+
146
158
assert original .shard == reconstructed .shard
147
159
assert original .realm == reconstructed .realm
148
160
assert original .contract == reconstructed .contract
149
161
162
+
150
163
def test_roundtrip_string_conversion ():
151
164
"""Test that converting to string and back preserves values."""
152
165
original = ContractId (shard = 7 , realm = 14 , contract = 21 )
153
166
string_repr = str (original )
154
167
reconstructed = ContractId .from_string (string_repr )
155
-
168
+
156
169
assert original .shard == reconstructed .shard
157
170
assert original .realm == reconstructed .realm
158
171
assert original .contract == reconstructed .contract
159
172
173
+
160
174
def test_equality ():
161
175
"""Test ContractId equality comparison."""
162
176
contract_id1 = ContractId (shard = 1 , realm = 2 , contract = 3 )
163
177
contract_id2 = ContractId (shard = 1 , realm = 2 , contract = 3 )
164
178
contract_id3 = ContractId (shard = 1 , realm = 2 , contract = 4 )
165
-
179
+
166
180
assert contract_id1 == contract_id2
167
- assert contract_id1 != contract_id3
181
+ assert contract_id1 != contract_id3
0 commit comments