def test_iter_defaults(): """It doesn't yield keys from defaults.""" this = ODD(a=1) that = ODD(b=2) this.defaults = dict(c=3) that.defaults = dict(d=4) a = DictTree(this, that) assert list(sorted(a)) == ['a', 'b']
def test_defaults_getitem_dict(): """It returns defaults if a value isn't set.""" this = ODD() that = ODD(a=2) both = DictTree(this, that) # default in this gets overridden by value in that this.defaults_ = dict(a=1) that.defaults_ = dict() assert both['a'] == 2 # default in first dict gets priority this.defaults_ = dict(b=1) that.defaults_ = dict(b=2) assert both['b'] == 1
def test_defaults_getitem_nested(): """It preserves the defaults_ behaviour of ODD in nested dicts.""" this = ODD() that = ODD(a=2) both = DictTree(dict(a=this), dict(a=that)) # default in this gets overridden by value in that this.defaults_ = dict(a=1) that.defaults_ = dict() assert both['a']['a'] == 2 # default in first dict gets priority this.defaults_ = dict(b=1) that.defaults_ = dict(b=2) assert both['a']['b'] == 1
def test_odd_getitem(): """It returns default values if none of the dicts contains the key.""" this = ODD(a=1) this.defaults_ = dict(c=3) that = ODD(b=2) that.defaults_ = dict(d=4) both = DictTree(this, that) # key from this assert both['a'] == 1 # key from that assert both['b'] == 2 # default from this assert both['c'] == 3 # default from that assert both['d'] == 4 # missing value with pytest.raises(KeyError): both['e']