rover: add non-negative coordinate validation

This commit is contained in:
Bruno BELANYI 2019-11-16 04:24:02 +01:00
parent ce0241b3f4
commit 1fbf7ece7c
2 changed files with 25 additions and 1 deletions

View File

@ -2,13 +2,25 @@ import enum
from copy import deepcopy
from typing import List
from pydantic import BaseModel
from pydantic import BaseModel, validator
class Vector(BaseModel):
x: int = 0
y: int = 0
@validator("x")
def _x_must_be_positive(x):
if x < 0:
raise ValueError("x must be positive")
return x
@validator("y")
def _y_must_be_positive(y):
if y < 0:
raise ValueError("y must be positive")
return y
class Direction(enum.Enum):
NORTH = "N"

View File

@ -1,4 +1,6 @@
import pytest
from pydantic import ValidationError
from rover import Commander, Direction, ObstacleError, Rover, Vector
@ -225,3 +227,13 @@ def test_commander_command_with_obstacles():
with pytest.raises(ObstacleError):
com.parse_execute("FFRF")
assert com.rover == Rover(pos=Vector(x=0, y=2), dir=Direction.EAST)
def test_rover_negative_x_construction():
with pytest.raises(ValidationError):
_ = Rover(pos=Vector(x=-1, y=0))
def test_rover_negative_y_construction():
with pytest.raises(ValidationError):
_ = Rover(pos=Vector(x=0, y=-1))