1- from percentify import percent
1+ import pytest
2+ from percentify import percent , change , difference , split , display
3+
4+
5+ # --- percent ---
26
37def test_percent_normal ():
48 assert percent (50 , 200 ) == 25.0
@@ -8,3 +12,104 @@ 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+ # --- change ---
28+
29+ def test_change_increase ():
30+ assert change (100 , 150 ) == 50.0
31+
32+ def test_change_decrease ():
33+ assert change (200 , 150 ) == - 25.0
34+
35+ def test_change_no_change ():
36+ assert change (100 , 100 ) == 0.0
37+
38+ def test_change_zero_old ():
39+ assert change (0 , 100 ) == 0.0
40+
41+ def test_change_negative_old ():
42+ assert change (- 100 , - 50 ) == 50.0
43+
44+ def test_change_custom_decimals ():
45+ assert change (3 , 7 , 4 ) == 133.3333
46+
47+
48+ # --- difference ---
49+
50+ def test_difference_basic ():
51+ assert difference (10 , 20 ) == 66.67
52+
53+ def test_difference_same ():
54+ assert difference (50 , 50 ) == 0.0
55+
56+ def test_difference_both_zero ():
57+ assert difference (0 , 0 ) == 0.0
58+
59+ def test_difference_order_independent ():
60+ assert difference (10 , 20 ) == difference (20 , 10 )
61+
62+ def test_difference_custom_decimals ():
63+ assert difference (3 , 7 , 4 ) == 80.0
64+
65+
66+ # --- split ---
67+
68+ def test_split_equal ():
69+ assert split (100 , [1 , 1 , 1 ]) == [pytest .approx (33.33 ), pytest .approx (33.33 ), pytest .approx (33.33 )]
70+
71+ def test_split_weighted ():
72+ assert split (200 , [1 , 3 ]) == [50.0 , 150.0 ]
73+
74+ def test_split_single ():
75+ assert split (100 , [5 ]) == [100.0 ]
76+
77+ def test_split_empty ():
78+ with pytest .raises (ValueError ):
79+ split (100 , [])
80+
81+ def test_split_zero_weights ():
82+ with pytest .raises (ValueError ):
83+ split (100 , [0 , 0 ])
84+
85+ def test_split_custom_decimals ():
86+ result = split (100 , [1 , 1 , 1 ], 4 )
87+ assert result == [pytest .approx (33.3333 ), pytest .approx (33.3333 ), pytest .approx (33.3333 )]
88+
89+
90+ # --- display ---
91+
92+ def test_display_basic ():
93+ assert display (25.0 ) == "25.0%"
94+
95+ def test_display_custom_decimals ():
96+ assert display (33.3333 , 1 ) == "33.3%"
97+
98+ def test_display_custom_suffix ():
99+ assert display (50 , suffix = " percent" ) == "50.0 percent"
100+
101+ def test_display_no_rounding ():
102+ result = display (33.3333 , None )
103+ assert result == "33.3333%"
104+
105+ def test_display_zero ():
106+ assert display (0 ) == "0.0%"
107+
108+ def test_display_multiply ():
109+ assert display (0.45 , multiply = True ) == "45.0%"
110+
111+ def test_display_multiply_small ():
112+ assert display (0.0725 , multiply = True ) == "7.25%"
113+
114+ def test_display_multiply_false ():
115+ assert display (0.45 ) == "0.45%"
0 commit comments