Using key query rather than try catch

Added a unit test for empty content
This commit is contained in:
venusbb
2018-01-17 13:51:57 +00:00
parent 9179802717
commit 7d84b4629a
3 changed files with 23 additions and 11 deletions

View File

@@ -119,12 +119,8 @@ def get_list_of_files_by_suffix(bucket_name, subfolder='', suffix='', last_modif
)
for page in page_iterator:
try:
for obj in page['Contents']:
for obj in page.get('Contents', []):
key = obj['Key'].lower()
if key.endswith(suffix.lower()):
if not last_modified or obj['LastModified'] >= last_modified:
yield key
except KeyError:
# Not content for today
pass

View File

@@ -208,3 +208,18 @@ def test_get_list_of_files_by_suffix(notify_api, mocker, suffix_str, days_before
assert sum(1 for x in key) == returned_no
for k in key:
assert k == 'bar/foo.ACK.txt'
def test_get_list_of_files_by_suffix_empty_contents_return_with_no_error(notify_api, mocker):
paginator_mock = mocker.patch('app.aws.s3.client')
multiple_pages_s3_object = [
{
"other_content": [
'some_values',
]
}
]
paginator_mock.return_value.get_paginator.return_value.paginate.return_value = multiple_pages_s3_object
key = get_list_of_files_by_suffix('foo-bucket', subfolder='bar', suffix='.pdf')
assert sum(1 for x in key) == 0

View File

@@ -1058,8 +1058,9 @@ def test_letter_not_raise_alert_if_ack_files_match_zip_list(mocker, notify_db):
subfoldername = datetime.utcnow().strftime('%Y-%m-%d')
assert mock_file_list.call_count == 2
assert mock_file_list.call_args_list == [
call(bucket_name='test-letters-pdf', subfolder=subfoldername, suffix='.zip'),
call(bucket_name='test.notify.com-ftp', subfolder='root/dispatch', suffix='.ACK.txt', last_modified=yesterday),
call(bucket_name=current_app.config['LETTERS_PDF_BUCKET_NAME'], subfolder=subfoldername, suffix='.zip'),
call(bucket_name=current_app.config['DVLA_RESPONSE_BUCKET_NAME'], subfolder='root/dispatch',
suffix='.ACK.txt', last_modified=yesterday),
]
assert mock_get_file.call_count == 1