-
Notifications
You must be signed in to change notification settings - Fork 19
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
## Summary Fixes #3536 ### Time to review: 15 mins ## Changes proposed Add `last_notified_at` to `user_saved_opportunity` Collect opportunity notifications, log them out and test. ## Context for reviewers Can be merged, now that #3527 is wrapped up. ## Additional information See attached unit tests
- Loading branch information
1 parent
73b32a7
commit 0dd0466
Showing
6 changed files
with
237 additions
and
15 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
api/src/db/migrations/versions/2025_01_24_add_last_notified_at_to_user_saved_.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,34 @@ | ||
"""Add last_notified_at to user saved opportunity table | ||
Revision ID: 43b179a7c92e | ||
Revises: dc04ce955a9a | ||
Create Date: 2025-01-24 17:15:14.064880 | ||
""" | ||
|
||
import sqlalchemy as sa | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "43b179a7c92e" | ||
down_revision = "9e7fc937646a" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.add_column( | ||
"user_saved_opportunity", | ||
sa.Column( | ||
"last_notified_at", sa.TIMESTAMP(timezone=True), server_default="NOW()", nullable=False | ||
), | ||
schema="api", | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_column("user_saved_opportunity", "last_notified_at", schema="api") | ||
# ### end Alembic commands ### |
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
127 changes: 127 additions & 0 deletions
127
api/tests/src/task/notifications/test_generate_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 |
---|---|---|
@@ -1,5 +1,132 @@ | ||
from datetime import timedelta | ||
|
||
import pytest | ||
|
||
import tests.src.db.models.factories as factories | ||
from src.db.models.user_models import UserNotificationLog | ||
from src.task.notifications.generate_notifications import NotificationConstants | ||
|
||
|
||
@pytest.fixture | ||
def clear_notification_logs(db_session): | ||
"""Clear all notification logs""" | ||
db_session.query(UserNotificationLog).delete() | ||
|
||
|
||
def test_via_cli(cli_runner, db_session, enable_factory_create, user): | ||
"""Simple test that verifies we can invoke the notification task via CLI""" | ||
result = cli_runner.invoke(args=["task", "generate-notifications"]) | ||
|
||
assert result.exit_code == 0 | ||
|
||
|
||
def test_collect_notifications_cli(cli_runner, db_session, enable_factory_create, user, caplog): | ||
"""Simple test that verifies we can invoke the notification task via CLI""" | ||
# Create a saved opportunity that needs notification | ||
opportunity = factories.OpportunityFactory.create() | ||
saved_opportunity = factories.UserSavedOpportunityFactory.create( | ||
user=user, | ||
opportunity=opportunity, | ||
last_notified_at=opportunity.updated_at - timedelta(days=1), | ||
) | ||
factories.OpportunityChangeAuditFactory.create( | ||
opportunity=opportunity, | ||
updated_at=saved_opportunity.last_notified_at + timedelta(minutes=1), | ||
) | ||
|
||
result = cli_runner.invoke(args=["task", "generate-notifications"]) | ||
|
||
assert result.exit_code == 0 | ||
|
||
# Verify expected log messages | ||
assert "Collected opportunity notifications" in caplog.text | ||
assert "Would send notification to user" in caplog.text | ||
|
||
# Verify the log contains the correct metrics | ||
log_records = [r for r in caplog.records if "Would send notification to user" in r.message] | ||
assert len(log_records) == 1 | ||
extra = log_records[0].__dict__ | ||
assert extra["user_id"] == user.user_id | ||
assert extra["opportunity_count"] == 1 | ||
assert extra["search_count"] == 0 | ||
|
||
|
||
def test_last_notified_at_updates(cli_runner, db_session, enable_factory_create, user): | ||
"""Test that last_notified_at gets updated after sending notifications""" | ||
# Create an opportunity that was updated after the last notification | ||
opportunity = factories.OpportunityFactory.create() | ||
saved_opp = factories.UserSavedOpportunityFactory.create( | ||
user=user, | ||
opportunity=opportunity, | ||
last_notified_at=opportunity.updated_at - timedelta(days=1), | ||
) | ||
factories.OpportunityChangeAuditFactory.create( | ||
opportunity=opportunity, | ||
updated_at=saved_opp.last_notified_at + timedelta(minutes=1), | ||
) | ||
# Store the original notification time | ||
original_notification_time = saved_opp.last_notified_at | ||
|
||
# Run the notification task | ||
result = cli_runner.invoke(args=["task", "generate-notifications"]) | ||
assert result.exit_code == 0 | ||
|
||
# Refresh the saved opportunity from the database | ||
db_session.refresh(saved_opp) | ||
|
||
# Verify last_notified_at was updated | ||
assert saved_opp.last_notified_at > original_notification_time | ||
# Verify last_notified_at is now after the opportunity's updated_at | ||
assert saved_opp.last_notified_at > opportunity.updated_at | ||
|
||
|
||
def test_notification_log_creation( | ||
cli_runner, db_session, enable_factory_create, clear_notification_logs, user | ||
): | ||
"""Test that notification logs are created when notifications are sent""" | ||
# Create a saved opportunity that needs notification | ||
opportunity = factories.OpportunityFactory.create() | ||
saved_opportunity = factories.UserSavedOpportunityFactory.create( | ||
user=user, | ||
opportunity=opportunity, | ||
last_notified_at=opportunity.updated_at - timedelta(days=1), | ||
) | ||
|
||
factories.OpportunityChangeAuditFactory.create( | ||
opportunity=opportunity, | ||
updated_at=saved_opportunity.last_notified_at + timedelta(minutes=1), | ||
) | ||
|
||
# Run the notification task | ||
result = cli_runner.invoke(args=["task", "generate-notifications"]) | ||
assert result.exit_code == 0 | ||
|
||
# Verify notification log was created | ||
notification_logs = db_session.query(UserNotificationLog).all() | ||
assert len(notification_logs) == 1 | ||
|
||
log = notification_logs[0] | ||
assert log.user_id == user.user_id | ||
assert log.notification_reason == NotificationConstants.OPPORTUNITY_UPDATES | ||
assert log.notification_sent is True | ||
|
||
|
||
def test_no_notification_log_when_no_updates( | ||
cli_runner, db_session, enable_factory_create, clear_notification_logs, user | ||
): | ||
"""Test that no notification log is created when there are no updates""" | ||
# Create a saved opportunity that doesn't need notification | ||
opportunity = factories.OpportunityFactory.create() | ||
factories.UserSavedOpportunityFactory.create( | ||
user=user, | ||
opportunity=opportunity, | ||
last_notified_at=opportunity.updated_at + timedelta(minutes=1), # After the update | ||
) | ||
|
||
# Run the notification task | ||
result = cli_runner.invoke(args=["task", "generate-notifications"]) | ||
assert result.exit_code == 0 | ||
|
||
# Verify no notification log was created | ||
notification_logs = db_session.query(UserNotificationLog).all() | ||
assert len(notification_logs) == 0 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.