mirror of https://github.com/django/django.git
Refs #18586 -- Split custom manager tests.
This commit is contained in:
parent
f66d9a2300
commit
6db122c7ab
|
@ -8,17 +8,25 @@ from .models import (Book, Car, FunPerson, OneToOneRestrictedModel, Person,
|
|||
|
||||
|
||||
class CustomManagerTests(TestCase):
|
||||
def setUp(self):
|
||||
self.b1 = Book.published_objects.create(
|
||||
custom_manager_names = [
|
||||
'custom_queryset_default_manager',
|
||||
'custom_queryset_custom_manager',
|
||||
]
|
||||
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
cls.b1 = Book.published_objects.create(
|
||||
title="How to program", author="Rodney Dangerfield", is_published=True)
|
||||
self.b2 = Book.published_objects.create(
|
||||
cls.b2 = Book.published_objects.create(
|
||||
title="How to be smart", author="Albert Einstein", is_published=False)
|
||||
|
||||
def test_manager(self):
|
||||
Person.objects.create(first_name="Bugs", last_name="Bunny", fun=True)
|
||||
droopy = Person.objects.create(first_name="Droopy", last_name="Dog", fun=False)
|
||||
cls.p1 = Person.objects.create(first_name="Bugs", last_name="Bunny", fun=True)
|
||||
cls.droopy = Person.objects.create(first_name="Droopy", last_name="Dog", fun=False)
|
||||
|
||||
# Test a custom `Manager` method.
|
||||
def test_custom_manager_basic(self):
|
||||
"""
|
||||
Test a custom Manager method.
|
||||
"""
|
||||
self.assertQuerysetEqual(
|
||||
Person.objects.get_fun_people(), [
|
||||
"Bugs Bunny"
|
||||
|
@ -26,24 +34,35 @@ class CustomManagerTests(TestCase):
|
|||
six.text_type
|
||||
)
|
||||
|
||||
# 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)
|
||||
def test_queryset_copied_to_default(self):
|
||||
"""
|
||||
The methods of a custom QuerySet are properly copied onto the
|
||||
default Manager.
|
||||
"""
|
||||
for manager_name in self.custom_manager_names:
|
||||
manager = getattr(Person, manager_name)
|
||||
|
||||
# Copy public methods.
|
||||
# Public methods are copied
|
||||
manager.public_method()
|
||||
# Don't copy private methods.
|
||||
# Private methods are not copied
|
||||
with self.assertRaises(AttributeError):
|
||||
manager._private_method()
|
||||
# Copy methods with `manager=True` even if they are private.
|
||||
|
||||
def test_manager_honors_queryset_only(self):
|
||||
for manager_name in self.custom_manager_names:
|
||||
manager = getattr(Person, manager_name)
|
||||
# Methods with queryset_only=False are copied even if they are private.
|
||||
manager._optin_private_method()
|
||||
# Don't copy methods with `manager=False` even if they are public.
|
||||
# Methods with queryset_only=True aren't copied even if they are public.
|
||||
with self.assertRaises(AttributeError):
|
||||
manager.optout_public_method()
|
||||
|
||||
# Test that the overridden method is called.
|
||||
def test_manager_use_queryset_methods(self):
|
||||
"""
|
||||
Custom manager will use the queryset methods
|
||||
"""
|
||||
for manager_name in self.custom_manager_names:
|
||||
manager = getattr(Person, manager_name)
|
||||
queryset = manager.filter()
|
||||
self.assertQuerysetEqual(queryset, ["Bugs Bunny"], six.text_type)
|
||||
self.assertEqual(queryset._filter_CustomQuerySet, True)
|
||||
|
@ -53,31 +72,40 @@ class CustomManagerTests(TestCase):
|
|||
self.assertEqual(list(queryset), [six.text_type("Bugs")])
|
||||
self.assertEqual(queryset._filter_CustomQuerySet, True)
|
||||
|
||||
# Test that the custom manager `__init__()` argument has been set.
|
||||
def test_init_args(self):
|
||||
"""
|
||||
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.
|
||||
def test_manager_attributes(self):
|
||||
"""
|
||||
Custom manager method is only available on the manager and not on
|
||||
querysets.
|
||||
"""
|
||||
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.
|
||||
def test_queryset_and_manager(self):
|
||||
"""
|
||||
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)
|
||||
|
||||
# The RelatedManager used on the 'books' descriptor extends the default
|
||||
# manager
|
||||
self.assertIsInstance(droopy.books, PublishedBookManager)
|
||||
|
||||
# The default manager, "objects", doesn't exist, because a custom one
|
||||
# was provided.
|
||||
def test_no_objects(self):
|
||||
"""
|
||||
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
|
||||
self.assertIsInstance(self.b2.authors, PersonManager)
|
||||
|
||||
def test_filtering(self):
|
||||
"""
|
||||
Custom managers respond to usual filtering methods
|
||||
"""
|
||||
self.assertQuerysetEqual(
|
||||
Book.published_objects.all(), [
|
||||
"How to program",
|
||||
|
@ -85,36 +113,6 @@ class CustomManagerTests(TestCase):
|
|||
lambda b: b.title
|
||||
)
|
||||
|
||||
Car.cars.create(name="Corvette", mileage=21, top_speed=180)
|
||||
Car.cars.create(name="Neon", mileage=31, top_speed=100)
|
||||
|
||||
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
|
||||
)
|
||||
|
||||
def test_fk_related_manager(self):
|
||||
Person.objects.create(first_name="Bugs", last_name="Bunny", fun=True, favorite_book=self.b1)
|
||||
Person.objects.create(first_name="Droopy", last_name="Dog", fun=False, favorite_book=self.b1)
|
||||
|
@ -466,6 +464,37 @@ class CustomManagerTests(TestCase):
|
|||
)
|
||||
|
||||
|
||||
class TestCars(TestCase):
|
||||
|
||||
def test_managers(self):
|
||||
# Each model class gets a "_default_manager" attribute, which is a
|
||||
# reference to the first manager defined in the class.
|
||||
Car.cars.create(name="Corvette", mileage=21, top_speed=180)
|
||||
Car.cars.create(name="Neon", mileage=31, top_speed=100)
|
||||
|
||||
self.assertQuerysetEqual(
|
||||
Car._default_manager.order_by("name"), [
|
||||
"Corvette",
|
||||
"Neon",
|
||||
],
|
||||
lambda c: c.name
|
||||
)
|
||||
self.assertQuerysetEqual(
|
||||
Car.cars.order_by("name"), [
|
||||
"Corvette",
|
||||
"Neon",
|
||||
],
|
||||
lambda c: c.name
|
||||
)
|
||||
# alternate manager
|
||||
self.assertQuerysetEqual(
|
||||
Car.fast_cars.all(), [
|
||||
"Corvette",
|
||||
],
|
||||
lambda c: c.name
|
||||
)
|
||||
|
||||
|
||||
class CustomManagersRegressTestCase(TestCase):
|
||||
def test_filtered_default_manager(self):
|
||||
"""Even though the default manager filters out some records,
|
||||
|
|
Loading…
Reference in New Issue