-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathtest_prepare_pricing_dataset.py
605 lines (553 loc) · 18.6 KB
/
test_prepare_pricing_dataset.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
"""
Test file for - publish from s3 to redis
========================================
Integration Tests
-----------------
Please ensure ``redis`` and ``minio`` are running and export this:
::
export INT_TESTS=1
"""
import os
import uuid
import json
import mock
from analysis_engine.mocks.mock_boto3_s3 import \
build_boto3_resource
from analysis_engine.mocks.mock_boto3_s3 import \
mock_publish_from_s3_to_redis
from analysis_engine.mocks.mock_boto3_s3 import \
mock_publish_from_s3_to_redis_err
from analysis_engine.mocks.mock_boto3_s3 import \
mock_publish_from_s3_exception
from analysis_engine.mocks.mock_redis import MockRedis
from analysis_engine.mocks.mock_redis import MockRedisFailToConnect
from analysis_engine.mocks.base_test import BaseTestCase
from analysis_engine.consts import SUCCESS
from analysis_engine.consts import ERR
from analysis_engine.consts import PREPARE_DATA_MIN_SIZE
from analysis_engine.api_requests \
import build_cache_ready_pricing_dataset
from analysis_engine.work_tasks.prepare_pricing_dataset \
import run_prepare_pricing_dataset
from analysis_engine.api_requests \
import build_prepare_dataset_request
from spylunking.log.setup_logging import build_colorized_logger
log = build_colorized_logger(
name=__name__)
def mock_success_task_result(
**kwargs):
"""mock_success_task_result
:param kwargs: keyword args dict
"""
log.info('MOCK - mock_success_task_result')
res = kwargs
res['result']['status'] = SUCCESS
res['result']['err'] = None
return res
# end of mock_success_task_result
def mock_err_task_result(
**kwargs):
"""mock_err_task_result
:param kwargs: keyword args dict
"""
log.info('MOCK - mock_err_task_result')
res = kwargs
res['result']['status'] = ERR
res['result']['err'] = 'test exception'
return res
# end of mock_err_task_result
def mock_s3_read_contents_from_key(
s3,
s3_bucket_name,
s3_key,
encoding='utf-8',
convert_as_json=True):
"""mock_s3_read_contents_from_key
Download the S3 key contents as a string. This
will raise exceptions.
:param s3_obj: existing S3 object
:param s3_bucket_name: bucket name
:param s3_key: S3 key
:param encoding: utf-8 by default
:param convert_to_json: auto-convert to a dict
"""
log.info('MOCK - mock_s3_read_contents_from_key')
data = build_cache_ready_pricing_dataset()
if not convert_as_json:
data = json.dumps(data)
return data
# end of mock_s3_read_contents_from_key
def mock_exception_run_publish_pricing_update(
**kwargs):
"""mock_exception_run_publish_pricing_update
:param kwargs: keyword args dict
"""
raise Exception(
'test throwing mock_exception_run_publish_pricing_update')
# end of mock_exception_run_publish_pricing_update
def mock_redis_get_exception(
**kwargs):
"""mock_redis_get_exception
:param kwargs: keyword args dict
"""
raise Exception(
'test throwing mock_redis_get_exception')
# end of mock_redis_get_exception
def mock_flatten_dict_err(
**kwargs):
"""mock_flatten_dict_err
:param kwargs: keyword args dict
"""
raise Exception(
'test throwing mock_flatten_dict_err')
# end of mock_flatten_dict_err
def mock_flatten_dict_empty(
**kwags):
"""mock_flatten_dict_empty
:param kwargs: keyword args dict
"""
return None
# end of mock_flatten_dict_empty
def mock_redis_get_data_none(
**kwargs):
return {
'status': SUCCESS,
'err': None,
'rec': {
'data': None
}
}
# end of mock_redis_get_data_none
def mock_flatten_dict_too_small(
**kwargs):
"""mock_flatten_dict_too_small
:param kwargs: keyword args dict
"""
return '012345678901234567890'[0:PREPARE_DATA_MIN_SIZE-1]
# end of mock_flatten_dict_too_small
class TestPreparePricingDataset(BaseTestCase):
"""TestPreparePricingDataset"""
def build_test_key(
self,
test_name=None):
"""build_test_key
:param test_name: use this test label name
"""
use_test_name = test_name
if not use_test_name:
use_test_name = str(uuid.uuid4())
test_key = f'{__name__}_{use_test_name}'
return test_key
# end of build_test_key
# throws on redis client creation
@mock.patch(
('redis.Redis'),
new=MockRedisFailToConnect)
def test_redis_connection_exception_prepare_pricing(self):
"""test_redis_connection_exception_prepare_pricing"""
expected_err = 'test MockRedisFailToConnect'
redis_key = self.build_test_key(
test_name='test_redis_connection_exception_prepare_pricing')
s3_key = redis_key
work = build_prepare_dataset_request()
work['s3_key'] = s3_key
work['redis_key'] = redis_key
res = run_prepare_pricing_dataset(
work)
self.assertEqual(
res['status'],
ERR)
self.assertTrue(
expected_err in res['err'])
self.assertTrue(
res['rec'] is not None)
# end of test_redis_connection_exception_prepare_pricing
# this will mock the redis set and get
@mock.patch(
('redis.Redis'),
new=MockRedis)
@mock.patch(
('analysis_engine.get_data_from_redis_key.'
'get_data_from_redis_key'),
new=mock_redis_get_exception)
def test_redis_get_exception_prepare_pricing(self):
"""test_redis_get_exception_prepare_pricing"""
expected_err = 'test throwing mock_redis_get_exception'
redis_key = self.build_test_key(
test_name='test_redis_get_exception_prepare_pricing')
s3_key = redis_key
work = build_prepare_dataset_request()
work['s3_key'] = s3_key
work['redis_key'] = redis_key
res = run_prepare_pricing_dataset(
work)
self.assertEqual(
res['status'],
ERR)
self.assertTrue(
expected_err in res['err'])
self.assertTrue(
res['rec'] is not None)
# end of test_redis_get_exception_prepare_pricing
# this will mock the redis set and get
@mock.patch(
('redis.Redis'),
new=MockRedis)
@mock.patch(
('boto3.resource'),
new=build_boto3_resource)
@mock.patch(
('analysis_engine.s3_read_contents_from_key.'
's3_read_contents_from_key'),
new=mock_s3_read_contents_from_key)
@mock.patch(
('analysis_engine.work_tasks.publish_from_s3_to_redis.'
'publish_from_s3_to_redis'),
new=mock_publish_from_s3_to_redis)
def test_redis_get_no_data_found_for_prepare_pricing(self):
"""test_redis_get_no_data_found_for_prepare_pricing"""
expected_err = (
'did not find any data to prepare in redis_key=')
test_name = 'test_redis_get_no_data_found_for_prepare_pricing'
redis_key = self.build_test_key(
test_name=test_name)
s3_key = redis_key
os.environ.pop('TEST_S3_CONTENTS', None)
work = build_prepare_dataset_request()
work['s3_key'] = s3_key
work['redis_key'] = redis_key
res = run_prepare_pricing_dataset(
work)
self.assertEqual(
res['status'],
ERR)
self.assertTrue(
expected_err in res['err'])
self.assertTrue(
res['rec'] is not None)
# end of test_redis_get_no_data_found_for_prepare_pricing
# this will mock the redis set and get
@mock.patch(
('redis.Redis'),
new=MockRedis)
@mock.patch(
('boto3.resource'),
new=build_boto3_resource)
@mock.patch(
('analysis_engine.s3_read_contents_from_key.'
's3_read_contents_from_key'),
new=mock_s3_read_contents_from_key)
@mock.patch(
('analysis_engine.work_tasks.publish_from_s3_to_redis.'
'publish_from_s3_to_redis'),
new=mock_publish_from_s3_to_redis_err)
def test_redis_get_no_data_found_for_prepare_pricing_err(self):
"""test_redis_get_no_data_found_for_prepare_pricing_err"""
expected_err = (
'prepare ERR failed loading from bucket')
test_name = 'test_redis_get_no_data_found_for_prepare_pricing_err'
redis_key = self.build_test_key(
test_name=test_name)
s3_key = redis_key
value = {
'test_name': test_name
}
os.environ['TEST_S3_CONTENTS'] = json.dumps(value)
work = build_prepare_dataset_request()
work['s3_key'] = s3_key
work['redis_key'] = redis_key
res = run_prepare_pricing_dataset(
work)
self.assertEqual(
res['status'],
ERR)
self.assertTrue(
expected_err in res['err'])
self.assertTrue(
res['rec'] is not None)
# end of test_redis_get_no_data_found_for_prepare_pricing_err
# this will mock the redis set and get
@mock.patch(
('redis.Redis'),
new=MockRedis)
@mock.patch(
('boto3.resource'),
new=build_boto3_resource)
@mock.patch(
('analysis_engine.s3_read_contents_from_key.'
's3_read_contents_from_key'),
new=mock_s3_read_contents_from_key)
@mock.patch(
('analysis_engine.work_tasks.publish_from_s3_to_redis.'
'publish_from_s3_to_redis'),
new=mock_publish_from_s3_to_redis)
def test_data_invalid_json_to_prepare(self):
"""test_data_invalid_json_to_prepare"""
test_name = 'test_data_invalid_json_to_prepare'
redis_key = self.build_test_key(
test_name=test_name)
s3_key = redis_key
expected_err = (
'prepare did not find any data to prepare in '
f'redis_key={redis_key}')
value = {
'BAD_JSON': test_name
}
value_str = json.dumps(value)[0:PREPARE_DATA_MIN_SIZE-1]
os.environ['TEST_S3_CONTENTS'] = value_str
work = build_prepare_dataset_request()
work['s3_key'] = s3_key
work['redis_key'] = redis_key
res = run_prepare_pricing_dataset(
work)
self.assertEqual(
res['status'],
ERR)
self.assertTrue(
expected_err in res['err'])
self.assertTrue(
res['rec'] is not None)
# end of test_data_invalid_json_to_prepare
# this will mock the redis set and get
@mock.patch(
('redis.Redis'),
new=MockRedis)
@mock.patch(
('boto3.resource'),
new=build_boto3_resource)
@mock.patch(
('analysis_engine.s3_read_contents_from_key.'
's3_read_contents_from_key'),
new=mock_s3_read_contents_from_key)
@mock.patch(
('analysis_engine.work_tasks.publish_from_s3_to_redis.'
'publish_from_s3_to_redis'),
new=mock_publish_from_s3_to_redis)
def test_data_too_small_to_prepare(self):
"""test_data_too_small_to_prepare"""
expected_err = (
'not enough data=')
test_name = 'test_data_too_small_to_prepare'
redis_key = self.build_test_key(
test_name=test_name)
s3_key = redis_key
value = {
'0': '1'
}
value_str = json.dumps(value)[0:PREPARE_DATA_MIN_SIZE-1]
os.environ['TEST_S3_CONTENTS'] = value_str
work = build_prepare_dataset_request()
work['s3_key'] = s3_key
work['redis_key'] = redis_key
res = run_prepare_pricing_dataset(
work)
self.assertEqual(
res['status'],
ERR)
self.assertTrue(
expected_err in res['err'])
self.assertTrue(
res['rec'] is not None)
# end of test_data_too_small_to_prepare
# this will mock the redis set and get
@mock.patch(
('redis.Redis'),
new=MockRedis)
@mock.patch(
('analysis_engine.work_tasks.publish_from_s3_to_redis.'
'publish_from_s3_to_redis'),
new=mock_publish_from_s3_exception)
def test_s3_publish_exception(self):
"""test_s3_publish_exception"""
expected_err = 'test mock_publish_from_s3_exception'
redis_key = self.build_test_key(
test_name='test_s3_publish_exception')
s3_key = redis_key
work = build_prepare_dataset_request()
work['s3_key'] = s3_key
work['redis_key'] = redis_key
res = run_prepare_pricing_dataset(
work)
self.assertEqual(
res['status'],
ERR)
self.assertTrue(
expected_err in res['err'])
self.assertTrue(
res['rec'] is not None)
# end of test_s3_publish_exception
# this will mock the redis set and get
@mock.patch(
('redis.Redis'),
new=MockRedis)
@mock.patch(
('boto3.resource'),
new=build_boto3_resource)
@mock.patch(
('analysis_engine.s3_read_contents_from_key.'
's3_read_contents_from_key'),
new=mock_s3_read_contents_from_key)
@mock.patch(
('analysis_engine.work_tasks.publish_from_s3_to_redis.'
'publish_from_s3_to_redis'),
new=mock_publish_from_s3_to_redis)
@mock.patch(
('analysis_engine.dict_to_csv.flatten_dict'),
new=mock_flatten_dict_err)
def test_failed_flatten_dict(self):
"""test_failed_flatten_dict"""
expected_err = (
'flatten - convert to csv failed with ex=')
test_name = 'test_failed_flatten_dict'
redis_key = self.build_test_key(
test_name=test_name)
s3_key = redis_key
value = self.get_pricing_test_data()
value_str = json.dumps(value)
os.environ['TEST_S3_CONTENTS'] = value_str
work = build_prepare_dataset_request()
work['s3_key'] = s3_key
work['redis_key'] = redis_key
res = run_prepare_pricing_dataset(
work)
self.assertEqual(
res['status'],
ERR)
self.assertTrue(
expected_err in res['err'])
self.assertTrue(
res['rec'] is not None)
# end of test_failed_flatten_dict
# this will mock the redis set and get
@mock.patch(
('redis.Redis'),
new=MockRedis)
@mock.patch(
('boto3.resource'),
new=build_boto3_resource)
@mock.patch(
('analysis_engine.s3_read_contents_from_key.'
's3_read_contents_from_key'),
new=mock_s3_read_contents_from_key)
@mock.patch(
('analysis_engine.work_tasks.publish_from_s3_to_redis.'
'publish_from_s3_to_redis'),
new=mock_publish_from_s3_to_redis)
@mock.patch(
('analysis_engine.dict_to_csv.flatten_dict'),
new=mock_flatten_dict_empty)
def test_empty_flatten_dict(self):
"""test_empty_flatten_dict"""
expected_err = (
'flatten - did not return any data from redis_key=')
test_name = 'test_empty_flatten_dict'
redis_key = self.build_test_key(
test_name=test_name)
s3_key = redis_key
value = self.get_pricing_test_data()
value_str = json.dumps(value)
os.environ['TEST_S3_CONTENTS'] = value_str
work = build_prepare_dataset_request()
work['s3_key'] = s3_key
work['redis_key'] = redis_key
res = run_prepare_pricing_dataset(
work)
self.assertEqual(
res['status'],
ERR)
self.assertTrue(
expected_err in res['err'])
self.assertTrue(
res['rec'] is not None)
# end of test_empty_flatten_dict
# this will mock the redis set and get
@mock.patch(
('redis.Redis'),
new=MockRedis)
@mock.patch(
('boto3.resource'),
new=build_boto3_resource)
@mock.patch(
('analysis_engine.s3_read_contents_from_key.'
's3_read_contents_from_key'),
new=mock_s3_read_contents_from_key)
@mock.patch(
('analysis_engine.work_tasks.publish_from_s3_to_redis.'
'publish_from_s3_to_redis'),
new=mock_publish_from_s3_to_redis)
@mock.patch(
('analysis_engine.dict_to_csv.flatten_dict'),
new=mock_flatten_dict_too_small)
def test_too_small_flatten_dict(self):
"""test_too_small_flatten_dict"""
expected_err = (
'prepare - there is not enough data=')
test_name = 'test_too_small_flatten_dict'
redis_key = self.build_test_key(
test_name=test_name)
s3_key = redis_key
value = self.get_pricing_test_data()
value_str = json.dumps(value)
os.environ['TEST_S3_CONTENTS'] = value_str
work = build_prepare_dataset_request()
work['s3_key'] = s3_key
work['redis_key'] = redis_key
res = run_prepare_pricing_dataset(
work)
self.assertEqual(
res['status'],
ERR)
self.assertTrue(
expected_err in res['err'])
self.assertTrue(
res['rec'] is not None)
# end of test_too_small_flatten_dict
# this will mock the redis set and get
@mock.patch(
('redis.Redis'),
new=MockRedis)
@mock.patch(
('boto3.resource'),
new=build_boto3_resource)
@mock.patch(
('analysis_engine.s3_read_contents_from_key.'
's3_read_contents_from_key'),
new=mock_s3_read_contents_from_key)
@mock.patch(
('analysis_engine.work_tasks.publish_from_s3_to_redis.'
'publish_from_s3_to_redis'),
new=mock_publish_from_s3_to_redis)
def test_prepare_pricing_data_success(self):
"""test_prepare_pricing_data_success"""
test_name = 'test_prepare_pricing_data_success'
redis_key = self.build_test_key(
test_name=test_name)
s3_key = redis_key
value = self.get_pricing_test_data()
value_str = json.dumps(value)
os.environ['TEST_S3_CONTENTS'] = value_str
work = build_prepare_dataset_request()
work['s3_key'] = s3_key
work['redis_key'] = redis_key
res = run_prepare_pricing_dataset(
work)
self.assertEqual(
res['status'],
SUCCESS)
self.assertEqual(
res['err'],
None)
self.assertTrue(
res['rec'] is not None)
self.assertEqual(
res['rec']['initial_size'],
3111)
self.assertTrue(
res['rec']['initial_data'] is not None)
self.assertEqual(
res['rec']['prepared_size'],
3743)
self.assertTrue(
res['rec']['prepared_data'] is not None)
# end of test_prepare_pricing_data_success
# end of TestPreparePricingDataset