-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Dharit Tantiviramanond <dharit.tan@gmail.com>
- Loading branch information
1 parent
f653e01
commit 9ba62e3
Showing
25 changed files
with
764 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const listenStreakReminderMessages = { | ||
title: 'Keep Your Streak Going!', | ||
body: (streak: number) => | ||
`Your ${streak} day listening streak will end in 6 hours! Keep listening to earn daily rewards!` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
156 changes: 156 additions & 0 deletions
156
...very-provider/integration_tests/tasks/test_create_listen_streak_reminder_notifications.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
import logging | ||
from datetime import datetime, timedelta | ||
|
||
from integration_tests.utils import populate_mock_db | ||
from src.models.notifications.notification import Notification | ||
from src.tasks.create_listen_streak_reminder_notifications import ( | ||
LISTEN_STREAK_REMINDER, | ||
_create_listen_streak_reminder_notifications, | ||
get_listen_streak_notification_group_id, | ||
) | ||
from src.utils.db_session import get_db | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
TEST_USER_ID = 1 | ||
TEST_STREAK = 3 | ||
TEST_DATE = (datetime.now() - timedelta(hours=18, minutes=30)).date() | ||
TEST_GROUP_ID = get_listen_streak_notification_group_id(TEST_USER_ID, TEST_DATE) | ||
|
||
|
||
def test_create_listen_streak_reminder_notification(app): | ||
"""Test basic creation of listen streak notification""" | ||
with app.app_context(): | ||
db = get_db() | ||
|
||
now = datetime.now() | ||
# Place listen time clearly within the window (between 18-19 hours ago) | ||
listen_time = now - timedelta(hours=18, minutes=30) | ||
|
||
entities = { | ||
"challenge_listen_streaks": [ | ||
{ | ||
"user_id": TEST_USER_ID, | ||
"listen_streak": TEST_STREAK, | ||
"last_listen_date": listen_time, | ||
} | ||
] | ||
} | ||
|
||
populate_mock_db(db, entities) | ||
with db.scoped_session() as session: | ||
_create_listen_streak_reminder_notifications(session) | ||
all_notifications = session.query(Notification).all() | ||
|
||
assert len(all_notifications) == 1 | ||
notification = all_notifications[0] | ||
assert notification.specifier == str(TEST_USER_ID) | ||
assert notification.group_id == TEST_GROUP_ID | ||
assert notification.data == {"streak": TEST_STREAK} | ||
assert notification.type == LISTEN_STREAK_REMINDER | ||
assert notification.user_ids == [TEST_USER_ID] | ||
|
||
|
||
def test_ignore_outside_time_window(app): | ||
"""Test that streaks outside the notification window are ignored""" | ||
with app.app_context(): | ||
db = get_db() | ||
|
||
now = datetime.now() | ||
too_recent = now - timedelta(hours=17) # Too recent (< 18 hours ago) | ||
too_old = now - timedelta(hours=20) # Too old (> 19 hours ago) | ||
|
||
entities = { | ||
"challenge_listen_streaks": [ | ||
{ | ||
"user_id": TEST_USER_ID, | ||
"listen_streak": TEST_STREAK, | ||
"last_listen_date": too_recent, | ||
}, | ||
{ | ||
"user_id": TEST_USER_ID + 1, | ||
"listen_streak": TEST_STREAK, | ||
"last_listen_date": too_old, | ||
}, | ||
] | ||
} | ||
|
||
populate_mock_db(db, entities) | ||
with db.scoped_session() as session: | ||
_create_listen_streak_reminder_notifications(session) | ||
all_notifications = session.query(Notification).all() | ||
assert len(all_notifications) == 0 | ||
|
||
|
||
def test_ignore_existing_notification(app): | ||
"""Test that duplicate notifications are not created""" | ||
with app.app_context(): | ||
db = get_db() | ||
|
||
now = datetime.now() | ||
listen_time = now - timedelta(hours=18, minutes=30) | ||
|
||
entities = { | ||
"challenge_listen_streaks": [ | ||
{ | ||
"user_id": TEST_USER_ID, | ||
"listen_streak": TEST_STREAK, | ||
"last_listen_date": listen_time, | ||
} | ||
], | ||
"notifications": [ | ||
{ | ||
"specifier": str(TEST_USER_ID), | ||
"group_id": TEST_GROUP_ID, | ||
"type": LISTEN_STREAK_REMINDER, | ||
"user_ids": [TEST_USER_ID], | ||
"data": {"streak": TEST_STREAK}, | ||
"timestamp": listen_time, | ||
} | ||
], | ||
} | ||
|
||
populate_mock_db(db, entities) | ||
with db.scoped_session() as session: | ||
_create_listen_streak_reminder_notifications(session) | ||
all_notifications = session.query(Notification).all() | ||
assert len(all_notifications) == 1 # Only the existing notification | ||
|
||
|
||
def test_multiple_streaks(app): | ||
"""Test handling multiple eligible listen streaks""" | ||
with app.app_context(): | ||
db = get_db() | ||
|
||
now = datetime.now() | ||
listen_time = now - timedelta(hours=18, minutes=30) | ||
|
||
entities = { | ||
"challenge_listen_streaks": [ | ||
{ | ||
"user_id": TEST_USER_ID, | ||
"listen_streak": TEST_STREAK, | ||
"last_listen_date": listen_time, | ||
}, | ||
{ | ||
"user_id": TEST_USER_ID + 1, | ||
"listen_streak": TEST_STREAK + 1, | ||
"last_listen_date": listen_time, | ||
}, | ||
] | ||
} | ||
|
||
populate_mock_db(db, entities) | ||
with db.scoped_session() as session: | ||
_create_listen_streak_reminder_notifications(session) | ||
all_notifications = session.query(Notification).all() | ||
|
||
assert len(all_notifications) == 2 | ||
for notification in all_notifications: | ||
assert notification.type == LISTEN_STREAK_REMINDER | ||
user_id = int(notification.specifier) | ||
assert notification.user_ids == [user_id] | ||
expected_streak = ( | ||
TEST_STREAK if user_id == TEST_USER_ID else TEST_STREAK + 1 | ||
) | ||
assert notification.data == {"streak": expected_streak} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
isort --diff . --skip ./src/tasks/core/gen --skip ./plugins | ||
isort . --skip ./src/tasks/core/gen --skip ./plugins | ||
flake8 . --exclude=./src/tasks/core/gen,./plugins | ||
black --diff . --exclude './src/tasks/core/gen|./plugins' | ||
black . --exclude './src/tasks/core/gen|./plugins' | ||
mypy . --exclude './src/tasks/core/gen|./plugins' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.