PageRenderTime 27ms CodeModel.GetById 15ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/defer_regress/tests.py

https://code.google.com/p/mango-py/
Python | 153 lines | 114 code | 23 blank | 16 comment | 0 complexity | de727685a7a56e05eb92f5447de1f9fd MD5 | raw file
  1from operator import attrgetter
  2
  3from django.conf import settings
  4from django.contrib.contenttypes.models import ContentType
  5from django.contrib.sessions.backends.db import SessionStore
  6from django.db import connection
  7from django.db.models.loading import cache
  8from django.test import TestCase
  9
 10from models import ResolveThis, Item, RelatedItem, Child, Leaf
 11
 12
 13class DeferRegressionTest(TestCase):
 14    def test_basic(self):
 15        # Deferred fields should really be deferred and not accidentally use
 16        # the field's default value just because they aren't passed to __init__
 17
 18        Item.objects.create(name="first", value=42)
 19        obj = Item.objects.only("name", "other_value").get(name="first")
 20        # Accessing "name" doesn't trigger a new database query. Accessing
 21        # "value" or "text" should.
 22        def test():
 23            self.assertEqual(obj.name, "first")
 24            self.assertEqual(obj.other_value, 0)
 25        self.assertNumQueries(0, test)
 26
 27        def test():
 28            self.assertEqual(obj.value, 42)
 29        self.assertNumQueries(1, test)
 30
 31        def test():
 32            self.assertEqual(obj.text, "xyzzy")
 33        self.assertNumQueries(1, test)
 34
 35        def test():
 36            self.assertEqual(obj.text, "xyzzy")
 37        self.assertNumQueries(0, test)
 38
 39        # Regression test for #10695. Make sure different instances don't
 40        # inadvertently share data in the deferred descriptor objects.
 41        i = Item.objects.create(name="no I'm first", value=37)
 42        items = Item.objects.only("value").order_by("-value")
 43        self.assertEqual(items[0].name, "first")
 44        self.assertEqual(items[1].name, "no I'm first")
 45
 46        RelatedItem.objects.create(item=i)
 47        r = RelatedItem.objects.defer("item").get()
 48        self.assertEqual(r.item_id, i.id)
 49        self.assertEqual(r.item, i)
 50
 51        # Some further checks for select_related() and inherited model
 52        # behaviour (regression for #10710).
 53        c1 = Child.objects.create(name="c1", value=42)
 54        c2 = Child.objects.create(name="c2", value=37)
 55        Leaf.objects.create(name="l1", child=c1, second_child=c2)
 56
 57        obj = Leaf.objects.only("name", "child").select_related()[0]
 58        self.assertEqual(obj.child.name, "c1")
 59
 60        self.assertQuerysetEqual(
 61            Leaf.objects.select_related().only("child__name", "second_child__name"), [
 62                "l1",
 63            ],
 64            attrgetter("name")
 65        )
 66
 67        # Models instances with deferred fields should still return the same
 68        # content types as their non-deferred versions (bug #10738).
 69        ctype = ContentType.objects.get_for_model
 70        c1 = ctype(Item.objects.all()[0])
 71        c2 = ctype(Item.objects.defer("name")[0])
 72        c3 = ctype(Item.objects.only("name")[0])
 73        self.assertTrue(c1 is c2 is c3)
 74
 75        # Regression for #10733 - only() can be used on a model with two
 76        # foreign keys.
 77        results = Leaf.objects.only("name", "child", "second_child").select_related()
 78        self.assertEqual(results[0].child.name, "c1")
 79        self.assertEqual(results[0].second_child.name, "c2")
 80
 81        results = Leaf.objects.only("name", "child", "second_child", "child__name", "second_child__name").select_related()
 82        self.assertEqual(results[0].child.name, "c1")
 83        self.assertEqual(results[0].second_child.name, "c2")
 84
 85        # Test for #12163 - Pickling error saving session with unsaved model
 86        # instances.
 87        SESSION_KEY = '2b1189a188b44ad18c35e1baac6ceead'
 88
 89        item = Item()
 90        item._deferred = False
 91        s = SessionStore(SESSION_KEY)
 92        s.clear()
 93        s["item"] = item
 94        s.save()
 95
 96        s = SessionStore(SESSION_KEY)
 97        s.modified = True
 98        s.save()
 99
100        i2 = s["item"]
101        self.assertFalse(i2._deferred)
102
103        # Regression for #11936 - loading.get_models should not return deferred
104        # models by default.
105        klasses = sorted(
106            cache.get_models(cache.get_app("defer_regress")),
107            key=lambda klass: klass.__name__
108        )
109        self.assertEqual(
110            klasses, [
111                Child,
112                Item,
113                Leaf,
114                RelatedItem,
115                ResolveThis,
116            ]
117        )
118
119        klasses = sorted(
120            map(
121                attrgetter("__name__"),
122                cache.get_models(
123                    cache.get_app("defer_regress"), include_deferred=True
124                ),
125            )
126        )
127        self.assertEqual(
128            klasses, [
129                "Child",
130                "Child_Deferred_value",
131                "Item",
132                "Item_Deferred_name",
133                "Item_Deferred_name_other_value_text",
134                "Item_Deferred_name_other_value_value",
135                "Item_Deferred_other_value_text_value",
136                "Item_Deferred_text_value",
137                "Leaf",
138                "Leaf_Deferred_child_id_second_child_id_value",
139                "Leaf_Deferred_name_value",
140                "Leaf_Deferred_second_child_value",
141                "Leaf_Deferred_value",
142                "RelatedItem",
143                "RelatedItem_Deferred_",
144                "RelatedItem_Deferred_item_id",
145                "ResolveThis",
146            ]
147        )
148
149    def test_resolve_columns(self):
150        rt = ResolveThis.objects.create(num=5.0, name='Foobar')
151        qs = ResolveThis.objects.defer('num')
152        self.assertEqual(1, qs.count())
153        self.assertEqual('Foobar', qs[0].name)