def test_merge_dictof(self): ## present non-empty inner spec (optional) spec = DictOf([ (Equals('a'), IsA(int, default=1)), ]) | Equals(None) # optional missing dictionary with required key assert spec.get_default_for(None) == None # optional empty dictionary with required key assert spec.get_default_for({}) == {'a': 1} ## present non-empty inner spec (optional) spec = DictOf([ (Equals('a'), IsA(int, default=1) | Equals(None)), ]) # optional missing dictionary with optional key # XXX CHANGED #assert spec.get_default_for(None) == None assert spec.get_default_for(None) == {'a': None} # optional empty dictionary with optional key # XXX CHANGED # (if the value can be either int or None, why choose one? # here we set None not because of Equals(None) but because # we *mean* None — no value could be chosen. #assert spec.get_default_for({}) == {'a': 1} assert spec.get_default_for({}) == {'a': None} ## present non-empty inner spec (required) spec = DictOf([ (Equals('a'), IsA(int, default=1)), ]) # required missing dictionary → inner spec assert spec.get_default_for({}) == {'a': 1} # required empty dictionary → inner spec assert spec.get_default_for({}) == {'a': 1} # XXX CHANGED ## required non-empty dictionary → inner spec #fallback = lambda s, v, **kw: v #assert merge_defaults(rule, {'a': 2}, {}, fallback) == {'a': 2} #assert merge_defaults(rule, {'b': 3}, {}, fallback) == {'a': 1, 'b': 3} # bogus value; will not pass validation but should be preserved assert spec.get_default_for(123) == 123
def test_merge_dictof(self): ## present non-empty inner spec (optional) spec = DictOf([(Equals("a"), IsA(int, default=1))]) | Equals(None) # optional missing dictionary with required key assert spec.get_default_for(None) == None # optional empty dictionary with required key assert spec.get_default_for({}) == {"a": 1} ## present non-empty inner spec (optional) spec = DictOf([(Equals("a"), IsA(int, default=1) | Equals(None))]) # optional missing dictionary with optional key # XXX CHANGED # assert spec.get_default_for(None) == None assert spec.get_default_for(None) == {"a": None} # optional empty dictionary with optional key # XXX CHANGED # (if the value can be either int or None, why choose one? # here we set None not because of Equals(None) but because # we *mean* None — no value could be chosen. # assert spec.get_default_for({}) == {'a': 1} assert spec.get_default_for({}) == {"a": None} ## present non-empty inner spec (required) spec = DictOf([(Equals("a"), IsA(int, default=1))]) # required missing dictionary → inner spec assert spec.get_default_for({}) == {"a": 1} # required empty dictionary → inner spec assert spec.get_default_for({}) == {"a": 1} # XXX CHANGED ## required non-empty dictionary → inner spec # fallback = lambda s, v, **kw: v # assert merge_defaults(rule, {'a': 2}, {}, fallback) == {'a': 2} # assert merge_defaults(rule, {'b': 3}, {}, fallback) == {'a': 1, 'b': 3} # bogus value; will not pass validation but should be preserved assert spec.get_default_for(123) == 123
def test_rule_as_key(self): spec_a = DictOf([(IsA(str), IsA(int))]) spec_b = DictOf([(IsA(str) | ~Exists(), IsA(int))]) assert spec_a.get_default_for({}) == {} assert spec_b.get_default_for({}) == {}