1.2.14
by Andres Rodriguez
Import upstream version 1.4+bzr1655+dfsg |
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]) |