'Make sure we don\'t perform unnecessary db operations when
none of document\'s fields were updated.'

def test_get_changed_fields_query_count(self):

    class Person(Document, ):
        name = StringField()
        owns = ListField(ReferenceField('Organization'))
        projects = ListField(ReferenceField('Project'))
    class Organization(Document, ):
        name = StringField()
        owner = ReferenceField(Person)
        employees = ListField(ReferenceField(Person))
    class Project(Document, ):
        name = StringField()
    Person.drop_collection()
    Organization.drop_collection()
    Project.drop_collection()
    r1 = Project(name='r1').save()
    r2 = Project(name='r2').save()
    r3 = Project(name='r3').save()
    p1 = Person(name='p1', projects=[r1, r2]).save()
    p2 = Person(name='p2', projects=[r2, r3]).save()
    o1 = Organization(name='o1', employees=[p1]).save()
    with query_counter() as q:
        self.assertEqual(q, 0)
        org = Organization.objects.get(id=o1.id)
        self.assertEqual(q, 1)
        org._get_changed_fields()
        self.assertEqual(q, 1)
    org = Organization.objects.get(id=o1.id)
    with query_counter() as q:
        org.save()
        self.assertEqual(q, 0)
    org = Organization.objects.get(id=o1.id)
    with query_counter() as q:
        org.save(cascade=False)
        self.assertEqual(q, 0)
    org = Organization.objects.get(id=o1.id)
    with query_counter() as q:
        org.employees.append(p2)
        org.save()
        self.assertEqual(q, 2)
