import logging import pathlib import unittest.mock from db_schema.schemas.slz import ContentMetadataStatusEnum from slz_hasher.hash import ReportHash, SavedHash, TaggedHash, parse_s3_path from . import FIXTURES_PATH def test_report_hash_s3_path_parsing(): bucket, key = parse_s3_path('s3://dev-delphi-config/test/file.zip') assert bucket == 'dev-delphi-config' assert key == 'test/file.zip' def test_report_hash_hash(): report_path = pathlib.Path(FIXTURES_PATH) / 'txt_report.txt' report_hash = ReportHash( logger=unittest.mock.Mock(), report_paths=[report_path], schema={ 'format': 'txt', 'delimiter': '#*#', 'has_metadata': True, } ) hash_of_records, num_of_records = report_hash.hash() assert hash_of_records == '4fa1b56c776d51b24e2a0dd573e05fc8' assert num_of_records == 7 def test_report_hash_hash_multiple_reports(): report_path = pathlib.Path(FIXTURES_PATH) / 'txt_report.txt' report_hash = ReportHash( logger=unittest.mock.Mock(), report_paths=[report_path, report_path], schema={ 'format': 'txt', 'delimiter': '#*#', 'has_metadata': True, } ) hash_of_records, num_of_records = report_hash.hash() assert hash_of_records == '4dd7a57d1ea0dd273acfc12565965bb1' assert num_of_records == 14 def test_saved_hash_hash(): params = unittest.mock.Mock() params.uow = uow_id = 'spotify-20190911-smejp-streams-v2' params.unit_of_work_id = None params.content_name = 'test_content_name' params.context = context = 'context' pg_repository = unittest.mock.Mock() report_hash = unittest.mock.Mock() report_hash.hash.return_value = hash_, num_of_records = 'hash', 10 saved_hash = SavedHash( logger=unittest.mock.Mock(), decorated=report_hash, pg_repository=pg_repository, params=params, ) hash_of_records, num_of_records = saved_hash.hash() pg_repository.update_content_status_metadata_by_uow_id.assert_called_once_with( uow_id=uow_id, context=context, num_of_records=10, hash_of_records='hash', process_status=ContentMetadataStatusEnum.COMPLETED, ) def test_tagging_called(): s3 = unittest.mock.Mock() report_hash = unittest.mock.Mock() report_hash.hash.return_value = ('hash', 10) tagged_hash = TaggedHash( logger=logging.getLogger(), decorated=report_hash, s3=s3, archive_path='s3://sme-archive/amazon/report.txt.gz', ) tagged_hash.hash() s3.put_object_tagging.assert_called_once_with( Bucket='sme-archive', Key='amazon/report.txt.gz', Tagging={ 'TagSet': [ { 'Key': 'Hash', 'Value': 'hash' }, { 'Key': 'RecordsCount', 'Value': '10' }, ] } ) def test_tagging_not_called(): s3 = unittest.mock.Mock() report_hash = unittest.mock.Mock() report_hash.hash.return_value = ('hash', 10) tagged_hash = TaggedHash( logger=logging.getLogger(), decorated=report_hash, s3=s3, archive_path=None, ) tagged_hash.hash() s3.put_object_tagging.assert_not_called()