"""Test utilities.""" import contextlib from flexmock import flexmock from moto import mock_aws from availability import config from availability.connectors import sql from availability.connectors import sqs from availability.constants import stores from availability.logic.queue import utils as queue_utils from availability.models import product_in_store from availability.models import store TEST_QUEUE_NAME = 'test-queue' def get_expected_countries( *, all_countries, live_countries, carveouts_countries, store_id): """Get test list of expected depending on the store_id. Args: all_countries (list): A list of all countries to check. live_countries (list): A list of live countries for a release. carveouts_countries (list): A list of countries in the ows-carveouts for a release. store_id (int): Store ID. Returns: list: A list of expected countries. """ excluded = live_countries if store_id == stores.STORE_ID_SPOTIFY: excluded += carveouts_countries return [c for c in all_countries if c not in excluded] @contextlib.contextmanager def mock_availability_queue(store_id=None): """Context manager that yields mock queue under mock_sqs context.""" with mock_aws(): connection = sqs.get_connection() connection.create_queue(QueueName=TEST_QUEUE_NAME) queue = sqs.get_queue(TEST_QUEUE_NAME, sqs_connection=connection) queue.set_attributes( Attributes={ 'VisibilityTimeout': str( config.SQS_MESSAGE_VISIBILITY_TIMEOUT_SECONDS), 'MessageRetentionPeriod': str( config.SQS_MESSAGE_RETENTION_PERIOD_SECONDS) } ) if store_id is None: store_id = config.STORE_ID if store_id is None: expected_queue_name = config.SQS_QUEUE_STORE_AVAILABILITY else: expected_queue_name = config.SQS_STORE_SPECIFIC_QUEUE.format( store_id=store_id) (flexmock(sqs) .should_receive('get_queue') .with_args(expected_queue_name) .and_return(queue)) queue = queue_utils.get_availability_queue(store_id) yield queue def create_product_per_store( *, store_id, product_id, product_in_store_status, sales_start_date, store_internal_id, countries, go_live_date, store_name=None, polling_delay_days=0, store_internal_status='', poll_days_after_sales=7): """Create one store and one product record for it. Args: store_id (int): Store ID. product_id (int): Product ID. product_in_store_status (str): Status of the product in this store. sales_start_date (datetime.datetime): Product sales start date. store_internal_id (str): Internal ID of this product in the store. countries (list): A list of country codes this product is avaialble in. go_live_date (date): Product go-live date in the store. store_name (str): Store name. polling_delay_days (int): Polling delay in days. store_internal_status (str): Store internal ID of this product. poll_days_after_sales (int): How many days to poll for the product. store_name (str): Store name. """ if store_name is None: store_name = str(store_id) store_obj = store.Store( store_id=store_id, name=store_name, polling_delay_days=polling_delay_days, poll_days_after_sales=poll_days_after_sales) product_in_store_obj = product_in_store.ProductInStore( product_id=product_id, store_id=store_id, status=product_in_store_status, sales_start_date=sales_start_date, store_internal_id=store_internal_id, store_internal_status=store_internal_status, countries=countries, go_live_date=go_live_date, ) with sql.session_scope() as session: session.add(store_obj) session.add(product_in_store_obj)