Coverage for app/domain/probe/commands/move_command.py: 100%

15 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-06-16 20:06 +0000

1from app.domain.probe.commands.command import Command 

2from app.domain.probe.entities.grid import Grid 

3from app.domain.probe.entities.probe import Probe 

4from app.domain.probe.exceptions import InvalidMovementError 

5from app.schemas.direction import Direction 

6 

7 

8class Move(Command): 

9 def execute(self, probe: Probe, grid: Grid) -> Probe: 

10 movements = { 

11 Direction.NORTH: (0, 1), # NORTH -> y -> +1 

12 Direction.SOUTH: (0, -1), # SOUTH -> y -> -1 

13 Direction.EAST: (1, 0), # EAST -> x -> +1 

14 Direction.WEST: (-1, 0), # WEST -> x -> -1 

15 } 

16 

17 x_move, y_move = movements[probe.direction] 

18 

19 new_x_position = probe.x + x_move 

20 new_y_position = probe.y + y_move 

21 

22 if not grid.is_movement_inside_grid_limits(new_x_position, new_y_position): 

23 raise InvalidMovementError( 

24 f"Movement outside grid limits. The probe must not exceed the grid size of ({grid.x_size}, {grid.y_size})." 

25 ) 

26 

27 new_probe = Probe(x=new_x_position, y=new_y_position, direction=probe.direction) 

28 

29 return new_probe