114 lines
5.2 KiB
Python
114 lines
5.2 KiB
Python
import unittest
|
|
import sqlite3
|
|
from tempfile import TemporaryDirectory
|
|
from unittest.mock import patch
|
|
|
|
from app.belki import api
|
|
from app.belki.api import ApiError, calculate_payload, inspect_bulk_payload, resolve_identity_payload
|
|
from app.belki.history import CalculationStore
|
|
from app.belki.identity import IdentityDirectory
|
|
|
|
|
|
class ApiTests(unittest.TestCase):
|
|
def test_calculate_coaching_payload(self):
|
|
response = calculate_payload(
|
|
{
|
|
"mode": "coaching",
|
|
"date": "15.08",
|
|
"skill": "ЛУ",
|
|
"start": "00:52",
|
|
"end": "01:52",
|
|
"escort_name": "Дикая Вишня",
|
|
"escort_id": 1604614,
|
|
"kitten_name": "Котёнок",
|
|
"kitten_id": 1676767,
|
|
"kitten_independence": "самостоятельно",
|
|
}
|
|
)
|
|
self.assertEqual(response["result"]["minutes"], 60)
|
|
self.assertIn("Баллы белки: 12", response["message"])
|
|
|
|
def test_calculate_rejects_unknown_mode(self):
|
|
with self.assertRaises(ApiError):
|
|
calculate_payload({"mode": "unknown"})
|
|
|
|
def test_goal_calculator_returns_remaining_points_and_hours(self):
|
|
response = calculate_payload({
|
|
"mode": "goal", "current_points": 10, "target_points": 34,
|
|
"minutes_per_session": 20, "skill": "ЛУ",
|
|
"kitten_independence": "самостоятельно", "kitten_count": 1,
|
|
})
|
|
self.assertEqual(response["result"]["remaining_points"], 24)
|
|
self.assertEqual(response["result"]["points_per_session"], 4)
|
|
self.assertEqual(response["result"]["sessions"], 6)
|
|
self.assertEqual(response["result"]["total_hours"], 2)
|
|
self.assertIn("Осталось добрать: 24", response["message"])
|
|
|
|
def test_goal_calculator_rejects_non_finite_json_number(self):
|
|
with self.assertRaisesRegex(ApiError, "конечным числом"):
|
|
calculate_payload({
|
|
"mode": "goal", "current_points": "NaN", "target_points": 34,
|
|
"minutes_per_session": 20, "kitten_count": 1,
|
|
})
|
|
|
|
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(
|
|
{
|
|
"mode": "inspection",
|
|
"character_name": "Дикая Вишня",
|
|
"character_id": 1604614,
|
|
"place": "дупло",
|
|
"inspections": 6,
|
|
"resources": [["вьюнок", 2], ["паук", 1]],
|
|
}
|
|
)
|
|
self.assertIn("осмотрел(-а) дупло 6 раз", response["message"])
|
|
self.assertEqual(response["result"]["points"], 15)
|
|
|
|
def test_bulk_inspection_payload_explains_missing_fields(self):
|
|
response = inspect_bulk_payload({"text": "/лу 15.08 11:00 11:20 | Белка [1] | Котёнок [2]"})
|
|
self.assertEqual(response["status"], "needs_clarification")
|
|
self.assertTrue(response["questions"])
|
|
|
|
def test_identity_resolution_without_directory_requests_missing_half(self):
|
|
with patch.object(api, "_IDENTITY_DIRECTORY", IdentityDirectory()):
|
|
response = resolve_identity_payload({"character_id": 1617140})
|
|
self.assertEqual(response["status"], "needs_input")
|
|
self.assertIn("имя", response["message"].lower())
|
|
|
|
def test_public_identity_completion_does_not_write_shared_directory(self):
|
|
directory = IdentityDirectory()
|
|
with patch.object(api, "_IDENTITY_DIRECTORY", directory):
|
|
response = resolve_identity_payload({"name": "Серолап", "character_id": 1617140, "remember": True})
|
|
self.assertEqual(response["status"], "provided")
|
|
self.assertIsNone(directory.resolve(character_id=1617140))
|
|
|
|
def test_calculation_store_persists_full_record(self):
|
|
with TemporaryDirectory() as directory:
|
|
path = f"{directory}/history.sqlite3"
|
|
store = CalculationStore(sqlite_path=path)
|
|
response = calculate_payload({
|
|
"mode": "training", "escort_name": "Дикая Вишня", "escort_id": 1604614,
|
|
"kitten_name": "Серолап", "kitten_id": 1617140,
|
|
})
|
|
record_id = store.save(actor_id=700, source="vk", payload={"mode": "training"}, response=response)
|
|
self.assertGreater(record_id, 0)
|
|
with sqlite3.connect(path) as connection:
|
|
row = connection.execute("SELECT actor_id, mode, source, message FROM calculations").fetchone()
|
|
self.assertEqual(row[:3], (700, "training", "vk"))
|
|
self.assertIn("#урок", row[3])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|