"""Test for TransferWise Transaction Persister.""" from decimal import Decimal from unittest.mock import ANY import pytest from collaborator.constants.statement_period import StatementPeriodStatus from collaborator.models.rds.statement_period_persister import StatementPeriodPersister from tests.testutils import db @db.test_schema_default_seed def test_get_statement_period_by_id(): """Test getting statement period by ID.""" period = StatementPeriodPersister.get_statement_period_by_id(1) assert period.statement_period_id == 1 period = StatementPeriodPersister.get_statement_period_by_id(42) assert period is None @db.test_schema_default_seed @pytest.mark.parametrize( "account_id, expected_result", [ ( 24601, { "id": 2, "name": "SP 2", "vendor_id": 24601, "created_date": "2022-04-06T06:07:08", "updated_date": "2022-04-06T06:07:08", "status": StatementPeriodStatus.OPEN, "abacus_statement_period_id": None, }, ), (5678, None), ], ) def test_get_open_statement_period(account_id, expected_result): """Test getting the open statement period.""" result = StatementPeriodPersister.get_open_statement_period(account_id) assert expected_result == result if result is None else result.to_dict() @db.test_schema_default_seed @pytest.mark.parametrize( "account_id, expected_result", [ ( # Get existing 24601, { "id": 2, "name": "SP 2", "vendor_id": 24601, "created_date": "2022-04-06T06:07:08", "updated_date": "2022-04-06T06:07:08", "status": StatementPeriodStatus.OPEN, "abacus_statement_period_id": None, }, ), ( # Create new 5678, { "id": 90211, "name": None, "vendor_id": 5678, "created_date": ANY, "updated_date": ANY, "status": StatementPeriodStatus.OPEN, "abacus_statement_period_id": 123, }, ), ], ) def test_get_or_create_open_statement_period( account_id, expected_result, mocker, ): """Test create vendor agreement.""" mocker.patch( "collaborator.models.rds.statement_period_persister.ows_royalties." "get_current_abacus_statement_period", return_value={"statement_period_id": 123}, ) result = StatementPeriodPersister.get_or_create_open_statement_period(account_id) assert result.to_dict() == expected_result @db.test_schema_default_seed def test_close_statement_period(mock_account, mocker): """Test closing statement periods.""" mocker.patch( "collaborator.models.rds.statement_period_persister.ows_royalties." "get_current_abacus_statement_period", return_value={"statement_period_id": 123}, ) _, period = StatementPeriodPersister.close_statement_period( int(mock_account.id), "PeriodName" ) assert period == { "id": 90211, "name": None, "vendor_id": int(mock_account.id), "created_date": ANY, "updated_date": ANY, "status": "OPEN", "abacus_statement_period_id": 123, } @db.test_schema_default_seed def test_bulk_close_statement_periods(): """Test bulk closing statement periods.""" new_period_name = "May 2025" dp_enabled_vendor_ids = [24601] new_abacus_statement_period_id = 123 get_statement_periods_common_args = { "statuses": [], "limit": None, "offset": None, "sort_key": None, "sort_direction": None, "term": None, } previous_statement_periods_by_vendor = { vendor_id: StatementPeriodPersister.get_statement_periods( vendor_id, **get_statement_periods_common_args )[0] for vendor_id in dp_enabled_vendor_ids } StatementPeriodPersister.bulk_close_statement_periods( new_period_name, dp_enabled_vendor_ids, new_abacus_statement_period_id ) for vendor_id in dp_enabled_vendor_ids: previous_statement_periods = previous_statement_periods_by_vendor[vendor_id] previous_statement_periods_ids = { period["id"] for period in previous_statement_periods } previous_open_statement_period_id = next( period["id"] for period in previous_statement_periods if period["status"] == StatementPeriodStatus.OPEN ) statement_periods, _ = StatementPeriodPersister.get_statement_periods( vendor_id, **get_statement_periods_common_args ) # One new statement period has been created assert len(statement_periods) == len(previous_statement_periods) + 1 new_statement_period = next( period for period in statement_periods if period["id"] not in previous_statement_periods_ids ) # New statement period is open assert new_statement_period["status"] == StatementPeriodStatus.OPEN assert ( new_statement_period["abacus_statement_period_id"] == new_abacus_statement_period_id ) previously_open_statement_period = next( period for period in statement_periods if period["id"] == previous_open_statement_period_id ) # Previously open statement period is now closed and has been named assert ( previously_open_statement_period["status"] == StatementPeriodStatus.CLOSED ) assert previously_open_statement_period["name"] == new_period_name @db.test_schema_default_seed def test_get_statement_period_participations(): """Test getting statement period participations.""" participations_response = ( StatementPeriodPersister.get_statement_period_participations( 10, 4, None, None, None, True, None, ) ) response_rows = participations_response[0] response_dict = ( dict(response_rows[0]._mapping) if response_rows and response_rows[0] else {} ) assert response_dict == { "statement_period_id": 4, "vendor_id": 120594, "collaborator_id": 10, "revenues_total": Decimal("500.000000"), "expenses_total": Decimal("-160.000000"), "payments_total": Decimal("-190.000000"), "opening_balance": Decimal("0.000000"), "closing_balance": Decimal("150.000000"), "currency": "USD", "currencies_count": 1, }