"""Fixtures for pytest.""" from types import SimpleNamespace from pathlib import Path import pytest from constants import database as db_consts from .utils_local_sftp import enable_local_sftp_monkeypatch @pytest.fixture(autouse=True) def no_interactive_prompts(monkeypatch): """Ensure tests never block for interactive input. Patch getpass.getpass to a non-blocking stub by default. Individual tests can override this to assert prompt behavior explicitly. """ import getpass monkeypatch.setattr(getpass, 'getpass', lambda prompt='': '') # Clear global mock mode flag unless a test explicitly sets it monkeypatch.delenv('SFTP_MOCK', raising=False) # Ensure no ambient SFTP password leaks across tests monkeypatch.delenv('SFTP_PASSWORD', raising=False) yield @pytest.fixture def local_sftp_server(tmp_path, monkeypatch): """Enable local ephemeral SFTP server mock when SFTP_HOST=localhost. Patches paramiko.SSHClient to a filesystem-backed mock using tmp_path. Restores original class on teardown. """ import os if os.environ.get("SFTP_HOST") != "localhost": # No-op if not targeting local host yield None return user = os.environ.get("SFTP_USER", "devuser") pw = os.environ.get("SFTP_PASSWORD", "devpass") undo = enable_local_sftp_monkeypatch(Path(tmp_path), user, pw) try: yield tmp_path finally: undo() @pytest.fixture def test_group_name_list(): """Fixture for stores.""" return [ 'Google Play - 496 - US - 256 - 202003', 'YouTube - 453 - US - 256 - 201710', 'YouTube - 453 - US - 256 - 201802', ] @pytest.fixture def test_group_name_list_message(): """Fixture for stores.""" ret_list = [ 'Google Play - 496 - US - 256 - 202003', 'YouTube - 453 - US - 256 - 201710', 'YouTube - 453 - US - 256 - 201802', ] return SimpleNamespace(message=ret_list) @pytest.fixture def test_store_list_message(): """Fixture for stores.""" ret_list = [ 1, 2, 3 ] return SimpleNamespace(message=ret_list) @pytest.fixture def test_store_for_period_list(): ret_list = [ 1, 2, 3 ] return SimpleNamespace(message=ret_list) @pytest.fixture def test_get_by_period_store(): return SimpleNamespace(message=['item']) @pytest.fixture def test_get_by_period_group_name(): return SimpleNamespace(message=['item']) @pytest.fixture def test_period_list(): """Fixture for periods.""" return [ 100, 101 ] @pytest.fixture def test_affiliate_list(): """Fixture for periods.""" return [ 'US', 'GB', 'CF' ] @pytest.fixture def test_territory_list(): """Fixture for territories.""" return [ 'T-209', 'T-210' ] @pytest.fixture def test_label_list(): """Fixture for labels.""" return [ 25834, 11600 ] @pytest.fixture def test_all_rows(): return [ { db_consts.PERIOD_ID: 100, db_consts.STORE_ID: 1, db_consts.LABEL_ID: 25834, db_consts.GROUP_INDICATOR: 'A B C' }, { db_consts.PERIOD_ID: 101, db_consts.STORE_ID: 1, db_consts.LABEL_ID: 11600, db_consts.GROUP_INDICATOR: 'A B C' } ] @pytest.fixture def test_file_name_list(): """Fixture generator for file name list.""" def _file_name_list(count): return ['File_{}.txt'.format(num) for num in range(count)] return _file_name_list @pytest.fixture def test_return_rows(): """Fixture generator for snowflake return rows.""" def _return_rows(count): rows = [] for num in range(count): rows.append( { db_consts.BODY_CONTENT: 'Head {}'.format(num), db_consts.RECORD_TYPE: 'H', db_consts.FILE_NAME: 'File_{}.txt'.format(num) }) rows.append( { db_consts.BODY_CONTENT: 'Body {}'.format(num), db_consts.RECORD_TYPE: 'N', db_consts.FILE_NAME: '' }) rows.append( { db_consts.BODY_CONTENT: 'Tail {}'.format(num), db_consts.RECORD_TYPE: 'T', db_consts.FILE_NAME: 'File_{}.txt'.format(num) }) yield rows rows = [] return _return_rows @pytest.fixture def test_process_us_results(): return (5, ['file_1', 'file_2', 'file_3']) @pytest.fixture def test_get_all_rows_result(): def _get_all_rows_result(count): return SimpleNamespace(message=count) return _get_all_rows_result @pytest.fixture def test_get_by_period_id(): return SimpleNamespace(message=['item']) @pytest.fixture def test_get_count_by_group_name(): return SimpleNamespace(message=7) @pytest.fixture def test_get_by_booking_affiliate_store(): return SimpleNamespace(message=['item'])