

def delta_db_field(self, DocClass):

    class Doc(DocClass):
        string_field = StringField(db_field='db_string_field')
        int_field = IntField(db_field='db_int_field')
        dict_field = DictField(db_field='db_dict_field')
        list_field = ListField(db_field='db_list_field')
    Doc.drop_collection()
    doc = Doc()
    doc.save()
    doc = Doc.objects.first()
    self.assertEqual(doc._get_changed_fields(), [])
    self.assertEqual(doc._delta(), ({
        
    }, {
        
    }))
    doc.string_field = 'hello'
    self.assertEqual(doc._get_changed_fields(), ['db_string_field'])
    self.assertEqual(doc._delta(), ({
        'db_string_field': 'hello',
    }, {
        
    }))
    doc._changed_fields = []
    doc.int_field = 1
    self.assertEqual(doc._get_changed_fields(), ['db_int_field'])
    self.assertEqual(doc._delta(), ({
        'db_int_field': 1,
    }, {
        
    }))
    doc._changed_fields = []
    dict_value = {
        'hello': 'world',
        'ping': 'pong',
    }
    doc.dict_field = dict_value
    self.assertEqual(doc._get_changed_fields(), ['db_dict_field'])
    self.assertEqual(doc._delta(), ({
        'db_dict_field': dict_value,
    }, {
        
    }))
    doc._changed_fields = []
    list_value = ['1', 2, {
        'hello': 'world',
    }]
    doc.list_field = list_value
    self.assertEqual(doc._get_changed_fields(), ['db_list_field'])
    self.assertEqual(doc._delta(), ({
        'db_list_field': list_value,
    }, {
        
    }))
    doc._changed_fields = []
    doc.dict_field = {
        
    }
    self.assertEqual(doc._get_changed_fields(), ['db_dict_field'])
    self.assertEqual(doc._delta(), ({
        
    }, {
        'db_dict_field': 1,
    }))
    doc._changed_fields = []
    doc.list_field = []
    self.assertEqual(doc._get_changed_fields(), ['db_list_field'])
    self.assertEqual(doc._delta(), ({
        
    }, {
        'db_list_field': 1,
    }))
    doc = Doc()
    doc.save()
    doc.string_field = 'hello'
    doc.int_field = 1
    doc.dict_field = {
        'hello': 'world',
    }
    doc.list_field = ['1', 2, {
        'hello': 'world',
    }]
    doc.save()
    doc = doc.reload(10)
    self.assertEqual(doc.string_field, 'hello')
    self.assertEqual(doc.int_field, 1)
    self.assertEqual(doc.dict_field, {
        'hello': 'world',
    })
    self.assertEqual(doc.list_field, ['1', 2, {
        'hello': 'world',
    }])

dataset/ETHPy150Open MongoEngine/mongoengine/tests/document/delta.py DeltaTest.delta_db_field/original