Model/tests/domain/magicplan/test_response.py
2026-06-05 14:07:28 +00:00

124 lines
3.8 KiB
Python

import json
from pathlib import Path
from typing import Any
import pytest
from domain.magicplan.api.response import MagicPlanPlan, PlansListResponse
FIXTURE_DIR = Path(__file__).parents[3] / "tests" / "magic_plan"
PLAN_ID = "72efd2e0-b2b9-48cd-b82e-41f5b3166c9a"
@pytest.fixture(scope="module")
def raw_data() -> dict[str, Any]:
payload = json.loads(
(FIXTURE_DIR / "magicplan_api_plan_response.json").read_text()
)
return payload["data"]
@pytest.fixture(scope="module")
def mp(raw_data: dict[str, Any]) -> MagicPlanPlan:
return MagicPlanPlan.model_validate(raw_data)
def test_model_validate_does_not_raise(raw_data: dict[str, Any]) -> None:
MagicPlanPlan.model_validate(raw_data)
def test_plan_id(mp: MagicPlanPlan) -> None:
assert mp.plan.id == PLAN_ID
def test_floor_count(mp: MagicPlanPlan) -> None:
assert len(mp.plan_detail.plan.floors) == 2
def test_first_room_name(mp: MagicPlanPlan) -> None:
assert mp.plan_detail.plan.floors[0].rooms[0].name == "Kitchen"
def test_room_area_is_float(mp: MagicPlanPlan) -> None:
room = mp.plan_detail.plan.floors[0].rooms[0]
assert isinstance(room.area, float)
def test_wall_item_symbol_id(mp: MagicPlanPlan) -> None:
room = mp.plan_detail.plan.floors[0].rooms[0]
assert room.wall_items[0].symbol.id != ""
def test_custom_displayable_fields_parsed(mp: MagicPlanPlan) -> None:
# Kitchen windowcasement carries custom_displayable_fields in the new fixture.
room = mp.plan_detail.plan.floors[0].rooms[0]
wi = next(w for w in room.wall_items if w.symbol.id == "windowcasement")
assert len(wi.custom_displayable_fields) > 0
def test_field_value_array(mp: MagicPlanPlan) -> None:
room = mp.plan_detail.plan.floors[0].rooms[0]
wi = next(w for w in room.wall_items if w.symbol.id == "windowcasement")
array_field = next(f for f in wi.custom_displayable_fields if f.value.is_array)
assert isinstance(array_field.value.value, list)
def test_field_value_scalar(mp: MagicPlanPlan) -> None:
room = mp.plan_detail.plan.floors[0].rooms[0]
wi = next(w for w in room.wall_items if w.symbol.id == "windowcasement")
scalar_field = next(f for f in wi.custom_displayable_fields if not f.value.is_array)
assert isinstance(scalar_field.value.value, str)
def test_extra_fields_ignored(raw_data: dict[str, Any]) -> None:
data_with_extra = {**raw_data, "unknown_future_field": "whatever"}
MagicPlanPlan.model_validate(data_with_extra)
# --- PlansListResponse ---
@pytest.fixture(scope="module")
def plans_raw_data() -> dict[str, Any]:
payload = json.loads(
(FIXTURE_DIR / "magicplan_api_plans_response_example.json").read_text()
)
return payload["data"]
@pytest.fixture(scope="module")
def plans_response(plans_raw_data: dict[str, Any]) -> PlansListResponse:
return PlansListResponse.model_validate(plans_raw_data)
def test_plans_list_model_validate_does_not_raise(
plans_raw_data: dict[str, Any],
) -> None:
PlansListResponse.model_validate(plans_raw_data)
def test_plans_list_count(plans_response: PlansListResponse) -> None:
assert len(plans_response.plans) == 1
def test_plans_list_first_plan_id(plans_response: PlansListResponse) -> None:
assert plans_response.plans[0].id == "9f9889ff-793e-4e9a-a6f0-e22f5b0f5365"
def test_plans_list_paging_page(plans_response: PlansListResponse) -> None:
assert plans_response.paging.page == 1
def test_plans_list_paging_next_page_is_false(
plans_response: PlansListResponse,
) -> None:
assert plans_response.paging.next_page is False
def test_plans_list_paging_count(plans_response: PlansListResponse) -> None:
assert plans_response.paging.count == 1
def test_plans_list_unknown_keys_ignored(plans_raw_data: dict[str, Any]) -> None:
data_with_extra = {**plans_raw_data, "unknown_future_field": "whatever"}
PlansListResponse.model_validate(data_with_extra)