"""ows-moneyhub connector tests.""" from unittest.mock import MagicMock, patch import httpx from oto import status import pytest from collaborator.constants import service_name from collaborator.models.ows.ows_moneyhub import get_currencies_for_period_range @patch("collaborator.models.ows.ows_moneyhub.ows_client") def test_get_currencies_for_period_range(mock_ows_client): """Test getting currencies successfully.""" account_id = 21234 period_id_from = 246 period_id_to = 248 mock_response = MagicMock() mock_response.raise_for_status.return_value = httpx.Response( status_code=status.OK, json=[ {"statement_period_id": 246, "currency_code": "USD"}, {"statement_period_id": 247, "currency_code": "USD"}, {"statement_period_id": 248, "currency_code": "NZD"}, {"statement_period_id": 249, "currency_code": "GBP"}, ], ) mock_ows_client.get.return_value = mock_response result = get_currencies_for_period_range(account_id, period_id_from, period_id_to) mock_ows_client.get.assert_called_once_with( service_name.OWS_MONEYHUB, f"/account/{account_id}/statement-periods" ) assert sorted(result) == sorted(["NZD", "USD"]) @patch("collaborator.models.ows.ows_moneyhub.ows_client") def test_get_currencies_for_period_range_not_ok(mock_ows_client): """Test getting currencies when response is not OK.""" account_id = 21234 period_id_from = 246 period_id_to = 248 mock_response = MagicMock() mock_response.raise_for_status.side_effect = httpx.HTTPError("Error") mock_ows_client.get.return_value = mock_response with pytest.raises(httpx.HTTPError): get_currencies_for_period_range(account_id, period_id_from, period_id_to) @patch("collaborator.models.ows.ows_moneyhub.ows_client") def test_get_currencies_for_period_range_missing_periods(mock_ows_client): """Test getting currencies when periods are missing.""" account_id = 21234 period_id_from = 246 period_id_to = 248 mock_response = MagicMock() mock_response.raise_for_status.return_value = httpx.Response( status_code=status.OK, json=[ {"statement_period_id": 246, "currency_code": "USD"}, {"statement_period_id": 247, "currency_code": "USD"}, ], ) mock_ows_client.get.return_value = mock_response with pytest.raises(ValueError) as e: get_currencies_for_period_range(account_id, period_id_from, period_id_to) assert str(e.value) == "Not all statement periods were found."