-
Notifications
You must be signed in to change notification settings - Fork 0
Add test coverage for get_mqtt_client_for_installation method #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,14 +1,17 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Tests for the Victron Energy VRM API client.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import logging | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import unittest.mock | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import aiohttp | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import pytest | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import random | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from victron_vrm import VictronVRMClient | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from victron_vrm.exceptions import VictronVRMError, AuthorizationError | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from victron_vrm.exceptions import VictronVRMError, AuthorizationError, NotFoundError, ClientError | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from victron_vrm.models import Site | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from victron_vrm.models.auth import AuthToken | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| from victron_vrm.mqtt import VRMMQTTClient | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Set up logging | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logging.basicConfig( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -21,6 +24,34 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| AUTH_DEMO_URL = "https://vrmapi.victronenergy.com/v2/auth/loginAsDemo" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Helper classes for mocking | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class _MockUser: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Mock user for testing.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| email = "test@example.com" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class _MockSite: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Mock site for testing.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| identifier = "test-vrm-id" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mqtt_hostname = "mqtt.victronenergy.com" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class _MockSiteNoHostname: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Mock site without MQTT hostname for testing.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| identifier = "test-vrm-id" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mqtt_hostname = None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def _create_mock_token(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Create a mock auth token for testing.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return AuthToken( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| access_token="mock_access_token", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| token_type="Bearer", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| expires_in=3600, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| scope="read" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @pytest.fixture(scope="session") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async def demo_token(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Get a demo token for testing (session-scoped to avoid 429s).""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -373,3 +404,72 @@ async def test_stats_false_to_none_transformation(vrm_client): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except VictronVRMError as e: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| logger.warning(f"Error in mock test: {e}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| pytest.skip(f"Error in mock test: {e}") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @pytest.mark.asyncio | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async def test_get_mqtt_client_for_installation_success(): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """Test getting MQTT client for a valid installation.""" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Create a mock client without needing actual network access | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async with VictronVRMClient(token="mock_token", token_type="Bearer") as client: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async def mock_gather(*args): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return _create_mock_token(), _MockUser(), [_MockSite()] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Mock asyncio.gather to avoid actual API calls | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| with unittest.mock.patch("asyncio.gather", side_effect=mock_gather): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Get MQTT client | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| mqtt_client = await client.get_mqtt_client_for_installation(67890) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Verify the client is properly configured | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert mqtt_client is not None | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert isinstance(mqtt_client, VRMMQTTClient) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Verify client attributes | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert mqtt_client.host == "mqtt.victronenergy.com" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert mqtt_client.username == "test@example.com" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert mqtt_client.password == "Bearer mock_access_token" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert mqtt_client.installation_id == "test-vrm-id" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert mqtt_client.installation_id == "test-vrm-id" | |
| installation_id = getattr(mqtt_client, "installation_id", None) | |
| if installation_id is not None: | |
| assert installation_id == "test-vrm-id" |
Copilot
AI
Jan 17, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mocking asyncio.gather globally is too broad and risky. This patches all asyncio.gather calls, which could affect other async operations in the client or test infrastructure. Instead, mock the specific methods called by get_mqtt_client_for_installation: client._get_auth_token(), client.users.get_me(), and client.users.list_sites(). This approach is more targeted, safer, and follows the pattern used in the existing test test_stats_false_to_none_transformation which mocks specific methods.
| async def mock_gather(*args): | |
| return _create_mock_token(), _MockUser(), [_MockSite()] | |
| # Mock asyncio.gather to avoid actual API calls | |
| with unittest.mock.patch("asyncio.gather", side_effect=mock_gather): | |
| # Get MQTT client | |
| mqtt_client = await client.get_mqtt_client_for_installation(67890) | |
| # Verify the client is properly configured | |
| assert mqtt_client is not None | |
| assert isinstance(mqtt_client, VRMMQTTClient) | |
| # Verify client attributes | |
| assert mqtt_client.host == "mqtt.victronenergy.com" | |
| assert mqtt_client.username == "test@example.com" | |
| assert mqtt_client.password == "Bearer mock_access_token" | |
| assert mqtt_client.installation_id == "test-vrm-id" | |
| logger.info("Successfully created MQTT client with mocked data") | |
| @pytest.mark.asyncio | |
| async def test_get_mqtt_client_for_installation_not_found(): | |
| """Test getting MQTT client for a non-existent installation.""" | |
| # Create a mock client | |
| async with VictronVRMClient(token="mock_token", token_type="Bearer") as client: | |
| async def mock_gather(*args): | |
| # Return empty list for installations | |
| return _create_mock_token(), _MockUser(), [] | |
| # Mock asyncio.gather to return empty installations list | |
| with unittest.mock.patch("asyncio.gather", side_effect=mock_gather): | |
| # Should raise NotFoundError | |
| with pytest.raises(NotFoundError) as exc_info: | |
| await client.get_mqtt_client_for_installation(999999999) | |
| # Verify the error message | |
| assert "not found" in str(exc_info.value).lower() | |
| assert "999999999" in str(exc_info.value) | |
| logger.info("NotFoundError correctly raised for invalid installation ID") | |
| @pytest.mark.asyncio | |
| async def test_get_mqtt_client_for_installation_missing_hostname(): | |
| """Test getting MQTT client for installation without MQTT hostname.""" | |
| # Create a mock client | |
| async with VictronVRMClient(token="mock_token", token_type="Bearer") as client: | |
| async def mock_gather(*args): | |
| return _create_mock_token(), _MockUser(), [_MockSiteNoHostname()] | |
| # Mock asyncio.gather | |
| with unittest.mock.patch("asyncio.gather", side_effect=mock_gather): | |
| # Should raise ClientError | |
| with pytest.raises(ClientError) as exc_info: | |
| await client.get_mqtt_client_for_installation(67890) | |
| # Verify the error message | |
| assert "mqtt hostname" in str(exc_info.value).lower() | |
| assert "67890" in str(exc_info.value) | |
| # Mock the specific async methods used by get_mqtt_client_for_installation | |
| mock_get_auth_token = unittest.mock.AsyncMock(return_value=_create_mock_token()) | |
| mock_get_me = unittest.mock.AsyncMock(return_value=_MockUser()) | |
| mock_list_sites = unittest.mock.AsyncMock(return_value=[_MockSite()]) | |
| with unittest.mock.patch.object(client, "_get_auth_token", mock_get_auth_token), \ | |
| unittest.mock.patch.object(client.users, "get_me", mock_get_me), \ | |
| unittest.mock.patch.object(client.users, "list_sites", mock_list_sites): | |
| # Get MQTT client | |
| mqtt_client = await client.get_mqtt_client_for_installation(67890) | |
| # Verify the client is properly configured | |
| assert mqtt_client is not None | |
| assert isinstance(mqtt_client, VRMMQTTClient) | |
| # Verify client attributes | |
| assert mqtt_client.host == "mqtt.victronenergy.com" | |
| assert mqtt_client.username == "test@example.com" | |
| assert mqtt_client.password == "Bearer mock_access_token" | |
| assert mqtt_client.installation_id == "test-vrm-id" | |
| logger.info("Successfully created MQTT client with mocked data") | |
| @pytest.mark.asyncio | |
| async def test_get_mqtt_client_for_installation_not_found(): | |
| """Test getting MQTT client for a non-existent installation.""" | |
| # Create a mock client | |
| async with VictronVRMClient(token="mock_token", token_type="Bearer") as client: | |
| # Mock the specific async methods used by get_mqtt_client_for_installation | |
| mock_get_auth_token = unittest.mock.AsyncMock(return_value=_create_mock_token()) | |
| mock_get_me = unittest.mock.AsyncMock(return_value=_MockUser()) | |
| # Return empty list for installations | |
| mock_list_sites = unittest.mock.AsyncMock(return_value=[]) | |
| with unittest.mock.patch.object(client, "_get_auth_token", mock_get_auth_token), \ | |
| unittest.mock.patch.object(client.users, "get_me", mock_get_me), \ | |
| unittest.mock.patch.object(client.users, "list_sites", mock_list_sites): | |
| # Should raise NotFoundError | |
| with pytest.raises(NotFoundError) as exc_info: | |
| await client.get_mqtt_client_for_installation(999999999) | |
| # Verify the error message | |
| assert "not found" in str(exc_info.value).lower() | |
| assert "999999999" in str(exc_info.value) | |
| logger.info("NotFoundError correctly raised for invalid installation ID") | |
| @pytest.mark.asyncio | |
| async def test_get_mqtt_client_for_installation_missing_hostname(): | |
| """Test getting MQTT client for installation without MQTT hostname.""" | |
| # Create a mock client | |
| async with VictronVRMClient(token="mock_token", token_type="Bearer") as client: | |
| # Mock the specific async methods used by get_mqtt_client_for_installation | |
| mock_get_auth_token = unittest.mock.AsyncMock(return_value=_create_mock_token()) | |
| mock_get_me = unittest.mock.AsyncMock(return_value=_MockUser()) | |
| mock_list_sites = unittest.mock.AsyncMock(return_value=[_MockSiteNoHostname()]) | |
| with unittest.mock.patch.object(client, "_get_auth_token", mock_get_auth_token), \ | |
| unittest.mock.patch.object(client.users, "get_me", mock_get_me), \ | |
| unittest.mock.patch.object(client.users, "list_sites", mock_list_sites): | |
| # Should raise ClientError | |
| with pytest.raises(ClientError) as exc_info: | |
| await client.get_mqtt_client_for_installation(67890) | |
| # Verify the error message | |
| assert "mqtt hostname" in str(exc_info.value).lower() | |
| assert "67890" in str(exc_info.value) |
Copilot
AI
Jan 17, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mocking asyncio.gather globally is too broad and risky. This patches all asyncio.gather calls, which could affect other async operations in the client or test infrastructure. Instead, mock the specific methods called by get_mqtt_client_for_installation: client._get_auth_token(), client.users.get_me(), and client.users.list_sites(). This approach is more targeted, safer, and follows the pattern used in the existing test test_stats_false_to_none_transformation which mocks specific methods.
| async def mock_gather(*args): | |
| return _create_mock_token(), _MockUser(), [_MockSite()] | |
| # Mock asyncio.gather to avoid actual API calls | |
| with unittest.mock.patch("asyncio.gather", side_effect=mock_gather): | |
| # Get MQTT client | |
| mqtt_client = await client.get_mqtt_client_for_installation(67890) | |
| # Verify the client is properly configured | |
| assert mqtt_client is not None | |
| assert isinstance(mqtt_client, VRMMQTTClient) | |
| # Verify client attributes | |
| assert mqtt_client.host == "mqtt.victronenergy.com" | |
| assert mqtt_client.username == "test@example.com" | |
| assert mqtt_client.password == "Bearer mock_access_token" | |
| assert mqtt_client.installation_id == "test-vrm-id" | |
| logger.info("Successfully created MQTT client with mocked data") | |
| @pytest.mark.asyncio | |
| async def test_get_mqtt_client_for_installation_not_found(): | |
| """Test getting MQTT client for a non-existent installation.""" | |
| # Create a mock client | |
| async with VictronVRMClient(token="mock_token", token_type="Bearer") as client: | |
| async def mock_gather(*args): | |
| # Return empty list for installations | |
| return _create_mock_token(), _MockUser(), [] | |
| # Mock asyncio.gather to return empty installations list | |
| with unittest.mock.patch("asyncio.gather", side_effect=mock_gather): | |
| # Should raise NotFoundError | |
| with pytest.raises(NotFoundError) as exc_info: | |
| await client.get_mqtt_client_for_installation(999999999) | |
| # Verify the error message | |
| assert "not found" in str(exc_info.value).lower() | |
| assert "999999999" in str(exc_info.value) | |
| logger.info("NotFoundError correctly raised for invalid installation ID") | |
| @pytest.mark.asyncio | |
| async def test_get_mqtt_client_for_installation_missing_hostname(): | |
| """Test getting MQTT client for installation without MQTT hostname.""" | |
| # Create a mock client | |
| async with VictronVRMClient(token="mock_token", token_type="Bearer") as client: | |
| async def mock_gather(*args): | |
| return _create_mock_token(), _MockUser(), [_MockSiteNoHostname()] | |
| # Mock asyncio.gather | |
| with unittest.mock.patch("asyncio.gather", side_effect=mock_gather): | |
| # Should raise ClientError | |
| with pytest.raises(ClientError) as exc_info: | |
| await client.get_mqtt_client_for_installation(67890) | |
| # Verify the error message | |
| assert "mqtt hostname" in str(exc_info.value).lower() | |
| assert "67890" in str(exc_info.value) | |
| logger.info("ClientError correctly raised for installation without MQTT hostname") | |
| # Mock the specific async methods used by get_mqtt_client_for_installation | |
| client._get_auth_token = unittest.mock.AsyncMock(return_value=_create_mock_token()) | |
| client.users.get_me = unittest.mock.AsyncMock(return_value=_MockUser()) | |
| client.users.list_sites = unittest.mock.AsyncMock(return_value=[_MockSite()]) | |
| # Get MQTT client | |
| mqtt_client = await client.get_mqtt_client_for_installation(67890) | |
| # Verify the client is properly configured | |
| assert mqtt_client is not None | |
| assert isinstance(mqtt_client, VRMMQTTClient) | |
| # Verify client attributes | |
| assert mqtt_client.host == "mqtt.victronenergy.com" | |
| assert mqtt_client.username == "test@example.com" | |
| assert mqtt_client.password == "Bearer mock_access_token" | |
| assert mqtt_client.installation_id == "test-vrm-id" | |
| logger.info("Successfully created MQTT client with mocked data") | |
| @pytest.mark.asyncio | |
| async def test_get_mqtt_client_for_installation_not_found(): | |
| """Test getting MQTT client for a non-existent installation.""" | |
| # Create a mock client | |
| async with VictronVRMClient(token="mock_token", token_type="Bearer") as client: | |
| # Mock the specific async methods used by get_mqtt_client_for_installation | |
| client._get_auth_token = unittest.mock.AsyncMock(return_value=_create_mock_token()) | |
| client.users.get_me = unittest.mock.AsyncMock(return_value=_MockUser()) | |
| # Return empty list for installations to simulate "not found" | |
| client.users.list_sites = unittest.mock.AsyncMock(return_value=[]) | |
| # Should raise NotFoundError | |
| with pytest.raises(NotFoundError) as exc_info: | |
| await client.get_mqtt_client_for_installation(999999999) | |
| # Verify the error message | |
| assert "not found" in str(exc_info.value).lower() | |
| assert "999999999" in str(exc_info.value) | |
| logger.info("NotFoundError correctly raised for invalid installation ID") | |
| @pytest.mark.asyncio | |
| async def test_get_mqtt_client_for_installation_missing_hostname(): | |
| """Test getting MQTT client for installation without MQTT hostname.""" | |
| # Create a mock client | |
| async with VictronVRMClient(token="mock_token", token_type="Bearer") as client: | |
| # Mock the specific async methods used by get_mqtt_client_for_installation | |
| client._get_auth_token = unittest.mock.AsyncMock(return_value=_create_mock_token()) | |
| client.users.get_me = unittest.mock.AsyncMock(return_value=_MockUser()) | |
| client.users.list_sites = unittest.mock.AsyncMock(return_value=[_MockSiteNoHostname()]) | |
| # Should raise ClientError | |
| with pytest.raises(ClientError) as exc_info: | |
| await client.get_mqtt_client_for_installation(67890) | |
| # Verify the error message | |
| assert "mqtt hostname" in str(exc_info.value).lower() | |
| assert "67890" in str(exc_info.value) | |
| logger.info("ClientError correctly raised for installation without MQTT hostname") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The _MockUser class is missing required attributes from the actual User model. According to the User model in victron_vrm/models/user.py, it requires
id,name,email, andcountryfields. Add these missing fields to make the mock match the expected structure.