mirror of
https://github.com/Hestia-Homes/Model.git
synced 2026-06-08 11:17:27 +00:00
Define dataclasses to represent API json 🟩
This commit is contained in:
parent
18a58486af
commit
c69f223343
1 changed files with 87 additions and 0 deletions
87
datatypes/magicplan/api/tests/test_response.py
Normal file
87
datatypes/magicplan/api/tests/test_response.py
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
import json
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import pytest
|
||||
|
||||
from datatypes.magicplan.api.response import MagicPlan
|
||||
|
||||
FIXTURE_DIR = Path(__file__).parents[4] / "backend" / "magic_plan"
|
||||
PLAN_ID = "a7285ed1-878d-47eb-8aa6-85ef9e187516"
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def raw_data() -> dict[str, Any]:
|
||||
payload = json.loads(
|
||||
(FIXTURE_DIR / "magicplan_api_plan_response_example.json").read_text()
|
||||
)
|
||||
return payload["data"]
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def mp(raw_data: dict[str, Any]) -> MagicPlan:
|
||||
return MagicPlan.model_validate(raw_data)
|
||||
|
||||
|
||||
def test_model_validate_does_not_raise(raw_data: dict[str, Any]):
|
||||
# act
|
||||
MagicPlan.model_validate(raw_data)
|
||||
|
||||
|
||||
def test_plan_id(mp: MagicPlan):
|
||||
# assert
|
||||
assert mp.plan.id == PLAN_ID
|
||||
|
||||
|
||||
def test_url_3d_alias(mp: MagicPlan):
|
||||
# assert
|
||||
assert mp.plan.url_3d is not None
|
||||
assert mp.plan.url_3d.startswith("http")
|
||||
|
||||
|
||||
def test_floor_count(mp: MagicPlan):
|
||||
# assert
|
||||
assert len(mp.plan_detail.plan.floors) == 2
|
||||
|
||||
|
||||
def test_first_room_name(mp: MagicPlan):
|
||||
# assert
|
||||
assert mp.plan_detail.plan.floors[0].rooms[0].name == "Kitchen"
|
||||
|
||||
|
||||
def test_room_area_is_float(mp: MagicPlan):
|
||||
# arrange
|
||||
room = mp.plan_detail.plan.floors[0].rooms[0]
|
||||
# assert
|
||||
assert isinstance(room.area, float)
|
||||
|
||||
|
||||
def test_wall_item_symbol_id(mp: MagicPlan):
|
||||
# arrange
|
||||
room = mp.plan_detail.plan.floors[0].rooms[0]
|
||||
# assert
|
||||
assert room.wall_items[0].symbol.id != ""
|
||||
|
||||
|
||||
def test_field_value_array(mp: MagicPlan):
|
||||
# arrange
|
||||
room = mp.plan_detail.plan.floors[0].rooms[0]
|
||||
array_field = next(f for f in room.displayable_fields if f.value.is_array)
|
||||
# assert
|
||||
assert isinstance(array_field.value.value, list)
|
||||
|
||||
|
||||
def test_field_value_scalar(mp: MagicPlan):
|
||||
# arrange
|
||||
room = mp.plan_detail.plan.floors[0].rooms[0]
|
||||
scalar_field = next(f for f in room.displayable_fields if not f.value.is_array)
|
||||
# assert
|
||||
assert isinstance(scalar_field.value.value, str)
|
||||
|
||||
|
||||
def test_extra_fields_ignored(raw_data: dict[str, Any]):
|
||||
# arrange
|
||||
data_with_extra = {**raw_data, "unknown_future_field": "whatever"}
|
||||
# act
|
||||
MagicPlan.model_validate(data_with_extra)
|
||||
print("")
|
||||
Loading…
Add table
Reference in a new issue