-
Notifications
You must be signed in to change notification settings - Fork 16
/
tests.py
89 lines (77 loc) · 3.32 KB
/
tests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import unittest
import secrets
import random
import hashlib
from decimal import Decimal
from webcash.webcashbase import (
PublicWebcash,
SecretWebcash,
secret_to_public,
LEGALESE,
)
from webcash.exceptions import (
AmountException
)
_ACKED_DISCLOSURES = {disclosure_name: True for disclosure_name in LEGALESE.keys()}
class SecretWebcashTestCase(unittest.TestCase):
def test_bank_webcash_constructor(self):
n1 = SecretWebcash(1, 2)
self.assertEqual(n1.amount, 1)
self.assertEqual(n1.secret_value, 2)
amount = 100
secret_value = secrets.token_hex(8)
n2 = SecretWebcash(amount, secret_value)
self.assertEqual(n2.amount, amount)
self.assertTrue(type(n2.secret_value) == str)
def test_bank_webcash_string_serialization(self):
amount = 500
secret_value = "feedbeef"
n1 = SecretWebcash(amount, secret_value)
self.assertEqual(str(n1), f"e{amount}:secret:{secret_value}")
def test_bank_webcash_repr(self):
amount = 120
n1 = SecretWebcash(amount, "feedbeef")
self.assertEqual(repr(n1), f"SecretWebcash(amount=\"{amount}\", secret_value=\"feedbeef\")")
def test_amounts(self):
count = 12
amounts = [random.randrange(1,100+1) for x in range(0, count)]
webcashes = [SecretWebcash(amount=amounts[x], secret_value=secrets.token_hex(8)) for x in range(0, count)]
self.assertEqual(len(webcashes), count)
self.assertEqual(sum(amounts), sum([webcash.amount for webcash in webcashes]))
def test_small_amounts(self):
amounts = [1, Decimal("0.1"), Decimal("0.001"), Decimal("0.0001"), Decimal("0.00001"), Decimal("0.00000001"), Decimal("0.12345678")]
webcashes = [SecretWebcash(amount=amount, secret_value=secrets.token_hex(8)) for amount in amounts]
assert all([webcashes[x].amount == amounts[x] for x in range(0, len(amounts))])
assert all([SecretWebcash.deserialize(str(webcash)) == webcash for webcash in webcashes])
assert all([SecretWebcash.deserialize(str(webcash)).amount == webcash.amount for webcash in webcashes])
expectations = [
{ "val": Decimal("1E-8"),
"in": "e1E-8:secret:feedbeef",
"out": "e0.00000001:secret:feedbeef"
},
{ "val": Decimal("1E-8"),
"in": "e0.00000001:secret:feedbeef",
"out": "e0.00000001:secret:feedbeef"
},
{ "val": Decimal("1E-6"),
"in": "e1E-6:secret:feedbeef",
"out": "e0.000001:secret:feedbeef"
},
{ "val": Decimal("1E-6"),
"in": "e0.00000100:secret:feedbeef",
"out": "e0.000001:secret:feedbeef"
},
{ "val": Decimal("100.001"),
"in": "e100.00100000:secret:feedbeef",
"out": "e100.001:secret:feedbeef"
},
];
for exp in expectations:
swc = SecretWebcash.deserialize(exp["in"])
self.assertEqual(str(swc), exp["out"])
self.assertEqual(swc.amount, exp["val"])
def test_invalid_amounts(self):
amount = Decimal("0.123456789") # too many decimals
self.assertRaises(AmountException, SecretWebcash, amount=amount, secret_value=secrets.token_hex(8) )
if __name__ == "__main__":
unittest.main()