88
99from manage_breast_screening .notifications .management .commands .create_reports import (
1010 Command ,
11- ReportConfig ,
1211)
1312from manage_breast_screening .notifications .queries .helper import Helper
1413
@@ -38,11 +37,7 @@ def mocked_dependencies(self, dataframe, csv_data, now):
3837 with patch (f"{ Command .__module__ } .datetime" ) as mock_datetime :
3938 mock_datetime .today .return_value = now
4039
41- with patch (f"{ Command .__module__ } .NhsMail" ) as mock_email :
42- mock_email_service = MagicMock ()
43- mock_email .return_value = mock_email_service
44-
45- yield (mock_read_sql , mock_blob_storage , mock_email_service )
40+ yield (mock_read_sql , mock_blob_storage )
4641
4742 @pytest .fixture
4843 def dataframe (self , csv_data ):
@@ -54,11 +49,10 @@ def test_handle_creates_all_reports(self, dataframe, csv_data, now):
5449 with self .mocked_dependencies (dataframe , csv_data , now ) as md :
5550 Command ().handle ()
5651
57- mock_read_sql , mock_blob_storage , mock_email_service = md
52+ mock_read_sql , mock_blob_storage = md
5853
5954 assert mock_read_sql .call_count == 2
6055 assert mock_blob_storage .add .call_count == 2
61- assert mock_email_service .send_reports_email .call_count == 1
6256
6357 for bso_code in Command .BSO_CODES :
6458 mock_read_sql .assert_any_call (
@@ -82,13 +76,6 @@ def test_handle_creates_all_reports(self, dataframe, csv_data, now):
8276 content_type = "text/csv" ,
8377 )
8478
85- mock_email_service .send_reports_email .assert_called_once_with (
86- {
87- failures_filename : csv_data ,
88- reconciliation_filename : csv_data ,
89- }
90- )
91-
9279 def test_handle_raises_command_error (self , mock_insights_logger ):
9380 with patch (f"{ Helper .__module__ } .Helper" ) as mock_query :
9481 mock_query .sql .side_effect = Exception ("err" )
@@ -127,7 +114,7 @@ def test_smoke_test_argument_uses_correct_configuration(
127114 with self .mocked_dependencies (dataframe , csv_data , now ) as md :
128115 Command ().handle (** {"smoke_test" : True })
129116
130- mock_read_sql , mock_blob_storage , mock_email_service = md
117+ mock_read_sql , mock_blob_storage = md
131118
132119 mock_read_sql .assert_called_once_with (
133120 Helper .sql ("reconciliation" ), connection , params = ["1 week" , "SM0K3" ]
@@ -137,71 +124,3 @@ def test_smoke_test_argument_uses_correct_configuration(
137124 csv_data ,
138125 content_type = "text/csv" ,
139126 )
140- mock_email_service .assert_not_called ()
141-
142- def test_handle_does_not_email_reports_with_send_email_false (
143- self , dataframe , csv_data , now , monkeypatch
144- ):
145- """
146- Test that internal reports are not emailed but still stored.
147- """
148- test_reports = [
149- ReportConfig ("external-report" , [now .date ()], True ),
150- ReportConfig ("internal-report" , [now .date ()], False ),
151- ]
152- monkeypatch .setattr (Command , "REPORTS" , test_reports )
153-
154- with patch (f"{ Helper .__module__ } .Helper.sql" ) as mock_helper_sql :
155- mock_helper_sql .return_value = "SELECT 1"
156-
157- with self .mocked_dependencies (dataframe , csv_data , now ) as md :
158- Command ().handle ()
159-
160- mock_read_sql , mock_blob_storage , mock_email_service = md
161-
162- assert mock_read_sql .call_count == 2
163- assert mock_blob_storage .add .call_count == 2
164- assert mock_email_service .send_reports_email .call_count == 1
165-
166- internal_filename = (
167- f"{ now .strftime ('%Y-%m-%dT%H:%M:%S' )} -MBD-internal-report-report.csv"
168- )
169- external_filename = (
170- f"{ now .strftime ('%Y-%m-%dT%H:%M:%S' )} -MBD-external-report-report.csv"
171- )
172- mock_blob_storage .add .assert_any_call (
173- internal_filename ,
174- csv_data ,
175- content_type = "text/csv" ,
176- )
177- mock_blob_storage .add .assert_any_call (
178- external_filename ,
179- csv_data ,
180- content_type = "text/csv" ,
181- )
182-
183- mock_email_service .send_reports_email .assert_called_once_with (
184- {external_filename : csv_data }
185- )
186-
187- def test_handle_with_internal_reports_does_not_call_email_service (
188- self , dataframe , csv_data , now , monkeypatch
189- ):
190- """
191- Test that internal reports are not emailed but still stored.
192- """
193- monkeypatch .setattr (
194- Command , "REPORTS" , [ReportConfig ("internal-report" , [1 ], False )]
195- )
196-
197- with patch (f"{ Helper .__module__ } .Helper.sql" ) as mock_helper_sql :
198- mock_helper_sql .return_value = "SELECT 1"
199-
200- with self .mocked_dependencies (dataframe , csv_data , now ) as md :
201- Command ().handle ()
202-
203- mock_read_sql , mock_blob_storage , mock_email_service = md
204-
205- assert mock_read_sql .call_count == 1
206- assert mock_blob_storage .add .call_count == 1
207- assert mock_email_service .send_reports_email .call_count == 0
0 commit comments