def test_combinator_edge_cases(): with raises_regexp(TypeError, 'got Exists class instead of its instance'): IsA(str) | Exists # translate() is applied to the right argument assert IsA(str) | 'Albatross!' == IsA(str) | IsA(str, default='Albatross!') assert IsA(str) | None == IsA(str) | Anything() assert Length(min=3) & [] == Length(min=3) & IsA(list)
def test_anything(): v = Anything() assert repr(v) == 'Anything()' v('foo') v(123) v(MISSING) v([5])
def test_regression_22(): ~Anything() ~IsA(str) ~Equals(5) ~Contains('x') ~InRange(5) ~Length(5) ~ListOf(str) ~DictOf([]) ~Exists()
def test_any_optional(self): "A value of any type or no value" spec = Anything() | ~Exists() # value is present spec(1) # value is missing spec(None) spec(MISSING)
def test_any_required(self): "A value of any type" spec = Anything() # value is present spec(1) # XXX CHANGED: # ## value is missing #with pytest.raises(MissingValue) as excinfo: # validate(Rule(datatype=None), None) #assert "MissingValue: expected a value, got None" in excinfo.exconly() # spec(None) spec(MISSING)
def test_none(self): assert translate(None) == Anything()
def test_merge_anything(self): # "any value is OK" assert Anything().get_default_for(None) == None assert Anything().get_default_for(1234) == 1234 assert Anything().get_default_for('hi') == 'hi' assert Anything().get_default_for({1: 2}) == {1: 2}
def test_merge_list(self): ## present but empty inner spec (optional) rule = ListOf([]) | Equals(None) # optional missing list assert rule.get_default_for(None) == None ## present but empty inner spec (required) rule = ListOf([]) # required missing list → empty list assert rule.get_default_for(None) == [] ## present non-empty inner spec (optional) rule = ListOf(IsA(int, default=123)) | Equals(None) # optional missing list with required item(s) assert rule.get_default_for(None) == None # optional empty list with required item(s) assert rule.get_default_for([]) == [] ## present non-empty inner spec (optional) elem_spec = IsA(int, default=123) | Equals(None) rule = ListOf(elem_spec) | Equals(None) # optional missing list with optional item(s) assert rule.get_default_for(None) == None # optional empty list with optional item assert rule.get_default_for([]) == [] ## present non-empty inner spec (required) rule = ListOf(IsA(int, default=123)) # required missing list → inner spec assert rule.get_default_for(None) == [] # required empty list → inner spec assert rule.get_default_for([]) == [] # required non-empty list → inner spec # fallback = lambda s, v, **kw: v assert rule.get_default_for([None]) == [None] assert rule.get_default_for([456]) == [456] ## present inner spec with empty item spec rule = ListOf(Anything()) assert rule.get_default_for([456]) == [456] ## present inner spec with item spec that has an inner spec #rule = Rule(datatype=list, inner_spec=[123]) rule = ListOf(ListOf(IsA(int, default=123))) # XXX CHANGED WTF was it before!? ##assert rule.get_default_for([None]) == [123] #assert rule.get_default_for([[]]) == [[123]] # bogus value; will not pass validation but should be preserved assert rule.get_default_for(123) == 123
def test_translate_none(): assert translate(None) == Anything()