-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwebserver_test.py
902 lines (623 loc) · 25.7 KB
/
webserver_test.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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
import builtins
import pathlib
from unittest.mock import mock_open
import fastapi
import pandas
import pytest
import webserver as w
pytest_plugins = ('pytest_asyncio',)
@pytest.fixture(autouse=True)
def run_before_each_test():
w.PREPARE_DICT = {}
w.PREDICTION_DICT = {}
@pytest.mark.asyncio
async def test_prepare_status_job_done():
w.PREPARE_DICT['job1'] = w.Condition(
err=None,
work_in_progress=False
)
response = w.Response()
result = await w.get_status_prepare("job1", response)
assert result.job_id == "job1"
assert result.message == "job done"
assert response.status_code == 200
@pytest.mark.asyncio
async def test_prepare_status_job_in_progress():
w.PREPARE_DICT['job1'] = w.Condition(
err=None,
work_in_progress=True
)
response = w.Response()
result = await w.get_status_prepare("job1", response)
assert result.job_id == "job1"
assert result.message == "job in progress"
assert response.status_code == 423
@pytest.mark.asyncio
async def test_prepare_status_job_not_found():
response = w.Response()
result = await w.get_status_prepare("job1", response)
assert result.message == "job not found"
assert response.status_code == 400
@pytest.mark.asyncio
async def test_prepare_status_job_error():
w.PREPARE_DICT['job1'] = w.Condition(
err=Exception("test"),
work_in_progress=False
)
response = w.Response()
result = await w.get_status_prepare("job1", response)
assert result.message.startswith("something went wrong:")
assert response.status_code == 500
@pytest.mark.asyncio
async def test_prepare_parquet_happy(mocker):
res = w.Response()
mock_background_task = mocker.patch.object(fastapi.BackgroundTasks, "add_task")
mock_UploadFile = mocker.patch.object(fastapi.UploadFile, "read")
mock_open = mocker.patch.object(builtins, "open")
mock_exits = mocker.patch.object(w.Path, "exists")
mock_exits.return_value = True
resp_json = await w.prepare_parquet("test.model", mock_UploadFile, mock_background_task, res)
assert res.status_code == 201
assert resp_json.message == "prepare started"
assert len(resp_json.job_id) > 0
mock_background_task.add_task.assert_called_once()
mock_open.assert_called_once()
@pytest.mark.asyncio
async def test_prepare_parquet_model_does_not_exist(mocker):
res = w.Response()
mock_background_task = mocker.patch.object(fastapi.BackgroundTasks, "add_task")
mock_UploadFile = mocker.patch.object(fastapi.UploadFile, "read")
mock_exits = mocker.patch.object(w.Path, "exists")
mock_exits.return_value = False
resp_json = await w.prepare_parquet("test.model", mock_UploadFile, mock_background_task, res)
assert res.status_code == 400
assert resp_json.message == "model not found"
assert resp_json.job_id == ""
@pytest.mark.asyncio
async def test_prepare_parquet_task_already_running(mocker):
res = w.Response()
w.PREPARE_DICT = {'id1': w.Condition(None, True)}
mock_background_task = mocker.patch.object(fastapi.BackgroundTasks, "add_task")
mock_UploadFile = mocker.patch.object(fastapi.UploadFile, "read")
mocker.patch.object(builtins, "open")
mock_exits = mocker.patch.object(w.Path, "exists")
mock_exits.return_value = True
resp_json = await w.prepare_parquet("test.model", mock_UploadFile, mock_background_task, res)
assert res.status_code == 423
assert resp_json.message == "preparation in progress"
@pytest.mark.asyncio
async def test_prepare_parquet_data_input_wrong(mocker):
res = w.Response()
mock_background_task = mocker.patch.object(fastapi.BackgroundTasks, "add_task")
mocker.patch.object(builtins, "open")
mock_exits = mocker.patch.object(w.Path, "exists")
mock_exits.return_value = True
resp_json = await w.prepare_parquet("test.model", None, mock_background_task, res)
assert res.status_code == 500
assert resp_json.message == "there was an error uploading the parquet file"
@pytest.mark.asyncio
async def test_prepare_parquet_data_could_not_write(mocker):
res = w.Response()
mock_background_task = mocker.patch.object(fastapi.BackgroundTasks, "add_task")
mock_UploadFile = mocker.patch.object(fastapi.UploadFile, "read")
mock_open = mocker.patch.object(builtins, "open")
mock_open.side_effect = Exception()
mock_exits = mocker.patch.object(w.Path, "exists")
mock_exits.return_value = True
resp_json = await w.prepare_parquet("test.model", mock_UploadFile, mock_background_task, res)
assert res.status_code == 500
assert resp_json.message == "there was an error uploading the parquet file"
@pytest.mark.asyncio
async def test_prepare_json_happy(mocker):
prepare = w.PrepareJson(
domain_name=["test1.de", "test2.de"],
body_text=["testdata for test1", "testdata for test2"],
meta_text=["meta1", "meta2"],
model='jeder zottel ist ein model')
res = w.Response()
mock_background_task = mocker.patch.object(fastapi.BackgroundTasks, "add_task")
mock_write_preparation_parquet_from_body = mocker.patch.object(w, "write_preparation_parquet_from_body")
mock_exits = mocker.patch.object(w.Path, "exists")
mock_exits.return_value = True
resp_json = await w.prepare_json(prepare, mock_background_task, res)
assert res.status_code == 201
assert resp_json.message == "prepare started"
assert len(resp_json.job_id) > 0
mock_background_task.add_task.assert_called_once()
mock_write_preparation_parquet_from_body.assert_called_once()
@pytest.mark.asyncio
async def test_prepare_json_model_not_exist(mocker):
prepare = w.PrepareJson(
domain_name=["test1.de", "test2.de"],
body_text=["testdata for test1", "testdata for test2"],
meta_text=["meta1", "meta2"],
model='jeder zottel ist ein model')
res = w.Response()
mock_background_task = mocker.patch.object(fastapi.BackgroundTasks, "add_task")
mock_exits = mocker.patch.object(w.Path, "exists")
mock_exits.return_value = False
resp_json = await w.prepare_json(prepare, mock_background_task, res)
assert res.status_code == 400
assert resp_json.message == "model not found"
assert resp_json.job_id == ""
@pytest.mark.asyncio
async def test_prepare_json_data_input_wrong(mocker):
prepare = w.PrepareJson(
domain_name=["test1.de", "test2.de"],
body_text=["testdata for test1", "testdata for test2"],
meta_text=["meta1", "meta2"],
model='jeder zottel ist ein model')
res = w.Response()
mock_background_task = mocker.patch.object(fastapi.BackgroundTasks, "add_task")
mock_write_preparation_parquet_from_body = mocker.patch.object(w, "write_preparation_parquet_from_body")
mock_write_preparation_parquet_from_body.side_effect = ValueError('test exception')
mock_exits = mocker.patch.object(w.Path, "exists")
mock_exits.return_value = True
resp_json = await w.prepare_json(prepare, mock_background_task, res)
assert res.status_code == 406
assert resp_json.message == "input data was not correct: test exception"
@pytest.mark.asyncio
async def test_prepare_json_task_already_running(mocker):
prepare = w.PrepareJson(
domain_name=["test1.de", "test2.de"],
body_text=["testdata for test1", "testdata for test2"],
meta_text=["meta1", "meta2"],
model='jeder zottel ist ein model')
res = w.Response()
w.PREPARE_DICT = {'id1': w.Condition(None, True)}
mock_background_task = mocker.patch.object(fastapi.BackgroundTasks, "add_task")
mock_exits = mocker.patch.object(w.Path, "exists")
mock_exits.return_value = True
resp_json1 = await w.prepare_json(prepare, mock_background_task, res)
assert res.status_code == 423
assert resp_json1.message == "job in progress"
@pytest.mark.asyncio
async def test_predict_status_happy_case(mocker):
response = w.Response()
prediction_data = w.PredictionData(
visit_id=["a.de"],
category=["cat1"]
)
mock_parquet_to_json = mocker.patch.object(w, "parquet_to_json")
mock_parquet_to_json.return_value = prediction_data
w.PREDICTION_DICT = {
"job1": w.Condition(
err=None,
work_in_progress=False
)}
result = await w.get_predict("job1", response)
assert result.job_id == "job1"
assert result.message == "job done"
assert result.data == prediction_data
assert response.status_code == 200
@pytest.mark.asyncio
async def test_predict_status_job_not_found(mocker):
mock_parquet_to_json = mocker.patch.object(w, "parquet_to_json")
mock_parquet_to_json.return_value = "fake_data"
response = w.Response()
result = await w.get_predict("job1", response)
assert result.job_id == "job1"
assert result.message == "job not found"
assert result.data is None
assert response.status_code == 400
@pytest.mark.asyncio
async def test_predict_status_job_in_progress(mocker):
w.PREDICTION_DICT = {
"job1": w.Condition(
err=None,
work_in_progress=True
)}
mock_parquet_to_json = mocker.patch.object(w, "parquet_to_json")
mock_parquet_to_json.return_value = "fake_data"
response = w.Response()
result = await w.get_predict("job1", response)
assert result.job_id == "job1"
assert result.message == "job in progress"
assert result.data is None
assert response.status_code == 423
@pytest.mark.asyncio
async def test_predict_status_no_parquet_data(mocker):
w.PREDICTION_DICT = {
"job1": w.Condition(
err=None,
work_in_progress=False
)}
mock_parquet_to_json = mocker.patch.object(w, "parquet_to_json")
mock_parquet_to_json.return_value = None
response = w.Response()
result = await w.get_predict("job1", response)
assert result.job_id == "job1"
assert result.message == "job is finished, but could not find result data"
assert response.status_code == 500
@pytest.mark.asyncio
async def test_predict_status_error(mocker):
w.PREDICTION_DICT = {
"job1": w.Condition(
err=Exception("test"),
work_in_progress=False
)}
mock_parquet_to_json = mocker.patch.object(w, "parquet_to_json")
mock_parquet_to_json.return_value = None
response = w.Response()
result = await w.get_predict("job1", response)
assert result.job_id == "job1"
assert result.message.startswith("something went wrong:")
assert response.status_code == 500
@pytest.mark.asyncio
async def test_predict_happy_case(mocker):
w.PREDICTION_DICT = {
"job1": w.Condition(
err=None,
work_in_progress=False
)}
mock_background_task = mocker.patch.object(w.BackgroundTasks, "add_task")
mock_exits = mocker.patch.object(w.Path, "exists")
mock_exits.return_value = True
response = w.Response()
predict = w.Predict(
model="jeder zottel ist ein model",
job_id="job1",
)
result = await w.predict(predict, mock_background_task, response)
assert result.job_id == "job1"
assert result.message == "job started"
assert response.status_code == 201
@pytest.mark.asyncio
async def test_predict_model_not_found(mocker):
w.PREDICTION_DICT = {
"job1": w.Condition(
err=None,
work_in_progress=False
)}
mock_background_task = mocker.patch.object(w.BackgroundTasks, "add_task")
mock_exits = mocker.patch.object(w.Path, "exists")
mock_exits.return_value = False
response = w.Response()
predict = w.Predict(
model="jeder zottel ist ein model",
job_id="job1",
)
result = await w.predict(predict, mock_background_task, response)
assert result.job_id == "job1"
assert result.message == "model not found"
assert response.status_code == 400
@pytest.mark.asyncio
async def test_predict_job_in_progress(mocker):
w.PREDICTION_DICT = {
"job1": w.Condition(
err=None,
work_in_progress=True
)}
mock_background_task = mocker.patch.object(w.BackgroundTasks, "add_task")
mock_exits = mocker.patch.object(w.Path, "exists")
mock_exits.return_value = True
response = w.Response()
predict = w.Predict(
model="jeder zottel ist ein model",
job_id="job1",
)
result = await w.predict(predict, mock_background_task, response)
assert result.job_id == "job1"
assert result.message == "job in progress"
assert response.status_code == 423
@pytest.mark.asyncio
async def test_get_models(mocker):
mock_listdir = mocker.patch.object(w, "listdir")
mock_listdir.return_value = ["model1.mod", "model2.mod"]
result = await w.get_models()
assert result["files"] == ["model1.mod", "model2.mod"]
@pytest.mark.asyncio
async def test_upload_model_happy_case(mocker):
response = w.Response()
mock_UploadFile = mocker.patch.object(fastapi.UploadFile, "read")
mock_UploadFile.file.read.return_value = "fake_data"
m = mock_open()
mocker.patch.object(w, "open", m)
result = await w.upload_model(response, mock_UploadFile)
handle = m()
handle.write.assert_called_with('fake_data')
assert response.status_code == 201
assert result["message"].startswith("successfully uploaded model")
@pytest.mark.asyncio
async def test_upload_model_content_error(mocker):
response = w.Response()
mock_UploadFile = mocker.patch.object(fastapi.UploadFile, "read")
mock_UploadFile.file.read.side_effect = Exception()
m = mock_open()
mocker.patch.object(w, "open", m)
result = await w.upload_model(response, mock_UploadFile)
assert response.status_code == 500
assert result["message"].startswith("there was an error uploading the model")
@pytest.mark.asyncio
async def test_upload_model_write_error(mocker):
response = w.Response()
mock_UploadFile = mocker.patch.object(fastapi.UploadFile, "read")
mock_UploadFile.file.read.return_value = "fake_data"
mock_open = mocker.patch.object(w, "open")
mock_open.side_effect = Exception()
result = await w.upload_model(response, mock_UploadFile)
assert response.status_code == 500
assert result["message"].startswith("there was an error uploading the model")
@pytest.mark.asyncio
async def test_delete_models_happy_case(mocker):
mocker.patch.object(w.Path, "unlink")
response = w.Response()
result = await w.delete_models("file1", response)
assert result["message"] == "deleted successfully"
assert response.status_code == 200
@pytest.mark.asyncio
async def test_delete_models_error(mocker):
mocker.patch.object(w.Path, "unlink").side_effect = Exception()
response = w.Response()
result = await w.delete_models("file1", response)
assert result["message"] == "there was an error deleting this model"
assert response.status_code == 500
@pytest.mark.asyncio
async def test_get_parquet_input_file(mocker):
mock_listdir = mocker.patch.object(w, "listdir")
mock_listdir.return_value = ["file1.parquet", "file2.parquet"]
result = await w.get_input_files()
assert result["files"] == ["file1.parquet", "file2.parquet"]
@pytest.mark.asyncio
async def test_delete_parquet_input_file_happy_case(mocker):
mocker.patch.object(w.Path, "unlink")
response = w.Response()
result = await w.delete_input_files("file1", response)
assert result["message"] == "deleted successfully"
assert response.status_code == 200
@pytest.mark.asyncio
async def test_delete_parquet_input_file_error(mocker):
mocker.patch.object(w.Path, "unlink").side_effect = Exception()
response = w.Response()
result = await w.delete_input_files("file1", response)
assert result["message"] == "there was an error deleting this parquet file"
assert response.status_code == 500
@pytest.mark.asyncio
async def test_get_parquet_output_file(mocker):
mock_listdir = mocker.patch.object(w, "listdir")
mock_listdir.return_value = ["file1.parquet", "file2.parquet"]
result = await w.get_output_files()
assert result["files"] == ["file1.parquet", "file2.parquet"]
@pytest.mark.asyncio
async def test_delete_parquet_output_file_happy_case(mocker):
mocker.patch.object(w.Path, "unlink")
response = w.Response()
result = await w.delete_output_files("file1", response)
assert result["message"] == "deleted successfully"
assert response.status_code == 200
@pytest.mark.asyncio
async def test_delete_parquet_output_file_error(mocker):
mocker.patch.object(w.Path, "unlink").side_effect = Exception()
response = w.Response()
result = await w.delete_output_files("file1", response)
assert result["message"] == "there was an error deleting this parquet file"
assert response.status_code == 500
@pytest.mark.asyncio
async def test_get_hdf5_file(mocker):
mock_listdir = mocker.patch.object(w, "listdir")
mock_listdir.return_value = ["file1.hdf5", "file2.hdf5"]
result = await w.get_hdf5_files()
assert result["files"] == ["file1.hdf5", "file2.hdf5"]
@pytest.mark.asyncio
async def test_delete_hdf5_file_happy_case(mocker):
mocker.patch.object(w.Path, "unlink")
response = w.Response()
result = await w.delete_hdf5_files("file1", response)
assert result["message"] == "deleted successfully"
assert response.status_code == 200
@pytest.mark.asyncio
async def test_delete_hdf5_file_error(mocker):
mocker.patch.object(w.Path, "unlink").side_effect = Exception()
response = w.Response()
result = await w.delete_hdf5_files("file1", response)
assert result["message"] == "there was an error deleting this hd5 file"
assert response.status_code == 500
@pytest.mark.asyncio
async def test_get_job_happy_case():
w.PREDICTION_DICT = {
"job1": w.Condition(
err=None,
work_in_progress=False
),
"job3": w.Condition(
err=None,
work_in_progress=False
),
}
w.PREPARE_DICT = {
"job2": w.Condition(
err=None,
work_in_progress=False
),
"job4": w.Condition(
err=None,
work_in_progress=True
)
}
result = await w.get_jobs()
assert result["jobs"]["prepare"] == w.PREPARE_DICT
assert result["jobs"]["predict"] == w.PREDICTION_DICT
@pytest.mark.asyncio
async def test_delete_job_happy_case(mocker):
w.PREDICTION_DICT = {
"job1": w.Condition(
err=None,
work_in_progress=False
)}
w.PREPARE_DICT = {
"job1": w.Condition(
err=None,
work_in_progress=False
)}
mock_listdir = mocker.patch.object(w, "listdir")
mock_listdir.return_value = ["job1.parquet", "job1.hdf5"]
response = w.Response()
mocker.patch.object(w.Path, "unlink")
assert "job1" in w.PREPARE_DICT
assert "job1" in w.PREDICTION_DICT
result = await w.delete_job("job1", response)
assert "job1" not in w.PREPARE_DICT
assert "job1" not in w.PREDICTION_DICT
assert result["deleted"] == ['input_file: job1.parquet', 'output_file: job1.parquet', 'hdf5_file: job1.hdf5']
assert result["message"].startswith("removed all files for job")
assert response.status_code == 200
@pytest.mark.asyncio
async def test_delete_job_predict_running(mocker):
w.PREDICTION_DICT = {
"job1": w.Condition(
err=None,
work_in_progress=True
)}
mock_listdir = mocker.patch.object(w, "listdir")
mock_listdir.return_value = ["job1.parquet", "job1.hdf5"]
response = w.Response()
mocker.patch.object(w.Path, "unlink")
result = await w.delete_job("job1", response)
assert result["deleted"] == []
assert result["message"].startswith("job in progress for")
assert (response.status_code == 423)
@pytest.mark.asyncio
async def test_delete_job_prepare_running(mocker):
w.PREPARE_DICT = {
"job1": w.Condition(
err=None,
work_in_progress=True
)}
mock_listdir = mocker.patch.object(w, "listdir")
mock_listdir.return_value = ["job1.parquet", "job1.hdf5"]
response = w.Response()
mocker.patch.object(w.Path, "unlink")
result = await w.delete_job("job1", response)
assert result["deleted"] == []
assert result["message"].startswith("job in progress for")
assert response.status_code == 423
@pytest.mark.asyncio
async def test_delete_job_unlink_exception(mocker):
mocker.patch.object(w.Path, "unlink").side_effect = Exception()
mock_listdir = mocker.patch.object(w, "listdir")
mock_listdir.return_value = ["job1.parquet", "job1.hdf5"]
response = w.Response()
result = await w.delete_job("job1", response)
assert result["deleted"] == []
assert result["error"].startswith("could not delete file")
assert response.status_code == 500
def test_do_preparation_happy_case(mocker):
mocker.patch.object(w, "dit_data_preparation")
w.do_preparation("job1", "test.model")
assert "job1" in w.PREPARE_DICT
assert w.PREPARE_DICT["job1"].err is None
assert w.PREPARE_DICT["job1"].work_in_progress is False
def test_do_preparation_error(mocker):
mocker.patch.object(w, "dit_data_preparation").side_effect = Exception()
w.do_preparation("job1", "test.model")
assert "job1" in w.PREPARE_DICT
assert w.PREPARE_DICT["job1"].err is not None
assert w.PREPARE_DICT["job1"].work_in_progress is False
def test_dit_data_preparation(mocker):
mock_load = mocker.patch.object(w.torch, "load")
mock_parquet = mocker.patch.object(w.pq, "ParquetFile")
mock_preprocess_x = mocker.patch.object(w, "preprocess_x")
model_path = "model_path"
parquet_path = "parquet_path"
hd5_path = "parquet_path"
w.dit_data_preparation(model_path, parquet_path, hd5_path)
mock_load.assert_called_once()
mock_parquet.assert_called_once()
mock_preprocess_x.assert_called_once()
def test_do_prediction_happy_case(mocker):
mock_dit_category_prediction = mocker.patch.object(w, "dit_category_prediction")
predict = w.Predict(
model="jeder zottel ist ein model",
job_id="job1",
)
w.do_prediction(predict)
assert "job1" in w.PREDICTION_DICT
assert w.PREDICTION_DICT["job1"].err is None
assert w.PREDICTION_DICT["job1"].work_in_progress is False
mock_dit_category_prediction.assert_called_once()
def test_do_prediction_error(mocker):
mocker.patch.object(w, "dit_category_prediction").side_effect = Exception()
predict = w.Predict(
model="jeder zottel ist ein model",
job_id="job1",
)
w.do_prediction(predict)
assert "job1" in w.PREDICTION_DICT
assert w.PREDICTION_DICT["job1"].err is not None
assert w.PREDICTION_DICT["job1"].work_in_progress is False
def test_dit_category_prediction(mocker):
class fakeLabelEncoder:
classes_ = [1, 2]
mock_load_model = mocker.patch.object(w, "load_model").return_value = 1, 2, 3, fakeLabelEncoder()
mock_load = mocker.patch.object(w.PreprocessedInputs, "load")
mock_make_predictions = mocker.patch.object(w, "make_predictions")
w.dit_category_prediction("a", "b", "c")
mock_load.assert_called_once()
mock_make_predictions.assert_called_once()
def test_write_preparation_parquet_from_body():
prepare = w.PrepareJson(
job_id="job1",
domain_name=["test1.de", "test2.de"],
body_text=["testdata for test1", "testdata for test2"],
meta_text=["meta1", "meta2"],
model='jeder zottel ist ein model',
visit_id=[],
external_hosts=[]
)
w.write_preparation_parquet_from_body(prepare)
file_path = f'{w.PARQUET_INPUT_PATH}/{prepare.job_id}.parquet'
df = pandas.read_parquet(file_path)
assert df["visit_id"].array == prepare.visit_id
assert df["domain_name"].array == prepare.domain_name
assert df["body_text"].array == prepare.body_text
assert df["meta_text"].array == prepare.meta_text
assert df["external_hosts"].array == prepare.external_hosts
pathlib.Path(file_path).unlink()
def test_write_preparation_parquet_diff_body_text():
prepare = w.PrepareJson(
job_id="job1",
domain_name=["test1.de", "test2.de"],
body_text=["testdata for test2"],
meta_text=["meta1", "meta2"],
model='jeder zottel ist ein model',
visit_id=[],
external_hosts=[]
)
with pytest.raises(ValueError, match="domain_name and body_text diff length"):
w.write_preparation_parquet_from_body(prepare)
def test_write_preparation_diff_meta_text_meta_text():
prepare = w.PrepareJson(
job_id="job1",
domain_name=["test1.de", "test2.de"],
body_text=["aaaa", "testdata for test2"],
meta_text=["meta2"],
model='jeder zottel ist ein model',
visit_id=[],
external_hosts=[]
)
with pytest.raises(ValueError, match="domain_name and meta_text diff length"):
w.write_preparation_parquet_from_body(prepare)
def test_write_preparation_parquet_diff_visit_id():
prepare = w.PrepareJson(
job_id="job1",
domain_name=["test1.de", "test2.de"],
body_text=["testdata for test1", "testdata for test2"],
meta_text=["meta1", "meta2"],
model='jeder zottel ist ein model',
visit_id=["visit_id"],
external_hosts=[]
)
with pytest.raises(ValueError, match="domain_name and visit_id diff length"):
w.write_preparation_parquet_from_body(prepare)
def test_write_preparation_external_hosts():
prepare = w.PrepareJson(
job_id="job1",
domain_name=["test1.de", "test2.de"],
body_text=["testdata for test1", "testdata for test2"],
meta_text=["meta1", "meta2"],
model='jeder zottel ist ein model',
visit_id=[],
external_hosts=["aaa"]
)
with pytest.raises(ValueError, match="domain_name and external_hosts diff length"):
w.write_preparation_parquet_from_body(prepare)