def test_given_wrong_numcoordinates_raises_assertionerror(self): with pytest.raises(AssertionError) as exc_mssg: Lateral.validate_coordinates(field_value=[42, 24], values=dict(numcoordinates=1)) assert ( str(exc_mssg.value) == "Number of coordinates given (2) not matching the numCoordinates value 1." )
def test_given_wrong_location_type_raises_valueerror( self, value: str): with pytest.raises(ValueError) as exc_mssg: Lateral.validate_location_type(value) assert ( str(exc_mssg.value) == f"Value given ({value}) not accepted, should be one of: 1d, 2d, all" )
def test_given_no_numcoordinates_raises_valueerror(self): with pytest.raises(ValueError) as exc_mssg: Lateral.validate_coordinates( field_value=[42, 24], values=dict(numcoordinates=None)) assert ( str(exc_mssg.value) == "numCoordinates should be given when providing xCoordinates or yCoordinates." )
def test_given_no_values_raises_valueerror(self, dict_values: dict): with pytest.raises(ValueError) as exc_err: Lateral.validate_location_dependencies(values=dict_values) assert ( str(exc_err.value) == "Either nodeId, branchId (with chainage) or numCoordinates (with xCoordinates and yCoordinates) are required." )
def test_given_branchid_and_no_chainage_raises_valueerror(self): with pytest.raises(ValueError) as exc_err: Lateral.validate_location_dependencies( dict( nodeid=None, branchid="aBranchId", chainage=None, )) assert (str( exc_err.value ) == "Chainage should be provided when branchId is specified.")
def test_given_1d_args_and_location_type_other_then_raises_valueerror( self, dict_values: dict): test_values = dict( numcoordinates=2, xcoordinates=[42, 24], ycoordinates=[24, 42], locationtype="wrongType", ) test_dict = {**dict_values, **test_values} with pytest.raises(ValueError) as exc_err: Lateral.validate_location_dependencies(test_dict) assert ( str(exc_err.value) == "LocationType should be 1d when nodeId (or branchId and chainage) is specified." )
def test_given_numcoords_but_missing_coordinates( self, missing_coordinates: str): test_dict = dict( nodeid=None, branchid=None, chainage=None, numcoordinates=2, xcoordinates=[42, 24], ycoordinates=[24, 42], ) test_dict[missing_coordinates.lower()] = None with pytest.raises(ValueError) as exc_error: Lateral.validate_location_dependencies(test_dict) assert str(exc_error.value ) == f"{missing_coordinates} should be given."
def test_given_valid_args_validates_locationtype( self, location_dict: str): # 1. Define test data. default_values = dict( id="42", discharge="fDischarge", numcoordinates=2, ) lateral_dict = {**default_values, **location_dict} # 2. Run test. lateral_cls = Lateral(**lateral_dict) # 3. Validate expectations. assert isinstance(lateral_cls, INIBasedModel) for key, value in lateral_dict.items(): assert lateral_cls.dict()[key] == value
def test_given_coordinates_not_matching_numcoordinates_raises( self, x_coord: List[int], y_coord: List[int]): with pytest.raises(ValidationError): Lateral( id="42", discharge="bDischarge", numcoordinates=2, xcoordinates=x_coord, ycoordinates=y_coord, )
def test_given_valid_location_args_constructs_lateral( self, location_values: dict): # 1. Define test data. default_values = dict( id="42", discharge="eDischarge", numcoordinates=2, xcoordinates=[42, 24], ycoordinates=[24, 42], locationtype="1d", ) test_dict = {**default_values, **location_values} # 2. Run test. new_lateral = Lateral(**test_dict) # 3. Validate final expectations. for key, value in location_values.items(): assert new_lateral.dict()[key] == value
def test_given_numcoordinates_and_valid_coordinates(self): test_dict = dict( nodeid=None, branchid=None, chainage=None, numcoordinates=2, xcoordinates=[42, 24], ycoordinates=[24, 42], ) return_value = Lateral.validate_location_dependencies( test_dict) assert return_value == test_dict
def test_given_1d_args_and_1d_location_type( self, dict_values: dict): test_values = dict( numcoordinates=2, xcoordinates=[42, 24], ycoordinates=[24, 42], locationtype="1d", ) test_dict = {**dict_values, **test_values} return_value = Lateral.validate_location_dependencies( test_dict) assert return_value == test_dict
def test_given_unknown_locationtype_raises(self): with pytest.raises(ValidationError) as exc_mssg: location_type = "loremIpsum" Lateral( id="42", discharge="dDischarge", numcoordinates=2, xcoordinates=[42, 24], ycoordinates=[24, 42], locationtype=location_type, ) expected_error_mssg = f"Value given ({location_type}) not accepted, should be one of: 1d, 2d, all" assert expected_error_mssg in str(exc_mssg.value)
def test_given_coordinates_but_no_numcoordinates_raises( self, x_coord: Optional[List[int]], y_coord: Optional[List[int]]): with pytest.raises(ValidationError) as exc_mssg: Lateral( id="42", discharge="aDischarge", numcoordinates=None, xcoordinates=x_coord, ycoordinates=y_coord, ) expected_error_mssg = "numCoordinates should be given when providing xCoordinates or yCoordinates." assert expected_error_mssg in str(exc_mssg.value)
def test_given_partial_coordinates_raises(self, missing_coord: str): lateral_dict = dict( id="42", discharge="cDischarge", numcoordinates=2, xcoordinates=[42, 24], ycoordinates=[24, 42], locationtype="all", ) lateral_dict[missing_coord.lower()] = None with pytest.raises(ValidationError) as exc_mssg: Lateral(**lateral_dict) assert f"{missing_coord} should be given." in str( exc_mssg.value)
def test_given_correct_locationtype(self, location_type: str): return_value = Lateral.validate_location_type(location_type) assert return_value == location_type
def test_given_correct_numcoordinates(self): return_value = Lateral.validate_coordinates( field_value=[42, 24], values=dict(numcoordinates=2)) assert return_value == [42, 24]
def test_given_1d_args_but_no_locationtype_then_sets_value( self, test_dict: dict, location_type: str): test_dict["locationtype"] = location_type return_value = Lateral.validate_location_dependencies( test_dict) assert return_value["locationtype"] == "1d"