Skip to content

Commit

Permalink
Add tests for user info output
Browse files Browse the repository at this point in the history
  • Loading branch information
TimJentzsch committed Apr 21, 2022
1 parent 0413c00 commit 1839af4
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 5 deletions.
13 changes: 8 additions & 5 deletions api/slack/commands/info.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from datetime import datetime, timedelta
from typing import Dict
from typing import Dict, Optional

from django.utils import timezone

Expand Down Expand Up @@ -39,7 +39,7 @@ def info_cmd(channel: str, message: str) -> None:

def user_info_text(user: BlossomUser) -> str:
"""Get the info message for the given user."""
name_link = f"<https://reddit.com/u/{user.username}|{user.username}>"
name_link = f"<https://reddit.com/u/{user.username}|u/{user.username}>"
title = f"Info about *{name_link}*:"

general = _format_info_section("General", user_general_info(user))
Expand Down Expand Up @@ -70,7 +70,7 @@ def user_general_info(user: BlossomUser) -> Dict:
recent_gamma = user.gamma_at_time(start_time=timezone.now() - timedelta(weeks=2))
gamma = f"{total_gamma} Γ ({recent_gamma} Γ in last 2 weeks)"
joined_on = _format_time(user.date_joined)
last_active = _format_time(user.date_last_active())
last_active = _format_time(user.date_last_active()) or "Never"

return {
"Gamma": gamma,
Expand Down Expand Up @@ -102,7 +102,7 @@ def user_transcription_quality_info(user: BlossomUser) -> Dict:
warnings_ratio = warnings_count / check_count if check_count > 0 else 0
warnings = f"{warnings_count} ({warnings_ratio:.1%} of checks)"

watch_status = user.transcription_check_reason()
watch_status = user.transcription_check_reason(ignore_low_activity=True)

return {
"Checks": checks,
Expand Down Expand Up @@ -131,8 +131,11 @@ def bool_str(bl: bool) -> str:
return "Yes" if bl else "No"


def _format_time(time: datetime) -> str:
def _format_time(time: Optional[datetime]) -> Optional[str]:
"""Format the given time in absolute and relative strings."""
if time is None:
return None

now = timezone.now()
absolute = time.date().isoformat()

Expand Down
118 changes: 118 additions & 0 deletions api/tests/slack/commands/test_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
from datetime import datetime, timedelta
from unittest.mock import patch

import pytz
from django.test import Client

from api.models import TranscriptionCheck
from api.slack.commands.info import user_info_text
from utils.test_helpers import (
create_check,
create_submission,
create_transcription,
setup_user_client,
)


def test_user_info_text_new_user(client: Client) -> None:
"""Verify that the string for a new user is generated correctly."""
client, headers, user = setup_user_client(
client,
id=123,
username="Userson",
date_joined=datetime(2021, 5, 21, tzinfo=pytz.UTC),
accepted_coc=False,
is_bot=False,
)

expected = """Info about *<https://reddit.com/u/Userson|u/Userson>*:
*General*:
- Gamma: 0 Γ (0 Γ in last 2 weeks)
- Joined on: 2021-05-21 (1.0 day ago)
- Last active: Never
*Transcription Quality*:
- Checks: 0 (0.0% of transcriptions)
- Warnings: 0 (0.0% of checks)
- Watch status: Automatic (100.0%)
*Debug Info*:
- ID: `123`
- Blacklisted: No
- Bot: No
- Accepted CoC: No"""

with patch(
"api.slack.commands.info.timezone.now",
return_value=datetime(2021, 5, 22, tzinfo=pytz.UTC),
):
actual = user_info_text(user)

assert actual == expected


def test_user_info_text_old_user(client: Client) -> None:
"""Verify that the string for a new user is generated correctly."""
client, headers, user = setup_user_client(
client,
id=123,
username="Userson",
date_joined=datetime(2020, 4, 21, tzinfo=pytz.UTC),
accepted_coc=True,
is_bot=False,
)

tr_data = [
(datetime(2020, 7, 3, tzinfo=pytz.UTC), True, False),
(datetime(2020, 7, 7, tzinfo=pytz.UTC), False, False),
(datetime(2020, 7, 9, tzinfo=pytz.UTC), False, False),
(datetime(2021, 4, 10, tzinfo=pytz.UTC), True, True),
(datetime(2021, 4, 12, tzinfo=pytz.UTC), False, False),
]

for idx, (date, has_check, is_warning) in enumerate(tr_data):
submission = create_submission(
id=100 + idx,
create_time=date - timedelta(days=1),
claim_time=date,
complete_time=date + timedelta(days=1),
claimed_by=user,
completed_by=user,
)
transcription = create_transcription(
submission, user, id=200 + idx, create_time=date
)

if has_check:
check_status = TranscriptionCheck.TranscriptionCheckStatus
status = (
check_status.WARNING_RESOLVED if is_warning else check_status.APPROVED
)
create_check(transcription, status=status)

expected = """Info about *<https://reddit.com/u/Userson|u/Userson>*:
*General*:
- Gamma: 5 Γ (2 Γ in last 2 weeks)
- Joined on: 2020-04-21 (1.0 year ago)
- Last active: 2021-04-13 (1.1 weeks ago)
*Transcription Quality*:
- Checks: 2 (40.0% of transcriptions)
- Warnings: 1 (50.0% of checks)
- Watch status: Automatic (100.0%)
*Debug Info*:
- ID: `123`
- Blacklisted: No
- Bot: No
- Accepted CoC: Yes"""

with patch(
"api.slack.commands.info.timezone.now",
return_value=datetime(2021, 4, 21, tzinfo=pytz.UTC),
):
actual = user_info_text(user)

assert actual == expected

0 comments on commit 1839af4

Please sign in to comment.