Exemplo n.º 1
0
 def test_context(self):
     def fail(v, cxt:ValidationContext):
         raise ValidationFailure(name=str(cxt.path))
     c = Converter("test", fail, True)
     v, f = c.convert(["1", "2", "3"], ValidationContext())
     assert v is None
     assert [f[i].name for i in range(3)] == ["[0]", "[1]", "[2]"]
Exemplo n.º 2
0
 def test_exception(self):
     c = Converter("test", int, False)
     v, f = c.convert("a")
     assert v is None
     assert isinstance(f, ConversionFailure)
     assert f.converter is c
     assert f.name == "test"
Exemplo n.º 3
0
 def test_exception_unjointed(self):
     c = Converter("test", int, True)
     cxt = ValidationContext()
     cxt.config.join_on_fail = False
     v, f = c.convert(["1", "a", "3"], cxt)
     assert v == [1, None, 3]
     assert isinstance(f, CompositeValidationFailure)
     assert isinstance(f[1], ConversionFailure)
Exemplo n.º 4
0
 def test_conversion_failure(self):
     def fail(v):
         ConversionFailure.abort("conversion failed")
     c = Converter("test", fail, False)
     v, f = c.convert("1")
     assert v is None
     assert isinstance(f, ConversionFailure)
     assert f.converter is c
     assert f.name == "test"
Exemplo n.º 5
0
 def test_validation_failure(self):
     def fail(v):
         raise ValidationFailure()
     c = Converter("test", fail, False)
     v, f = c.convert("1")
     assert v is None
     assert isinstance(f, ValidationFailure)
     assert not isinstance(f, ConversionFailure)
     assert f.name == "invalid"
Exemplo n.º 6
0
 def test_isinstance_builtin(self):
     c = Converter("test", int, True)
     cxt = ValidationContext().configure(isinstance_builtin=True, join_on_fail=False)
     v, f = c.convert(["1", 2, "3"], cxt)
     assert v == [None, 2, None]
     assert isinstance(f, CompositeValidationFailure)
     assert isinstance(f[0], ConversionFailure)
     assert f[1] is None
     assert isinstance(f[2], ConversionFailure)
Exemplo n.º 7
0
 def test_context(self):
     def fail(v, cxt:ValidationContext):
         raise ValidationFailure(name=cxt.name)
     c = Converter("test", fail, False)
     cxt = ValidationContext().put(
         name = "context",
     )
     v, f = c.convert("1", cxt)
     assert v is None
     assert f.name == "context"
Exemplo n.º 8
0
 def test_isinstance_builtin(self):
     c = Converter("test", int, False)
     cxt = ValidationContext().configure(isinstance_builtin=True)
     v, f = c.convert("1", cxt)
     assert v is None
     assert isinstance(f, ConversionFailure)
     assert f.converter is c
     assert f.name == "test"
     v, f = c.convert(1, cxt)
     assert v == 1
     assert f is None
Exemplo n.º 9
0
 def test_isinstance_any(self):
     class T:
         def __init__(self, t):
             self.t = t
     c = Converter("test", T, False)
     cxt = ValidationContext().configure(isinstance_any=True)
     v, f = c.convert("1", cxt)
     assert v is None
     assert isinstance(f, ConversionFailure)
     assert f.converter is c
     assert f.name == "test"
     v, f = c.convert(T("1"), cxt)
     assert v.t == "1"
     assert f is None
Exemplo n.º 10
0
 def test_isinstance_any(self):
     class T:
         def __init__(self, t):
             self.t = t
     c = Converter("test", T, True)
     cxt = ValidationContext().configure(isinstance_any=True, join_on_fail=False)
     v, f = c.convert(["1", T(2), "3"], cxt)
     assert v[0] is None
     assert v[1].t == 2
     assert v[2] is None
     assert isinstance(f, CompositeValidationFailure)
     assert isinstance(f[0], ConversionFailure)
     assert f[1] is None
     assert isinstance(f[2], ConversionFailure)
Exemplo n.º 11
0
 def test_ignore_empty(self):
     config = default_config().derive()
     config.allow_empty = True
     v = +Validator(Converter("conv", lambda x: x, False), [], config)
     assert v.requirement.missing == RequirementPolicy.FAIL
     assert v.requirement.null == RequirementPolicy.REQUIRES
     assert v.requirement.empty == RequirementPolicy.REQUIRES
Exemplo n.º 12
0
 def _validator(self):
     return Validator(
         Converter("conv", lambda x: x, False),
         [
             Verifier("gt", lambda x: x > 0, True),
             Verifier("lt", lambda x: x < 10, True)
         ],
     )
Exemplo n.º 13
0
 def _validator(self):
     return Validator(
         Converter("conv", int, True),
         [],
     )
Exemplo n.º 14
0
 def test_exception(self):
     c = Converter("test", int, True)
     v, f = c.convert(["1", "a", "3"])
     assert v is None
     assert isinstance(f, CompositeValidationFailure)
     assert isinstance(f[1], ConversionFailure)
Exemplo n.º 15
0
 def test_convert(self):
     c = Converter("test", int, True)
     v, f = c.convert(["1", "2", "3"])
     assert v == [1, 2, 3]
     assert f is None
Exemplo n.º 16
0
 def test_convert(self):
     c = Converter("test", int, False)
     v, f = c.convert("1")
     assert v == 1
     assert f is None
Exemplo n.º 17
0
 def _validator(self):
     return Validator(
         Converter("conv", lambda x: x, False),
         [],
     )