mirror of
https://github.com/jpawlowski/hass.tibber_prices.git
synced 2026-03-29 21:03:40 +00:00
fix(tests): remove unused mock_config_entry and update price_avg to base currency in percentage calculations
This commit is contained in:
parent
87f0022baa
commit
d00935e697
3 changed files with 43 additions and 38 deletions
|
|
@ -104,7 +104,6 @@ def test_midnight_crossing_period_consistency(period_config: TibberPricesPeriodC
|
||||||
all_prices_before,
|
all_prices_before,
|
||||||
config=period_config,
|
config=period_config,
|
||||||
time=time_service_before,
|
time=time_service_before,
|
||||||
config_entry=mock_config_entry,
|
|
||||||
)
|
)
|
||||||
periods_before = result_before["periods"]
|
periods_before = result_before["periods"]
|
||||||
|
|
||||||
|
|
@ -131,7 +130,6 @@ def test_midnight_crossing_period_consistency(period_config: TibberPricesPeriodC
|
||||||
all_prices_after,
|
all_prices_after,
|
||||||
config=period_config,
|
config=period_config,
|
||||||
time=time_service_after,
|
time=time_service_after,
|
||||||
config_entry=mock_config_entry,
|
|
||||||
)
|
)
|
||||||
periods_after = result_after["periods"]
|
periods_after = result_after["periods"]
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
"""Test Bug #9, #10, #11: Percentage calculations with negative prices use abs() correctly."""
|
"""Test Bug #9, #10, #11: Percentage calculations with negative prices use abs() correctly."""
|
||||||
|
|
||||||
from datetime import UTC, datetime
|
from datetime import UTC, datetime
|
||||||
from unittest.mock import Mock
|
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
|
@ -39,19 +38,14 @@ def test_bug9_period_price_diff_negative_reference(price_context_negative: dict)
|
||||||
Now: Uses abs(ref_price) → correct percentage direction
|
Now: Uses abs(ref_price) → correct percentage direction
|
||||||
"""
|
"""
|
||||||
start_time = datetime(2025, 11, 22, 12, 0, tzinfo=UTC)
|
start_time = datetime(2025, 11, 22, 12, 0, tzinfo=UTC)
|
||||||
price_avg = -10.0 # Period average in minor units (ct)
|
price_avg = -0.10 # Period average in base currency (EUR) = -10 ct
|
||||||
|
|
||||||
mock_config_entry = Mock()
|
period_diff, period_diff_pct = calculate_period_price_diff(price_avg, start_time, price_context_negative)
|
||||||
mock_config_entry.options.get.return_value = "minor" # Default display mode
|
|
||||||
|
|
||||||
period_diff, period_diff_pct = calculate_period_price_diff(
|
# Reference price: -0.20 EUR (-20 ct)
|
||||||
price_avg, start_time, price_context_negative, mock_config_entry
|
# Difference: -0.10 - (-0.20) = 0.10 EUR (period is 10 ct MORE EXPENSIVE than reference)
|
||||||
)
|
# Percentage: 0.10 / abs(-0.20) * 100 = +50% (correctly shows increase)
|
||||||
|
assert period_diff == 0.10, "Difference should be +0.10 EUR (+10 ct)"
|
||||||
# Reference price: -20 ct
|
|
||||||
# Difference: -10 - (-20) = 10 ct (period is 10 ct MORE EXPENSIVE than reference)
|
|
||||||
# Percentage: 10 / abs(-20) * 100 = +50% (correctly shows increase)
|
|
||||||
assert period_diff == 10.0, "Difference should be +10 ct"
|
|
||||||
assert period_diff_pct == 50.0, "Percentage should be +50% (more expensive than ref)"
|
assert period_diff_pct == 50.0, "Percentage should be +50% (more expensive than ref)"
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -63,19 +57,14 @@ def test_bug9_period_price_diff_more_negative_than_reference(price_context_negat
|
||||||
the percentage correctly shows negative (cheaper).
|
the percentage correctly shows negative (cheaper).
|
||||||
"""
|
"""
|
||||||
start_time = datetime(2025, 11, 22, 12, 0, tzinfo=UTC)
|
start_time = datetime(2025, 11, 22, 12, 0, tzinfo=UTC)
|
||||||
price_avg = -25.0 # More negative (cheaper) than reference -20 ct
|
price_avg = -0.25 # More negative (cheaper) than reference: -0.25 EUR = -25 ct
|
||||||
|
|
||||||
mock_config_entry = Mock()
|
period_diff, period_diff_pct = calculate_period_price_diff(price_avg, start_time, price_context_negative)
|
||||||
mock_config_entry.options.get.return_value = "minor" # Default display mode
|
|
||||||
|
|
||||||
period_diff, period_diff_pct = calculate_period_price_diff(
|
# Reference: -0.20 EUR (-20 ct)
|
||||||
price_avg, start_time, price_context_negative, mock_config_entry
|
# Difference: -0.25 - (-0.20) = -0.05 EUR (period is 5 ct CHEAPER)
|
||||||
)
|
# Percentage: -0.05 / abs(-0.20) * 100 = -25% (correctly shows decrease)
|
||||||
|
assert period_diff == -0.05, "Difference should be -0.05 EUR (-5 ct)"
|
||||||
# Reference: -20 ct
|
|
||||||
# Difference: -25 - (-20) = -5 ct (period is 5 ct CHEAPER)
|
|
||||||
# Percentage: -5 / abs(-20) * 100 = -25% (correctly shows decrease)
|
|
||||||
assert period_diff == -5.0, "Difference should be -5 ct"
|
|
||||||
assert period_diff_pct == -25.0, "Percentage should be -25% (cheaper than ref)"
|
assert period_diff_pct == -25.0, "Percentage should be -25% (cheaper than ref)"
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -86,19 +75,14 @@ def test_bug9_period_price_diff_positive_reference(price_context_positive: dict)
|
||||||
Verifies that abs() doesn't break normal positive price calculations.
|
Verifies that abs() doesn't break normal positive price calculations.
|
||||||
"""
|
"""
|
||||||
start_time = datetime(2025, 11, 22, 12, 0, tzinfo=UTC)
|
start_time = datetime(2025, 11, 22, 12, 0, tzinfo=UTC)
|
||||||
price_avg = 30.0 # ct
|
price_avg = 0.30 # Period average in base currency (EUR) = 30 ct
|
||||||
|
|
||||||
mock_config_entry = Mock()
|
period_diff, period_diff_pct = calculate_period_price_diff(price_avg, start_time, price_context_positive)
|
||||||
mock_config_entry.options.get.return_value = "minor" # Default display mode
|
|
||||||
|
|
||||||
period_diff, period_diff_pct = calculate_period_price_diff(
|
# Reference: 0.20 EUR (20 ct)
|
||||||
price_avg, start_time, price_context_positive, mock_config_entry
|
# Difference: 0.30 - 0.20 = 0.10 EUR (10 ct)
|
||||||
)
|
# Percentage: 0.10 / 0.20 * 100 = +50%
|
||||||
|
assert period_diff == 0.10, "Difference should be +0.10 EUR (+10 ct)"
|
||||||
# Reference: 20 ct
|
|
||||||
# Difference: 30 - 20 = 10 ct
|
|
||||||
# Percentage: 10 / 20 * 100 = +50%
|
|
||||||
assert period_diff == 10.0, "Difference should be +10 ct"
|
|
||||||
assert period_diff_pct == 50.0, "Percentage should be +50%"
|
assert period_diff_pct == 50.0, "Percentage should be +50%"
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,9 @@ The fix implements data validation that:
|
||||||
- Raises exception if currency cannot be determined (no silent EUR fallback)
|
- Raises exception if currency cannot be determined (no silent EUR fallback)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from datetime import timedelta
|
from datetime import datetime, timedelta
|
||||||
|
from unittest.mock import Mock
|
||||||
|
from zoneinfo import ZoneInfo
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
|
@ -29,6 +31,27 @@ from custom_components.tibber_prices.coordinator.data_fetching import (
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_api_client() -> Mock:
|
||||||
|
"""Create a mock API client."""
|
||||||
|
return Mock()
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_time_service() -> Mock:
|
||||||
|
"""Create a mock time service."""
|
||||||
|
time_service = Mock()
|
||||||
|
time_service.now.return_value = datetime(2025, 11, 22, 12, 0, 0, tzinfo=ZoneInfo("Europe/Berlin"))
|
||||||
|
time_service.as_local.side_effect = lambda dt: dt
|
||||||
|
return time_service
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_store() -> Mock:
|
||||||
|
"""Create a mock store."""
|
||||||
|
return Mock()
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.unit
|
@pytest.mark.unit
|
||||||
def test_validate_user_data_complete(mock_api_client, mock_time_service, mock_store) -> None: # noqa: ANN001
|
def test_validate_user_data_complete(mock_api_client, mock_time_service, mock_store) -> None: # noqa: ANN001
|
||||||
"""Test that complete user data passes validation."""
|
"""Test that complete user data passes validation."""
|
||||||
|
|
@ -143,7 +166,7 @@ def test_validate_user_data_subscription_without_currency(mock_api_client, mock_
|
||||||
"timeZone": "Europe/Berlin",
|
"timeZone": "Europe/Berlin",
|
||||||
"currentSubscription": {
|
"currentSubscription": {
|
||||||
"priceInfo": {
|
"priceInfo": {
|
||||||
"current": {} # Currency missing!
|
"current": {"currency": None} # Currency explicitly None
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue