

def delta_recursive(self, DocClass, EmbeddedClass):

    class Embedded(EmbeddedClass):
        id = StringField()
        string_field = StringField()
        int_field = IntField()
        dict_field = DictField()
        list_field = ListField()

    class Doc(DocClass):
        string_field = StringField()
        int_field = IntField()
        dict_field = DictField()
        list_field = ListField()
        embedded_field = EmbeddedDocumentField(Embedded)
    Doc.drop_collection()
    doc = Doc()
    doc.save()
    doc = Doc.objects.first()
    self.assertEqual(doc._get_changed_fields(), [])
    self.assertEqual(doc._delta(), ({
        
    }, {
        
    }))
    embedded_1 = Embedded()
    embedded_1.id = '010101'
    embedded_1.string_field = 'hello'
    embedded_1.int_field = 1
    embedded_1.dict_field = {
        'hello': 'world',
    }
    embedded_1.list_field = ['1', 2, {
        'hello': 'world',
    }]
    doc.embedded_field = embedded_1
    self.assertEqual(doc._get_changed_fields(), ['embedded_field'])
    embedded_delta = {
        'id': '010101',
        'string_field': 'hello',
        'int_field': 1,
        'dict_field': {
            'hello': 'world',
        },
        'list_field': ['1', 2, {
            'hello': 'world',
        }],
    }
    self.assertEqual(doc.embedded_field._delta(), (embedded_delta, {
        
    }))
    self.assertEqual(doc._delta(), ({
        'embedded_field': embedded_delta,
    }, {
        
    }))
    doc.save()
    doc = doc.reload(10)
    doc.embedded_field.dict_field = {
        
    }
    self.assertEqual(doc._get_changed_fields(), ['embedded_field.dict_field'])
    self.assertEqual(doc.embedded_field._delta(), ({
        
    }, {
        'dict_field': 1,
    }))
    self.assertEqual(doc._delta(), ({
        
    }, {
        'embedded_field.dict_field': 1,
    }))
    doc.save()
    doc = doc.reload(10)
    self.assertEqual(doc.embedded_field.dict_field, {
        
    })
    doc.embedded_field.list_field = []
    self.assertEqual(doc._get_changed_fields(), ['embedded_field.list_field'])
    self.assertEqual(doc.embedded_field._delta(), ({
        
    }, {
        'list_field': 1,
    }))
    self.assertEqual(doc._delta(), ({
        
    }, {
        'embedded_field.list_field': 1,
    }))
    doc.save()
    doc = doc.reload(10)
    self.assertEqual(doc.embedded_field.list_field, [])
    embedded_2 = Embedded()
    embedded_2.string_field = 'hello'
    embedded_2.int_field = 1
    embedded_2.dict_field = {
        'hello': 'world',
    }
    embedded_2.list_field = ['1', 2, {
        'hello': 'world',
    }]
    doc.embedded_field.list_field = ['1', 2, embedded_2]
    self.assertEqual(doc._get_changed_fields(), ['embedded_field.list_field'])
    self.assertEqual(doc.embedded_field._delta(), ({
        'list_field': ['1', 2, {
            '_cls': 'Embedded',
            'string_field': 'hello',
            'dict_field': {
                'hello': 'world',
            },
            'int_field': 1,
            'list_field': ['1', 2, {
                'hello': 'world',
            }],
        }],
    }, {
        
    }))
    self.assertEqual(doc._delta(), ({
        'embedded_field.list_field': ['1', 2, {
            '_cls': 'Embedded',
            'string_field': 'hello',
            'dict_field': {
                'hello': 'world',
            },
            'int_field': 1,
            'list_field': ['1', 2, {
                'hello': 'world',
            }],
        }],
    }, {
        
    }))
    doc.save()
    doc = doc.reload(10)
    self.assertEqual(doc.embedded_field.list_field[0], '1')
    self.assertEqual(doc.embedded_field.list_field[1], 2)
    for k in doc.embedded_field.list_field[2]._fields:
        self.assertEqual(doc.embedded_field.list_field[2][k], embedded_2[k])
    doc.embedded_field.list_field[2].string_field = 'world'
    self.assertEqual(doc._get_changed_fields(), ['embedded_field.list_field.2.string_field'])
    self.assertEqual(doc.embedded_field._delta(), ({
        'list_field.2.string_field': 'world',
    }, {
        
    }))
    self.assertEqual(doc._delta(), ({
        'embedded_field.list_field.2.string_field': 'world',
    }, {
        
    }))
    doc.save()
    doc = doc.reload(10)
    self.assertEqual(doc.embedded_field.list_field[2].string_field, 'world')
    doc.embedded_field.list_field[2].string_field = 'hello world'
    doc.embedded_field.list_field[2] = doc.embedded_field.list_field[2]
    self.assertEqual(doc._get_changed_fields(), ['embedded_field.list_field.2'])
    self.assertEqual(doc.embedded_field._delta(), ({
        'list_field.2': {
            '_cls': 'Embedded',
            'string_field': 'hello world',
            'int_field': 1,
            'list_field': ['1', 2, {
                'hello': 'world',
            }],
            'dict_field': {
                'hello': 'world',
            },
        },
    }, {
        
    }))
    self.assertEqual(doc._delta(), ({
        'embedded_field.list_field.2': {
            '_cls': 'Embedded',
            'string_field': 'hello world',
            'int_field': 1,
            'list_field': ['1', 2, {
                'hello': 'world',
            }],
            'dict_field': {
                'hello': 'world',
            },
        },
    }, {
        
    }))
    doc.save()
    doc = doc.reload(10)
    self.assertEqual(doc.embedded_field.list_field[2].string_field, 'hello world')
    doc.embedded_field.list_field[2].list_field.pop(0)
    self.assertEqual(doc._delta(), ({
        'embedded_field.list_field.2.list_field': [2, {
            'hello': 'world',
        }],
    }, {
        
    }))
    doc.save()
    doc = doc.reload(10)
    doc.embedded_field.list_field[2].list_field.append(1)
    self.assertEqual(doc._delta(), ({
        'embedded_field.list_field.2.list_field': [2, {
            'hello': 'world',
        }, 1],
    }, {
        
    }))
    doc.save()
    doc = doc.reload(10)
    self.assertEqual(doc.embedded_field.list_field[2].list_field, [2, {
        'hello': 'world',
    }, 1])
    doc.embedded_field.list_field[2].list_field.sort(key=str)
    doc.save()
    doc = doc.reload(10)
    self.assertEqual(doc.embedded_field.list_field[2].list_field, [1, 2, {
        'hello': 'world',
    }])
    del doc.embedded_field.list_field[2].list_field[2]['hello']
    self.assertEqual(doc._delta(), ({
        
    }, {
        'embedded_field.list_field.2.list_field.2.hello': 1,
    }))
    doc.save()
    doc = doc.reload(10)
    del doc.embedded_field.list_field[2].list_field
    self.assertEqual(doc._delta(), ({
        
    }, {
        'embedded_field.list_field.2.list_field': 1,
    }))
    doc.save()
    doc = doc.reload(10)
    doc.dict_field['Embedded'] = embedded_1
    doc.save()
    doc = doc.reload(10)
    doc.dict_field['Embedded'].string_field = 'Hello World'
    self.assertEqual(doc._get_changed_fields(), ['dict_field.Embedded.string_field'])
    self.assertEqual(doc._delta(), ({
        'dict_field.Embedded.string_field': 'Hello World',
    }, {
        
    }))

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