-
Notifications
You must be signed in to change notification settings - Fork 106
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update user listen history query to use new indexed table (#2185)
* Update user listen history query to use new indexed table * remove unnecessary args * revert add users to tracks * clean up integration test * Add arg details * add extra user id back * sort imports * sort test imports
- Loading branch information
Showing
6 changed files
with
298 additions
and
197 deletions.
There are no files selected for viewing
117 changes: 0 additions & 117 deletions
117
discovery-provider/integration_tests/queries/test_get_history_for_user.py
This file was deleted.
Oops, something went wrong.
193 changes: 193 additions & 0 deletions
193
discovery-provider/integration_tests/queries/test_get_user_listening_history.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,193 @@ | ||
from datetime import datetime, timedelta | ||
|
||
from integration_tests.utils import populate_mock_db | ||
from src.queries import response_name_constants | ||
from src.queries.get_user_listening_history import ( | ||
GetUserListeningHistoryArgs, | ||
_get_user_listening_history, | ||
) | ||
from src.tasks.user_listening_history.index_user_listening_history import ( | ||
_index_user_listening_history, | ||
) | ||
from src.utils.db_session import get_db | ||
|
||
TIMESTAMP = datetime(2011, 1, 1) | ||
|
||
test_entities = { | ||
"plays": [ | ||
{"user_id": 1, "item_id": 1, "created_at": TIMESTAMP + timedelta(minutes=1)}, | ||
{"user_id": 1, "item_id": 2, "created_at": TIMESTAMP + timedelta(minutes=3)}, | ||
{ | ||
"user_id": 1, | ||
"item_id": 1, | ||
"created_at": TIMESTAMP + timedelta(minutes=2), | ||
}, # duplicate play | ||
{"user_id": 1, "item_id": 3, "created_at": TIMESTAMP + timedelta(minutes=4)}, | ||
{"user_id": 2, "item_id": 2, "created_at": TIMESTAMP}, | ||
], | ||
"tracks": [ | ||
{"track_id": 1, "title": "track 1", "owner_id": 1, "is_delete": True}, | ||
{"track_id": 2, "title": "track 2", "owner_id": 2}, | ||
{"track_id": 3, "title": "track 3", "owner_id": 3}, | ||
], | ||
"users": [ | ||
{"user_id": 1, "handle": "user-1"}, | ||
{"user_id": 2, "handle": "user-2"}, | ||
{"user_id": 3, "handle": "user-3"}, | ||
], | ||
} | ||
|
||
|
||
def test_get_user_listening_history_multiple_plays(app): | ||
"""Tests listening history from user with multiple plays""" | ||
with app.app_context(): | ||
db = get_db() | ||
|
||
populate_mock_db(db, test_entities) | ||
|
||
with db.scoped_session() as session: | ||
_index_user_listening_history(session) | ||
|
||
track_history = _get_user_listening_history( | ||
session, | ||
GetUserListeningHistoryArgs( | ||
user_id=1, | ||
current_user_id=1, | ||
limit=10, | ||
offset=0, | ||
), | ||
) | ||
|
||
assert len(track_history) == 3 | ||
assert ( | ||
track_history[0][response_name_constants.user][response_name_constants.balance] | ||
is not None | ||
) | ||
assert track_history[0][response_name_constants.track_id] == 3 | ||
assert track_history[0][response_name_constants.activity_timestamp] == str( | ||
TIMESTAMP + timedelta(minutes=4) | ||
) | ||
assert ( | ||
track_history[1][response_name_constants.user][response_name_constants.balance] | ||
is not None | ||
) | ||
assert track_history[1][response_name_constants.track_id] == 2 | ||
assert track_history[1][response_name_constants.activity_timestamp] == str( | ||
TIMESTAMP + timedelta(minutes=3) | ||
) | ||
assert ( | ||
track_history[2][response_name_constants.user][response_name_constants.balance] | ||
is not None | ||
) | ||
assert track_history[2][response_name_constants.track_id] == 1 | ||
assert track_history[2][response_name_constants.activity_timestamp] == str( | ||
TIMESTAMP + timedelta(minutes=2) | ||
) | ||
|
||
|
||
def test_get_user_listening_history_no_plays(app): | ||
"""Tests a listening history with no plays""" | ||
with app.app_context(): | ||
db = get_db() | ||
|
||
populate_mock_db(db, test_entities) | ||
|
||
with db.scoped_session() as session: | ||
_index_user_listening_history(session) | ||
|
||
track_history = _get_user_listening_history( | ||
session, | ||
GetUserListeningHistoryArgs( | ||
user_id=3, | ||
current_user_id=3, | ||
limit=10, | ||
offset=0, | ||
), | ||
) | ||
|
||
assert len(track_history) == 0 | ||
|
||
|
||
def test_get_user_listening_history_single_play(app): | ||
"""Tests a listening history with a single play""" | ||
with app.app_context(): | ||
db = get_db() | ||
|
||
populate_mock_db(db, test_entities) | ||
|
||
with db.scoped_session() as session: | ||
_index_user_listening_history(session) | ||
|
||
track_history = _get_user_listening_history( | ||
session, | ||
GetUserListeningHistoryArgs( | ||
user_id=2, | ||
current_user_id=2, | ||
limit=10, | ||
offset=0, | ||
), | ||
) | ||
|
||
assert len(track_history) == 1 | ||
assert ( | ||
track_history[0][response_name_constants.user][response_name_constants.balance] | ||
is not None | ||
) | ||
assert track_history[0][response_name_constants.track_id] == 2 | ||
assert track_history[0][response_name_constants.activity_timestamp] == str( | ||
TIMESTAMP | ||
) | ||
|
||
|
||
def test_get_user_listening_history_pagination(app): | ||
"""Tests a track history that's limit bounded""" | ||
with app.app_context(): | ||
db = get_db() | ||
|
||
populate_mock_db(db, test_entities) | ||
|
||
with db.scoped_session() as session: | ||
_index_user_listening_history(session) | ||
|
||
track_history = _get_user_listening_history( | ||
session, | ||
GetUserListeningHistoryArgs( | ||
user_id=1, | ||
current_user_id=1, | ||
limit=1, | ||
offset=1, | ||
), | ||
) | ||
|
||
assert len(track_history) == 1 | ||
assert ( | ||
track_history[0][response_name_constants.user][response_name_constants.balance] | ||
is not None | ||
) | ||
assert track_history[0][response_name_constants.track_id] == 2 | ||
assert track_history[0][response_name_constants.activity_timestamp] == str( | ||
TIMESTAMP + timedelta(minutes=3) | ||
) | ||
|
||
|
||
def test_get_user_listening_history_mismatch_user_id(app): | ||
"""Tests a listening history with mismatching user ids""" | ||
with app.app_context(): | ||
db = get_db() | ||
|
||
populate_mock_db(db, test_entities) | ||
|
||
with db.scoped_session() as session: | ||
_index_user_listening_history(session) | ||
|
||
track_history = _get_user_listening_history( | ||
session, | ||
GetUserListeningHistoryArgs( | ||
user_id=1, | ||
current_user_id=2, | ||
limit=10, | ||
offset=0, | ||
), | ||
) | ||
|
||
assert len(track_history) == 0 |
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.