51 lines
1.5 KiB
Python
51 lines
1.5 KiB
Python
from datetime import datetime
|
|
from zoneinfo import ZoneInfo
|
|
|
|
import pytest
|
|
|
|
from app.database import Database
|
|
from app.scheduler import _send_due_reports
|
|
|
|
|
|
class FakeBot:
|
|
def __init__(self):
|
|
self.messages = []
|
|
|
|
async def send_message(self, **kwargs):
|
|
self.messages.append(kwargs)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_family_daily_report_is_sent_to_group_only_once(tmp_path):
|
|
database = Database(tmp_path / "finance.db")
|
|
await database.initialize()
|
|
now = datetime.now(ZoneInfo("UTC"))
|
|
for user_id, name in ((10, "Алексей"), (20, "Мария")):
|
|
await database.upsert_user(
|
|
user_id=user_id,
|
|
chat_id=user_id,
|
|
username=None,
|
|
full_name=name,
|
|
timezone="UTC",
|
|
report_time="00:00",
|
|
now=now,
|
|
)
|
|
family = await database.create_family(10, now)
|
|
await database.request_family_join(20, family.invite_code, now)
|
|
await database.resolve_family_join(
|
|
owner_user_id=10,
|
|
candidate_user_id=20,
|
|
approve=True,
|
|
now=now,
|
|
)
|
|
await database.bind_family_chat(10, -100500, "Наш бюджет")
|
|
|
|
bot = FakeBot()
|
|
await _send_due_reports(bot, database)
|
|
assert [message["chat_id"] for message in bot.messages].count(10) == 1
|
|
assert [message["chat_id"] for message in bot.messages].count(20) == 1
|
|
assert [message["chat_id"] for message in bot.messages].count(-100500) == 1
|
|
|
|
await _send_due_reports(bot, database)
|
|
assert len(bot.messages) == 3
|
|
|