"""Shared fixtures for tests.""" import os from decimal import Decimal import pytest from abacus_worksheet.api import create_app, db from abacus_worksheet.config import Config from abacus_worksheet.constants import constants from tests.utils.factories import WorksheetAdjustmentFactory, WorksheetCorrectionFactory class TestConfig(Config): """Test configuration.""" MYSQL_DB_NAME = os.environ.get('MYSQL_TEST_DB_NAME', Config.MYSQL_DB_NAME + '_test') @pytest.fixture(scope='session', autouse=True) def test_app(): """Create a test application.""" return create_app(TestConfig) @pytest.fixture(autouse=True) def test_app_in_context(test_app): """Push the test app onto the context.""" with test_app.app_context(): yield test_app @pytest.fixture(autouse=True) def test_app_request(test_app_in_context): """Push the test app onto the context and trigger preprocessing.""" with test_app_in_context.test_request_context(): test_app_in_context.preprocess_request() yield test_app_in_context @pytest.fixture def fixture_client(test_app_in_context): """Create a client fixture.""" with test_app_in_context.test_client() as test_client: yield test_client @pytest.fixture def fresh_db(): """Refresh the test database.""" table_names = [ 'abacus_event', 'abacus_state', 'account_payment_term', 'account', 'contract', 'ledger_adjustment_applied', 'ledger_correction', 'reference_adjustment_type', 'statement_period', 'statement_period_adjustment_file', 'worksheet_adjustment', 'worksheet_adjustment_detail', 'worksheet_correction', ] con = db.engine.connect() con.execute('SET FOREIGN_KEY_CHECKS = 0;') trans = con.begin() for table_name in table_names: con.execute(f'TRUNCATE TABLE `{table_name}`') trans.commit() con.execute('SET FOREIGN_KEY_CHECKS = 1;') create_mock_account() mock_account_payment_term() create_mock_contract() create_mock_reference_adjustment_type() create_mock_statement_periods() create_mock_abacus_event() create_mock_statement_period_adjustment_file() def create_mock_account(): """Insert account data.""" SQL_QUERY = """ INSERT INTO account ( `account_id`, `account_name`, `created_by`, `created_at`, `last_modified_by`, `last_modified` ) VALUES ( 1, 'Test 1', 1, '2021-01-01 00:00:00', 1, '2021-01-01 00:00:00' ), ( 2, 'Test 2', 1, '2021-01-01 00:00:00', 1, '2021-01-01 00:00:00' ), ( 3, 'Greenivor Ltd', 1, '2021-01-01 00:00:00', 1, '2021-01-01 00:00:00' ), ( 4, 'Stephen Singer', 1, '2021-01-01 00:00:00', 1, '2021-01-01 00:00:00' ); """ db.engine.execute(SQL_QUERY) def mock_account_payment_term(): """Mock account_payment_term insert.""" SQL_QUERY = """ INSERT INTO account_payment_term( `account_payment_term_id`, `account_id`, `currency_code`, `payment_minimum`, `payment_entity_id`, `payment_schedule`, `created_by`, `created_at`, `last_modified_by`, `last_modified` ) VALUES (1, 1, 'AUD', 42.00, 1, null, 'Test', NOW(), 'Test', NOW()), (2, 2, 'EUR', 43.00, 1, null, 'Test', NOW(), 'Test', NOW()); """ db.engine.execute(SQL_QUERY) def create_mock_contract(): """Mock contract insert.""" SQL_QUERY = """ INSERT INTO contract ( `contract_id`, `reference_signing_entity_id`, `reference_sap_profit_center_id`, `contract_name`, `created_by`, `created_at`, `last_modified_by`, `last_modified` ) VALUES ( 1, 1, 1, 'foo-1-1', '', NOW(), '', NOW() ), ( 2, 1, 1, 'foo-1-2', '', NOW(), '', NOW() ), ( 3, 1, 1, 'AGRR811178 - AWAL Distribution Agreement', '', NOW(), '', NOW() ), ( 4, 1, 1, 'Justin Hayward Young', '', NOW(), '', NOW() ), ( 5, 1, 1, 'AJR Productions', '', NOW(), '', NOW() ); """ db.engine.execute('SET FOREIGN_KEY_CHECKS=0') db.engine.execute(SQL_QUERY) db.engine.execute('SET FOREIGN_KEY_CHECKS=1') def create_mock_account_contract(): """Mock account_contract insert.""" SQL_QUERY = """ INSERT INTO account_contract ( `account_id`, `contract_id` ) VALUES (1, 1), (2, 2), (3, 3), (4, 4), (4, 5); """ db.engine.execute('SET FOREIGN_KEY_CHECKS=0') db.engine.execute(SQL_QUERY) db.engine.execute('SET FOREIGN_KEY_CHECKS=1') def create_mock_statement_periods(): """Mock statement period insert.""" SQL_QUERY = """ INSERT INTO statement_period( statement_period_id, statement_period_name, statement_period_status, statement_month, statement_year ) VALUES(1, 'Jan 23', 'current', 1, 2023), (2, 'Feb 23', 'open', 2, 2023), (3, 'MAr 23', 'open', 3, 2023); """ db.engine.execute('SET FOREIGN_KEY_CHECKS=0') db.engine.execute(SQL_QUERY) db.engine.execute('SET FOREIGN_KEY_CHECKS=1') def create_mock_abacus_event(): """Mock abacus_event insert.""" SQL_QUERY = """ INSERT INTO abacus_event ( `abacus_event_id`, `statement_period_id`, `event_date`, `event_name`, `target_type`, `target_id`, `created_by` ) VALUES ( 1, 1, '2020-01-01', 'commit_royalty_reversal', 'statement_period', 1, 'Test' ) """ db.engine.execute(SQL_QUERY) def create_mock_statement_period_adjustment_file(): """Mock statement_period_adjustment_file insert.""" SQL_QUERY = """ INSERT INTO statement_period_adjustment_file( `statement_period_adjustment_file_id`, `statement_period_id`, `file_name`, `valid_file_location`, `invalid_file_location`, `valid_row_count`, `invalid_row_count`, `total_file_amount_multicurrency`, `total_rounded_amount_multicurrency`, `md5sum`, `error_type`, `created_by`, `created_at`, `last_modified_by`, `last_modified`, `deleted_by`, `deleted_at` ) VALUES (1, 1, 'Test Adjustment file', 's3://qa-abacus-adjustments/text_excel.xlsx', NULL, 5, NULL, '358.90123', '358.91', NULL, NULL, 'Test User', NOW(), 'Test User', NOW(), NULL, NULL), (2, 1, 'Test Adjustment file 1', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'Test User', NOW(), 'Test User', NOW(), NULL, NULL), (3, 1, 'Test Adjustment file 2', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'Test User', NOW(), 'Test User', NOW(), 'Test User', NOW()) """ db.engine.execute('SET FOREIGN_KEY_CHECKS=0') db.engine.execute(SQL_QUERY) db.engine.execute('SET FOREIGN_KEY_CHECKS=1') def create_mock_reference_adjustment_type(): """Mock reference_adjustment_type insert.""" SQL_QUERY = """ INSERT INTO reference_adjustment_type( `reference_adjustment_type_id`, `type_name`, `oa_category_name` ) values (1, 'Label Earnings', ''), (2, 'Publisher Earnings', ''), (65, 'Account Expense', ''); """ db.engine.execute('SET FOREIGN_KEY_CHECKS=0') db.engine.execute(SQL_QUERY) db.engine.execute('SET FOREIGN_KEY_CHECKS=1') @pytest.fixture def create_mock_ledger_correction(): """Mock ledger_correction insert.""" worksheet_correction = WorksheetCorrectionFactory.create( correction_type=constants.CORRECTION_TYPES.ROYALTY_REVERSAL ) SQL_QUERY = """ INSERT INTO ledger_correction ( `ledger_correction_id`, `abacus_event_id`, `worksheet_correction_id`, `account_id`, `contract_id`, `statement_period_id`, `correction_statement_period_id`, `correction_type`, `currency_code`, `gross_revenue`, `distribution_fee`, `net_revenue`, `created_by`, `created_at`, `last_modified_by`, `last_modified` ) VALUES ( 1, 1, {}, 1, 1, 2, 1, 'royalty_reversal', 'USD', '-100.98', '0.00', '-100.98', 'Test account', '2021-01-01 00:00:00', 'Test account', '2021-01-01 00:00:00' ); """ db.engine.execute(SQL_QUERY.format(worksheet_correction.worksheet_correction_id)) @pytest.fixture def mock_worksheet_correction_data(): """Mock worksheet correction data.""" return { 'account_id': 1, 'contract_id': 1, 'correction_statement_period_id': 1, 'currency_code': 'GBP', 'correction_type': 'royalty_reversal', 'gross_revenue': Decimal(9876.5432), 'distribution_fee': Decimal(1975.30864), 'net_revenue': Decimal(7901.23456), 'statement_period_id': 2, } @pytest.fixture def mock_worksheet_adjustment_data(): """Mock worksheet_adjustment data.""" return { 'statement_period_adjustment_file_id': 1, 'abacus_event_id': 1, 'account_id': 1, 'contract_id': 1, 'activity_statement_period_id': 1, 'apply_to_statement_period_id': 2, 'reference_adjustment_type_id': 1, 'adjustment_amount': Decimal(9876.5432), 'adjustment_currency_code': 'USD', 'note': 'informative message', } @pytest.fixture def mock_worksheet_adjustment_detail_data(): """Mock worksheet_adjustment_detail data.""" return { 'statement_period_adjustment_file_id': 1, 'worksheet_adjustment_id': 1, 'account_id': 1, 'contract_id': 1, 'activity_statement_period_id': 1, 'apply_to_statement_period_id': 2, 'reference_adjustment_type_id': 1, 'currency_code': 'USD', 'amount': Decimal(9876.5432), 'upc': '555444333222111', 'distribution_type': 'digital', 'note': 'informative message', 'internal_note': 'internal informative message', } @pytest.fixture def mock_adjustment_row(): """Mock adjustment row data.""" return { 'account_id': '68952', 'contract_id': '533565', 'upc': '196626383143', 'amount': '-50.8979990', 'currency': 'USD', 'activity_year': 2023, 'activity_month': 1, 'statement_year': 2023, 'statement_month': 2, 'adjustment_type': 'Label Earnings', 'client_facing_comments': None, 'distribution_type': 'digital', 'internal_note': None, } @pytest.fixture def mock_worksheet_adjustment_and_detail_data(): """Mock worksheet adjustment and detail data.""" return { 'worksheet_adjustment_id': 1, 'statement_period_adjustment_file_id': 1, 'abacus_event_id': 1, 'account_id': 1, 'contract_id': 1, 'activity_statement_period_id': 1, 'apply_to_statement_period_id': 2, 'reference_adjustment_type_id': 1, 'adjustment_amount': '125.42', 'adjustment_currency_code': 'USD', 'note': 'informative message', 'internal_note': None, 'distribution_type': 'digital', 'upc': '123455555', 'worksheet_adjustment_detail_id': 2, } @pytest.fixture def mock_applied_worksheet_adjustment(): """Mock WorksheetAdjustment insert with ledger_adjustment_applied record.""" worksheet_adjustment = WorksheetAdjustmentFactory.create() SQL_QUERY = f""" INSERT INTO ledger_adjustment_applied ( `ledger_adjustment_applied_id`, `abacus_event_id`, `account_id`, `contract_id`, `statement_period_id`, `ledger_adjustment_id`, `worksheet_adjustment_id`, `adjustment_amount`, `adjustment_currency_code`, `adjustment_amount_payee_currency`, `adjustment_payee_currency_code`, `created_by`, `created_at`, `last_modified_by`, `last_modified` ) VALUES ( 1, 1, 1, 1, 2, NULL, {worksheet_adjustment.worksheet_adjustment_id}, '-100.98', 'USD', '-100.98', 'USD', 'Test account', NOW(), 'Test account', NOW() ); """ db.engine.execute(SQL_QUERY) return worksheet_adjustment @pytest.fixture def insert_apply_file_state(): """Insert an abacus_state apply_file row for a file id and return that id.""" def _insert(file_id=987, status='complete'): db.engine.execute( """ INSERT INTO abacus_state (parent_table_id, parent_table_name, action_name, action_status, created_by, created_at, last_modified_by, last_modified) VALUES (%(file_id)s, 'statement_period_adjustment_file', 'apply_file', %(status)s, 'test', NOW(), 'test', NOW()) """, {'file_id': file_id, 'status': status}, ) return file_id return _insert