2
2
3
3
# standard library
4
4
import argparse
5
+ from typing import Iterable
5
6
import unittest
6
7
from unittest .mock import MagicMock
7
8
@@ -27,9 +28,7 @@ def _path_details(self):
27
28
# a file with a data error
28
29
('path/b.csv' , ('src_b' , 'sig_b' , 'week' , 'msa' , 202016 , 202017 , 1 )),
29
30
# emulate a file that's named incorrectly
30
- ('path/c.csv' , None ),
31
- # another good file w/ wip
32
- ('path/d.csv' , ('src_d' , 'wip_sig_d' , 'week' , 'msa' , 202016 , 202017 , 1 )),
31
+ ('path/c.csv' , None )
33
32
]
34
33
35
34
def test_collect_files (self ):
@@ -65,15 +64,16 @@ def load_csv_impl(path, *args):
65
64
yield make_row ('b1' )
66
65
yield None
67
66
yield make_row ('b3' )
68
- elif path == 'path/d.csv' :
69
- yield make_row ('d1' )
70
67
else :
71
68
# fail the test for any other path
72
69
raise Exception ('unexpected path' )
73
70
71
+ def iter_len (l : Iterable ) -> int :
72
+ return len (list (l ))
73
+
74
74
data_dir = 'data_dir'
75
75
mock_database = MagicMock ()
76
- mock_database .insert_or_update_bulk . return_value = 2
76
+ mock_database .insert_or_update_bulk = MagicMock ( wraps = iter_len )
77
77
mock_csv_importer = MagicMock ()
78
78
mock_csv_importer .load_csv = load_csv_impl
79
79
mock_file_archiver = MagicMock ()
@@ -87,30 +87,28 @@ def load_csv_impl(path, *args):
87
87
mock_logger ,
88
88
csv_importer_impl = mock_csv_importer )
89
89
90
- self .assertEqual (modified_row_count , 4 )
90
+ self .assertEqual (modified_row_count , 3 )
91
91
# verify that appropriate rows were added to the database
92
- self .assertEqual (mock_database .insert_or_update_bulk .call_count , 2 )
92
+ self .assertEqual (mock_database .insert_or_update_bulk .call_count , 1 )
93
93
call_args_list = mock_database .insert_or_update_bulk .call_args_list
94
94
actual_args = [[(a .source , a .signal , a .time_type , a .geo_type , a .time_value ,
95
95
a .geo_value , a .value , a .stderr , a .sample_size , a .issue , a .lag )
96
96
for a in call .args [0 ]] for call in call_args_list ]
97
97
expected_args = [
98
98
[('src_a' , 'sig_a' , 'day' , 'hrr' , 20200419 , 'a1' , 'a1' , 'a1' , 'a1' , 20200420 , 1 ),
99
99
('src_a' , 'sig_a' , 'day' , 'hrr' , 20200419 , 'a2' , 'a2' , 'a2' , 'a2' , 20200420 , 1 ),
100
- ('src_a' , 'sig_a' , 'day' , 'hrr' , 20200419 , 'a3' , 'a3' , 'a3' , 'a3' , 20200420 , 1 )],
101
- [('src_d' , 'wip_sig_d' , 'week' , 'msa' , 202016 , 'd1' , 'd1' , 'd1' , 'd1' , 202017 , 1 )]
100
+ ('src_a' , 'sig_a' , 'day' , 'hrr' , 20200419 , 'a3' , 'a3' , 'a3' , 'a3' , 20200420 , 1 )]
102
101
]
103
102
self .assertEqual (actual_args , expected_args )
104
103
105
104
# verify that two files were successful (a, d) and two failed (b, c)
106
- self .assertEqual (mock_file_archiver .archive_file .call_count , 4 )
105
+ self .assertEqual (mock_file_archiver .archive_file .call_count , 3 )
107
106
call_args_list = mock_file_archiver .archive_file .call_args_list
108
107
actual_args = [args for (args , kwargs ) in call_args_list ]
109
108
expected_args = [
110
109
('path' , 'data_dir/archive/successful/src_a' , 'a.csv' , True ),
111
110
('path' , 'data_dir/archive/failed/src_b' , 'b.csv' , False ),
112
111
('path' , 'data_dir/archive/failed/unknown' , 'c.csv' , False ),
113
- ('path' , 'data_dir/archive/successful/src_d' , 'd.csv' , True ),
114
112
]
115
113
self .assertEqual (actual_args , expected_args )
116
114
0 commit comments