2013-07-29 17:19:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2011-10-13 18:04:12 +00:00
|
|
|
|
2010-09-12 20:02:57 +00:00
|
|
|
from django.test import TestCase
|
2012-07-20 12:48:51 +00:00
|
|
|
from django.utils import six
|
2010-09-12 20:02:57 +00:00
|
|
|
|
2011-10-13 18:04:12 +00:00
|
|
|
from .models import Person, Book, Car, PersonManager, PublishedBookManager
|
2010-09-12 20:02:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CustomManagerTests(TestCase):
|
|
|
|
def test_manager(self):
|
2013-09-08 19:20:01 +00:00
|
|
|
Person.objects.create(first_name="Bugs", last_name="Bunny", fun=True)
|
2010-09-12 20:02:57 +00:00
|
|
|
p2 = Person.objects.create(first_name="Droopy", last_name="Dog", fun=False)
|
|
|
|
|
2013-07-26 08:59:40 +00:00
|
|
|
# Test a custom `Manager` method.
|
2010-09-12 20:02:57 +00:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Person.objects.get_fun_people(), [
|
|
|
|
"Bugs Bunny"
|
|
|
|
],
|
2012-07-20 12:48:51 +00:00
|
|
|
six.text_type
|
2010-09-12 20:02:57 +00:00
|
|
|
)
|
2013-07-26 08:59:40 +00:00
|
|
|
|
|
|
|
# Test that the methods of a custom `QuerySet` are properly
|
|
|
|
# copied onto the default `Manager`.
|
|
|
|
for manager in ['custom_queryset_default_manager',
|
|
|
|
'custom_queryset_custom_manager']:
|
|
|
|
manager = getattr(Person, manager)
|
|
|
|
|
|
|
|
# Copy public methods.
|
|
|
|
manager.public_method()
|
|
|
|
# Don't copy private methods.
|
|
|
|
with self.assertRaises(AttributeError):
|
2013-09-03 18:22:21 +00:00
|
|
|
manager._private_method()
|
2013-07-26 08:59:40 +00:00
|
|
|
# Copy methods with `manager=True` even if they are private.
|
|
|
|
manager._optin_private_method()
|
|
|
|
# Don't copy methods with `manager=False` even if they are public.
|
|
|
|
with self.assertRaises(AttributeError):
|
2013-09-03 18:22:21 +00:00
|
|
|
manager.optout_public_method()
|
2013-07-26 08:59:40 +00:00
|
|
|
|
2013-07-28 01:45:25 +00:00
|
|
|
# Test that the overridden method is called.
|
2013-07-26 08:59:40 +00:00
|
|
|
queryset = manager.filter()
|
|
|
|
self.assertQuerysetEqual(queryset, ["Bugs Bunny"], six.text_type)
|
|
|
|
self.assertEqual(queryset._filter_CustomQuerySet, True)
|
|
|
|
|
|
|
|
# Test that specialized querysets inherit from our custom queryset.
|
|
|
|
queryset = manager.values_list('first_name', flat=True).filter()
|
|
|
|
self.assertEqual(list(queryset), [six.text_type("Bugs")])
|
|
|
|
self.assertEqual(queryset._filter_CustomQuerySet, True)
|
|
|
|
|
|
|
|
# Test that the custom manager `__init__()` argument has been set.
|
|
|
|
self.assertEqual(Person.custom_queryset_custom_manager.init_arg, 'hello')
|
|
|
|
|
|
|
|
# Test that the custom manager method is only available on the manager.
|
|
|
|
Person.custom_queryset_custom_manager.manager_only()
|
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
Person.custom_queryset_custom_manager.all().manager_only()
|
|
|
|
|
|
|
|
# Test that the queryset method doesn't override the custom manager method.
|
|
|
|
queryset = Person.custom_queryset_custom_manager.filter()
|
|
|
|
self.assertQuerysetEqual(queryset, ["Bugs Bunny"], six.text_type)
|
|
|
|
self.assertEqual(queryset._filter_CustomManager, True)
|
|
|
|
|
2010-09-12 20:02:57 +00:00
|
|
|
# The RelatedManager used on the 'books' descriptor extends the default
|
|
|
|
# manager
|
2013-05-21 09:42:15 +00:00
|
|
|
self.assertIsInstance(p2.books, PublishedBookManager)
|
2010-09-12 20:02:57 +00:00
|
|
|
|
2013-09-08 19:20:01 +00:00
|
|
|
Book.published_objects.create(
|
2010-09-12 20:02:57 +00:00
|
|
|
title="How to program", author="Rodney Dangerfield", is_published=True
|
|
|
|
)
|
|
|
|
b2 = Book.published_objects.create(
|
|
|
|
title="How to be smart", author="Albert Einstein", is_published=False
|
|
|
|
)
|
|
|
|
|
|
|
|
# The default manager, "objects", doesn't exist, because a custom one
|
|
|
|
# was provided.
|
|
|
|
self.assertRaises(AttributeError, lambda: Book.objects)
|
|
|
|
|
|
|
|
# The RelatedManager used on the 'authors' descriptor extends the
|
|
|
|
# default manager
|
2013-05-21 09:42:15 +00:00
|
|
|
self.assertIsInstance(b2.authors, PersonManager)
|
2010-09-12 20:02:57 +00:00
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Book.published_objects.all(), [
|
|
|
|
"How to program",
|
|
|
|
],
|
|
|
|
lambda b: b.title
|
|
|
|
)
|
|
|
|
|
2013-09-08 19:20:01 +00:00
|
|
|
Car.cars.create(name="Corvette", mileage=21, top_speed=180)
|
|
|
|
Car.cars.create(name="Neon", mileage=31, top_speed=100)
|
2010-09-12 20:02:57 +00:00
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Car.cars.order_by("name"), [
|
|
|
|
"Corvette",
|
|
|
|
"Neon",
|
|
|
|
],
|
|
|
|
lambda c: c.name
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Car.fast_cars.all(), [
|
|
|
|
"Corvette",
|
|
|
|
],
|
|
|
|
lambda c: c.name
|
|
|
|
)
|
|
|
|
|
|
|
|
# Each model class gets a "_default_manager" attribute, which is a
|
|
|
|
# reference to the first manager defined in the class. In this case,
|
|
|
|
# it's "cars".
|
|
|
|
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Car._default_manager.order_by("name"), [
|
|
|
|
"Corvette",
|
|
|
|
"Neon",
|
|
|
|
],
|
|
|
|
lambda c: c.name
|
|
|
|
)
|