81 lines
2.2 KiB
Python
81 lines
2.2 KiB
Python
from datetime import datetime
|
|
|
|
from sqlmodel import Session
|
|
|
|
from app.models import EntityCache, EntityFlag, AuditLog
|
|
|
|
|
|
def _seed_entity(session: Session):
|
|
session.add(EntityCache(
|
|
entity_id="light.test",
|
|
domain="light",
|
|
friendly_name="Test",
|
|
state="on",
|
|
fetched_at=datetime.utcnow(),
|
|
))
|
|
session.commit()
|
|
|
|
|
|
def test_favorite_entity(client, session):
|
|
_seed_entity(session)
|
|
resp = client.post("/api/entities/actions", json={
|
|
"action": "favorite",
|
|
"entity_ids": ["light.test"],
|
|
})
|
|
assert resp.status_code == 200
|
|
data = resp.json()
|
|
assert data["action"] == "favorite"
|
|
assert data["results"][0]["ok"] is True
|
|
|
|
# Vérifier le flag
|
|
flag = session.get(EntityFlag, "light.test")
|
|
assert flag is not None
|
|
assert flag.favorite is True
|
|
|
|
|
|
def test_unfavorite_entity(client, session):
|
|
_seed_entity(session)
|
|
# D'abord favori
|
|
client.post("/api/entities/actions", json={
|
|
"action": "favorite",
|
|
"entity_ids": ["light.test"],
|
|
})
|
|
# Puis défavori
|
|
resp = client.post("/api/entities/actions", json={
|
|
"action": "unfavorite",
|
|
"entity_ids": ["light.test"],
|
|
})
|
|
assert resp.status_code == 200
|
|
flag = session.get(EntityFlag, "light.test")
|
|
assert flag.favorite is False
|
|
|
|
|
|
def test_ignore_entity(client, session):
|
|
_seed_entity(session)
|
|
resp = client.post("/api/entities/actions", json={
|
|
"action": "ignore",
|
|
"entity_ids": ["light.test"],
|
|
})
|
|
assert resp.status_code == 200
|
|
flag = session.get(EntityFlag, "light.test")
|
|
assert flag.ignored_local is True
|
|
|
|
|
|
def test_bulk_action(client, session):
|
|
session.add(EntityCache(entity_id="light.a", domain="light", state="on", fetched_at=datetime.utcnow()))
|
|
session.add(EntityCache(entity_id="light.b", domain="light", state="off", fetched_at=datetime.utcnow()))
|
|
session.commit()
|
|
|
|
resp = client.post("/api/entities/actions", json={
|
|
"action": "favorite",
|
|
"entity_ids": ["light.a", "light.b"],
|
|
})
|
|
data = resp.json()
|
|
assert len(data["results"]) == 2
|
|
|
|
# Vérifier audit_log
|
|
from sqlmodel import select
|
|
logs = session.exec(select(AuditLog)).all()
|
|
assert len(logs) >= 1
|
|
assert logs[-1].action == "favorite"
|