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