MCPcopy Create free account
hub / github.com/MongoEngine/mongoengine / delta_db_field

Method delta_db_field

tests/document/test_delta.py:369–430  ·  view source on GitHub ↗
(self, DocClass)

Source from the content-addressed store, hash-verified

367 self.delta_db_field(DynamicDocument)
368
369 def delta_db_field(self, DocClass):
370 class Doc(DocClass):
371 string_field = StringField(db_field="db_string_field")
372 int_field = IntField(db_field="db_int_field")
373 dict_field = DictField(db_field="db_dict_field")
374 list_field = ListField(db_field="db_list_field")
375
376 Doc.drop_collection()
377 doc = Doc()
378 doc.save()
379
380 doc = Doc.objects.first()
381 assert doc._get_changed_fields() == []
382 assert doc._delta() == ({}, {})
383
384 doc.string_field = "hello"
385 assert doc._get_changed_fields() == ["db_string_field"]
386 assert doc._delta() == ({"db_string_field": "hello"}, {})
387
388 doc._changed_fields = []
389 doc.int_field = 1
390 assert doc._get_changed_fields() == ["db_int_field"]
391 assert doc._delta() == ({"db_int_field": 1}, {})
392
393 doc._changed_fields = []
394 dict_value = {"hello": "world", "ping": "pong"}
395 doc.dict_field = dict_value
396 assert doc._get_changed_fields() == ["db_dict_field"]
397 assert doc._delta() == ({"db_dict_field": dict_value}, {})
398
399 doc._changed_fields = []
400 list_value = ["1", 2, {"hello": "world"}]
401 doc.list_field = list_value
402 assert doc._get_changed_fields() == ["db_list_field"]
403 assert doc._delta() == ({"db_list_field": list_value}, {})
404
405 # Test unsetting
406 doc._changed_fields = []
407 doc.dict_field = {}
408 assert doc._get_changed_fields() == ["db_dict_field"]
409 assert doc._delta() == ({}, {"db_dict_field": 1})
410
411 doc._changed_fields = []
412 doc.list_field = []
413 assert doc._get_changed_fields() == ["db_list_field"]
414 assert doc._delta() == ({}, {"db_list_field": 1})
415
416 # Test it saves that data
417 doc = Doc()
418 doc.save()
419
420 doc.string_field = "hello"
421 doc.int_field = 1
422 doc.dict_field = {"hello": "world"}
423 doc.list_field = ["1", 2, {"hello": "world"}]
424 doc.save()
425 doc = doc.reload(10)
426

Callers 1

test_delta_db_fieldMethod · 0.95

Calls 7

drop_collectionMethod · 0.80
_get_changed_fieldsMethod · 0.80
_deltaMethod · 0.80
reloadMethod · 0.80
DocClass · 0.70
saveMethod · 0.45
firstMethod · 0.45

Tested by

no test coverage detected