"""Pytest conftest module.""" import os from typing import NamedTuple from uuid import uuid4 import boto3 import pytest import xmltodict from ddex_ingester_common.constants.xmltodict import DDEX_LIST_FIELDS from ddex_ingester_common.schemas.state_machine_schema import \ StateMachineSchema from config import LAMBDA_NAME DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'unit/data',) # noqa class TestWorkspace(NamedTuple): """Test directory class.""" bucket: str key: str data_directory: str def parse_xml_to_dict(xml): """Parse given XML file using xmltodict.parse().""" with open(os.path.join(DATA_DIR, xml)) as fd: return xmltodict.parse(fd.read(), force_list=DDEX_LIST_FIELDS) @pytest.fixture() def data_directory(): """Return data directory.""" return os.path.abspath( os.path.join(os.path.dirname(__file__), 'integration/data/') ) @pytest.fixture def context_object(): """Context object representing DDEX data.""" return { 'execution_name': 'execution_name', 'state_machine_name': 'state_machine_name', 'execution_start_time': '2019-03-26T20:14:13.192Z', 'update_indicator': 'OriginalMessage', 'message_id': '12345', 'message_thread_id': '12345-TEST', 'bucket': 'ddex-ingester', 'key': 'sme_ddex/upc/', 'deals': None, 'is_purged_release': False, 'product': { 'status': None, 'display_artist_name': 'Cool Artist', 'vendor_id': None, 'subaccount_id': None, 'upc': '886448369561', 'is_main_release': True, 'product_id': None, 'release_reference': 'R0', 'artwork': { 'filename': 'artwork.tif', 'filepath': 'resources/', 'bucket': None, 'key': None }, 'release_type': 'Album', 'grid': 'A0145345234523453452', 'catalog_number': 'G0134523452345245', 'product_name': 'Test Artist' }, 'tracks': [ { 'release_reference': 'R1', 'resource_reference': 'A1', 'asset': { 'filename': 'audio.flac', 'filepath': 'resources/', 'bucket': None, 'key': None }, 'volume': 1, 'tuid': None, 'sequence_number': 1, 'isrc': 'USSM12001699', 'release_type': 'TrackRelease', 'resource_reference': 'A1', } ], 'video': { 'release_reference': 'T1', 'isrc': 'US234234322', 'tuid': None, 'assets': [ { 'filepath': 'resources/', 'key': '1_Video_001-001.mp4', 'bucket': 'qa-ddex-ingester', 'filename': '1_Video_001-001.mp4', 'aspect_ratio': '1.333' }, { 'filepath': 'resources/', 'key': '2_Video_001-001.mp4', 'bucket': 'qa-ddex-ingester', 'filename': '1_Video_001-001.mp4', 'aspect_ratio': '1.778' } ], }, 'ddex_provider': 'SME', } @pytest.fixture def deserialized_context(context_object): """Deserialized context object fixture.""" return StateMachineSchema().load(context_object) @pytest.fixture() def test_workspace(s3_client, data_directory): """Test workspace setup.""" bucket = 'qa-ddex-ingester' key = f'integration_tests/{uuid4()}' s3_client.put_object(Bucket=bucket, Key=key) yield TestWorkspace(bucket, key, data_directory) s3 = boto3.resource('s3') bucket = s3.Bucket(bucket) # To delete all file objects under test workspace bucket.objects.filter(Prefix=key).delete() @pytest.fixture() def lambda_client(): """Lambda client.""" return boto3.client('lambda', region_name='us-east-1') @pytest.fixture() def s3_client(): """Lambda client.""" return boto3.client('s3', region_name='us-east-1') @pytest.fixture() def lambda_name(): """Lambda name.""" return f'{LAMBDA_NAME}-qa:qa' @pytest.fixture def ddex_document(): """DDEX document deserialized into a dict.""" return parse_xml_to_dict('ddex-single-with-extended-metadata.xml') @pytest.fixture def unparsed_awal_ddex_2(): """AWAL DDEX document deserialized into a dict.""" with open(os.path.join(DATA_DIR, 'awal_ddex_document_2.xml')) as fd: # noqa return fd.read() @pytest.fixture def extended_metadata(): """DDEX document deserialized into a dict.""" return parse_xml_to_dict('extended_metadata.xml') @pytest.fixture def unparsed_awal_ddex(): """DDEX document.""" with open(os.path.join(DATA_DIR, 'awal_ddex_document_3.xml')) as fd: # noqa return fd.read() @pytest.fixture def extended_awal_ddex(): """DDEX document.""" return parse_xml_to_dict('awal_ddex_document_3_ext.xml') @pytest.fixture def unparsed_awal_ddex_audio_takedown(): """AWAL DDEX document deserialized into a dict.""" with open(os.path.join(DATA_DIR, 'awal_takedown_audio.xml')) as fd: # noqa return fd.read() @pytest.fixture def unparsed_88_rising_ddex(): """DDEX document.""" with open(os.path.join(DATA_DIR, '88_rising_ddex_document.xml')) as fd: # noqa return fd.read()