-
Notifications
You must be signed in to change notification settings - Fork 106
/
test_social_feature_entity_manager.py
342 lines (309 loc) · 10.7 KB
/
test_social_feature_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
import logging
from typing import List
from integration_tests.challenges.index_helpers import UpdateTask
from integration_tests.utils import populate_mock_db
from src.models.playlists.aggregate_playlist import AggregatePlaylist
from src.models.social.follow import Follow
from src.models.social.repost import Repost
from src.models.social.save import Save
from src.tasks.entity_manager.entity_manager import entity_manager_update
from src.tasks.entity_manager.utils import EntityType
from src.utils.db_session import get_db
from web3 import Web3
from web3.datastructures import AttributeDict
logger = logging.getLogger(__name__)
def test_index_valid_social_features(app, mocker):
"Tests valid batch of social create/update/delete actions"
# setup db and mocked txs
with app.app_context():
db = get_db()
web3 = Web3()
update_task = UpdateTask(None, web3, None)
"""
const resp = await this.manageEntity({
userId,
entityType: EntityType.USER,
entityId: followeeUserId,
action: isUnfollow ? Action.UNFOLLOW : Action.FOLLOW,
metadataMultihash: ''
})
"""
tx_receipts = {
"FollowUserTx1": [
{
"args": AttributeDict(
{
"_entityId": 2,
"_entityType": "User",
"_userId": 1,
"_action": "Follow",
"_metadata": "",
"_signer": "user1wallet",
}
)
},
],
"UnfollowUserTx2": [
{
"args": AttributeDict(
{
"_entityId": 3,
"_entityType": "User",
"_userId": 1,
"_action": "Unfollow",
"_metadata": "",
"_signer": "user1wallet",
}
)
},
],
"SaveTrackTx3": [
{
"args": AttributeDict(
{
"_entityId": 1,
"_entityType": "Track",
"_userId": 1,
"_action": "Save",
"_metadata": "",
"_signer": "user1wallet",
}
)
},
],
"UnsaveTrackTx3": [
{
"args": AttributeDict(
{
"_entityId": 1,
"_entityType": "Track",
"_userId": 1,
"_action": "Unsave",
"_metadata": "",
"_signer": "user1wallet",
}
)
},
],
"UnrepostPlaylistTx3": [
{
"args": AttributeDict(
{
"_entityId": 1,
"_entityType": "Playlist",
"_userId": 1,
"_action": "Unrepost",
"_metadata": "",
"_signer": "user1wallet",
}
)
},
],
"RepostPlaylistTx4": [
{
"args": AttributeDict(
{
"_entityId": 1,
"_entityType": "Playlist",
"_userId": 1,
"_action": "Repost",
"_metadata": "",
"_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,
)
entities = {
"users": [
{"user_id": 1, "handle": "user-1", "wallet": "user1wallet"},
{"user_id": 2, "handle": "user-2", "wallet": "user2wallet"},
{"user_id": 3, "handle": "user-3", "wallet": "user3wallet"},
],
"follows": [{"follower_user_id": 1, "followee_user_id": 3}],
"tracks": [{"track_id": 1}],
"reposts": [{"repost_item_id": 1, "repost_type": "playlist", "user_id": 1}],
"playlists": [{"playlist_id": 1}],
}
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=1,
block_timestamp=1585336422,
block_hash=0,
metadata={},
)
# Verify follows
all_follows: List[Follow] = session.query(Follow).all()
assert len(all_follows) == 3
user_3_follows: List[Follow] = (
session.query(Follow)
.filter(Follow.is_current == True, Follow.followee_user_id == 3)
.all()
)
assert len(user_3_follows) == 1
user_3_follow = user_3_follows[0]
assert user_3_follow.is_delete == True
user_2_follows: List[Follow] = (
session.query(Follow)
.filter(Follow.is_current == True, Follow.followee_user_id == 2)
.all()
)
assert len(user_2_follows) == 1
user_2_follow = user_2_follows[0]
assert user_2_follow.is_delete == False
# Verify saves
all_saves: List[Save] = session.query(Save).all()
assert len(all_saves) == 2
current_saves: List[Save] = (
session.query(Save).filter(Save.is_current == True).all()
)
assert len(current_saves) == 1
current_save = current_saves[0]
assert current_save.is_delete == True
assert current_save.save_type == EntityType.TRACK.value.lower()
assert current_save.save_item_id == 1
# Verify repost
all_reposts: List[Repost] = session.query(Repost).all()
assert len(all_reposts) == 3
current_reposts: List[Repost] = (
session.query(Repost).filter(Repost.is_current == True).all()
)
assert len(current_reposts) == 1
current_repost = current_reposts[0]
assert current_repost.is_delete == False
assert current_repost.repost_type == EntityType.PLAYLIST.value.lower()
assert current_repost.repost_item_id == 1
# ensure session is flushed, invalidating old records before bulk saving
aggregate_playlists: List[aggregate_playlists] = (
session.query(AggregatePlaylist)
.filter(AggregatePlaylist.playlist_id == 1)
.all()
)
assert len(aggregate_playlists) == 1
aggregate_palylist = aggregate_playlists[0]
assert aggregate_palylist.repost_count == 1
def test_index_invalid_social_features(app, mocker):
"Tests valid batch of social 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 = {
"UserDoesNotExistTx1": [
{
"args": AttributeDict(
{
"_entityId": 2,
"_entityType": "User",
"_userId": 21345,
"_action": "Follow",
"_metadata": "",
"_signer": "user1wallet",
}
)
},
],
"UserDoesNotMatchSignerTx2": [
{
"args": AttributeDict(
{
"_entityId": 3,
"_entityType": "User",
"_userId": 1,
"_action": "Unfollow",
"_metadata": "",
"_signer": "invalidwallet",
}
)
},
],
"EntityDoesNotExistTx3": [
{
"args": AttributeDict(
{
"_entityId": 12323,
"_entityType": "Track",
"_userId": 1,
"_action": "Save",
"_metadata": "",
"_signer": "user1wallet",
}
)
},
],
"UserCannotFollowThemselfTx4": [
{
"args": AttributeDict(
{
"_entityId": 1,
"_entityType": "User",
"_userId": 1,
"_action": "Follow",
"_metadata": "",
"_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,
)
entities = {
"users": [
{"user_id": 1, "handle": "user-1", "wallet": "user1wallet"},
{"user_id": 2, "handle": "user-2", "wallet": "user2wallet"},
{"user_id": 3, "handle": "user-3", "wallet": "user3wallet"},
],
"follows": [{"follower_user_id": 1, "followee_user_id": 3}],
"tracks": [{"track_id": 1}],
"playlists": [{"playlist_id": 1}],
}
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=1,
block_timestamp=1585336422,
block_hash=0,
metadata={},
)
# Verify follows
all_follows: List[Follow] = session.query(Follow).all()
assert len(all_follows) == 1
# Verify saves
all_saves: List[Save] = session.query(Save).all()
assert len(all_saves) == 0
# Verify repost
all_reposts: List[Repost] = session.query(Repost).all()
assert len(all_reposts) == 0