diff --git a/app/belki/api.py b/app/belki/api.py index c535342..58028cc 100644 --- a/app/belki/api.py +++ b/app/belki/api.py @@ -27,7 +27,7 @@ class ApiError(ValueError): # Лениво загружаемые синглотоны _IDENTITY_DIRECTORY: IdentityDirectory | None = None -_CALUCALUTION_STORE: CalculationStore | None = None +_CALCULATION_STORE: CalculationStore | None = None def _identity_directory() -> IdentityDirectory: diff --git a/app/belki/domain.py b/app/belki/domain.py index ea8758a..280b6b7 100644 --- a/app/belki/domain.py +++ b/app/belki/domain.py @@ -2,6 +2,7 @@ from __future__ import annotations from dataclasses import dataclass, field from difflib import SequenceMatcher +from math import ceil import re from typing import Literal @@ -150,7 +151,7 @@ def calculate_goal( nonself_bonus = minutes_per_session * 0.5 / 5 if kitten_independence == "несамостоятельно" else 0 points_per_session = _points(minutes_per_session * ward_multiplier / 5 + nonself_bonus) remaining = _points(max(target_points - current_points, 0)) - sessions = 0 if remaining == 0 else int(-(-remaining / points_per_session)) + sessions = 0 if remaining == 0 else ceil(remaining / points_per_session) total_minutes = sessions * minutes_per_session total_hours_value = total_minutes / 60 total_hours = int(total_hours_value) if total_hours_value.is_integer() else round(total_hours_value, 2) diff --git a/tests/test_api.py b/tests/test_api.py index dbeee33..6a76b23 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -44,6 +44,16 @@ class ApiTests(unittest.TestCase): self.assertEqual(response["result"]["total_hours"], 2) self.assertIn("Осталось добрать: 24", response["message"]) + def test_calculate_and_store_uses_calculation_store(self): + with TemporaryDirectory() as directory: + with patch.dict("os.environ", {"BELKI_DATABASE_URL": "", "BELKI_HISTORY_DB": f"{directory}/history.sqlite3"}, clear=False): + api._CALCULATION_STORE = None + response = api.calculate_and_store({ + "mode": "goal", "current_points": 0, "target_points": 25, + "minutes_per_session": 20, "skill": "ЛУ", "kitten_count": 1, + }, actor_id=None, source="web") + self.assertEqual(response["result"]["sessions"], 7) + def test_calculate_inspection_payload(self): response = calculate_payload( {