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
|
from uuid import uuid4
import pytest
from generalresearch.models import Source
from generalresearch.models.thl.product import (
Product,
SourceConfig,
UserCreateConfig,
SourcesConfig,
UserHealthConfig,
ProfilingConfig,
SupplyPolicy,
SupplyConfig,
)
from test_utils.models.conftest import product_factory
class TestProductManagerGetMethods:
def test_get_by_uuid(self, product_manager):
product: Product = product_manager.create_dummy(
product_id=uuid4().hex,
team_id=uuid4().hex,
name=f"Test Product ID #{uuid4().hex[:6]}",
)
instance = product_manager.get_by_uuid(product_uuid=product.id)
assert isinstance(instance, Product)
# self.assertEqual(instance.model_dump(mode="json"), product.model_dump(mode="json"))
assert instance.id == product.id
with pytest.raises(AssertionError) as cm:
product_manager.get_by_uuid(product_uuid="abc123")
assert "invalid uuid" in str(cm.value)
with pytest.raises(AssertionError) as cm:
product_manager.get_by_uuid(product_uuid=uuid4().hex)
assert "product not found" in str(cm.value)
def test_get_by_uuids(self, product_manager):
cnt = 5
product_uuids = [uuid4().hex for idx in range(cnt)]
for product_id in product_uuids:
product_manager.create_dummy(
product_id=product_id,
team_id=uuid4().hex,
name=f"Test Product ID #{uuid4().hex[:6]}",
)
res = product_manager.get_by_uuids(product_uuids=product_uuids)
assert isinstance(res, list)
assert cnt == len(res)
for instance in res:
assert isinstance(instance, Product)
with pytest.raises(AssertionError) as cm:
product_manager.get_by_uuids(product_uuids=product_uuids + [uuid4().hex])
assert "incomplete product response" in str(cm.value)
with pytest.raises(AssertionError) as cm:
product_manager.get_by_uuids(product_uuids=product_uuids + ["abc123"])
assert "invalid uuid" in str(cm.value)
def test_get_by_uuid_if_exists(self, product_manager):
product: Product = product_manager.create_dummy(
product_id=uuid4().hex,
team_id=uuid4().hex,
name=f"Test Product ID #{uuid4().hex[:6]}",
)
instance = product_manager.get_by_uuid_if_exists(product_uuid=product.id)
assert isinstance(instance, Product)
instance = product_manager.get_by_uuid_if_exists(product_uuid="abc123")
assert instance == None
def test_get_by_uuids_if_exists(self, product_manager):
product_uuids = [uuid4().hex for _ in range(2)]
for product_id in product_uuids:
product_manager.create_dummy(
product_id=product_id,
team_id=uuid4().hex,
name=f"Test Product ID #{uuid4().hex[:6]}",
)
res = product_manager.get_by_uuids_if_exists(product_uuids=product_uuids)
assert isinstance(res, list)
assert 2 == len(res)
for instance in res:
assert isinstance(instance, Product)
res = product_manager.get_by_uuids_if_exists(
product_uuids=product_uuids + [uuid4().hex]
)
assert isinstance(res, list)
assert 2 == len(res)
for instance in res:
assert isinstance(instance, Product)
# # This will raise an error b/c abc123 isn't a uuid.
# res = product_manager.get_by_uuids_if_exists(
# product_uuids=product_uuids + ["abc123"]
# )
# assert isinstance(res, list)
# assert 2 == len(res)
# for instance in res:
# assert isinstance(instance, Product)
def test_get_by_business_ids(self, product_manager):
business_ids = [uuid4().hex for i in range(5)]
product_manager.fetch_uuids(business_uuids=business_ids)
for business_id in business_ids:
product_manager.create(
product_id=uuid4().hex,
team_id=None,
business_id=business_id,
redirect_url="https://www.example.com",
name=f"Test Product ID #{uuid4().hex[:6]}",
user_create_config=None,
)
class TestProductManagerCreation:
def test_base(self, product_manager):
instance = product_manager.create_dummy(
product_id=uuid4().hex,
team_id=uuid4().hex,
name=f"New Test Product {uuid4().hex[:6]}",
)
assert isinstance(instance, Product)
class TestProductManagerCreate:
def test_create_simple(self, product_manager):
# Always required: product_id, team_id, name, redirect_url
# Required internally - if not passed use default: harmonizer_domain,
# commission_pct, sources
product_id = uuid4().hex
team_id = uuid4().hex
business_id = uuid4().hex
product_manager.create(
product_id=product_id,
team_id=team_id,
business_id=business_id,
name=f"Test Product ID #{uuid4().hex[:6]}",
redirect_url="https://www.example.com",
)
instance = product_manager.get_by_uuid(product_uuid=product_id)
assert team_id == instance.team_id
assert business_id == instance.business_id
class TestProductManager:
sources = [
SourceConfig.model_validate(x)
for x in [
{"name": "d", "active": True},
{
"name": "f",
"active": False,
"banned_countries": ["ps", "in", "in"],
},
{
"name": "s",
"active": True,
"supplier_id": "3488",
"allow_pii_only_buyers": True,
"allow_unhashed_buyers": True,
},
{"name": "e", "active": True, "withhold_profiling": True},
]
]
def test_get_by_uuid1(self, product_manager, team, product, product_factory):
p1 = product_factory(team=team)
instance = product_manager.get_by_uuid(product_uuid=p1.uuid)
assert instance.id == p1.id
# No Team and no user_create_config
assert instance.team_id == team.uuid
# user_create_config can't be None, so ensure the default was set.
assert isinstance(instance.user_create_config, UserCreateConfig)
assert 0 == instance.user_create_config.min_hourly_create_limit
assert instance.user_create_config.max_hourly_create_limit is None
def test_get_by_uuid2(self, product_manager, product_factory):
p2 = product_factory()
instance = product_manager.get_by_uuid(p2.id)
assert instance.id, p2.id
# Team and default user_create_config
assert instance.team_id is not None
assert instance.team_id == p2.team_id
assert 0 == instance.user_create_config.min_hourly_create_limit
assert instance.user_create_config.max_hourly_create_limit is None
def test_get_by_uuid3(self, product_manager, product_factory):
p3 = product_factory()
instance = product_manager.get_by_uuid(p3.id)
assert instance.id == p3.id
# Team and default user_create_config
assert instance.team_id is not None
assert instance.team_id == p3.team_id
assert (
p3.user_create_config.min_hourly_create_limit
== instance.user_create_config.min_hourly_create_limit
)
assert instance.user_create_config.max_hourly_create_limit is None
assert not instance.user_wallet_config.enabled
def test_sources(self, product_manager):
user_defined = [SourceConfig(name=Source.DYNATA, active=False)]
sources_config = SourcesConfig(user_defined=user_defined)
p = product_manager.create_dummy(sources_config=sources_config)
p2 = product_manager.get_by_uuid(p.id)
assert p == p2
assert p2.sources_config.user_defined == user_defined
# Assert d is off and everything else is on
dynata = p2.sources_dict[Source.DYNATA]
assert not dynata.active
assert all(x.active is True for x in p2.sources if x.name != Source.DYNATA)
def test_global_sources(self, product_manager):
sources_config = SupplyConfig(
policies=[
SupplyPolicy(
name=Source.DYNATA,
active=True,
address=["https://www.example.com"],
distribute_harmonizer_active=True,
)
]
)
p1 = product_manager.create_dummy(sources_config=sources_config)
p2 = product_manager.get_by_uuid(p1.id)
assert p1 == p2
p1.sources_config.policies.append(
SupplyPolicy(
name=Source.CINT,
active=True,
address=["https://www.example.com"],
distribute_harmonizer_active=True,
)
)
product_manager.update(p1)
p2 = product_manager.get_by_uuid(p1.id)
assert p1 == p2
def test_user_health_config(self, product_manager):
p = product_manager.create_dummy(
user_health_config=UserHealthConfig(banned_countries=["ng", "in"])
)
p2 = product_manager.get_by_uuid(p.id)
assert p == p2
assert p2.user_health_config.banned_countries == ["in", "ng"]
assert p2.user_health_config.allow_ban_iphist
def test_profiling_config(self, product_manager):
p = product_manager.create_dummy(
profiling_config=ProfilingConfig(max_questions=1)
)
p2 = product_manager.get_by_uuid(p.id)
assert p == p2
assert p2.profiling_config.max_questions == 1
# def test_user_create_config(self):
# # -- Product 1 ---
# instance1 = PM.get_by_uuid(self.product_id1)
# self.assertEqual(instance1.user_create_config.min_hourly_create_limit, 0)
#
# self.assertEqual(instance1.user_create_config.max_hourly_create_limit, None)
# self.assertEqual(60, instance1.user_create_config.clip_hourly_create_limit(60))
#
# # -- Product 2 ---
# instance2 = PM.get_by_uuid(self.product2.id)
# self.assertEqual(instance2.user_create_config.min_hourly_create_limit, 200)
# self.assertEqual(instance2.user_create_config.max_hourly_create_limit, None)
#
# self.assertEqual(200, instance2.user_create_config.clip_hourly_create_limit(60))
# self.assertEqual(300, instance2.user_create_config.clip_hourly_create_limit(300))
#
# def test_create_and_cache(self):
# PM.uuid_cache.clear()
#
# # Hit it once, should hit mysql
# with self.assertLogs(level='INFO') as cm:
# p = PM.get_by_uuid(product_id3)
# self.assertEqual(cm.output, [f"INFO:root:Product.get_by_uuid:{product_id3}"])
# self.assertIsNotNone(p)
# self.assertEqual(p.id, product_id3)
#
# # Hit it again, should be pulled from cachetools cache
# with self.assertLogs(level='INFO') as cm:
# logger.info("nothing")
# p = PM.get_by_uuid(product_id3)
# self.assertEqual(cm.output, [f"INFO:root:nothing"])
# self.assertEqual(len(cm.output), 1)
# self.assertIsNotNone(p)
# self.assertEqual(p.id, product_id3)
class TestProductManagerUpdate:
def test_update(self, product_manager):
p = product_manager.create_dummy()
p.name = "new name"
p.enabled = False
p.user_create_config = UserCreateConfig(min_hourly_create_limit=200)
p.sources_config = SourcesConfig(
user_defined=[SourceConfig(name=Source.DYNATA, active=False)]
)
product_manager.update(new_product=p)
# We cleared the cache in the update
# PM.id_cache.clear()
p2 = product_manager.get_by_uuid(p.id)
assert p2.name == "new name"
assert not p2.enabled
assert p2.user_create_config.min_hourly_create_limit == 200
assert not p2.sources_dict[Source.DYNATA].active
class TestProductManagerCacheClear:
def test_cache_clear(self, product_manager):
p = product_manager.create_dummy()
product_manager.get_by_uuid(product_uuid=p.id)
product_manager.get_by_uuid(product_uuid=p.id)
product_manager.pg_config.execute_write(
query="""
UPDATE userprofile_brokerageproduct
SET name = 'test-6d9a5ddfd'
WHERE id = %s""",
params=[p.id],
)
product_manager.cache_clear(p.id)
# Calling this with or without kwargs hits different internal keys in the cache!
p2 = product_manager.get_by_uuid(product_uuid=p.id)
assert p2.name == "test-6d9a5ddfd"
p2 = product_manager.get_by_uuid(product_uuid=p.id)
assert p2.name == "test-6d9a5ddfd"
|