test_robot.py 4.0 KB

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