Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
69 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from src.models import Play | ||
from src.utils import db_session | ||
|
||
def get_latest_play(): | ||
""" | ||
Gets the latest play in the database | ||
""" | ||
db = db_session.get_db_read_replica() | ||
with db.scoped_session() as session: | ||
play_query = session.query(Play.created_at).order_by( | ||
Play.created_at.desc() | ||
).limit(1) | ||
play = play_query.scalar() | ||
return play |
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,30 @@ | ||
from datetime import datetime | ||
from src.queries.get_latest_play import get_latest_play | ||
from src.models import Play | ||
|
||
def test_get_latest_play(db_mock): | ||
"""Tests that the latest play is returned""" | ||
date1 = datetime(2020, 10, 4, 10, 35, 0) | ||
date2 = datetime(2020, 10, 1, 10, 10, 0) | ||
date3 = datetime(2020, 9, 20, 8, 1, 0) | ||
|
||
with db_mock.scoped_session() as session: | ||
Play.__table__.create(db_mock._engine) | ||
session.add(Play( | ||
user_id=1, | ||
play_item_id=1, | ||
created_at=date1 | ||
)) | ||
session.add(Play( | ||
user_id=2, | ||
play_item_id=1, | ||
created_at=date2 | ||
)) | ||
session.add(Play( | ||
user_id=3, | ||
play_item_id=2, | ||
created_at=date3 | ||
)) | ||
|
||
latest_play = get_latest_play() | ||
assert latest_play == date1 |
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