tests.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from __future__ import absolute_import
  2. from django.test import TestCase
  3. from django.utils import six
  4. from .models import Person, Book, Car, PersonManager, PublishedBookManager
  5. class CustomManagerTests(TestCase):
  6. def test_manager(self):
  7. p1 = Person.objects.create(first_name="Bugs", last_name="Bunny", fun=True)
  8. p2 = Person.objects.create(first_name="Droopy", last_name="Dog", fun=False)
  9. # Test a custom `Manager` method.
  10. self.assertQuerysetEqual(
  11. Person.objects.get_fun_people(), [
  12. "Bugs Bunny"
  13. ],
  14. six.text_type
  15. )
  16. # Test that the methods of a custom `QuerySet` are properly
  17. # copied onto the default `Manager`.
  18. for manager in ['custom_queryset_default_manager',
  19. 'custom_queryset_custom_manager']:
  20. manager = getattr(Person, manager)
  21. # Copy public methods.
  22. manager.public_method()
  23. # Don't copy private methods.
  24. with self.assertRaises(AttributeError):
  25. manager._private_method()
  26. # Copy methods with `manager=True` even if they are private.
  27. manager._optin_private_method()
  28. # Don't copy methods with `manager=False` even if they are public.
  29. with self.assertRaises(AttributeError):
  30. manager.optout_public_method()
  31. # Test that the overriden method is called.
  32. queryset = manager.filter()
  33. self.assertQuerysetEqual(queryset, ["Bugs Bunny"], six.text_type)
  34. self.assertEqual(queryset._filter_CustomQuerySet, True)
  35. # Test that specialized querysets inherit from our custom queryset.
  36. queryset = manager.values_list('first_name', flat=True).filter()
  37. self.assertEqual(list(queryset), [six.text_type("Bugs")])
  38. self.assertEqual(queryset._filter_CustomQuerySet, True)
  39. # Test that the custom manager `__init__()` argument has been set.
  40. self.assertEqual(Person.custom_queryset_custom_manager.init_arg, 'hello')
  41. # Test that the custom manager method is only available on the manager.
  42. Person.custom_queryset_custom_manager.manager_only()
  43. with self.assertRaises(AttributeError):
  44. Person.custom_queryset_custom_manager.all().manager_only()
  45. # Test that the queryset method doesn't override the custom manager method.
  46. queryset = Person.custom_queryset_custom_manager.filter()
  47. self.assertQuerysetEqual(queryset, ["Bugs Bunny"], six.text_type)
  48. self.assertEqual(queryset._filter_CustomManager, True)
  49. # The RelatedManager used on the 'books' descriptor extends the default
  50. # manager
  51. self.assertIsInstance(p2.books, PublishedBookManager)
  52. b1 = Book.published_objects.create(
  53. title="How to program", author="Rodney Dangerfield", is_published=True
  54. )
  55. b2 = Book.published_objects.create(
  56. title="How to be smart", author="Albert Einstein", is_published=False
  57. )
  58. # The default manager, "objects", doesn't exist, because a custom one
  59. # was provided.
  60. self.assertRaises(AttributeError, lambda: Book.objects)
  61. # The RelatedManager used on the 'authors' descriptor extends the
  62. # default manager
  63. self.assertIsInstance(b2.authors, PersonManager)
  64. self.assertQuerysetEqual(
  65. Book.published_objects.all(), [
  66. "How to program",
  67. ],
  68. lambda b: b.title
  69. )
  70. c1 = Car.cars.create(name="Corvette", mileage=21, top_speed=180)
  71. c2 = Car.cars.create(name="Neon", mileage=31, top_speed=100)
  72. self.assertQuerysetEqual(
  73. Car.cars.order_by("name"), [
  74. "Corvette",
  75. "Neon",
  76. ],
  77. lambda c: c.name
  78. )
  79. self.assertQuerysetEqual(
  80. Car.fast_cars.all(), [
  81. "Corvette",
  82. ],
  83. lambda c: c.name
  84. )
  85. # Each model class gets a "_default_manager" attribute, which is a
  86. # reference to the first manager defined in the class. In this case,
  87. # it's "cars".
  88. self.assertQuerysetEqual(
  89. Car._default_manager.order_by("name"), [
  90. "Corvette",
  91. "Neon",
  92. ],
  93. lambda c: c.name
  94. )