'Ensures reloading updates weakrefs correctly.'

def test_reload_referencing(self):

    class Embedded(EmbeddedDocument, ):
        dict_field = DictField()
        list_field = ListField()
    class Doc(Document, ):
        dict_field = DictField()
        list_field = ListField()
        embedded_field = EmbeddedDocumentField(Embedded)
    Doc.drop_collection()
    doc = Doc()
    doc.dict_field = {'hello': 'world'}
    doc.list_field = ['1', 2, {'hello': 'world'}]
    embedded_1 = Embedded()
    embedded_1.dict_field = {'hello': 'world'}
    embedded_1.list_field = ['1', 2, {'hello': 'world'}]
    doc.embedded_field = embedded_1
    doc.save()
    doc = doc.reload(10)
    doc.list_field.append(1)
    doc.dict_field['woot'] = 'woot'
    doc.embedded_field.list_field.append(1)
    doc.embedded_field.dict_field['woot'] = 'woot'
    self.assertEqual(doc._get_changed_fields(), ['list_field', 'dict_field.woot', 'embedded_field.list_field', 'embedded_field.dict_field.woot'])
    doc.save()
    self.assertEqual(len(doc.list_field), 4)
    doc = doc.reload(10)
    self.assertEqual(doc._get_changed_fields(), [])
    self.assertEqual(len(doc.list_field), 4)
    self.assertEqual(len(doc.dict_field), 2)
    self.assertEqual(len(doc.embedded_field.list_field), 4)
    self.assertEqual(len(doc.embedded_field.dict_field), 2)
    doc.list_field.append(1)
    doc.save()
    doc.dict_field['extra'] = 1
    doc = doc.reload(10, 'list_field')
    self.assertEqual(doc._get_changed_fields(), [])
    self.assertEqual(len(doc.list_field), 5)
    self.assertEqual(len(doc.dict_field), 3)
    self.assertEqual(len(doc.embedded_field.list_field), 4)
    self.assertEqual(len(doc.embedded_field.dict_field), 2)
