'Test complex dynamic embedded documents setups'

def test_complex_embedded_documents(self):

    class Embedded(DynamicEmbeddedDocument, ):
        pass
    class Doc(DynamicDocument, ):
        pass
    Doc.drop_collection()
    doc = Doc()
    embedded_1 = Embedded()
    embedded_1.string_field = 'hello'
    embedded_1.int_field = 1
    embedded_1.dict_field = {'hello': 'world'}
    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'}]
    embedded_1.list_field = ['1', 2, embedded_2]
    doc.embedded_field = embedded_1
    self.assertEqual(doc.to_mongo(), {'embedded_field': {'_cls': 'Embedded', 'string_field': 'hello', 'int_field': 1, 'dict_field': {'hello': 'world'}, 'list_field': ['1', 2, {'_cls': 'Embedded', 'string_field': 'hello', 'int_field': 1, 'dict_field': {'hello': 'world'}, 'list_field': ['1', 2, {'hello': 'world'}]}]}})
    doc.save()
    doc = Doc.objects.first()
    self.assertEqual(doc.embedded_field.__class__, Embedded)
    self.assertEqual(doc.embedded_field.string_field, 'hello')
    self.assertEqual(doc.embedded_field.int_field, 1)
    self.assertEqual(doc.embedded_field.dict_field, {'hello': 'world'})
    self.assertEqual(doc.embedded_field.list_field[0], '1')
    self.assertEqual(doc.embedded_field.list_field[1], 2)
    embedded_field = doc.embedded_field.list_field[2]
    self.assertEqual(embedded_field.__class__, Embedded)
    self.assertEqual(embedded_field.string_field, 'hello')
    self.assertEqual(embedded_field.int_field, 1)
    self.assertEqual(embedded_field.dict_field, {'hello': 'world'})
    self.assertEqual(embedded_field.list_field, ['1', 2, {'hello': 'world'}])
