1- from unittest import TestCase
1+ import pytest
22
3- from pycardano .address import Address , PointerAddress
4- from pycardano .exception import DeserializeException
3+ from pycardano .address import Address , AddressType , PointerAddress
4+ from pycardano .exception import (
5+ DecodingException ,
6+ DeserializeException ,
7+ InvalidAddressInputException ,
8+ )
9+ from pycardano .hash import (
10+ SCRIPT_HASH_SIZE ,
11+ VERIFICATION_KEY_HASH_SIZE ,
12+ ScriptHash ,
13+ VerificationKeyHash ,
14+ )
515from pycardano .key import PaymentVerificationKey
616from pycardano .network import Network
717
@@ -20,25 +30,184 @@ def test_payment_addr():
2030 )
2131
2232
23- class PointerAddressTest (TestCase ):
24- def test_from_primitive_invalid_value (self ):
25- with self .assertRaises (DeserializeException ):
26- PointerAddress .from_primitive (1 )
33+ def test_to_primitive_pointer_addr ():
34+ assert PointerAddress (1 , 2 , 3 ).to_primitive () == b"\x01 \x02 \x03 "
2735
28- with self .assertRaises (DeserializeException ):
29- PointerAddress .from_primitive ([])
3036
31- with self .assertRaises (DeserializeException ):
32- PointerAddress .from_primitive ({})
37+ def test_from_primitive_pointer_addr ():
38+ assert PointerAddress .from_primitive (
39+ b"\x01 \x02 \x03 "
40+ ) == PointerAddress .from_primitive (b"\x01 \x02 \x03 " )
3341
3442
35- class AddressTest (TestCase ):
36- def test_from_primitive_invalid_value (self ):
37- with self .assertRaises (DeserializeException ):
38- Address .from_primitive (1 )
43+ def test_from_primitive_invalid_value_pointer_addr ():
44+ with pytest .raises (DecodingException ):
45+ PointerAddress .decode (data = b"\x01 \x02 " )
3946
40- with self . assertRaises (DeserializeException ):
41- Address .from_primitive ([] )
47+ with pytest . raises (DeserializeException ):
48+ PointerAddress .from_primitive (1 )
4249
43- with self .assertRaises (DeserializeException ):
44- Address .from_primitive ({})
50+ with pytest .raises (DeserializeException ):
51+ PointerAddress .from_primitive ([])
52+
53+ with pytest .raises (DeserializeException ):
54+ PointerAddress .from_primitive ({})
55+
56+
57+ def test_equality_pointer_addr ():
58+ assert PointerAddress (1 , 2 , 3 ) == PointerAddress (1 , 2 , 3 )
59+
60+
61+ def test_inequality_different_values_pointer_addr ():
62+ assert PointerAddress (1 , 2 , 3 ) != PointerAddress (4 , 5 , 6 )
63+
64+
65+ def test_inequality_not_pointer_addr ():
66+ assert PointerAddress (1 , 2 , 3 ) != (1 , 2 , 3 )
67+
68+
69+ def test_inequality_null_pointer_addr ():
70+ assert PointerAddress (1 , 2 , 3 ) != None
71+
72+
73+ def test_self_equality_pointer_addr ():
74+ assert PointerAddress (1 , 2 , 3 ) == PointerAddress (1 , 2 , 3 )
75+
76+
77+ def test_from_primitive_invalid_value_addr ():
78+ with pytest .raises (DeserializeException ):
79+ Address .from_primitive (1 )
80+
81+ with pytest .raises (DeserializeException ):
82+ Address .from_primitive ([])
83+
84+ with pytest .raises (DeserializeException ):
85+ Address .from_primitive ({})
86+
87+
88+ def test_key_script_addr ():
89+ address = Address (
90+ VerificationKeyHash (b"1" * VERIFICATION_KEY_HASH_SIZE ),
91+ ScriptHash (b"1" * SCRIPT_HASH_SIZE ),
92+ )
93+ assert address .address_type == AddressType .KEY_SCRIPT
94+
95+
96+ def test_script_key_addr ():
97+ address = Address (
98+ ScriptHash (b"1" * SCRIPT_HASH_SIZE ),
99+ VerificationKeyHash (b"1" * VERIFICATION_KEY_HASH_SIZE ),
100+ )
101+ assert address .address_type == AddressType .SCRIPT_KEY
102+
103+
104+ def test_script_point_addr ():
105+ address = Address (ScriptHash (b"1" * SCRIPT_HASH_SIZE ), PointerAddress (1 , 2 , 3 ))
106+ assert address .address_type == AddressType .SCRIPT_POINTER
107+
108+
109+ def test_none_script_hash_addr ():
110+ address = Address (None , ScriptHash (b"1" * SCRIPT_HASH_SIZE ))
111+ assert address .address_type == AddressType .NONE_SCRIPT
112+
113+
114+ def test_invalid_combination_unhandled_types_addr ():
115+ class UnknownType :
116+ pass
117+
118+ with pytest .raises (InvalidAddressInputException ):
119+ Address (UnknownType (), UnknownType ())
120+
121+
122+ def test_equality_same_values_addr ():
123+ a1 = Address (
124+ VerificationKeyHash (b"1" * VERIFICATION_KEY_HASH_SIZE ),
125+ ScriptHash (b"1" * SCRIPT_HASH_SIZE ),
126+ )
127+ a2 = Address (
128+ VerificationKeyHash (b"1" * VERIFICATION_KEY_HASH_SIZE ),
129+ ScriptHash (b"1" * SCRIPT_HASH_SIZE ),
130+ )
131+ assert a1 == a2
132+
133+
134+ def test_inequality_not_address_addr ():
135+ a1 = Address (
136+ VerificationKeyHash (b"1" * VERIFICATION_KEY_HASH_SIZE ),
137+ ScriptHash (b"1" * SCRIPT_HASH_SIZE ),
138+ )
139+ not_address = (1 , 2 , 3 )
140+ assert a1 != not_address
141+
142+
143+ def test_from_primitive_address_type_key_script_addr ():
144+ header = AddressType .KEY_SCRIPT .value << 4
145+ payment = b"\x01 " * VERIFICATION_KEY_HASH_SIZE
146+ staking = b"\x02 " * SCRIPT_HASH_SIZE
147+ value = bytes ([header ]) + payment + staking
148+
149+ address = Address .from_primitive (value )
150+
151+ assert isinstance (address .payment_part , VerificationKeyHash )
152+
153+ assert isinstance (address .staking_part , ScriptHash )
154+
155+
156+ def test_from_primitive_type_verification_key_hash_addr ():
157+ header = AddressType .KEY_POINTER .value << 4
158+ payment = b"\x01 " * VERIFICATION_KEY_HASH_SIZE
159+ staking = b"\x01 \x02 \x03 "
160+ value = bytes ([header ]) + payment + staking
161+
162+ address = Address .from_primitive (value )
163+
164+ assert isinstance (address .payment_part , VerificationKeyHash )
165+
166+ assert isinstance (address .staking_part , PointerAddress )
167+
168+
169+ def test_from_primitive_staking_script_hash_addr ():
170+ header = AddressType .SCRIPT_KEY .value << 4
171+ payment = b"\x01 " * SCRIPT_HASH_SIZE
172+ staking = b"\x02 " * VERIFICATION_KEY_HASH_SIZE
173+ value = bytes ([header ]) + payment + staking
174+
175+ address = Address .from_primitive (value )
176+
177+ assert isinstance (address .payment_part , ScriptHash )
178+
179+ assert isinstance (address .staking_part , VerificationKeyHash )
180+
181+
182+ def test_from_primitive_payment_script_hash_addr ():
183+ header = AddressType .SCRIPT_POINTER .value << 4
184+ payment = b"\x01 " * SCRIPT_HASH_SIZE
185+ staking = b"\x01 \x02 \x03 "
186+ value = bytes ([header ]) + payment + staking
187+
188+ address = Address .from_primitive (value )
189+
190+ assert isinstance (address .payment_part , ScriptHash )
191+
192+
193+ def test_from_primitive_type_none_addr ():
194+ header = AddressType .NONE_SCRIPT .value << 4
195+ payment = b"\x01 " * 14
196+ staking = b"\x02 " * 14
197+ value = bytes ([header ]) + payment + staking
198+
199+ address = Address .from_primitive (value )
200+
201+ assert address .payment_part is None
202+
203+ assert isinstance (address .staking_part , ScriptHash )
204+
205+
206+ def test_from_primitive_invalid_type_addr ():
207+ header = AddressType .BYRON .value << 4
208+ payment = b"\x01 " * 14
209+ staking = b"\x02 " * 14
210+ value = bytes ([header ]) + payment + staking
211+
212+ with pytest .raises (DeserializeException ):
213+ Address .from_primitive (value )
0 commit comments