Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ def upsert_inventory_room(ctx: ServerContext, *, home_id: int, room_name: str) -
None,
)
if existing_room is not None:
room_payload = {"id": as_int(existing_room.get("id"), 0), "name": str(existing_room.get("name") or normalized_name)}
# The match ignores case so a room isn't created twice. A name that differs only in case
# is a correction to that room's spelling, so store it; otherwise the first spelling could
# never be changed, since nothing else renames a room.
existing_room["name"] = normalized_name
room_payload = {"id": as_int(existing_room.get("id"), 0), "name": normalized_name}
created = False
else:
next_room_id = max((as_int(room.get("id"), 0) for room in rooms if isinstance(room, dict)), default=0) + 1
Expand Down
33 changes: 33 additions & 0 deletions tests/test_admin_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1602,3 +1602,36 @@ def test_create_room_returns_bare_id_in_result_and_object_in_data(tmp_path: Path
assert body["data"]["id"] == body["result"]
assert body["data"]["name"] == "Study"
assert body["data"]["created"] is True


def test_create_room_with_new_capitalization_updates_existing_room(tmp_path: Path) -> None:
config_file = write_release_config(tmp_path)
config = load_config(config_file)
paths = resolve_paths(config_file, config)
paths.inventory_path.parent.mkdir(parents=True, exist_ok=True)
rooms = [{"id": 11100, "name": "Master bedroom"}, {"id": 11101, "name": "Hallway"}]
paths.inventory_path.write_text(
json.dumps({"home": {"id": 1316433, "name": "My Home", "rooms": rooms}, "rooms": rooms}) + "\n",
encoding="utf-8",
)
_seed_protocol_snapshot(paths.cloud_snapshot_path)

supervisor = ReleaseSupervisor(config=config, paths=paths)
client = TestClient(supervisor.app)

path = "/user/homes/1316433/rooms"
response = client.post(
path,
data={"name": "Master Bedroom"},
headers=_hawk_headers(paths.cloud_snapshot_path, path, form_values={"name": "Master Bedroom"}),
)

assert response.status_code == 200
body = response.json()
assert body["result"] == 11100
assert body["data"]["name"] == "Master Bedroom"
assert body["data"]["created"] is False

stored_inventory = json.loads(paths.inventory_path.read_text(encoding="utf-8"))
for stored_rooms in (stored_inventory["home"]["rooms"], stored_inventory["rooms"]):
assert stored_rooms == [{"id": 11100, "name": "Master Bedroom"}, {"id": 11101, "name": "Hallway"}]
Loading