60 lines
1.9 KiB
Python
60 lines
1.9 KiB
Python
# Copyright 2026 Developer
|
|
# Licensed under the Apache License, Version 2.0
|
|
|
|
"""Test suite for the room classification node."""
|
|
|
|
from unittest.mock import MagicMock, patch
|
|
|
|
import pytest
|
|
import rclpy
|
|
from scene_graph_generation_pipeline.room_classification.room_classification_node import ( # noqa: E501
|
|
RoomClassificationNode
|
|
)
|
|
|
|
|
|
@pytest.fixture(scope="function")
|
|
def ros_init():
|
|
"""Initialize and shutdown the ROS2 context for testing."""
|
|
if not rclpy.ok():
|
|
rclpy.init()
|
|
yield
|
|
if rclpy.ok():
|
|
rclpy.shutdown()
|
|
|
|
|
|
def test_room_classification(ros_init):
|
|
"""Test room classification rules for kitchen vs office."""
|
|
mock_classifier = MagicMock()
|
|
def predict_side_effect(input_vector):
|
|
# Index of 'appliances' (from refrigerator) is 37 in the mpcat40 list
|
|
if input_vector[0][37] > 0:
|
|
return [10] # kitchen
|
|
return [14] # office
|
|
mock_classifier.predict = MagicMock(side_effect=predict_side_effect)
|
|
|
|
with patch('builtins.open', MagicMock()), \
|
|
patch('pickle.load', return_value=mock_classifier):
|
|
node = RoomClassificationNode()
|
|
|
|
# Test case 1: room containing a refrigerator returns "kitchen"
|
|
room_label_kitchen = node.classify_room(
|
|
['chair', 'table', 'refrigerator', 'sink']
|
|
)
|
|
assert room_label_kitchen == 'kitchen'
|
|
|
|
# Test case 2: room containing a refrigerator (different casing)
|
|
room_label_kitchen_caps = node.classify_room(
|
|
['REFRIGERATOR', 'cup']
|
|
)
|
|
assert room_label_kitchen_caps == 'kitchen'
|
|
|
|
# Test case 3: room without a refrigerator returns "office"
|
|
room_label_office = node.classify_room(['chair', 'table', 'laptop'])
|
|
assert room_label_office == 'office'
|
|
|
|
# Test case 4: room with unknown objects returns None
|
|
room_label_unknown = node.classify_room(['random_unknown_object'])
|
|
assert room_label_unknown is None
|
|
|
|
node.destroy_node()
|