-
Notifications
You must be signed in to change notification settings - Fork 106
/
test_playlist_entity_manager.py
448 lines (424 loc) · 14.4 KB
/
test_playlist_entity_manager.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
from datetime import datetime
from typing import List
from integration_tests.challenges.index_helpers import UpdateTask
from integration_tests.utils import populate_mock_db
from src.models.playlists.playlist import Playlist
from src.tasks.entity_manager.entity_manager import entity_manager_update
from src.tasks.entity_manager.utils import PLAYLIST_ID_OFFSET
from src.utils.db_session import get_db
from web3 import Web3
from web3.datastructures import AttributeDict
def test_index_valid_playlists(app, mocker):
"Tests valid batch of playlists create/update/delete actions"
# setup db and mocked txs
with app.app_context():
db = get_db()
web3 = Web3()
update_task = UpdateTask(None, web3, None)
tx_receipts = {
"CreatePlaylist1Tx": [
{
"args": AttributeDict(
{
"_entityId": PLAYLIST_ID_OFFSET,
"_entityType": "Playlist",
"_userId": 1,
"_action": "Create",
"_metadata": "QmCreatePlaylist1",
"_signer": "user1wallet",
}
)
},
],
"UpdatePlaylist1Tx": [
{
"args": AttributeDict(
{
"_entityId": PLAYLIST_ID_OFFSET,
"_entityType": "Playlist",
"_userId": 1,
"_action": "Update",
"_metadata": "QmUpdatePlaylist1",
"_signer": "user1wallet",
}
)
},
],
"DeletePlaylist1Tx": [
{
"args": AttributeDict(
{
"_entityId": PLAYLIST_ID_OFFSET,
"_entityType": "Playlist",
"_userId": 1,
"_action": "Delete",
"_metadata": "",
"_signer": "user1wallet",
}
)
},
],
"CreatePlaylist2Tx": [
{
"args": AttributeDict(
{
"_entityId": PLAYLIST_ID_OFFSET + 1,
"_entityType": "Playlist",
"_userId": 1,
"_action": "Create",
"_metadata": "QmCreatePlaylist2",
"_signer": "user1wallet",
}
)
},
],
"UpdatePlaylist3Tx": [
{
"args": AttributeDict(
{
"_entityId": PLAYLIST_ID_OFFSET + 2,
"_entityType": "Playlist",
"_userId": 1,
"_action": "Update",
"_metadata": "QmUpdatePlaylist3",
"_signer": "user1wallet",
}
)
},
],
"CreateAlbumTx": [
{
"args": AttributeDict(
{
"_entityId": PLAYLIST_ID_OFFSET + 3,
"_entityType": "Playlist",
"_userId": 1,
"_action": "Create",
"_metadata": "QmCreateAlbum4",
"_signer": "user1wallet",
}
)
},
],
}
entity_manager_txs = [
AttributeDict({"transactionHash": update_task.web3.toBytes(text=tx_receipt)})
for tx_receipt in tx_receipts
]
def get_events_side_effect(_, tx_receipt):
return tx_receipts[tx_receipt.transactionHash.decode("utf-8")]
mocker.patch(
"src.tasks.entity_manager.entity_manager.get_entity_manager_events_tx",
side_effect=get_events_side_effect,
autospec=True,
)
test_metadata = {
"QmCreatePlaylist1": {
"playlist_contents": {"track_ids": []},
"description": "",
"playlist_image_sizes_multihash": "",
"playlist_name": "playlist 1",
},
"QmCreatePlaylist2": {
"playlist_contents": {"track_ids": []},
"description": "test description",
"playlist_image_sizes_multihash": "",
"playlist_name": "playlist 2",
},
"QmUpdatePlaylist1": {
"playlist_contents": {"track_ids": [{"time": 1660927554, "track": 1}]},
"description": "",
"playlist_image_sizes_multihash": "",
"playlist_name": "playlist 1 updated",
},
"QmUpdatePlaylist3": {
"playlist_contents": {"track_ids": []},
"description": "",
"playlist_image_sizes_multihash": "",
"playlist_name": "playlist 3 updated",
},
"QmCreateAlbum4": {
"playlist_contents": {"track_ids": [{"time": 1660927554, "track": 1}]},
"description": "",
"playlist_image_sizes_multihash": "",
"playlist_name": "album",
"is_album": True,
},
}
entities = {
"users": [
{"user_id": 1, "handle": "user-1", "wallet": "user1wallet"},
],
"playlists": [
{
"playlist_id": PLAYLIST_ID_OFFSET + 2,
"playlist_owner_id": 1,
"playlist_name": "playlist 3",
}
],
}
populate_mock_db(db, entities)
with db.scoped_session() as session:
# index transactions
entity_manager_update(
None,
update_task,
session,
entity_manager_txs,
block_number=0,
block_timestamp=1585336422,
block_hash=0,
metadata=test_metadata,
)
# validate db records
all_playlists: List[Playlist] = session.query(Playlist).all()
assert len(all_playlists) == 7
playlists_1: List[Playlist] = (
session.query(Playlist)
.filter(
Playlist.is_current == True, Playlist.playlist_id == PLAYLIST_ID_OFFSET
)
.all()
)
assert len(playlists_1) == 1
playlist_1 = playlists_1[0]
assert datetime.timestamp(playlist_1.last_added_to) == 1585336422
assert playlist_1.playlist_name == "playlist 1 updated"
assert playlist_1.is_delete == True
assert playlist_1.is_current == True
playlists_2: List[Playlist] = (
session.query(Playlist)
.filter(
Playlist.is_current == True,
Playlist.playlist_id == PLAYLIST_ID_OFFSET + 1,
)
.all()
)
assert len(playlists_2) == 1
playlist_2 = playlists_2[0]
assert playlist_2.last_added_to == None
assert playlist_2.playlist_name == "playlist 2"
assert playlist_2.is_delete == False
assert playlist_2.is_current == True
playlists_3: List[Playlist] = (
session.query(Playlist)
.filter(
Playlist.is_current == True,
Playlist.playlist_id == PLAYLIST_ID_OFFSET + 2,
)
.all()
)
assert len(playlists_3) == 1
playlist_3 = playlists_3[0]
assert playlist_3.last_added_to == None
assert playlist_3.playlist_name == "playlist 3 updated"
assert playlist_3.is_delete == False
assert playlist_3.is_current == True
albums: List[Playlist] = (
session.query(Playlist)
.filter(Playlist.is_current == True, Playlist.is_album == True)
.all()
)
assert len(albums) == 1
album = albums[0]
assert datetime.timestamp(album.last_added_to) == 1585336422
assert album.playlist_name == "album"
assert album.is_delete == False
assert album.is_current == True
def test_index_invalid_playlists(app, mocker):
"Tests invalid batch of playlists create/update/delete actions"
# setup db and mocked txs
with app.app_context():
db = get_db()
web3 = Web3()
update_task = UpdateTask(None, web3, None)
tx_receipts = {
# invalid create
"CreatePlaylistBelowOffset": [
{
"args": AttributeDict(
{
"_entityId": 1,
"_entityType": "Playlist",
"_userId": 1,
"_action": "Create",
"_metadata": "",
"_signer": "user1wallet",
}
)
},
],
"CreatePlaylistUserDoesNotExist": [
{
"args": AttributeDict(
{
"_entityId": PLAYLIST_ID_OFFSET + 1,
"_entityType": "Playlist",
"_userId": 2,
"_action": "Create",
"_metadata": "",
"_signer": "user1wallet",
}
)
},
],
"CreatePlaylistUserDoesNotMatchSigner": [
{
"args": AttributeDict(
{
"_entityId": PLAYLIST_ID_OFFSET + 1,
"_entityType": "Playlist",
"_userId": 1,
"_action": "Create",
"_metadata": "",
"_signer": "InvalidWallet",
}
)
},
],
"CreatePlaylistAlreadyExists": [
{
"args": AttributeDict(
{
"_entityId": PLAYLIST_ID_OFFSET,
"_entityType": "Playlist",
"_userId": 1,
"_action": "Create",
"_metadata": "",
"_signer": "user1wallet",
}
)
},
],
"CreatePlaylistInvalidMetadata": [
{
"args": AttributeDict(
{
"_entityId": PLAYLIST_ID_OFFSET + 1,
"_entityType": "Playlist",
"_userId": 1,
"_action": "Create",
"_metadata": "QmCreatePlaylist1",
"_signer": "user1wallet",
}
)
},
],
# invalid updates
"UpdatePlaylistInvalidSigner": [
{
"args": AttributeDict(
{
"_entityId": PLAYLIST_ID_OFFSET,
"_entityType": "Playlist",
"_userId": 1,
"_action": "Update",
"_metadata": "",
"_signer": "InvalidWallet",
}
)
},
],
"UpdatePlaylistInvalidOwner": [
{
"args": AttributeDict(
{
"_entityId": PLAYLIST_ID_OFFSET,
"_entityType": "Playlist",
"_userId": 2,
"_action": "Update",
"_metadata": "",
"_signer": "User2Wallet",
}
)
},
],
# invalid deletes
"DeletePlaylistInvalidSigner": [
{
"args": AttributeDict(
{
"_entityId": PLAYLIST_ID_OFFSET,
"_entityType": "Playlist",
"_userId": 1,
"_action": "Delete",
"_metadata": "",
"_signer": "InvalidWallet",
}
)
},
],
"DeletePlaylistDoesNotExist": [
{
"args": AttributeDict(
{
"_entityId": PLAYLIST_ID_OFFSET + 1,
"_entityType": "Playlist",
"_userId": 1,
"_action": "Update",
"_metadata": "",
"_signer": "user1wallet",
}
)
},
],
"DeletePlaylistInvalidOwner": [
{
"args": AttributeDict(
{
"_entityId": PLAYLIST_ID_OFFSET + 1,
"_entityType": "Playlist",
"_userId": 2,
"_action": "Update",
"_metadata": "",
"_signer": "User2Wallet",
}
)
},
],
}
entity_manager_txs = [
AttributeDict({"transactionHash": update_task.web3.toBytes(text=tx_receipt)})
for tx_receipt in tx_receipts
]
def get_events_side_effect(_, tx_receipt):
return tx_receipts[tx_receipt.transactionHash.decode("utf-8")]
mocker.patch(
"src.tasks.entity_manager.entity_manager.get_entity_manager_events_tx",
side_effect=get_events_side_effect,
autospec=True,
)
entities = {
"users": [
{"user_id": 1, "handle": "user-1", "wallet": "user1wallet"},
{"user_id": 2, "handle": "user-1", "wallet": "User2Wallet"},
],
"playlists": [
{"playlist_id": PLAYLIST_ID_OFFSET, "playlist_owner_id": 1},
],
}
populate_mock_db(db, entities)
test_metadata = {
"QmCreatePlaylist1": {
# missing playlist_contents, invalid metadata
"description": "",
"playlist_image_sizes_multihash": "",
"playlist_name": "playlist 1",
}
}
with db.scoped_session() as session:
# index transactions
entity_manager_update(
None,
update_task,
session,
entity_manager_txs,
block_number=0,
block_timestamp=1585336422,
block_hash=0,
metadata=test_metadata,
)
# validate db records
all_playlists: List[Playlist] = session.query(Playlist).all()
assert len(all_playlists) == 1 # no new playlists indexed
assert all_playlists[0].is_current == True