11import re
2+ from unittest .mock import call
23
34import pytest
45from connect .client import ClientError
6+ from connect .client .rql import R
57from connect .eaas .core .inject .models import Context
68from sqlalchemy .exc import DBAPIError
79
@@ -213,7 +215,10 @@ def test_create_upload_schedule_task(
213215 'account_id' : installation ['owner' ]['id' ],
214216 },
215217 }
216- report_file = {'id' : 'RP-262-019-481' , 'renderer' : 'csv' }
218+ report_file = [
219+ {'id' : 'RP-262-019-481' , 'renderer' : 'csv' },
220+ {'id' : 'RP-262-019-482' , 'renderer' : 'csv' },
221+ ]
217222 ext = ConnectBiReporterEventsApplication (
218223 connect_client ,
219224 logger ,
@@ -232,37 +237,66 @@ def test_create_upload_schedule_task(
232237 'connect_bi_reporter.uploads.services.get_report_schedule' ,
233238 return_value = report_schedule ,
234239 )
235- mocker .patch (
240+ p_get_reporting_report = mocker .patch (
236241 'connect_bi_reporter.uploads.services.get_reporting_report' ,
237- return_value = report_file ,
242+ side_effect = report_file ,
238243 )
239244 mocker .patch (
240245 'connect_bi_reporter.scheduler.create_schedule_task' ,
241246 return_value = eaas_schedule_task ,
242247 )
243- feed = feed_factory (
248+ feed1 = feed_factory (
249+ schedule_id = report_schedule ['id' ],
250+ account_id = installation ['owner' ]['id' ],
251+ status = feed_factory ._meta .model .STATUSES .enabled ,
252+ )
253+ feed2 = feed_factory (
244254 schedule_id = report_schedule ['id' ],
245255 account_id = installation ['owner' ]['id' ],
246256 status = feed_factory ._meta .model .STATUSES .enabled ,
247257 )
248258
249259 result = ext .create_uploads (schedule )
250- upload = dbsession .query (upload_factory ._meta .model ).first ()
251- assert result .status == 'success'
252- assert upload .report_id == report_file ['id' ]
253- assert upload .status == upload_factory ._meta .model .STATUSES .pending
254- assert upload .feed_id == feed .id
260+ uploads = dbsession .query (upload_factory ._meta .model ).all ()
261+ p_get_reporting_report .assert_has_calls (
262+ [
263+ call (
264+ connect_client , (
265+ R ().status .eq ('succeeded' ) & R ().account .id .eq (feed1 .account_id )
266+ & R ().schedule .id .eq (feed1 .schedule_id )
267+ ),
268+ ),
269+ call (
270+ connect_client , (
271+ R ().status .eq ('succeeded' ) & R ().account .id .eq (feed2 .account_id )
272+ & R ().schedule .id .eq (feed2 .schedule_id )
273+ ),
274+ ),
275+ ],
276+ )
277+ for idx , zipped in enumerate (zip (uploads , [feed1 , feed2 ])):
278+ upload , feed = zipped
279+ assert result .status == 'success'
280+ assert upload .report_id == report_file [idx ]['id' ]
281+ assert upload .status == upload_factory ._meta .model .STATUSES .pending
282+ assert upload .feed_id == feed .id
283+
255284 assert logger .method_calls [0 ].args [0 ] == (
256- f'New Uploads were created: `Upload={ upload .id } '
257- f' for Feed={ feed .id } `.'
285+ f'New Uploads were created: `Upload={ uploads [ 0 ] .id } for Feed= { feed1 . id } , '
286+ f'Upload= { uploads [ 1 ]. id } for Feed={ feed2 .id } `.'
258287 )
259288 assert logger .method_calls [1 ].args [0 ] == (
260289 f'Periodic Schedule Task created: `{ eaas_schedule_task ["id" ]} `.'
261290 )
262291 assert logger .method_calls [2 ].args [0 ] == (
263292 f'New Scheduled Task `{ eaas_schedule_task ["id" ]} `'
264- f' created for Upload `{ upload .id } `: '
265- f'Will process Report File `{ report_file ["id" ]} `'
293+ f' created for Upload `{ uploads [0 ].id } `: '
294+ f'Will process Report File `{ report_file [0 ]["id" ]} `'
295+ )
296+ assert logger .method_calls [4 ].args [0 ] == (
297+ f'New Scheduled Task `{ eaas_schedule_task ["id" ]} `'
298+ f' created for Upload `{ uploads [1 ].id } `: '
299+ f'Will process Report File `{ report_file [1 ]["id" ]} `'
266300 )
267301
268302
0 commit comments