/
test_get_repost_feed_for_user.py
119 lines (107 loc) · 5.21 KB
/
test_get_repost_feed_for_user.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from src.queries.get_repost_feed_for_user import _get_repost_feed_for_user
from src.utils.db_session import get_db
from integration_tests.utils import populate_mock_db
def test_get_repost_feed_for_user(app):
"""Tests that a repost feed for a user can be queried"""
with app.app_context():
db = get_db()
test_entities = {
"reposts": [
# Note these reposts are in chronological order in addition
# so the repost feed should pull them "backwards" for reverse chronological
# sort order.
{"user_id": 1, "repost_item_id": 5, "repost_type": "track"},
{"user_id": 1, "repost_item_id": 2, "repost_type": "track"},
{"user_id": 1, "repost_item_id": 3, "repost_type": "track"},
{"user_id": 1, "repost_item_id": 1, "repost_type": "track"},
{"user_id": 1, "repost_item_id": 4, "repost_type": "track"},
{"user_id": 1, "repost_item_id": 4, "repost_type": "playlist"},
{"user_id": 1, "repost_item_id": 8, "repost_type": "album"},
{"user_id": 1, "repost_item_id": 6, "repost_type": "track"},
],
"tracks": [
{"track_id": 1, "title": "track 1"},
{"track_id": 2, "title": "track 2"},
{"track_id": 3, "title": "track 3"},
{"track_id": 4, "title": "track 4"},
{"track_id": 5, "title": "track 5"},
{"track_id": 6, "title": "track 6"},
{"track_id": 7, "title": "track 7"},
{"track_id": 8, "title": "track 8"},
],
"playlists": [
{"playlist_id": 1, "playlist_name": "playlist 1"},
{"playlist_id": 2, "playlist_name": "playlist 2"},
{"playlist_id": 3, "playlist_name": "playlist 3"},
{"playlist_id": 4, "playlist_name": "playlist 4"},
{"playlist_id": 5, "playlist_name": "playlist 5"},
{"playlist_id": 6, "playlist_name": "playlist 6"},
{"playlist_id": 7, "playlist_name": "playlist 7"},
{"playlist_id": 8, "playlist_name": "album 8"},
],
}
populate_mock_db(db, test_entities)
with db.scoped_session() as session:
repost_feed = _get_repost_feed_for_user(session, 1, {"limit": 10, "offset": 0})
assert repost_feed[0]["title"] == "track 6"
assert repost_feed[1]["playlist_name"] == "album 8"
assert repost_feed[2]["playlist_name"] == "playlist 4"
assert repost_feed[3]["title"] == "track 4"
assert repost_feed[4]["title"] == "track 1"
assert repost_feed[5]["title"] == "track 3"
assert repost_feed[6]["title"] == "track 2"
assert repost_feed[7]["title"] == "track 5"
def test_get_repost_feed_for_user_limit_bounds(app):
"""
Tests that a repost feed for a user can be queried and respect a limit
with deleted tracks.
"""
with app.app_context():
db = get_db()
test_entities = {
"reposts": [
# Note these reposts are in chronological order in addition
# so the repost feed should pull them "backwards" for reverse chronological
# sort order.
{"user_id": 1, "repost_item_id": 5, "repost_type": "track"},
{"user_id": 1, "repost_item_id": 2, "repost_type": "track"},
{"user_id": 1, "repost_item_id": 3, "repost_type": "track"},
{"user_id": 1, "repost_item_id": 1, "repost_type": "track"},
{"user_id": 1, "repost_item_id": 4, "repost_type": "track"},
{"user_id": 1, "repost_item_id": 4, "repost_type": "playlist"},
{"user_id": 1, "repost_item_id": 8, "repost_type": "album"},
{"user_id": 1, "repost_item_id": 6, "repost_type": "track"},
],
"tracks": [
{"track_id": 1, "title": "track 1", "is_delete": True},
{"track_id": 2, "title": "track 2"},
{"track_id": 3, "title": "track 3"},
{"track_id": 4, "title": "track 4"},
{"track_id": 5, "title": "track 5"},
{"track_id": 6, "title": "track 6"},
{"track_id": 7, "title": "track 7"},
{"track_id": 8, "title": "track 8"},
],
"playlists": [
{"playlist_id": 1, "playlist_name": "playlist 1"},
{"playlist_id": 2, "playlist_name": "playlist 2"},
{"playlist_id": 3, "playlist_name": "playlist 3"},
{"playlist_id": 4, "playlist_name": "playlist 4"},
{"playlist_id": 5, "playlist_name": "playlist 5"},
{"playlist_id": 6, "playlist_name": "playlist 6"},
{"playlist_id": 7, "playlist_name": "playlist 7"},
{"playlist_id": 8, "playlist_name": "album 8"},
],
}
populate_mock_db(db, test_entities)
with db.scoped_session() as session:
repost_feed = _get_repost_feed_for_user(session, 1, {"limit": 5, "offset": 0})
# Query for 5 reposts. The problem is the 5th one was deleted, so
# we only return 4 here. This is broken.
# TODO fix me.
assert repost_feed[0]["title"] == "track 6"
assert repost_feed[1]["playlist_name"] == "album 8"
assert repost_feed[2]["playlist_name"] == "playlist 4"
assert repost_feed[3]["title"] == "track 4"
# Should skip track 1 because it is deleted
assert repost_feed[4]["title"] == "track 3"