Files
devplacepy/tests/unit/database/notifications.py
T

114 lines
3.7 KiB
Python
Raw Normal View History

# retoor <retoor@molodetz.nl>
from datetime import datetime, timezone
from devplacepy.database import (
NOTIFICATION_GROUP_KEYS,
count_unread_groups,
get_notification_grouping_enabled,
get_notification_throttle_enabled,
notification_group_key,
set_notification_grouping_enabled,
set_notification_throttle_enabled,
)
from devplacepy.utils import generate_uid
def _row(ntype, related="actor-1", target="/posts/x", uid=None):
return {
"type": ntype,
"related_uid": related,
"target_url": target,
"uid": uid or generate_uid(),
}
def test_group_key_actor_target_single():
assert notification_group_key(_row("message")) == ("message", "actor-1", "/posts/x")
assert notification_group_key(_row("vote", related="a")) == notification_group_key(
_row("vote", related="b")
)
assert notification_group_key(_row("follow", related="a", target="/profile/a")) == ("follow",)
assert notification_group_key(_row("follow", related="b", target="/profile/b")) == ("follow",)
single = _row("badge")
assert notification_group_key(single) == ("badge", single["uid"])
def test_group_key_unlisted_type_is_single():
row = _row("some_future_type")
assert NOTIFICATION_GROUP_KEYS.get("some_future_type", "single") == "single"
assert notification_group_key(row) == ("some_future_type", row["uid"])
def test_all_registered_types_have_a_group_shape():
from devplacepy.database import NOTIFICATION_TYPES
for entry in NOTIFICATION_TYPES:
assert NOTIFICATION_GROUP_KEYS[entry["key"]] in ("actor", "target", "single")
def _seed_user(local_db, suffix):
uid = f"notif-{suffix}-{generate_uid()[-8:]}"
local_db["users"].insert(
{
"uid": uid,
"username": uid,
"terms_version": "1",
"role": "Member",
"created_at": "1990-01-01T00:00:00",
"deleted_at": None,
}
)
return uid
def _seed_notifications(local_db, user_uid, count, ntype="vote"):
table = local_db["notifications"]
now = datetime.now(timezone.utc).isoformat()
for i in range(count):
table.insert(
{
"uid": generate_uid(),
"user_uid": user_uid,
"type": ntype,
"message": f"seeded {i}",
"related_uid": f"actor-{i}",
"target_url": "/posts/seeded",
"read": False,
"created_at": now,
}
)
def test_count_unread_groups_respects_setting(local_db):
uid = _seed_user(local_db, "groups")
try:
_seed_notifications(local_db, uid, 5)
assert get_notification_grouping_enabled(uid) is True
assert count_unread_groups(uid) == 1
set_notification_grouping_enabled(uid, False)
assert get_notification_grouping_enabled(uid) is False
assert count_unread_groups(uid) == 5
set_notification_grouping_enabled(uid, True)
assert count_unread_groups(uid) == 1
finally:
local_db["notifications"].delete(user_uid=uid)
local_db["users"].delete(uid=uid)
def test_throttle_setting_defaults_on_and_toggles(local_db):
uid = _seed_user(local_db, "throttle")
try:
assert get_notification_throttle_enabled(uid) is True
set_notification_throttle_enabled(uid, False)
assert get_notification_throttle_enabled(uid) is False
set_notification_throttle_enabled(uid, True)
assert get_notification_throttle_enabled(uid) is True
finally:
local_db["users"].delete(uid=uid)
def test_missing_user_defaults_on():
assert get_notification_grouping_enabled("no-such-user") is True
assert get_notification_throttle_enabled("no-such-user") is True