示例#1
0
 def test_must_raise_validation_exception_when_input_str_does_not_match_format(
         self, format, input):
     with pytest.raises(ValidationException) as exc_info:
         is_date(format=format)(input)
     assert exc_info.value.args[
         0] == "'{}' does not match expected format({})".format(
             input, format)
示例#2
0
class TestListValidator:
    """
    1. must reject none input whend field is required
    2. must return default value when field isnot required and default is provided
    4. must validate all entries against the validator.
    5. must require all entries to pass validation by default
    6. when all is set to false, must require that at least one entry pass valdiation
    7. must return only validated entries
    6. on error, must return all errors encountered
    """
    def test_must_raise_validation_error_when_input_is_none_but_required_is_true(
            self):
        with pytest.raises(ValidationException) as exc_info:
            is_list(required=True, validator=is_int())(None)
        assert exc_info.value.errors == "required but was missing"

    def test_must_return_default_value_when_input_is_none(self):
        default = [1, 2]
        assert default == is_list(required=False,
                                  default=[1, 2],
                                  validator=is_int())(None)

    @pytest.mark.parametrize("input", ["value", {"id": 23}, object, 2.8])
    def test_must_raise_validation_exception_for_non_list_input(self, input):
        with pytest.raises(ValidationException) as exc:
            is_list(validator=Mock())(input)
        assert exc.value.errors == "expected a list but got {}".format(
            type(input))

    def test_must_validate_all_input_against_validator(self):
        validator = Mock()
        is_list(validator=validator)([-1, 8])
        validator.assert_has_calls([call(-1), call(8)])

    @pytest.mark.parametrize(("validator", "input", "errors"), [
        (is_int(min=1), [-1, 2, 8], ["'-1' is less than minimum allowed (1)"]),
        (is_int(max=5), [8, 10], [
            "'8' is greater than maximum allowed (5)",
            "'10' is greater than maximum allowed (5)"
        ]),
        (is_str(pattern=r"\A\d{3}\Z"), ["2323", "128"],
         ["'2323' does not match expected pattern(\\A\\d{3}\\Z)"])
    ])
    def test_must_raise_validation_when_at_least_one_entry_is_invalid_by_default(
            self, validator, input, errors):
        with pytest.raises(ValidationException) as exc:
            is_list(validator=validator)(input)
        assert exc.value.errors == errors

    def test_must_raise_validation_exception_only_when_all_entries_are_invalid_when_all_is_false(
            self):
        input = [-1, 2, 8]
        try:
            is_list(validator=is_int(min=1), all=False)(input)
        except ValidationException:
            raise AssertionError("should not throw")

    @pytest.mark.parametrize(
        ("validator", "input", "return_val"),
        [(is_int(required=True), [-3, 8, 112], [-3, 8, 112]),
         (is_str(required=True), ["one", "three ", " four "
                                  ], ["one", "three", "four"]),
         (is_date(format="%Y-%m-%d"), ["2021-02-07 "],
          [datetime.datetime(year=2021, month=2, day=7)])])
    def test_must_return_newly_validated_input(self, validator, input,
                                               return_val):
        assert is_list(validator=validator)(input) == return_val

    def test_must_return_only_valid_inputs_when_all_is_false(self):
        input = [1, -8, 3]
        assert is_list(validator=is_int(min=1), all=False)(input) == [1, 3]
示例#3
0
 def test_must_return_newly_validated_date_as_datetime_object(self, input):
     assert is_date()(input) == datetime.datetime.strptime(
         input, "%Y-%m-%d")
示例#4
0
 def test_must_return_none_when_input_is_none_and_required_is_false_and_default_is_not_provided(
         self):
     assert is_date(required=False)(None) is None
示例#5
0
 def test_when_input_date_is_none_must_return_default_date_if_available(
         self):
     today = datetime.datetime.today()
     assert today == is_date(default=today)(None)
示例#6
0
 def test_must_support_datetime_objects_as_input_dates(self):
     today = datetime.datetime.today()
     assert today == is_date()(today)
示例#7
0
 def test_must_raise_validation_exception_when_date_is_older_than_latest_by_if_defined(
         self, max, input):
     with pytest.raises(ValidationException) as exc_info:
         is_date(max=datetime.datetime.strptime(max, "%Y-%m-%d"))(input)
     assert exc_info.value.args[
         0] == "'{}' occurs after maximum date({})".format(input, max)
示例#8
0
 def test_must_use_iso_8601_format_when_format_is_not_supplied(self, input):
     date = is_date()(input)
     assert date == datetime.datetime.strptime(input.strip(), "%Y-%m-%d")
示例#9
0
 def test_must_raise_validation_exception_when_input_is_missing_and_required_is_true(
         self):
     with pytest.raises(ValidationException) as exc_info:
         is_date(required=True)(None)
     assert exc_info.value.args[0] == "required but was missing"