Files
cwshed-clone/tests/test_belki.py
T

283 lines
14 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):
session = 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, "самостоятельно")),
)
self.assertEqual(calculate_coaching(session).escort_points, 32.7)
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_sessions_reject_duplicate_participant_ids(self):
with self.assertRaisesRegex(ValueError, "не должны повторяться"):
CoachingSession(
"15.08", "ЛУ", "11:00", "12:00", "Белка", 1, "Первый", 2,
extra_kittens=(("Дубль", 2, "самостоятельно"),),
)
with self.assertRaisesRegex(ValueError, "разными персонажами"):
TrainingSession("Белка", 1, "Белка", 1)
def test_sessions_reject_non_text_participant_names_with_a_domain_error(self):
with self.assertRaisesRegex(ValueError, "Имена участников"):
TrainingSession("Белка", 1, None, 2) # type: ignore[arg-type]
with self.assertRaisesRegex(ValueError, "дополнительного котёнка"):
CoachingSession(
"15.08", "ЛУ", "11:00", "12:00", "Белка", 1, "Первый", 2,
extra_kittens=((None, 3, "самостоятельно"),), # type: ignore[arg-type]
)
def test_zu_allows_a_non_self_kitten(self):
session = CoachingSession(
date="16.08", skill="ЗУ", start="13:00", end="13:30",
escort_name="Дикая Вишня", escort_id=1604614,
kitten_name="Муся", kitten_id=134437, kitten_independence="несамостоятельно",
)
self.assertEqual(calculate_coaching(session).escort_points, 9)
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)
self.assertEqual(points("самостоятельно", "несамостоятельно"), 24)
self.assertEqual(points("несамостоятельно", "несамостоятельно"), 30)
def test_training_is_fixed(self):
self.assertEqual(calculate_training(TrainingSession("Белка", 1, "Котёнок", 2)).points, 25)
class ReportTests(unittest.TestCase):
def test_coaching_report_uses_the_compact_copyable_format(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("15.08", text)
self.assertIn("Дикая Вишня [1604614], помог(ла) Серолап [1617140].", text)
self.assertIn("Время: 11:00 – 11: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_training_report_includes_the_web_date(self):
text = render_training(TrainingSession("Белка", 1, "Котёнок", 2, date="01.09"))
self.assertEqual(text, "#урок\n01.09\nБелка [1], провёл(-ла) урок Котёнок [2].")
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),))
with self.assertRaises(ValueError):
InspectionReport("Белка", 1, "дупло", 1, (("вьюнок", True),))
def test_inspection_resource_aliases_typos_and_all_snakes(self):
report = InspectionReport(
character_name="Белка", character_id=1, place="дупло", inspections=0,
resources=(("ПАУТИНАА", 1), ("обычный мохх", 1), ("водяной мох", 1),
("Луговая змея", 1), ("змеяя", 1), ("щитомордник", 1),
("аспид", 1), ("бурундук", 1), ("дичь", 1), ("гнездо", 1),
("ядовитая дичь", 1), ("ядовитый гриб", 1)),
)
self.assertEqual(calculate_inspection(report).points, 7 + 3 + 2 + 1 + 1 + 1 + 1 + 1 + 1 + 1)
def test_inspection_resource_aliases_cover_plain_and_water_filled_moss(self):
report = InspectionReport(
"Белка", 1, "дупло", 0,
(("мох", 1), ("наполненный водой мох", 2)),
)
result = calculate_inspection(report)
self.assertEqual(result.points, 7)
self.assertEqual([item["points"] for item in result.resource_points], [3, 4])
if __name__ == "__main__":
unittest.main()
"""Domain regression tests for validation, scoring, and report wording."""