def test_access_by_mongo_name(self): class MySchema(EmbeddedSchema): a = fields.IntField() b = fields.IntField(attribute='in_mongo_b') d = DataProxy(MySchema(), data={'a': 1, 'b': 2}) assert d.get_by_mongo_name('in_mongo_b') == 2 assert d.get_by_mongo_name('a') == 1 with pytest.raises(KeyError): d.get_by_mongo_name('b') d.set_by_mongo_name('in_mongo_b', 3) assert d.to_mongo(update=True) == {'$set': {'in_mongo_b': 3}} assert d.get_by_mongo_name('in_mongo_b') == 3 d.delete_by_mongo_name('in_mongo_b') assert d.to_mongo(update=True) == {'$unset': {'in_mongo_b': ''}}
def test_access_by_mongo_name(self): class MySchema(EmbeddedSchema): a = fields.IntField() b = fields.IntField(attribute='in_mongo_b') d = DataProxy(MySchema(), data={'a': 1, 'b': 2}) assert d.get_by_mongo_name('in_mongo_b') == 2 assert d.get_by_mongo_name('a') == 1 with pytest.raises(KeyError): d.get_by_mongo_name('b') d.set_by_mongo_name('in_mongo_b', 3) assert d.to_mongo(update=True) == {'$set': {'in_mongo_b': 3}} assert d.get_by_mongo_name('in_mongo_b') == 3 d.delete_by_mongo_name('in_mongo_b') assert d.to_mongo(update=True) == {'$unset': ['in_mongo_b']}
def test_set_to_missing_fields(self): class MySchema(EmbeddedSchema): a = fields.IntField() b = fields.IntField(attribute='in_mongo_b') d = DataProxy(MySchema(), data={'a': 1}) assert d.get('b') is missing assert d.get_by_mongo_name('in_mongo_b') is missing assert d._data['in_mongo_b'] is missing d.set('b', 2) assert d.get('b') == 2 d.delete('b') # Can do it two time in a row without error d.delete('b') assert d._data['in_mongo_b'] is missing
def test_set_to_missing_fields(self): class MySchema(EmbeddedSchema): a = fields.IntField() b = fields.IntField(attribute='in_mongo_b') d = DataProxy(MySchema(), data={'a': 1}) assert d.get('b') is None assert d.get_by_mongo_name('in_mongo_b') is None assert d._data['in_mongo_b'] is missing with pytest.raises(KeyError): d.delete('b') d.set('b', 2) assert d.get('b') == 2 d.delete('b') assert d._data['in_mongo_b'] is missing