1import unittest
2
3import solution
4
5
6class TestSanity(unittest.TestCase):
7 """Check if all data is present."""
8
9 def test_requirements(self):
10 names = [
11 "курс_в_лева",
12 "валута_към_левчета",
13 "е_патриотична",
14 ]
15 unimported = [name for name in names if name not in dir(solution)]
16 self.assertEqual(
17 unimported, [], "\n\nЕлементите по-горе липсват (проверете си имената)!"
18 )
19
20
21if __name__ == "__main__":
22 unittest.main()
1import unittest
2
3from solution import *
4
5
6class TestКурсВЛева(unittest.TestCase):
7 def test_курс_в_лева(self):
8 """Output should be properly converted to BGN-relative exchange rate."""
9 exchange_rate = {"NOK": 1, "HKD": 2, "NZD": 0.5}
10 exchange_rate_in_bgn = {"NOK": 1, "HKD": 0.5, "NZD": 2}
11 self.assertEqual(exchange_rate_in_bgn, курс_в_лева(exchange_rate))
12
13 def test_курс_в_лева_rounding(self):
14 """Output should be rounded to the 4th digit after the decimal point."""
15 exchange_rate = {"NOK": 0.1740, "HKD": 0.2147, "NZD": 0.9941, "CHF": 1.1011}
16 exchange_rate_in_bgn = {
17 "NOK": 5.7471,
18 "HKD": 4.6577,
19 "NZD": 1.0059,
20 "CHF": 0.9082,
21 }
22 self.assertEqual(exchange_rate_in_bgn, курс_в_лева(exchange_rate))
23
24
25class TestВалутаКъмЛевчета(unittest.TestCase):
26 def assertContainersSomewhatEqual(self, expected, actual):
27 """Assert if the contents of the containers are equal, regardless of their type."""
28 if isinstance(expected, dict):
29 expected = list(expected.items())
30 if isinstance(actual, dict):
31 actual = list(actual.items())
32 not_found = [item for item in expected if item not in actual]
33 extra = [item for item in actual if item not in expected]
34 if (not_found, extra) != ([], []):
35 raise AssertionError(
36 "Collections mismatch (type-independent):\n"
37 "Items in the first collection but not the second:\n"
38 if not_found
39 else ""
40 f"{not_found}\n"
41 "Items in the second collection but not the second:\n"
42 if extra
43 else f"{extra}\n"
44 )
45
46 def test_валута_към_левчета_takes_variable_length_args(self):
47 """Function should take a variable length of args and kwargs."""
48 валута_към_левчета(
49 ("EUR", 1.5),
50 ("USD", 10),
51 ("DKK", 10),
52 ("EUR", 2.5),
53 ("EUR", 1.5),
54 ("USD", 10),
55 ("DKK", 10),
56 ("EUR", 2.5),
57 EUR=0.5,
58 USD=0.8,
59 DKK=7,
60 NOK=0.5,
61 NZD=0.8,
62 JPY=7,
63 )
64
65 def test_валута_към_левчета_with_bgn(self):
66 """Entries with BGN should be converted 1:1."""
67 expected_in_leva = {("HUF", 10), ("BGN", 10000.5)}
68 in_leva = валута_към_левчета(
69 ("HUF", 10),
70 ("BGN", 10000.5),
71 HUF=1,
72 )
73 self.assertContainersSomewhatEqual(expected_in_leva, in_leva)
74
75 def test_валута_към_левчета_sums(self):
76 """Entries for the same currency should be summed up."""
77 expected_in_leva = {("TRY", 6), ("JPY", 1010)}
78 in_leva = валута_към_левчета(
79 ("JPY", 10),
80 ("TRY", 5),
81 ("TRY", 1),
82 ("JPY", 1000),
83 JPY=1,
84 TRY=1,
85 )
86 self.assertContainersSomewhatEqual(expected_in_leva, in_leva)
87
88 def test_валута_към_левчета_conversion(self):
89 """Entries should be converted to BGN."""
90 expected_in_leva = {("THB", 10), ("ZAR", 50)}
91 in_leva = валута_към_левчета(
92 ("THB", 100),
93 ("ZAR", 5),
94 THB=10,
95 ZAR=0.1,
96 )
97 self.assertContainersSomewhatEqual(expected_in_leva, in_leva)
98
99 def test_валута_към_левчета_rounding(self):
100 """Output should be rounded to the 4th digit after the decimal point."""
101 expected_in_leva = {("SGD", 6.5876), ("PHP", 2.8715)}
102 in_leva = валута_към_левчета(
103 ("SGD", 5),
104 ("PHP", 100),
105 PHP=34.8251,
106 SGD=0.759,
107 )
108 self.assertContainersSomewhatEqual(expected_in_leva, in_leva)
109
110
111class TestЕПатриотична(unittest.TestCase):
112 def test_е_патриотична_цели(self):
113 """The total should be evaluated by means of whether the sum is a round number."""
114 патриотична = е_патриотична(
115 [("ASL", 10), ("PLS", 5)],
116 {"ASL": 10, "PLS": 0.1},
117 )
118 self.assertEqual("ПАТРИОТИЧНА!", патриотична)
119
120 непатриотична = е_патриотична(
121 [("ASL", 9), ("PLS", 5)],
122 {"ASL": 10, "PLS": 0.1},
123 )
124 self.assertEqual("НЕПАТРИОТИЧНА!", непатриотична)
125
126 def test_е_патриотична_стотинки(self):
127 """The total should be evaluated with a precision of 2 digits after the decimal point."""
128 патриотична = е_патриотична(
129 [("KRW", 10000), ("INR", 4856)],
130 {"KRW": 872.4512, "INR": 54.846},
131 )
132 self.assertEqual("ПАТРИОТИЧНА!", патриотична)
133
134 непатриотична = е_патриотична(
135 [("KRW", 10000), ("INR", 4860)],
136 {"KRW": 872.4512, "INR": 54.846},
137 )
138 self.assertEqual("НЕПАТРИОТИЧНА!", непатриотична)
139
140 def test_е_патриотична_no_side_effects(self):
141 """There should be no side effects on the input data."""
142 currency = [("MYR", 20), ("MYR", 25)]
143 rates = {"MYR": 0.4287}
144 е_патриотична(
145 currency,
146 rates,
147 )
148 self.assertEqual(
149 currency, [("MYR", 20), ("MYR", 25)], "Function has side effects"
150 )
151 self.assertEqual(rates, {"MYR": 0.4287}, "Function has side effects")
152
153
154if __name__ == "__main__":
155 unittest.main()
Николина Маджарова
05.03.2026 17:14В примера за курс-в-лева при EUR = 1.9558 се изчислява 1 / 1.9558 = 0.51129…, което се закръглява на 0.5113, т.е. петият знак се взима предвид.
В примера за валута-към-левчета при 10 / 7 = 1.42857… се закръглява на 1.4285, т.е. петият знак не се взима предвид.
Кое закръгляне е правилното, понеже в "уговорките" за двете функции пише един и същ начин?
| |
Ариф Мехмедали
05.03.2026 09:10В описанието на функцията е_патриотична пише, че стойностите в речника са "брой единици чуждестранна валута за едно левче". Например 1BGN = 0.5EUR.
Ако имаме 4EUR, не трябва ли да ги конвертираме като 4/0.5, а не като 4 * 0.5, както е в примера?
Нишка
|
Нишка
05.03.2026 17:45