From cf9d1d4dd52bf3ee4ab05ef1aac6825f6a6b0ec5 Mon Sep 17 00:00:00 2001 From: Michael Trier Date: Tue, 10 Jun 2008 21:09:55 -0400 Subject: [PATCH] added new custom managers tests. Added another assert to clean things up. --- django_sqlalchemy/test/asserts.py | 11 ++- tests/managers/__init__.py | 0 tests/managers/test_custom_managers.py | 94 ++++++++++++++++++++++++++ tests/settings.py | 2 +- 4 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 tests/managers/__init__.py create mode 100644 tests/managers/test_custom_managers.py diff --git a/django_sqlalchemy/test/asserts.py b/django_sqlalchemy/test/asserts.py index a1e5724..85dee44 100644 --- a/django_sqlalchemy/test/asserts.py +++ b/django_sqlalchemy/test/asserts.py @@ -3,7 +3,9 @@ from nose import tools from nose.tools import * -__all__ = ['assert_instance_of', 'assert_not_instance_of', 'assert_none', 'assert_not_none'] + tools.__all__ +__all__ = ['assert_instance_of', 'assert_not_instance_of', + 'assert_none', 'assert_not_none', + 'assert_list_same'] + tools.__all__ def assert_instance_of(expected, actual, msg=None): """Verify that object is an instance of expected """ @@ -19,4 +21,9 @@ def assert_none(actual, msg=None): def assert_not_none(actual, msg=None): """verify that item is None""" - assert_not_equal(None, actual, msg) \ No newline at end of file + assert_not_equal(None, actual, msg) + +def assert_list_same(expected, actual, msg=None): + """verify that the list contains the expected""" + assert_equal([repr(e) for e in expected], + [repr(a) for a in actual]) \ No newline at end of file diff --git a/tests/managers/__init__.py b/tests/managers/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/managers/test_custom_managers.py b/tests/managers/test_custom_managers.py new file mode 100644 index 0000000..eaca64e --- /dev/null +++ b/tests/managers/test_custom_managers.py @@ -0,0 +1,94 @@ +from django_sqlalchemy.test import * +from django_sqlalchemy.backend import metadata +from django.db import models + +# An example of a custom manager called "objects". + +class PersonManager(models.Manager): + def get_fun_people(self): + return self.filter(fun=True) + +class Person(models.Model): + first_name = models.CharField(max_length=30) + last_name = models.CharField(max_length=30) + fun = models.BooleanField() + objects = PersonManager() + + def __unicode__(self): + return u"%s %s" % (self.first_name, self.last_name) + +# An example of a custom manager that sets get_query_set(). + +class PublishedBookManager(models.Manager): + def get_query_set(self): + return super(PublishedBookManager, self).get_query_set().filter(is_published=True) + +class Book(models.Model): + title = models.CharField(max_length=50) + author = models.CharField(max_length=30) + is_published = models.BooleanField() + published_objects = PublishedBookManager() + authors = models.ManyToManyField(Person, related_name='books') + + def __unicode__(self): + return self.title + +# An example of providing multiple custom managers. + +class FastCarManager(models.Manager): + def get_query_set(self): + return super(FastCarManager, self).get_query_set().filter(top_speed__gt=150) + +class Car(models.Model): + name = models.CharField(max_length=10) + mileage = models.IntegerField() + top_speed = models.IntegerField(help_text="In miles per hour.") + cars = models.Manager() + fast_cars = FastCarManager() + + def __unicode__(self): + return self.name + +metadata.create_all() + +p1 = Person(first_name='Bugs', last_name='Bunny', fun=True) +p1.save() +p2 = Person(first_name='Droopy', last_name='Dog', fun=False) +p2.save() + +b1 = Book(title='How to program', author='Rodney Dangerfield', is_published=True) +b1.save() +b2 = Book(title='How to be smart', author='Albert Einstein', is_published=False) +b2.save() + +c1 = Car(name='Corvette', mileage=21, top_speed=180) +c1.save() +c2 = Car(name='Neon', mileage=31, top_speed=100) +c2.save() + +class TestCustomManager(object): + def setup(self): + pass + + def test_should_see_custom_manager_method(self): + assert_list_same([p1], Person.objects.get_fun_people()) + + def test_should_extend_default_manager(self): + assert_instance_of(PublishedBookManager, p2.books) + + @raises(AttributeError) + def test_should_not_contain_a_default_manager_if_custom_provided(self): + Book.objects + + def test_should_extend_default_manager_with_related_manager(self): + assert_instance_of(PersonManager, b2.authors) + + def test_should_only_return_published_objects(self): + assert_list_same([b1], Book.published_objects.all()) + + def test_should_order_by(self): + assert_list_same([c1, c2], Car.cars.order_by('name')) + assert_list_same([c1], Car.fast_cars.all()) + + def test_should_return_default_manager_as_first_manager_in_class(self): + assert_list_same([c1, c2], Car._default_manager.order_by('name')) diff --git a/tests/settings.py b/tests/settings.py index ecfdcd1..1bfe877 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -15,7 +15,7 @@ 'apps.news', 'apps.norelations', 'apps.categories', - # 'django.contrib.auth', + 'django.contrib.auth', ) TEMPLATE_LOADERS = (