test_robot.py 4.5 KB

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