"""conftest. This file gets picked up when running py.test tests: http://pytest.org/latest/writing_plugins.html#conftest """ from collections import namedtuple from unittest.mock import MagicMock import application from oto import response from owslogger import flask_logger import pytest from werkzeug.datastructures import ImmutableMultiDict from conflict_manager import config from conflict_manager.models import fact_conflict from conflict_manager.models import ows_masters_registry from conflict_manager.models import ows_sound_recordings from conflict_manager.utils.account_utils import Account from tests.fixtures.models import ows_product from tests.fixtures.models import ows_territories from tests.fixtures.models import ows_track @pytest.fixture def mock_snowflake_config(monkeypatch): """Mock snowflake credentials.""" monkeypatch.setattr(config, 'SNOWFLAKE_CONNECT_ARGS', {'private_key': 'test'}) monkeypatch.setattr(config, 'SNOWFLAKE_USER', 'test') @pytest.fixture def client(): """Return flask test client. Returns: flask client: flask test client """ return application.app.test_client() @pytest.fixture def test_vendor_id(): """Test vendor_id.""" return 7123 @pytest.fixture def test_values(): """Return test values.""" return { 'product_id': 10382, 'tuid': 391, 'upc': 19876543312, 'display_upc': '019876543312', 'isrc': 'QM4TW1500001' } @pytest.fixture def test_values2(): """Return test values (2nd set).""" return { 'product_id': 11482, 'tuid': 433, 'upc': 8681511040000, 'display_upc': '8681511040000', 'isrc': 'US6TA1600031' } @pytest.fixture def test_values3(): """Return test values (3rd set).""" return { 'product_id': 12482, 'tuid': 704, 'upc': 7041990123, 'display_upc': '7041990123', 'isrc': 'CA6TA1600031' } @pytest.fixture def territories_data(): """Return ows_territories successful response.""" return ows_territories.TERRITORY_RESPONSE @pytest.fixture def ows_track_succesful_response(): """Return ows_track successful response.""" return response.Response(ows_track.TRACK_RESPONSE) @pytest.fixture def ows_not_found_response(): """Return not found response.""" return response.create_not_found_response() @pytest.fixture def ows_product_succesful_response(): """Return ows_product successful response.""" return response.Response(ows_product.PRODUCT_RESPONSE) @pytest.fixture def account_tuple(): """Return test account named tuple.""" return Account('vendor', '7123') @pytest.fixture def query_args(): """Return a dict representing parsed query args.""" return ImmutableMultiDict({ 'query': 'remix', 'fields': ['track_artists'] }) @pytest.fixture def query_args_bad(): """Return a dict representing parsed query args.""" return ImmutableMultiDict({ 'shoe': 'remix', 'fields': ['track_artists'] }) @pytest.fixture def delete_args(): """Return a function to generate parsed query args. Args: hit_count(int): The number of results to delete. """ def _delete_ids(id_count=0): ids = [] for i in range(1, id_count + 1): ids.append(str(i)) return ids return _delete_ids @pytest.fixture def result_hits(): """Fixture to represent the number of elasticsearch hits. Args: hit_count(int): The number of results to return. """ def _return_hits(hit_count=0): Result = namedtuple('Result', ['hits']) hits = [] for i in range(1, hit_count + 1): hits.append(str(i)) return Result(hits) return _return_hits @pytest.fixture def count_response(): """Fixture to represent the number of count API response. Args: count(int): The count value to return. """ def _return_count(count=0): return { 'count': count, '_shards': { 'total': 5, 'successful': 5, 'skipped': 0, 'failed': 0 } } return _return_count @pytest.fixture def get_conflicts_territories_mock(mocker): """Return get_conflicts_territories mock.""" items = [ { 'conflict_date': '2017-11-01', 'territories': ['CA', 'US', 'FO'], 'conflicting_owner': 'adrevuppm', 'isrc': 'USY9R0915512', 'tuid': 9873204 }, { 'conflict_date': '2017-11-01', 'territories': ['FO'], 'conflicting_owner': 'adrevuppm', 'isrc': 'USY9R0911133', 'tuid': 9334577 } ] return mocker.patch.object( fact_conflict, 'get_conflicts_territories', return_value=items) @pytest.fixture def bulk_remove_territories_mock(mocker): """Return bulk_remove_territories mock.""" return mocker.patch.object(ows_masters_registry, 'bulk_remove_territories') @pytest.fixture def get_rules_mock(mocker): """Return get_rules mock with a Response object containing a .message attribute.""" # noqa: E501 from oto import response # Example rules structure, adjust as needed for your tests rules_message = [ { 'id': 40953007, 'rules': [ { 'active': False, 'created_at': '2025-03-25T14:56:51.008000+00:00', 'created_by_profile_id': '179', 'created_by_profile_type': 'OrchAdminProfile', 'end': None, 'last_modified_at': '2025-03-25T14:56:51.008000+00:00', 'last_modified_by_profile_id': '179', 'last_modified_by_profile_type': 'OrchAdminProfile', 'policy': 'carveout', 'service': 'meta', 'start': None, 'territory': 'RU' }, { 'active': False, 'created_at': '2025-03-25T14:56:51.009000+00:00', 'created_by_profile_id': '179', 'created_by_profile_type': 'OrchAdminProfile', 'end': None, 'last_modified_at': '2025-03-25T14:56:51.009000+00:00', 'last_modified_by_profile_id': '179', 'last_modified_by_profile_type': 'OrchAdminProfile', 'policy': 'carveout', 'service': 'tiktok', 'start': None, 'territory': 'RU' } ] } ] mock_response = response.Response(rules_message) return mocker.patch.object( ows_sound_recordings, 'get_rules', return_value=mock_response ) @pytest.fixture def bulk_create_fingerprint_rules_mock(mocker): """Return bulk_create_fingerprint_rules mock.""" return mocker.patch.object( ows_sound_recordings, 'bulk_create_fingerprint_rules' ) @pytest.fixture def context(): """Return flask app context.""" context = application.app.app_context() context.g.ows = flask_logger.Ows() context.g.ows.log = MagicMock() return context