test_robot.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import pytest
  2. from robot.movement import RobotMovement
  3. from robot.controller import RobotController, Cell, CellStatus
  4. from utils.config import ConfigParser
  5. @pytest.fixture
  6. def robot_movement():
  7. return RobotMovement()
  8. @pytest.fixture
  9. def robot_controller():
  10. config_parser = ConfigParser()
  11. config_parser.config["mqtt"]["broker"] = "debug" # connects to test mqtt broker
  12. controller = RobotController(config_parser)
  13. return controller
  14. class TestRobotMovement:
  15. def test_init_position(self, robot_movement: RobotMovement):
  16. assert robot_movement.get_position() == (0.0, 0.0, 0.0)
  17. assert not robot_movement.is_moving
  18. def test_set_speed(self, robot_movement: RobotMovement):
  19. robot_movement.set_speed(200.0)
  20. assert robot_movement.speed == 200.0
  21. with pytest.raises(ValueError):
  22. robot_movement.set_speed(-100.0)
  23. @pytest.mark.asyncio
  24. async def test_move_to_position(self, robot_movement: RobotMovement):
  25. result = await robot_movement.move_to_position(100.0, 200.0, 300.0)
  26. assert result is True
  27. assert robot_movement.get_position() == (100.0, 200.0, 300.0)
  28. @pytest.mark.asyncio
  29. async def test_move_relative(self, robot_movement: RobotMovement):
  30. await robot_movement.move_to_position(100.0, 100.0, 100.0)
  31. assert await robot_movement.move_relative(50.0, -50.0, 25.0)
  32. assert robot_movement.get_position() == (150.0, 50.0, 125.0)
  33. class TestRobotController:
  34. def test_init_controller(self, robot_controller: RobotController):
  35. assert len(robot_controller.devices) > 0
  36. assert robot_controller.feeder is not None
  37. assert not robot_controller.gripper_occupied
  38. @pytest.mark.asyncio
  39. async def test_get_next_free_slot(self, robot_controller: RobotController):
  40. slot = robot_controller.get_next_free_slot()
  41. assert slot is not None
  42. assert not slot.occupied
  43. @pytest.mark.asyncio
  44. async def test_insert_and_collect_cell(self, robot_controller: RobotController):
  45. # Test cell insertion
  46. cell = Cell(id="test_cell", status=CellStatus.WAITING)
  47. slot = robot_controller.get_next_free_slot()
  48. robot_controller.gripper_occupied = True
  49. await robot_controller.insert_cell_to_slot(cell, slot)
  50. assert slot.occupied
  51. assert slot.cell_id == cell.id
  52. # Test cell collection
  53. collected_cell = await robot_controller.collect_cell_from_slot(slot)
  54. assert not slot.occupied
  55. assert slot.cell_id is None
  56. assert collected_cell == cell.id
  57. @pytest.mark.asyncio
  58. async def test_sort_empty_gripper(self, robot_controller: RobotController):
  59. cell = Cell(
  60. id="test_cell",
  61. status=CellStatus.COMPLETED,
  62. measurement_slot=1,
  63. capacity=3000.0
  64. )
  65. robot_controller.gripper_occupied = False
  66. assert not await robot_controller.sort_cell(cell)
  67. @pytest.mark.asyncio
  68. async def test_sort_failed_cell(self, robot_controller: RobotController):
  69. failed_cell = Cell(
  70. id="failed_cell",
  71. status=CellStatus.FAILED,
  72. measurement_slot=1
  73. )
  74. robot_controller.gripper_occupied = True
  75. await robot_controller.sort_cell(failed_cell)
  76. assert not robot_controller.gripper_occupied
  77. @pytest.mark.asyncio
  78. async def test_sort_cell_by_capacity(self, robot_controller: RobotController):
  79. # Test sorting a cell with capacity that matches a grade
  80. cell = Cell(
  81. id="test_cell",
  82. status=CellStatus.COMPLETED,
  83. measurement_slot=1,
  84. capacity=3000.0
  85. )
  86. robot_controller.gripper_occupied = True
  87. await robot_controller.sort_cell(cell)
  88. assert not robot_controller.gripper_occupied
  89. # Test sorting a cell with capacity that doesn't match any grade
  90. low_cap_cell = Cell(
  91. id="low_cap_cell",
  92. status=CellStatus.COMPLETED,
  93. measurement_slot=1,
  94. capacity=0.0
  95. )
  96. robot_controller.gripper_occupied = True
  97. await robot_controller.sort_cell(low_cap_cell)
  98. assert not robot_controller.gripper_occupied