def test_dict(self): class MySchema(Schema): dict = fields.DictField(attribute='in_mongo_dict') d = DataProxy(MySchema()) d.load({'dict': {'a': 1, 'b': {'c': True}}}) with pytest.raises(KeyError): d.get('in_mongo_dict') assert d.dump() == {'dict': {'a': 1, 'b': {'c': True}}} assert d.get('dict') == {'a': 1, 'b': {'c': True}} assert d.to_mongo() == {'in_mongo_dict': {'a': 1, 'b': {'c': True}}} # Must manually set_dirty to take the changes into account dict_ = d.get('dict') dict_['a'] = 1 assert d.to_mongo(update=True) is None dict_.set_modified() assert d.to_mongo(update=True) == {'$set': {'in_mongo_dict': {'a': 1, 'b': {'c': True}}}} dict_.clear_modified() assert d.to_mongo(update=True) is None d2 = DataProxy(MySchema(), {'dict': {}}) d2.to_mongo() == {'dict': {}} d3 = DataProxy(MySchema()) d3.from_mongo({}) assert isinstance(d3.get('dict'), Dict) assert d3.to_mongo() == {} assert d3.to_mongo(update=True) is None d3.get('dict')['field'] = 'value' assert d3.to_mongo(update=True) is None d3.get('dict').set_modified() assert d3.to_mongo(update=True) == {'$set': {'in_mongo_dict': {'field': 'value'}}} assert d3.to_mongo() == {'in_mongo_dict': {'field': 'value'}}
def test_from_mongo(self): class MySchema(EmbeddedSchema): in_front = fields.IntField(attribute='in_mongo') d = DataProxy(MySchema()) with pytest.raises(KeyError): d.from_mongo({'in_front': 42}) d.from_mongo({'in_mongo': 42}) assert d.get('in_front') == 42
def test_dict(self): class MySchema(Schema): dict = fields.DictField(attribute='in_mongo_dict') d = DataProxy(MySchema()) d.load({'dict': {'a': 1, 'b': {'c': True}}}) with pytest.raises(KeyError): d.get('in_mongo_dict') assert d.dump() == {'dict': {'a': 1, 'b': {'c': True}}} assert d.get('dict') == {'a': 1, 'b': {'c': True}} assert d.to_mongo() == {'in_mongo_dict': {'a': 1, 'b': {'c': True}}} # Must manually set_dirty to take the changes into account dict_ = d.get('dict') dict_['a'] = 1 assert d.to_mongo(update=True) is None dict_.set_modified() assert d.to_mongo(update=True) == { '$set': { 'in_mongo_dict': { 'a': 1, 'b': { 'c': True } } } } dict_.clear_modified() assert d.to_mongo(update=True) is None d2 = DataProxy(MySchema(), {'dict': {}}) d2.to_mongo() == {'dict': {}} d3 = DataProxy(MySchema()) d3.from_mongo({}) assert isinstance(d3.get('dict'), Dict) assert d3.to_mongo() == {} assert d3.to_mongo(update=True) is None d3.get('dict')['field'] = 'value' assert d3.to_mongo(update=True) is None d3.get('dict').set_modified() assert d3.to_mongo(update=True) == { '$set': { 'in_mongo_dict': { 'field': 'value' } } } assert d3.to_mongo() == {'in_mongo_dict': {'field': 'value'}}
def test_list(self): class MySchema(Schema): list = fields.ListField(fields.IntField(), attribute='in_mongo_list') d = DataProxy(MySchema()) assert d.to_mongo() == {} d.load({'list': [1, 2, 3]}) assert d.dump() == {'list': [1, 2, 3]} assert d.to_mongo() == {'in_mongo_list': [1, 2, 3]} assert d.get('list') == [1, 2, 3] d.get('list').append(4) assert d.to_mongo(update=True) == {'$set': {'in_mongo_list': [1, 2, 3, 4]}} d.clear_modified() d.set('list', [5, 6, 7]) assert d.dump() == {'list': [5, 6, 7]} assert d.to_mongo(update=True) == {'$set': {'in_mongo_list': [5, 6, 7]}} d.clear_modified() d.get('list').pop() assert d.dump() == {'list': [5, 6]} assert d.to_mongo(update=True) == {'$set': {'in_mongo_list': [5, 6]}} d.clear_modified() d.get('list').clear() assert d.dump() == {'list': []} assert d.to_mongo(update=True) == {'$unset': ['in_mongo_list']} d.set('list', [1, 2, 3]) d.clear_modified() d.get('list').remove(1) assert d.dump() == {'list': [2, 3]} assert d.to_mongo(update=True) == {'$set': {'in_mongo_list': [2, 3]}} d.clear_modified() d.get('list').reverse() assert d.dump() == {'list': [3, 2]} assert d.to_mongo(update=True) == {'$set': {'in_mongo_list': [3, 2]}} d.clear_modified() d.get('list').sort() assert d.dump() == {'list': [2, 3]} assert d.to_mongo(update=True) == {'$set': {'in_mongo_list': [2, 3]}} d.clear_modified() d.get('list').extend([4, 5]) assert d.dump() == {'list': [2, 3, 4, 5]} assert d.to_mongo(update=True) == {'$set': {'in_mongo_list': [2, 3, 4, 5]}} d2 = DataProxy(MySchema()) d2.from_mongo({}) assert isinstance(d2.get('list'), List) assert d2.to_mongo() == {} d2.get('list').append(1) assert d2.to_mongo() == {'in_mongo_list': [1]} assert d2.to_mongo(update=True) == {'$set': {'in_mongo_list': [1]}} # Test repr readability repr_d = repr(d.get('list')) assert repr_d == "<object umongo.data_objects.List([2, 3, 4, 5])>"
def test_embedded_document(self): class MyEmbeddedDocument(EmbeddedDocument): a = fields.IntField(attribute='in_mongo_a') b = fields.IntField() class MySchema(Schema): embedded = fields.EmbeddedField(MyEmbeddedDocument, attribute='in_mongo_embedded') # Make sure embedded document doesn't have implicit _id field assert '_id' not in MyEmbeddedDocument.Schema().fields assert 'id' not in MyEmbeddedDocument.Schema().fields d = DataProxy(MySchema()) d.load(data={'embedded': {'a': 1, 'b': 2}}) assert d.dump() == {'embedded': {'a': 1, 'b': 2}} embedded = d.get('embedded') assert type(embedded) == MyEmbeddedDocument assert embedded.a == 1 assert embedded.b == 2 assert embedded.dump() == {'a': 1, 'b': 2} assert embedded.to_mongo() == {'in_mongo_a': 1, 'b': 2} assert d.to_mongo() == {'in_mongo_embedded': {'in_mongo_a': 1, 'b': 2}} d2 = DataProxy(MySchema()) d2.from_mongo(data={'in_mongo_embedded': {'in_mongo_a': 1, 'b': 2}}) assert d == d2 embedded.a = 3 assert embedded.to_mongo(update=True) == {'$set': {'in_mongo_a': 3}} assert d.to_mongo(update=True) == {'$set': {'in_mongo_embedded': {'in_mongo_a': 3, 'b': 2}}} embedded.clear_modified() assert embedded.to_mongo(update=True) == None assert d.to_mongo(update=True) == None del embedded.a assert embedded.to_mongo(update=True) == {'$unset': ['in_mongo_a']} assert d.to_mongo(update=True) == {'$set': {'in_mongo_embedded': {'b': 2}}} d.set('embedded', MyEmbeddedDocument(a=4)) assert d.get('embedded').to_mongo(update=True) is None assert d.to_mongo(update=True) == {'$set': {'in_mongo_embedded': {'in_mongo_a': 4}}} embedded_doc = MyEmbeddedDocument(a=1, b=2) assert embedded_doc.a == 1 assert embedded_doc.b == 2 assert embedded_doc == {'in_mongo_a': 1, 'b': 2} assert embedded_doc == MyEmbeddedDocument(a=1, b=2) assert embedded_doc['a'] == 1 assert embedded_doc['b'] == 2 with pytest.raises(ValidationError): MyEmbeddedDocument(in_mongo_a=1, b=2) embedded_doc['a'] = 1 assert embedded_doc.a == embedded_doc['a'] == 1 del embedded_doc['a'] assert embedded_doc.a == embedded_doc['a'] == None # Test repr readability repr_d = repr(MyEmbeddedDocument(a=1, b=2)) assert 'tests.test_fields.MyEmbeddedDocument' in repr_d assert "'in_mongo_a': 1" in repr_d assert "'b': 2" in repr_d
def test_list(self): class MySchema(Schema): list = fields.ListField(fields.IntField(), attribute='in_mongo_list') d = DataProxy(MySchema()) assert d.to_mongo() == {} d.load({'list': [1, 2, 3]}) assert d.dump() == {'list': [1, 2, 3]} assert d.to_mongo() == {'in_mongo_list': [1, 2, 3]} assert d.get('list') == [1, 2, 3] d.get('list').append(4) assert d.to_mongo(update=True) == { '$set': { 'in_mongo_list': [1, 2, 3, 4] } } d.clear_modified() d.set('list', [5, 6, 7]) assert d.dump() == {'list': [5, 6, 7]} assert d.to_mongo(update=True) == { '$set': { 'in_mongo_list': [5, 6, 7] } } d.clear_modified() d.get('list').pop() assert d.dump() == {'list': [5, 6]} assert d.to_mongo(update=True) == {'$set': {'in_mongo_list': [5, 6]}} d.clear_modified() d.get('list').clear() assert d.dump() == {'list': []} assert d.to_mongo(update=True) == {'$unset': ['in_mongo_list']} d.set('list', [1, 2, 3]) d.clear_modified() d.get('list').remove(1) assert d.dump() == {'list': [2, 3]} assert d.to_mongo(update=True) == {'$set': {'in_mongo_list': [2, 3]}} d.clear_modified() d.get('list').reverse() assert d.dump() == {'list': [3, 2]} assert d.to_mongo(update=True) == {'$set': {'in_mongo_list': [3, 2]}} d.clear_modified() d.get('list').sort() assert d.dump() == {'list': [2, 3]} assert d.to_mongo(update=True) == {'$set': {'in_mongo_list': [2, 3]}} d.clear_modified() d.get('list').extend([4, 5]) assert d.dump() == {'list': [2, 3, 4, 5]} assert d.to_mongo(update=True) == { '$set': { 'in_mongo_list': [2, 3, 4, 5] } } d2 = DataProxy(MySchema()) d2.from_mongo({}) assert isinstance(d2.get('list'), List) assert d2.to_mongo() == {} d2.get('list').append(1) assert d2.to_mongo() == {'in_mongo_list': [1]} assert d2.to_mongo(update=True) == {'$set': {'in_mongo_list': [1]}} # Test repr readability repr_d = repr(d.get('list')) assert repr_d == "<object umongo.data_objects.List([2, 3, 4, 5])>"
def test_embedded_document(self): class MyEmbeddedDocument(EmbeddedDocument): a = fields.IntField(attribute='in_mongo_a') b = fields.IntField() class MySchema(Schema): embedded = fields.EmbeddedField(MyEmbeddedDocument, attribute='in_mongo_embedded') # Make sure embedded document doesn't have implicit _id field assert '_id' not in MyEmbeddedDocument.Schema().fields assert 'id' not in MyEmbeddedDocument.Schema().fields d = DataProxy(MySchema()) d.load(data={'embedded': {'a': 1, 'b': 2}}) assert d.dump() == {'embedded': {'a': 1, 'b': 2}} embedded = d.get('embedded') assert type(embedded) == MyEmbeddedDocument assert embedded.a == 1 assert embedded.b == 2 assert embedded.dump() == {'a': 1, 'b': 2} assert embedded.to_mongo() == {'in_mongo_a': 1, 'b': 2} assert d.to_mongo() == {'in_mongo_embedded': {'in_mongo_a': 1, 'b': 2}} d2 = DataProxy(MySchema()) d2.from_mongo(data={'in_mongo_embedded': {'in_mongo_a': 1, 'b': 2}}) assert d == d2 embedded.a = 3 assert embedded.to_mongo(update=True) == {'$set': {'in_mongo_a': 3}} assert d.to_mongo(update=True) == { '$set': { 'in_mongo_embedded': { 'in_mongo_a': 3, 'b': 2 } } } embedded.clear_modified() assert embedded.to_mongo(update=True) == None assert d.to_mongo(update=True) == None del embedded.a assert embedded.to_mongo(update=True) == {'$unset': ['in_mongo_a']} assert d.to_mongo(update=True) == { '$set': { 'in_mongo_embedded': { 'b': 2 } } } d.set('embedded', MyEmbeddedDocument(a=4)) assert d.get('embedded').to_mongo(update=True) is None assert d.to_mongo(update=True) == { '$set': { 'in_mongo_embedded': { 'in_mongo_a': 4 } } } embedded_doc = MyEmbeddedDocument(a=1, b=2) assert embedded_doc.a == 1 assert embedded_doc.b == 2 assert embedded_doc == {'in_mongo_a': 1, 'b': 2} assert embedded_doc == MyEmbeddedDocument(a=1, b=2) assert embedded_doc['a'] == 1 assert embedded_doc['b'] == 2 with pytest.raises(ValidationError): MyEmbeddedDocument(in_mongo_a=1, b=2) embedded_doc['a'] = 1 assert embedded_doc.a == embedded_doc['a'] == 1 del embedded_doc['a'] assert embedded_doc.a == embedded_doc['a'] == None # Test repr readability repr_d = repr(MyEmbeddedDocument(a=1, b=2)) assert 'tests.test_fields.MyEmbeddedDocument' in repr_d assert "'in_mongo_a': 1" in repr_d assert "'b': 2" in repr_d
def test_embedded_document(self): @self.instance.register class MyEmbeddedDocument(EmbeddedDocument): a = fields.IntField(attribute='in_mongo_a') b = fields.IntField() @self.instance.register class MyDoc(Document): embedded = fields.EmbeddedField(MyEmbeddedDocument, attribute='in_mongo_embedded') MySchema = MyDoc.Schema modified_schema = MyDoc.Schema(load_only=('embedded', )) modified_embedded_schema = MyEmbeddedDocument.Schema(load_only=('a', )) # Make sure embedded document doesn't have implicit _id field assert '_id' not in MyEmbeddedDocument.Schema().fields assert 'id' not in MyEmbeddedDocument.Schema().fields d = DataProxy(MySchema()) d.load(data={'embedded': {'a': 1, 'b': 2}}) assert d.dump() == {'embedded': {'a': 1, 'b': 2}} assert d.dump(schema=modified_schema) == {} embedded = d.get('embedded') assert type(embedded) == MyEmbeddedDocument assert embedded.a == 1 assert embedded.b == 2 assert embedded.dump() == {'a': 1, 'b': 2} assert embedded.dump(schema=modified_embedded_schema) == {'b': 2} assert embedded.to_mongo() == {'in_mongo_a': 1, 'b': 2} assert d.to_mongo() == {'in_mongo_embedded': {'in_mongo_a': 1, 'b': 2}} d2 = DataProxy(MySchema()) d2.from_mongo(data={'in_mongo_embedded': {'in_mongo_a': 1, 'b': 2}}) assert d == d2 embedded.a = 3 assert embedded.to_mongo(update=True) == {'$set': {'in_mongo_a': 3}} assert d.to_mongo(update=True) == { '$set': { 'in_mongo_embedded': { 'in_mongo_a': 3, 'b': 2 } } } embedded.clear_modified() assert embedded.to_mongo(update=True) is None assert d.to_mongo(update=True) is None del embedded.a assert embedded.to_mongo(update=True) == {'$unset': {'in_mongo_a': ''}} assert d.to_mongo(update=True) == { '$set': { 'in_mongo_embedded': { 'b': 2 } } } d.set('embedded', MyEmbeddedDocument(a=4)) assert d.get('embedded').to_mongo(update=True) is None assert d.to_mongo(update=True) == { '$set': { 'in_mongo_embedded': { 'in_mongo_a': 4 } } } embedded_doc = MyEmbeddedDocument(a=1, b=2) assert embedded_doc.a == 1 assert embedded_doc.b == 2 assert embedded_doc == {'in_mongo_a': 1, 'b': 2} assert embedded_doc == MyEmbeddedDocument(a=1, b=2) assert embedded_doc['a'] == 1 assert embedded_doc['b'] == 2 with pytest.raises(ValidationError): MyEmbeddedDocument(in_mongo_a=1, b=2) embedded_doc['a'] = 1 assert embedded_doc.a == embedded_doc['a'] == 1 del embedded_doc['a'] assert embedded_doc.a is embedded_doc['a'] is None # Test repr readability repr_d = repr(MyEmbeddedDocument(a=1, b=2)) assert 'tests.test_fields.MyEmbeddedDocument' in repr_d assert "'in_mongo_a': 1" in repr_d assert "'b': 2" in repr_d # Test unknown fields with pytest.raises(AttributeError): embedded_doc.dummy with pytest.raises(AttributeError): embedded_doc.dummy = None with pytest.raises(AttributeError): del embedded_doc.dummy with pytest.raises(KeyError): embedded_doc['dummy'] with pytest.raises(KeyError): embedded_doc['dummy'] = None with pytest.raises(KeyError): del embedded_doc['dummy']
def test_partial(self): class MySchema(EmbeddedSchema): with_default = fields.StrField(default='default_value') with_missing = fields.StrField(missing='missing_value') normal = fields.StrField() loaded = fields.StrField() loaded_but_empty = fields.StrField() d = DataProxy(MySchema()) d.from_mongo({ 'loaded': "foo", 'loaded_but_empty': missing }, partial=True) assert d.partial is True for field in ('with_default', 'with_missing', 'normal'): with pytest.raises(exceptions.FieldNotLoadedError): d.get(field) with pytest.raises(exceptions.FieldNotLoadedError): d.set(field, "test") with pytest.raises(exceptions.FieldNotLoadedError): d.delete(field) assert d.get('loaded') == "foo" assert d.get('loaded_but_empty') is missing d.set('loaded_but_empty', "bar") assert d.get('loaded_but_empty') == "bar" d.delete('loaded') # Can still access the deleted field assert d.get('loaded') is missing # Same test, but using `load` d = DataProxy(MySchema()) d.load({'loaded': "foo", 'loaded_but_empty': missing}, partial=True) assert d.partial is True for field in ('with_default', 'with_missing', 'normal'): with pytest.raises(exceptions.FieldNotLoadedError): d.get(field) with pytest.raises(exceptions.FieldNotLoadedError): d.set(field, "test") with pytest.raises(exceptions.FieldNotLoadedError): d.delete(field) assert d.get('loaded') == "foo" assert d.get('loaded_but_empty') is missing d.set('loaded_but_empty', "bar") assert d.get('loaded_but_empty') == "bar" d.delete('loaded') # Can still access the deleted field assert d.get('loaded') is missing # Not partial d = DataProxy(MySchema()) d.from_mongo({'loaded': "foo", 'loaded_but_empty': missing}) assert d.partial is False assert d.get('with_default') == 'default_value' assert d.get('with_missing') == 'missing_value' assert d.get('normal') is missing assert d.get('loaded') == "foo" assert d.get('loaded_but_empty') == missing # Same test with load d = DataProxy(MySchema()) d.load({'loaded': "foo", 'loaded_but_empty': missing}) assert d.partial is False assert d.partial is False assert d.get('with_default') == 'default_value' assert d.get('with_missing') == 'missing_value' assert d.get('normal') is missing assert d.get('loaded') == "foo" assert d.get('loaded_but_empty') == missing