Skip to content

Commit

Permalink
added new custom managers tests. Added another assert to clean things…
Browse files Browse the repository at this point in the history
… up.
  • Loading branch information
empty committed Jun 11, 2008
1 parent 3cc325f commit cf9d1d4
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 3 deletions.
11 changes: 9 additions & 2 deletions django_sqlalchemy/test/asserts.py
Expand Up @@ -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 """
Expand All @@ -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)
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])
Empty file added tests/managers/__init__.py
Empty file.
94 changes: 94 additions & 0 deletions 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'))
2 changes: 1 addition & 1 deletion tests/settings.py
Expand Up @@ -15,7 +15,7 @@
'apps.news',
'apps.norelations',
'apps.categories',
# 'django.contrib.auth',
'django.contrib.auth',
)

TEMPLATE_LOADERS = (
Expand Down

0 comments on commit cf9d1d4

Please sign in to comment.