- Replace admin_user_ids (list[int]) with admin_usernames (list[str]) - Update all service methods to use username for permission checks - Add delete button to bot responses for message cleanup - Update tests to match new implementation Note: Breaking change - existing data files need fresh start
185 lines
5.0 KiB
Python
185 lines
5.0 KiB
Python
"""Tests for core/models.py — domain dataclasses."""
|
|
|
|
import time
|
|
|
|
import pytest
|
|
|
|
from core.models import (
|
|
Bounty,
|
|
TrackedBounty,
|
|
RoomData,
|
|
TrackingData,
|
|
)
|
|
|
|
|
|
class TestBounty:
|
|
def test_create_bounty(self):
|
|
b = Bounty(
|
|
id=1,
|
|
text="Fix the bug",
|
|
link="https://github.com/example/repo/issues/1",
|
|
due_date_ts=1735689600,
|
|
created_at=1735603200,
|
|
created_by_user_id=123,
|
|
)
|
|
assert b.id == 1
|
|
assert b.text == "Fix the bug"
|
|
assert b.link == "https://github.com/example/repo/issues/1"
|
|
assert b.due_date_ts == 1735689600
|
|
assert b.created_at == 1735603200
|
|
assert b.created_by_user_id == 123
|
|
assert b.deleted_at is None
|
|
assert b.created_by_username is None
|
|
|
|
def test_bounty_with_new_fields(self):
|
|
b = Bounty(
|
|
id=1,
|
|
text="Fix the bug",
|
|
link="https://github.com/example/repo/issues/1",
|
|
due_date_ts=1735689600,
|
|
created_at=1735603200,
|
|
created_by_user_id=123,
|
|
deleted_at=1736200000,
|
|
created_by_username="johndoe",
|
|
)
|
|
assert b.deleted_at == 1736200000
|
|
assert b.created_by_username == "johndoe"
|
|
|
|
def test_bounty_optional_fields_can_be_none(self):
|
|
b = Bounty(
|
|
id=1,
|
|
text=None,
|
|
link=None,
|
|
due_date_ts=None,
|
|
created_at=0,
|
|
created_by_user_id=123,
|
|
)
|
|
assert b.text is None
|
|
assert b.link is None
|
|
assert b.due_date_ts is None
|
|
assert b.deleted_at is None
|
|
assert b.created_by_username is None
|
|
|
|
def test_bounty_comparison_equal(self):
|
|
b1 = Bounty(
|
|
id=1,
|
|
text="a",
|
|
link=None,
|
|
due_date_ts=None,
|
|
created_at=0,
|
|
created_by_user_id=123,
|
|
)
|
|
b2 = Bounty(
|
|
id=1,
|
|
text="a",
|
|
link=None,
|
|
due_date_ts=None,
|
|
created_at=0,
|
|
created_by_user_id=123,
|
|
)
|
|
assert b1 == b2
|
|
|
|
def test_bounty_comparison_not_equal(self):
|
|
b1 = Bounty(
|
|
id=1,
|
|
text="a",
|
|
link=None,
|
|
due_date_ts=None,
|
|
created_at=0,
|
|
created_by_user_id=123,
|
|
)
|
|
b2 = Bounty(
|
|
id=2,
|
|
text="b",
|
|
link=None,
|
|
due_date_ts=None,
|
|
created_at=0,
|
|
created_by_user_id=456,
|
|
)
|
|
assert b1 != b2
|
|
|
|
|
|
class TestTrackedBounty:
|
|
def test_create_tracked_bounty(self):
|
|
tb = TrackedBounty(bounty_id=5, created_at=1735600000)
|
|
assert tb.bounty_id == 5
|
|
assert tb.created_at == 1735600000
|
|
|
|
def test_tracked_bounty_comparison(self):
|
|
tb1 = TrackedBounty(bounty_id=1, created_at=0)
|
|
tb2 = TrackedBounty(bounty_id=1, created_at=0)
|
|
assert tb1 == tb2
|
|
|
|
|
|
class TestRoomData:
|
|
def test_create_group_room_data(self):
|
|
rd = RoomData(
|
|
room_id=-1001,
|
|
bounties=[],
|
|
next_id=1,
|
|
)
|
|
assert rd.room_id == -1001
|
|
assert rd.bounties == []
|
|
assert rd.next_id == 1
|
|
assert rd.timezone is None
|
|
assert rd.admin_usernames == []
|
|
|
|
def test_create_dm_room_data(self):
|
|
rd = RoomData(
|
|
room_id=123456,
|
|
bounties=[],
|
|
next_id=1,
|
|
)
|
|
assert rd.room_id == 123456
|
|
assert rd.bounties == []
|
|
assert rd.next_id == 1
|
|
assert rd.timezone is None
|
|
assert rd.admin_usernames == []
|
|
|
|
def test_room_data_with_bounties(self):
|
|
b = Bounty(
|
|
id=1,
|
|
text="Task",
|
|
link=None,
|
|
due_date_ts=None,
|
|
created_at=0,
|
|
created_by_user_id=123,
|
|
)
|
|
rd = RoomData(room_id=-1001, bounties=[b], next_id=2)
|
|
assert len(rd.bounties) == 1
|
|
assert rd.bounties[0].text == "Task"
|
|
assert rd.bounties[0].created_by_user_id == 123
|
|
|
|
def test_room_data_with_new_fields(self):
|
|
rd = RoomData(
|
|
room_id=-1001,
|
|
bounties=[],
|
|
next_id=1,
|
|
timezone="Asia/Jakarta",
|
|
admin_usernames=["alice", "bob"],
|
|
)
|
|
assert rd.timezone == "Asia/Jakarta"
|
|
assert rd.admin_usernames == ["alice", "bob"]
|
|
|
|
def test_room_data_admin_usernames_defaults_to_empty_list(self):
|
|
rd = RoomData(
|
|
room_id=-1001,
|
|
bounties=[],
|
|
next_id=1,
|
|
)
|
|
assert rd.admin_usernames == []
|
|
|
|
|
|
class TestTrackingData:
|
|
def test_create_tracking_data(self):
|
|
td = TrackingData(room_id=-1001, user_id=123456, tracked=[])
|
|
assert td.room_id == -1001
|
|
assert td.user_id == 123456
|
|
assert td.tracked == []
|
|
|
|
def test_tracking_data_with_tracked(self):
|
|
tb = TrackedBounty(bounty_id=5, created_at=0)
|
|
td = TrackingData(room_id=-1001, user_id=123, tracked=[tb])
|
|
assert len(td.tracked) == 1
|
|
assert td.tracked[0].bounty_id == 5
|