| |   |
| 3 | 3 | from nose import tools |
| 4 | 4 | from nose.tools import * |
| 5 | 5 | |
| 6 | | __all__ = ['assert_instance_of', 'assert_not_instance_of', 'assert_none', 'assert_not_none'] + tools.__all__ |
| 6 | __all__ = ['assert_instance_of', 'assert_not_instance_of', |
| 7 | 'assert_none', 'assert_not_none', |
| 8 | 'assert_list_same'] + tools.__all__ |
| 7 | 9 | |
| 8 | 10 | def assert_instance_of(expected, actual, msg=None): |
| 9 | 11 | """Verify that object is an instance of expected """ |
| … | … | |
| 21 | 21 | |
| 22 | 22 | def assert_not_none(actual, msg=None): |
| 23 | 23 | """verify that item is None""" |
| 24 | | assert_not_equal(None, actual, msg) |
| 24 | assert_not_equal(None, actual, msg) |
| 25 | |
| 26 | def assert_list_same(expected, actual, msg=None): |
| 27 | """verify that the list contains the expected""" |
| 28 | assert_equal([repr(e) for e in expected], |
| 29 | [repr(a) for a in actual]) |
| toggle raw diff |
--- a/django_sqlalchemy/test/asserts.py
+++ b/django_sqlalchemy/test/asserts.py
@@ -3,7 +3,9 @@ import unittest
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 |
| |   |
| 1 | from django_sqlalchemy.test import * |
| 2 | from django_sqlalchemy.backend import metadata |
| 3 | from django.db import models |
| 4 | |
| 5 | # An example of a custom manager called "objects". |
| 6 | |
| 7 | class PersonManager(models.Manager): |
| 8 | def get_fun_people(self): |
| 9 | return self.filter(fun=True) |
| 10 | |
| 11 | class Person(models.Model): |
| 12 | first_name = models.CharField(max_length=30) |
| 13 | last_name = models.CharField(max_length=30) |
| 14 | fun = models.BooleanField() |
| 15 | objects = PersonManager() |
| 16 | |
| 17 | def __unicode__(self): |
| 18 | return u"%s %s" % (self.first_name, self.last_name) |
| 19 | |
| 20 | # An example of a custom manager that sets get_query_set(). |
| 21 | |
| 22 | class PublishedBookManager(models.Manager): |
| 23 | def get_query_set(self): |
| 24 | return super(PublishedBookManager, self).get_query_set().filter(is_published=True) |
| 25 | |
| 26 | class Book(models.Model): |
| 27 | title = models.CharField(max_length=50) |
| 28 | author = models.CharField(max_length=30) |
| 29 | is_published = models.BooleanField() |
| 30 | published_objects = PublishedBookManager() |
| 31 | authors = models.ManyToManyField(Person, related_name='books') |
| 32 | |
| 33 | def __unicode__(self): |
| 34 | return self.title |
| 35 | |
| 36 | # An example of providing multiple custom managers. |
| 37 | |
| 38 | class FastCarManager(models.Manager): |
| 39 | def get_query_set(self): |
| 40 | return super(FastCarManager, self).get_query_set().filter(top_speed__gt=150) |
| 41 | |
| 42 | class Car(models.Model): |
| 43 | name = models.CharField(max_length=10) |
| 44 | mileage = models.IntegerField() |
| 45 | top_speed = models.IntegerField(help_text="In miles per hour.") |
| 46 | cars = models.Manager() |
| 47 | fast_cars = FastCarManager() |
| 48 | |
| 49 | def __unicode__(self): |
| 50 | return self.name |
| 51 | |
| 52 | metadata.create_all() |
| 53 | |
| 54 | p1 = Person(first_name='Bugs', last_name='Bunny', fun=True) |
| 55 | p1.save() |
| 56 | p2 = Person(first_name='Droopy', last_name='Dog', fun=False) |
| 57 | p2.save() |
| 58 | |
| 59 | b1 = Book(title='How to program', author='Rodney Dangerfield', is_published=True) |
| 60 | b1.save() |
| 61 | b2 = Book(title='How to be smart', author='Albert Einstein', is_published=False) |
| 62 | b2.save() |
| 63 | |
| 64 | c1 = Car(name='Corvette', mileage=21, top_speed=180) |
| 65 | c1.save() |
| 66 | c2 = Car(name='Neon', mileage=31, top_speed=100) |
| 67 | c2.save() |
| 68 | |
| 69 | class TestCustomManager(object): |
| 70 | def setup(self): |
| 71 | pass |
| 72 | |
| 73 | def test_should_see_custom_manager_method(self): |
| 74 | assert_list_same([p1], Person.objects.get_fun_people()) |
| 75 | |
| 76 | def test_should_extend_default_manager(self): |
| 77 | assert_instance_of(PublishedBookManager, p2.books) |
| 78 | |
| 79 | @raises(AttributeError) |
| 80 | def test_should_not_contain_a_default_manager_if_custom_provided(self): |
| 81 | Book.objects |
| 82 | |
| 83 | def test_should_extend_default_manager_with_related_manager(self): |
| 84 | assert_instance_of(PersonManager, b2.authors) |
| 85 | |
| 86 | def test_should_only_return_published_objects(self): |
| 87 | assert_list_same([b1], Book.published_objects.all()) |
| 88 | |
| 89 | def test_should_order_by(self): |
| 90 | assert_list_same([c1, c2], Car.cars.order_by('name')) |
| 91 | assert_list_same([c1], Car.fast_cars.all()) |
| 92 | |
| 93 | def test_should_return_default_manager_as_first_manager_in_class(self): |
| 94 | assert_list_same([c1, c2], Car._default_manager.order_by('name')) |
| toggle raw diff |
--- /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')) |