Files
cwshed-clone/tests/test_belki.py
T

263 lines
12 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import unittest
from math import inf, nan
from app.belki.domain import (
CoachingSession,
InspectionReport,
TrainingSession,
calculate_coaching,
calculate_inspection,
calculate_goal,
calculate_training,
format_clock,
parse_clock,
parse_report_date,
)
from app.belki.reports import render_coaching, render_inspection, render_training
class ClockTests(unittest.TestCase):
def test_duration_supports_midnight(self):
self.assertEqual(parse_clock("23:55"), 23 * 60 + 55)
self.assertEqual(format_clock(parse_clock("23:05")), "23:05")
self.assertEqual(
CoachingSession.duration_minutes("23:55", "00:10"),
15,
)
def test_invalid_clock_is_rejected(self):
with self.assertRaises(ValueError):
parse_clock("5:5")
def test_report_date_requires_a_real_calendar_day(self):
self.assertEqual(parse_report_date("29.02"), (29, 2))
for value in ("31.02", "32.01", "15/08", "today"):
with self.subTest(value=value), self.assertRaises(ValueError):
parse_report_date(value)
class CalculationTests(unittest.TestCase):
def test_goal_rejects_non_finite_and_unbounded_input(self):
for current_points, target_points in ((nan, 1), (1, inf)):
with self.assertRaisesRegex(ValueError, "конечным числом"):
calculate_goal(
current_points=current_points, target_points=target_points,
minutes_per_session=20, kitten_count=1,
)
with self.assertRaisesRegex(ValueError, "от 1 до 1440"):
calculate_goal(current_points=0, target_points=1, minutes_per_session=1441)
with self.assertRaisesRegex(ValueError, "от 1 до 50"):
calculate_goal(current_points=0, target_points=1, minutes_per_session=20, kitten_count=51)
def test_self_sufficient_kitten(self):
result = calculate_coaching(
CoachingSession(
date="15.08",
skill="ЛУ",
start="00:52",
end="01:52",
escort_name="Дикая Вишня",
escort_id=1604614,
kitten_name="Котёнок",
kitten_id=1676767,
kitten_independence="самостоятельно",
)
)
self.assertEqual(result.minutes, 60)
self.assertEqual(result.escort_points, 12)
self.assertEqual(result.kitten_points, 12)
def test_non_self_kitten_and_extra_companion(self):
with self.assertRaisesRegex(ValueError, "Для ЗУ все котята должны быть самостоятельными"):
CoachingSession(
date="15.08",
skill="ЗУ",
start="11:00",
end="12:00",
escort_name="Белка",
escort_id=1,
kitten_name="Первый",
kitten_id=2,
kitten_independence="несамостоятельно",
extra_kittens=(
("Второй", 3, "несамостоятельно"),
("Третий", 4, "самостоятельно"),
),
)
def test_domain_rejects_excessive_coaching_and_inspection_input(self):
extras = tuple((f"Котёнок {index}", index + 2, "самостоятельно") for index in range(50))
with self.assertRaisesRegex(ValueError, "не более 50 котят"):
CoachingSession("15.08", "ЛУ", "11:00", "12:00", "Белка", 1, "Первый", 2, extra_kittens=extras)
with self.assertRaisesRegex(ValueError, "от 0 до 100000"):
InspectionReport("Белка", 1, "дупло", 100001)
with self.assertRaisesRegex(ValueError, "не более 100 ресурсов"):
InspectionReport("Белка", 1, "дупло", 1, tuple(("вьюнок", 1) for _ in range(101)))
def test_zu_rejects_non_self_kitten(self):
with self.assertRaisesRegex(ValueError, "Для ЗУ все котята должны быть самостоятельными"):
CoachingSession(
date="16.08",
skill="ЗУ",
start="13:00",
end="13:30",
escort_name="Дикая Вишня",
escort_id=1604614,
kitten_name="Муся",
kitten_id=134437,
kitten_independence="несамостоятельно",
)
def test_non_self_60_minutes_gives_adult_18_and_kitten_12(self):
result = calculate_coaching(
CoachingSession(
date="16.08",
skill="ЛУ",
start="13:00",
end="14:00",
escort_name="Дикая Вишня",
escort_id=1604614,
kitten_name="Муся",
kitten_id=134437,
kitten_independence="несамостоятельно",
)
)
self.assertEqual(result.escort_points, 18)
self.assertEqual(result.kitten_points, 12)
def test_additional_wards_use_multipliers_instead_of_full_sum(self):
base = CoachingSession("15.08", "ЛУ", "11:00", "12:00", "Белка", 1, "Первый", 2)
session = CoachingSession(
"15.08", "ЛУ", "11:00", "12:00", "Белка", 1, "Первый", 2,
extra_kittens=(("Второй", 3, "самостоятельно"), ("Третий", 4, "самостоятельно")),
)
self.assertEqual(calculate_coaching(base).escort_points, 12)
result = calculate_coaching(session)
self.assertEqual(result.kitten_count, 3)
self.assertEqual(result.escort_multiplier, 1.725)
self.assertEqual(result.escort_points, 20.7)
def test_nonself_additional_ward_adds_half_base_bonus(self):
with self.assertRaisesRegex(ValueError, "только один"):
CoachingSession(
"15.08", "ЛУ", "10:00", "10:55", "Белка", 1, "Первый", 2,
kitten_independence="несамостоятельно",
extra_kittens=(("Второй", 3, "несамостоятельно"),),
)
def test_two_nonself_wards_add_two_bonuses(self):
with self.assertRaisesRegex(ValueError, "только один"):
CoachingSession(
"15.08", "ЛУ", "10:00", "10:55", "Белка", 1, "Первый", 2,
kitten_independence="несамостоятельно",
extra_kittens=(("Второй", 3, "несамостоятельно"),),
)
def test_zu_independence_matrix_matches_reported_values(self):
def points(primary, extra):
return calculate_coaching(
CoachingSession(
"16.08", "ЗУ", "10:00", "11:00", "Белка", 1, "Первый", 2,
kitten_independence=primary,
extra_kittens=(("Второй", 3, extra),),
)
).escort_points
self.assertEqual(points("самостоятельно", "самостоятельно"), 18)
with self.assertRaisesRegex(ValueError, "Для ЗУ все котята должны быть самостоятельными"):
points("самостоятельно", "несамостоятельно")
with self.assertRaisesRegex(ValueError, "Для ЗУ все котята должны быть самостоятельными"):
points("несамостоятельно", "несамостоятельно")
def test_training_is_fixed(self):
self.assertEqual(calculate_training(TrainingSession("Белка", 1, "Котёнок", 2)).points, 25)
class ReportTests(unittest.TestCase):
def test_coaching_report_contains_new_fields_without_screenshot_placeholder(self):
session = CoachingSession(
date="15.08",
skill="ЛУ",
start="11:00",
end="11:20",
escort_name="Дикая Вишня",
escort_id=1604614,
kitten_name="Серолап",
kitten_id=1617140,
kitten_independence="несамостоятельно",
)
text = render_coaching(session, calculate_coaching(session))
self.assertIn("#лазание", text)
self.assertIn("Дикая Вишня [1604614], помог(-ла) Серолап [1617140] прокачать ЛУ.", text)
self.assertIn("Время: 11:00 – 11:20 [20 минут].", text)
self.assertIn("Баллы белки: 6 баллов.", text)
self.assertIn("Баллы бельчонка: 4 балла.", text)
self.assertNotIn("Скриншот", text)
self.assertNotIn("истории", text)
def test_coaching_report_uses_plural_for_multiple_kittens(self):
session = CoachingSession(
"15.08", "ЛУ", "11:00", "11:20", "Белка", 1, "Первый", 2,
extra_kittens=(("Второй", 3, "самостоятельно"),),
)
self.assertIn("помог(-ла) котятам прокачать ЛУ: Первый [2], Второй [3].", render_coaching(session, calculate_coaching(session)))
def test_training_report_is_separate(self):
text = render_training(TrainingSession("Белка", 1, "Котёнок", 2))
self.assertIn("#урок", text)
self.assertIn("провёл(-ла) урок", text)
def test_inspection_report_lists_zero_and_resources(self):
text = render_inspection(
InspectionReport(
character_name="Дикая Вишня",
character_id=1604614,
place="дупло",
inspections=6,
resources=(("вьюнок", 2), ("паук", 1)),
)
)
self.assertIn("осмотрел(-а) дупло 6 раз", text)
self.assertIn("#осмотр", text)
self.assertIn("+2 вьюнок.", text)
self.assertIn("+1 паук.", text)
self.assertNotIn("Скриншот", text)
self.assertIn("Баллы осмотра: 15 баллов.", text)
def test_inspection_report_explains_an_empty_resource_list(self):
self.assertIn("Ресурсов не найдено.", render_inspection(InspectionReport("Белка", 1, "дупло", 1)))
def test_spider_and_web_have_different_values(self):
report = InspectionReport(
character_name="Дикая Вишня", character_id=1604614, place="дупло", inspections=4,
resources=(("вьюнок", 1), ("паутина", 1), ("паук", 2)),
)
self.assertEqual(calculate_inspection(report).points, 18)
def test_inspection_count_uses_correct_russian_form(self):
for count, phrase in ((1, "1 раз"), (2, "2 раза"), (4, "4 раза"), (5, "5 раз"), (21, "21 раз"), (24, "24 раза")):
report = InspectionReport("Белка", 1, "дупло", count)
self.assertIn(phrase, render_inspection(report))
def test_inspection_points_include_attempts_and_resource_values(self):
report = InspectionReport(
character_name="Дикая Вишня", character_id=1604614,
place="дупло", inspections=2,
resources=(("паутина", 1), ("надпись", 1)),
)
self.assertEqual(calculate_inspection(report).points, 11)
self.assertIn("Баллы осмотра: 11 баллов.", render_inspection(report))
def test_inspection_rejects_negative_resources(self):
with self.assertRaises(ValueError):
InspectionReport("Белка", 1, "дупло", 1, (("вьюнок", -1),))
def test_inspection_resource_aliases_and_typos(self):
report = InspectionReport(
character_name="Белка", character_id=1, place="дупло", inspections=0,
resources=(("ПАУТИНАА", 1), ("обычный мохх", 1), ("водяной мох", 1),
("Луговая змея", 1), ("змеяя", 1), ("бурундук", 1)),
)
self.assertEqual(calculate_inspection(report).points, 7 + 3 + 2 + 1 + 0 + 1)
if __name__ == "__main__":
unittest.main()