/
test_currency_tools.py
99 lines (81 loc) · 3.39 KB
/
test_currency_tools.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
90
91
92
93
94
95
96
97
98
99
import decimal
from hypothesis import given
from hypothesis import strategies as st
import pytest
from eth_utils.currency import MAX_WEI, MIN_WEI, from_wei, to_wei, units
@given(
amount_in_wei=st.integers(min_value=MIN_WEI, max_value=MAX_WEI),
intermediate_unit=st.sampled_from(tuple(units.keys())),
)
def test_conversion_round_trip(amount_in_wei, intermediate_unit):
intermediate_amount = from_wei(amount_in_wei, intermediate_unit)
result_amount = to_wei(intermediate_amount, intermediate_unit)
assert result_amount == amount_in_wei
MAX_ETHER_WHOLE = 115792089237316195423570985008687907853269984665640564039457
MAX_ETHER_DECIMAL_MAX = 584007913129639935
MAX_ETHER_DECIMAL = 999999999999999999
def make_ether_string_value(amount_in_wei):
s_amount_in_wei = str(amount_in_wei)
whole_part = s_amount_in_wei[:-18] or "0"
decimal_part = s_amount_in_wei[-18:]
s_amount_in_ether = "{0}.{1}".format(
whole_part, decimal_part.zfill(18).rstrip("0")
).rstrip(".")
return s_amount_in_ether
@given(st.integers(min_value=0, max_value=MAX_WEI).map(make_ether_string_value))
def test_conversion_revers_round_trip_trip(amount_in_ether):
intermediate_amount = to_wei(amount_in_ether, "ether")
result_amount = from_wei(intermediate_amount, "ether")
assert decimal.Decimal(result_amount) == decimal.Decimal(str(amount_in_ether))
@pytest.mark.parametrize(
"value,expected",
[
([1000000000000000000, "wei"], "1000000000000000000"),
([1000000000000000000, "kwei"], "1000000000000000"),
([1000000000000000000, "mwei"], "1000000000000"),
([1000000000000000000, "gwei"], "1000000000"),
([1000000000000000000, "szabo"], "1000000"),
([1000000000000000000, "finney"], "1000"),
([1000000000000000000, "ether"], "1"),
([1000000000000000000, "kether"], "0.001"),
([1000000000000000000, "grand"], "0.001"),
([1000000000000000000, "mether"], "0.000001"),
([1000000000000000000, "gether"], "0.000000001"),
([1000000000000000000, "tether"], "0.000000000001"),
],
)
def test_from_wei(value, expected):
assert from_wei(*value) == decimal.Decimal(expected)
@pytest.mark.parametrize(
"value,expected",
[
([1, "wei"], "1"),
([1, "kwei"], "1000"),
([1, "Kwei"], "1000"),
([1, "babbage"], "1000"),
([1, "mwei"], "1000000"),
([1, "Mwei"], "1000000"),
([1, "lovelace"], "1000000"),
([1, "gwei"], "1000000000"),
([1, "Gwei"], "1000000000"),
([1, "shannon"], "1000000000"),
([1, "szabo"], "1000000000000"),
([1, "finney"], "1000000000000000"),
([1, "ether"], "1000000000000000000"),
([1, "kether"], "1000000000000000000000"),
([1, "grand"], "1000000000000000000000"),
([1, "mether"], "1000000000000000000000000"),
([1, "gether"], "1000000000000000000000000000"),
([1, "tether"], "1000000000000000000000000000000"),
([0.05, "ether"], "50000000000000000"),
([1.2, "ether"], "1200000000000000000"),
],
)
def test_to_wei(value, expected):
assert to_wei(*value) == decimal.Decimal(expected)
@pytest.mark.parametrize("value,unit", ((1, "wei1"), (1, "not-a-unit"), (-1, "ether")))
def test_invalid_to_wei_values(value, unit):
with pytest.raises(ValueError):
to_wei(value, unit)
with pytest.raises(ValueError):
from_wei(value, unit)