~ubuntu-branches/ubuntu/saucy/maas/saucy

« back to all changes in this revision

Viewing changes to src/maasserver/models/tests/test_managers.py

  • Committer: Package Import Robot
  • Author(s): Andres Rodriguez
  • Date: 2013-08-28 11:17:44 UTC
  • mfrom: (1.2.14)
  • Revision ID: package-import@ubuntu.com-20130828111744-a8hqmsmvvc1wnanc
Tags: 1.4+bzr1655+dfsg-0ubuntu1
* New Upstream release. (LP: #1218526)
* debian/control:
  - Depends on python-djorm-ext-pgarray, python-curtin,
    python-simplestreams, ubuntu-cloud-keyring.
  - Depends on maas-dns, maas-dhcp to get them seeded and
    into main (LP: #1227353)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright 2012, 2013 Canonical Ltd.  This software is licensed under the
 
2
# GNU Affero General Public License version 3 (see the file LICENSE).
 
3
 
 
4
"""Test maasserver model managers."""
 
5
 
 
6
from __future__ import (
 
7
    absolute_import,
 
8
    print_function,
 
9
    unicode_literals,
 
10
    )
 
11
 
 
12
__metaclass__ = type
 
13
__all__ = []
 
14
 
 
15
from maasserver.testing.testcase import MAASServerTestCase
 
16
from maasserver.tests.models import (
 
17
    BulkManagerParentTestModel,
 
18
    BulkManagerTestModel,
 
19
    )
 
20
from maastesting.djangotestcase import TestModelMixin
 
21
 
 
22
 
 
23
class BulkManagerTest(TestModelMixin, MAASServerTestCase):
 
24
 
 
25
    app = 'maasserver.tests'
 
26
 
 
27
    def test_manager_iterator_uses_cache(self):
 
28
        parents = set()
 
29
        for i in range(3):
 
30
            parents.add(BulkManagerParentTestModel.objects.create())
 
31
        for i in range(10):
 
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
 
41
        # BulkManager.
 
42
        self.assertNumQueries(
 
43
            2,
 
44
            lambda: [list(parent.bulkmanagertestmodel_set.iterator())
 
45
                for parent in parents])