"""Functional tests for ledger contract advance endpoints.""" from contextlib import contextmanager from moneyhub.connectors.snowflake import db from moneyhub.models import CombinedAdvances from tests.functional.conftest import insert_mock_data @contextmanager def _using_mock_data(): """Create the necessary mock tables and fills them with the data.""" CombinedAdvances.__table__.drop(db.engine, checkfirst=True) db.session.commit() CombinedAdvances.__table__.create(db.engine) insert_mock_data({ 'combined_advances_dbt': { 'account_id': 24601, 'advance_amount': 1000.00, 'advance_amount_payee_currency': 800.00, 'advance_currency_code': 'USD', 'advance_description': 'Test Advance', 'unique_key': '1', 'advance_payee_currency_code': 'GBP', 'contract_id': 1001, 'statement_period_id': 123, }, }, database='snowflake') yield db.session.commit() CombinedAdvances.__table__.drop(db.engine) db.session.close() def test_get_by_account_and_statement_period(fixture_client): """Test getting applied advances.""" account_id = 24601 statement_period_id = 123 with _using_mock_data(): result = fixture_client.get( f'/applied-advances/account/{account_id}/statement-period/{statement_period_id}') assert result.status_code == 200 assert result.json() == [ { 'account_id': 24601, 'advance_amount': 1000.00, 'advance_amount_payee_currency': 800.00, 'advance_currency_code': 'USD', 'advance_description': 'Test Advance', 'advance_id': '1', 'advance_payee_currency_code': 'GBP', 'contract_id': 1001, 'statement_period_id': 123, }, ] def test_get_by_account_and_statement_period_no_results(fixture_client): """Test getting applied advances with no results.""" account_id = 24601 statement_period_id = 999 with _using_mock_data(): result = fixture_client.get( f'/applied-advances/account/{account_id}/statement-period/{statement_period_id}') assert result.status_code == 200 assert result.json() == []