'Ensure that ReferenceFields can reference their own documents.'

def test_recursive_reference(self):

    class Employee(Document, ):
        name = StringField()
        boss = ReferenceField('self')
        friends = ListField(ReferenceField('self'))
    Employee.drop_collection()
    bill = Employee(name='Bill    Lumbergh')
    bill.save()
    michael = Employee(name='Michael    Bolton')
    michael.save()
    samir = Employee(name='Samir    Nagheenanajar')
    samir.save()
    friends = [michael, samir]
    peter = Employee(name='Peter    Gibbons', boss=bill, friends=friends)
    peter.save()
    Employee(name='Funky    Gibbon', boss=bill, friends=friends).save()
    Employee(name='Funky    Gibbon', boss=bill, friends=friends).save()
    Employee(name='Funky    Gibbon', boss=bill, friends=friends).save()
    with query_counter() as q:
        self.assertEqual(q, 0)
        peter = Employee.objects.with_id(peter.id)
        self.assertEqual(q, 1)
        peter.boss
        self.assertEqual(q, 2)
        peter.friends
        self.assertEqual(q, 3)
    with query_counter() as q:
        self.assertEqual(q, 0)
        peter = Employee.objects.with_id(peter.id).select_related()
        self.assertEqual(q, 2)
        self.assertEqual(peter.boss, bill)
        self.assertEqual(q, 2)
        self.assertEqual(peter.friends, friends)
        self.assertEqual(q, 2)
    with query_counter() as q:
        self.assertEqual(q, 0)
        employees = Employee.objects(boss=bill).select_related()
        self.assertEqual(q, 2)
        for employee in employees:
            self.assertEqual(employee.boss, bill)
            self.assertEqual(q, 2)
            self.assertEqual(employee.friends, friends)
            self.assertEqual(q, 2)
