1
# Copyright 2012, 2013 Canonical Ltd. This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
4
"""Test maasserver model managers."""
6
from __future__ import (
15
from maasserver.testing.testcase import MAASServerTestCase
16
from maasserver.tests.models import (
17
BulkManagerParentTestModel,
20
from maastesting.djangotestcase import TestModelMixin
23
class BulkManagerTest(TestModelMixin, MAASServerTestCase):
25
app = 'maasserver.tests'
27
def test_manager_iterator_uses_cache(self):
30
parents.add(BulkManagerParentTestModel.objects.create())
32
for parent in parents:
33
BulkManagerTestModel.objects.create(parent=parent)
34
parents = BulkManagerParentTestModel.objects.all().prefetch_related(
35
'bulkmanagertestmodel_set')
36
# Only two queries are used to fetch all the objects:
37
# One to fetch the parents, one to fetch the childrens (the query from
38
# the prefetch_related statement).
39
# Even if we call iterator() on the related objects, the cache is
40
# used because BulkManagerTestModel has a manager based on
42
self.assertNumQueries(
44
lambda: [list(parent.bulkmanagertestmodel_set.iterator())
45
for parent in parents])