"""Test cases for worksheet_payable_balance_after_Tax repository.""" from datetime import datetime from payment.models import WorksheetPayableBalanceAfterTax from payment.repository import worksheet_payable_balance_after_tax as repository from tests.utils.factories import ( WorksheetAccountContractClosingBalanceFactory, WorksheetPayableBalanceAfterTaxFactory, ) def test_get_filtered_active_records( mock_statement_periods, mock_accounts, mock_contracts, mock_abacus_event, mock_ledger_account_contracts, ): """Test get_filtered_active_records method.""" statement_period_id = 1 event_id = 1 worksheet_closing_balance = WorksheetAccountContractClosingBalanceFactory.create( statement_period_id=statement_period_id, abacus_event_id=event_id, ) worksheet_after_tax = WorksheetPayableBalanceAfterTaxFactory.create( # noqa worksheet_account_contract_closing_balance_id=worksheet_closing_balance.worksheet_account_contract_closing_balance_id, statement_period_id=statement_period_id, abacus_event_id=event_id, ) # soft deleted WorksheetPayableBalanceAfterTaxFactory.create( # noqa worksheet_account_contract_closing_balance_id=worksheet_closing_balance.worksheet_account_contract_closing_balance_id, statement_period_id=statement_period_id, abacus_event_id=event_id, deleted_at=datetime.now(), ) # other statement period WorksheetPayableBalanceAfterTaxFactory.create( # noqa worksheet_account_contract_closing_balance_id=worksheet_closing_balance.worksheet_account_contract_closing_balance_id, statement_period_id=statement_period_id + 1, abacus_event_id=event_id + 1, ) worksheet_contract_search = WorksheetPayableBalanceAfterTaxFactory.create( # noqa contract_id=2, worksheet_account_contract_closing_balance_id=worksheet_closing_balance.worksheet_account_contract_closing_balance_id, statement_period_id=statement_period_id + 1, abacus_event_id=event_id + 1, ) items, total_count = repository.get_filtered_active_records( # noqa 0, statement_period_id, None, 100, 0 ) assert total_count == 1 assert items == [worksheet_after_tax] items, total_count = repository.get_filtered_active_records( # noqa event_id, 0, None, 100, 0 ) assert total_count == 1 assert items == [worksheet_after_tax] items, total_count = repository.get_filtered_active_records( # noqa event_id, statement_period_id, None, 100, 0 ) assert total_count == 1 assert items == [worksheet_after_tax] # search by contract_ids items, total_count = repository.get_filtered_active_records( # noqa event_id + 1, statement_period_id + 1, [2], 100, 0 ) assert total_count == 1 assert items == [worksheet_contract_search] items, total_count = repository.get_filtered_active_records( # noqa 0, 0, [2], 100, 0 ) assert total_count == 1 assert items == [worksheet_contract_search] worksheet_closing_balance2 = WorksheetAccountContractClosingBalanceFactory.create( statement_period_id=statement_period_id, abacus_event_id=event_id, account_id=2, contract_id=2, ) worksheet_after_tax2 = WorksheetPayableBalanceAfterTaxFactory.create( worksheet_account_contract_closing_balance_id=worksheet_closing_balance2.worksheet_account_contract_closing_balance_id, statement_period_id=statement_period_id, abacus_event_id=event_id, account_id=2, contract_id=2, ) items, total_count = repository.get_filtered_active_records( # noqa event_id, 0, None, 100, 0, sort_by='account_name' ) assert total_count == 2 assert items == [worksheet_after_tax, worksheet_after_tax2] items, total_count = repository.get_filtered_active_records( # noqa event_id, 0, None, 100, 0, sort_by='contract_name' ) assert total_count == 2 assert items == [worksheet_after_tax, worksheet_after_tax2] items, total_count = repository.get_filtered_active_records( # noqa event_id, 0, None, 100, 0, sort_by='account_name', sort_order='desc' ) assert total_count == 2 assert items == [worksheet_after_tax2, worksheet_after_tax] items, total_count = repository.get_filtered_active_records( # noqa event_id, 0, None, 100, 0, sort_by='contract_name', sort_order='desc' ) assert total_count == 2 assert items == [worksheet_after_tax2, worksheet_after_tax] items, total_count = repository.get_filtered_active_records( # noqa event_id, 0, None, 100, 0, search_term='Account 2' ) assert total_count == 1 assert items == [worksheet_after_tax2] for field in WorksheetPayableBalanceAfterTax.__table__.columns.keys(): repository.get_filtered_active_records( # noqa event_id, 0, None, 100, 0, sort_by=field, sort_order='desc' ) repository.get_filtered_active_records( # noqa event_id, 0, None, 100, 0, sort_by=field ) items, total_count = repository.get_filtered_active_records( # noqa event_id, 0, None, 100, 0, sort_by='account_id', sort_order='desc' ) assert items == [worksheet_after_tax2, worksheet_after_tax] items, total_count = repository.get_filtered_active_records( # noqa event_id, 0, None, 100, 0, sort_by='account_id' ) assert items == [worksheet_after_tax, worksheet_after_tax2] items, total_count = repository.get_filtered_active_records( # noqa event_id, 0, None, 100, 0, search_term='Account', sort_by='contract_id', sort_order='desc', ) assert total_count == 2 assert items == [worksheet_after_tax2, worksheet_after_tax]