M8-R06: refresh Tibber prices after startup and configuration
This commit is contained in:
@@ -48,6 +48,7 @@ from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import Any
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
from fastapi.testclient import TestClient
|
||||
@@ -614,6 +615,52 @@ def test_activate_contract_mutual_exclusion(contracts_client):
|
||||
assert active_contracts[0].id == id_b
|
||||
|
||||
|
||||
def test_activating_tibber_contract_triggers_refresh_only_after_commit(contracts_client):
|
||||
"""Inactive→active Tibber is the only contract transition that requests refresh."""
|
||||
client, engine = contracts_client
|
||||
_login(client)
|
||||
created = client.post(
|
||||
"/api/energy/contracts",
|
||||
json=_tibber_payload(),
|
||||
headers={"X-CSRF-Token": _CSRF},
|
||||
)
|
||||
contract_id = created.json()["id"]
|
||||
|
||||
with patch("app.api.routes.api.energy_contracts.trigger_tibber_refresh") as trigger:
|
||||
response = client.patch(
|
||||
f"/api/energy/contracts/{contract_id}",
|
||||
json={"active": True},
|
||||
headers={"X-CSRF-Token": _CSRF},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
assert trigger.call_count == 1
|
||||
with Session(engine) as session:
|
||||
assert session.get(EnergyContract, contract_id).active is True
|
||||
|
||||
with patch("app.api.routes.api.energy_contracts.trigger_tibber_refresh") as trigger:
|
||||
response = client.patch(
|
||||
f"/api/energy/contracts/{contract_id}",
|
||||
json={"active": True},
|
||||
headers={"X-CSRF-Token": _CSRF},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
trigger.assert_not_called()
|
||||
|
||||
manual = client.post(
|
||||
"/api/energy/contracts",
|
||||
json=_manual_payload(),
|
||||
headers={"X-CSRF-Token": _CSRF},
|
||||
).json()
|
||||
with patch("app.api.routes.api.energy_contracts.trigger_tibber_refresh") as trigger:
|
||||
response = client.patch(
|
||||
f"/api/energy/contracts/{manual['id']}",
|
||||
json={"active": True},
|
||||
headers={"X-CSRF-Token": _CSRF},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
trigger.assert_not_called()
|
||||
|
||||
|
||||
def test_deactivate_contract(contracts_client):
|
||||
"""PATCH active=false deactivates the contract without touching others."""
|
||||
client, _ = contracts_client
|
||||
|
||||
Reference in New Issue
Block a user