1- from percentify import percent
1+ import pytest
2+ from percentify import percent , percent_change , percent_diff , percent_distribute , percent_format
3+
4+
5+ # --- percent ---
26
37def test_percent_normal ():
48 assert percent (50 , 200 ) == 25.0
@@ -8,3 +12,95 @@ def test_percent_fraction():
812
913def test_percent_zero_division ():
1014 assert percent (5 , 0 ) == 0.0
15+
16+ def test_percent_no_rounding ():
17+ assert percent (1 , 3 , None ) == pytest .approx (33.33333333333333 )
18+
19+ def test_percent_custom_decimals ():
20+ assert percent (7 , 9 , 4 ) == 77.7778
21+
22+ def test_percent_negative_decimals ():
23+ with pytest .raises (ValueError ):
24+ percent (1 , 3 , - 1 )
25+
26+
27+ # --- percent_change ---
28+
29+ def test_percent_change_increase ():
30+ assert percent_change (100 , 150 ) == 50.0
31+
32+ def test_percent_change_decrease ():
33+ assert percent_change (200 , 150 ) == - 25.0
34+
35+ def test_percent_change_no_change ():
36+ assert percent_change (100 , 100 ) == 0.0
37+
38+ def test_percent_change_zero_old ():
39+ assert percent_change (0 , 100 ) == 0.0
40+
41+ def test_percent_change_negative_old ():
42+ assert percent_change (- 100 , - 50 ) == 50.0
43+
44+ def test_percent_change_custom_decimals ():
45+ assert percent_change (3 , 7 , 4 ) == 133.3333
46+
47+
48+ # --- percent_diff ---
49+
50+ def test_percent_diff_basic ():
51+ assert percent_diff (10 , 20 ) == 66.67
52+
53+ def test_percent_diff_same ():
54+ assert percent_diff (50 , 50 ) == 0.0
55+
56+ def test_percent_diff_both_zero ():
57+ assert percent_diff (0 , 0 ) == 0.0
58+
59+ def test_percent_diff_order_independent ():
60+ assert percent_diff (10 , 20 ) == percent_diff (20 , 10 )
61+
62+ def test_percent_diff_custom_decimals ():
63+ assert percent_diff (3 , 7 , 4 ) == 80.0
64+
65+
66+ # --- percent_distribute ---
67+
68+ def test_distribute_equal ():
69+ assert percent_distribute (100 , [1 , 1 , 1 ]) == [pytest .approx (33.33 ), pytest .approx (33.33 ), pytest .approx (33.33 )]
70+
71+ def test_distribute_weighted ():
72+ assert percent_distribute (200 , [1 , 3 ]) == [50.0 , 150.0 ]
73+
74+ def test_distribute_single ():
75+ assert percent_distribute (100 , [5 ]) == [100.0 ]
76+
77+ def test_distribute_empty ():
78+ with pytest .raises (ValueError ):
79+ percent_distribute (100 , [])
80+
81+ def test_distribute_zero_weights ():
82+ with pytest .raises (ValueError ):
83+ percent_distribute (100 , [0 , 0 ])
84+
85+ def test_distribute_custom_decimals ():
86+ result = percent_distribute (100 , [1 , 1 , 1 ], 4 )
87+ assert result == [pytest .approx (33.3333 ), pytest .approx (33.3333 ), pytest .approx (33.3333 )]
88+
89+
90+ # --- percent_format ---
91+
92+ def test_format_basic ():
93+ assert percent_format (25.0 ) == "25.0%"
94+
95+ def test_format_custom_decimals ():
96+ assert percent_format (33.3333 , 1 ) == "33.3%"
97+
98+ def test_format_custom_suffix ():
99+ assert percent_format (50 , suffix = " percent" ) == "50.0 percent"
100+
101+ def test_format_no_rounding ():
102+ result = percent_format (33.3333 , None )
103+ assert result == "33.3333%"
104+
105+ def test_format_zero ():
106+ assert percent_format (0 ) == "0.0%"
0 commit comments