Beispiel #1
0
def test_cmp_funcs():
    range_validator = validate.Range(0, 10)
    daterange_validator = validate.DateRange(
        "2019-01-01", "2019-01-05", step={"days": 2}
    )
    choice_validator = validate.OneOf(choices=["one", "two"])

    cases = [
        ("one", "two", fields.Str(validate=[choice_validator])),
        (
            datetime.date(2019, 1, 2),
            datetime.date(2019, 1, 3),
            fields.Date(validate=[daterange_validator]),
        ),
        (2, 5, fields.Integer(validate=[range_validator])),
    ]

    for (min_, max_, field) in cases:
        cmp_funcs = field.cmp_funcs()
        assert cmp_funcs["gt"](min_, max_) is False
        assert cmp_funcs["lt"](min_, max_) is True
        assert cmp_funcs["eq"](min_, max_) is False
        assert cmp_funcs["eq"](max_, max_) is True
        assert cmp_funcs["lte"](max_, max_) is True
        assert cmp_funcs["lte"](min_, max_) is True
        assert cmp_funcs["gte"](max_, min_) is True
Beispiel #2
0
 def _get_choice_validator(self, vname, choice_dict, param_name, dims,
                           param_spec, raw_data):
     choices = choice_dict["choices"]
     dim_suffix = f" for dimensions {dims}" if dims else ""
     if len(choices) < 20:
         error_template = (
             '{param_name} "{input}" must be in list of choices '
             "{choices}{dim_suffix}.")
     else:
         error_template = '{param_name} "{input}" must be in list of choices{dim_suffix}.'
     error = error_template.format(
         param_name=param_name,
         dims=dims,
         input="{input}",
         choices="{choices}",
         dim_suffix=dim_suffix,
     )
     return contrib_validate.OneOf(choices, error=error)
Beispiel #3
0
 def _get_choice_validator(self, vname, choice_dict, param_name, param_spec,
                           raw_data):
     choices = choice_dict["choices"]
     labels = utils.make_label_str(param_spec)
     label_suffix = f" for labels {labels}" if labels else ""
     if len(choices) < 20:
         error_template = (
             '{param_name} "{input}" must be in list of choices '
             "{choices}{label_suffix}.")
     else:
         error_template = '{param_name} "{input}" must be in list of choices{label_suffix}.'
     error = error_template.format(
         param_name=param_name,
         labels=labels,
         input="{input}",
         choices="{choices}",
         label_suffix=label_suffix,
     )
     return contrib_validate.OneOf(choices, error=error)
Beispiel #4
0
def test_contrib_fields():
    range_validator = validate.Range(0, 10)
    daterange_validator = validate.DateRange("2019-01-01",
                                             "2019-01-05",
                                             step={"days": 2})
    choice_validator = validate.OneOf(choices=["one", "two"])

    s = fields.Str(validate=[choice_validator])
    assert s.mesh() == ["one", "two"]
    s = fields.Str()
    assert s.mesh() == []

    s = fields.Integer(validate=[range_validator])
    assert s.mesh() == [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    s = fields.Str()
    assert s.mesh() == []

    # date will need an interval argument.
    s = fields.Date(validate=[daterange_validator])
    assert s.mesh() == [datetime.date(2019, 1, i) for i in range(1, 6, 2)]