Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C-749] Support req pagination and ordering in hist/fav/tracks #3643

Merged
merged 4 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion discovery-provider/integration_tests/queries/test_get_tracks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,63 +11,78 @@ def populate_tracks(db):
"tracks": [
{
"track_id": 1,
"title": "track 1",
"owner_id": 1287289,
"release_date": "Fri Dec 20 2019 12:00:00 GMT-0800",
"created_at": datetime(2018, 5, 17),
},
{"track_id": 2, "owner_id": 1287289, "created_at": datetime(2018, 5, 18)},
{
"track_id": 2,
"title": "track 2",
"owner_id": 1287289,
"created_at": datetime(2018, 5, 18),
},
{
"track_id": 3,
"title": "track 3",
"owner_id": 1287289,
"release_date": "Wed Dec 18 2019 12:00:00 GMT-0800",
"created_at": datetime(2020, 5, 17),
},
{
"track_id": 4,
"title": "track 4",
"owner_id": 1287289,
"release_date": "",
"created_at": datetime(2018, 5, 19),
},
{
"track_id": 5,
"title": "track 5",
"owner_id": 1287289,
"release_date": "garbage-should-not-parse",
"created_at": datetime(2018, 5, 20),
},
{
"track_id": 6,
"title": "track 6",
"owner_id": 4,
"release_date": "Wed Dec 18 2019 12:00:00 GMT-0800",
"created_at": datetime(2020, 5, 17),
},
{
"track_id": 7,
"title": "track 7",
"owner_id": 4,
"release_date": "",
"created_at": datetime(2018, 5, 19),
},
{
"track_id": 8,
"title": "track 8",
"owner_id": 4,
"release_date": "garbage-should-not-parse",
"created_at": datetime(2018, 5, 20),
},
{
"track_id": 9,
"title": "track 9",
"owner_id": 4,
"release_date": "Wed Dec 25 2019 12:00:00 GMT-0800",
"created_at": datetime(2018, 5, 20),
"is_unlisted": True,
},
{
"track_id": 10,
"title": "track 10",
"owner_id": 4,
"release_date": "Wed Dec 25 2019 12:00:00 GMT-0800",
"created_at": datetime(2018, 5, 21),
"is_delete": True,
},
{
"track_id": 11,
"title": "track 11",
"owner_id": 1287289,
"release_date": "Fri Dec 19 2019 12:00:00 GMT-0800",
"created_at": datetime(2018, 5, 17),
Expand Down Expand Up @@ -131,6 +146,23 @@ def test_get_tracks_by_date(app):
assert tracks[4]["permalink"] == "/some-test-user/track-2"


def test_get_tracks_with_query(app):
"""Test getting tracks with a query"""

with app.app_context():
db = get_db()

populate_tracks(db)

with db.scoped_session() as session:
tracks = _get_tracks(
session, {"user_id": 1287289, "offset": 0, "limit": 10, "query": "track 5"}
)

assert len(tracks) == 1
assert tracks[0]["track_id"] == 5


def test_get_tracks_by_date_authed(app):
"""
Test getting tracks ordering by date with an authed user.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ def test_get_user_listening_history_multiple_plays(app):
track_history = _get_user_listening_history(
session,
GetUserListeningHistoryArgs(
user_id=1,
current_user_id=1,
limit=10,
offset=0,
user_id=1, current_user_id=1, limit=10, offset=0, query=None
),
)

Expand Down Expand Up @@ -98,10 +95,7 @@ def test_get_user_listening_history_no_plays(app):
track_history = _get_user_listening_history(
session,
GetUserListeningHistoryArgs(
user_id=3,
current_user_id=3,
limit=10,
offset=0,
user_id=3, current_user_id=3, limit=10, offset=0, query=None
),
)

Expand All @@ -121,10 +115,7 @@ def test_get_user_listening_history_single_play(app):
track_history = _get_user_listening_history(
session,
GetUserListeningHistoryArgs(
user_id=2,
current_user_id=2,
limit=10,
offset=0,
user_id=2, current_user_id=2, limit=10, offset=0, query=None
),
)

Expand Down Expand Up @@ -152,10 +143,7 @@ def test_get_user_listening_history_pagination(app):
track_history = _get_user_listening_history(
session,
GetUserListeningHistoryArgs(
user_id=1,
current_user_id=1,
limit=1,
offset=1,
user_id=1, current_user_id=1, limit=1, offset=1, query=None
),
)

Expand Down Expand Up @@ -183,11 +171,34 @@ def test_get_user_listening_history_mismatch_user_id(app):
track_history = _get_user_listening_history(
session,
GetUserListeningHistoryArgs(
user_id=1,
current_user_id=2,
limit=10,
offset=0,
user_id=1, current_user_id=2, limit=10, offset=0, query=None
),
)

assert len(track_history) == 0


def test_get_user_listening_history_with_query(app):
"""Tests listening history from user with a query"""
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, query="track 2"
),
)

# We should only get one history item back
assert len(track_history) == 1

assert track_history[0][response_name_constants.track_id] == 2
assert track_history[0][response_name_constants.activity_timestamp] == str(
TIMESTAMP + timedelta(minutes=3)
)
30 changes: 28 additions & 2 deletions discovery-provider/src/api/v1/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from flask_restx import reqparse
from src import api_helpers
from src.api.v1.models.common import full_response
from src.models.rewards.challenge import ChallengeType
from src.queries.get_challenges import ChallengeResponse
from src.queries.get_support_for_user import SupportResponse
Expand All @@ -13,8 +14,6 @@
from src.utils.helpers import decode_string_id, encode_int_id
from src.utils.spl_audio import to_wei_string

from .models.common import full_response

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -487,6 +486,29 @@ def __schema__(self):
search_parser = reqparse.RequestParser(argument_class=DescriptiveArgument)
search_parser.add_argument("query", required=True, description="The search query")

track_history_parser = pagination_with_current_user_parser.copy()
track_history_parser.add_argument(
"query", required=False, description="The filter query"
)

user_favorited_tracks_parser = pagination_with_current_user_parser.copy()
user_favorited_tracks_parser.add_argument(
"query", required=False, description="The filter query"
)

user_tracks_route_parser = pagination_with_current_user_parser.copy()
user_tracks_route_parser.add_argument(
"sort",
required=False,
type=str,
default="date",
choices=("date", "plays"),
description="Field to sort by",
)
user_tracks_route_parser.add_argument(
"query", required=False, description="The filter query"
)

full_search_parser = pagination_with_current_user_parser.copy()
full_search_parser.add_argument("query", required=True, description="The search query")
full_search_parser.add_argument(
Expand Down Expand Up @@ -552,6 +574,10 @@ def format_offset(args, max_offset=MAX_LIMIT):
return max(min(int(offset), max_offset), MIN_OFFSET)


def format_query(args):
return args.get("query", None)


def get_default_max(value, default, max=None):
if not isinstance(value, int):
return default
Expand Down
Loading