2013-07-30 01:19:04 +08:00
|
|
|
from __future__ import unicode_literals
|
2011-10-14 02:04:12 +08:00
|
|
|
|
2010-09-13 02:43:06 +08:00
|
|
|
from django.core.exceptions import FieldError
|
|
|
|
from django.test import TestCase
|
2012-07-20 20:48:51 +08:00
|
|
|
from django.utils import six
|
2010-09-13 02:43:06 +08:00
|
|
|
|
2011-10-14 02:04:12 +08:00
|
|
|
from .models import Author, Article
|
2010-09-13 02:43:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
class CustomColumnsTests(TestCase):
|
|
|
|
|
2014-09-23 02:07:30 +08:00
|
|
|
def setUp(self):
|
|
|
|
self.a1 = Author.objects.create(first_name="John", last_name="Smith")
|
|
|
|
self.a2 = Author.objects.create(first_name="Peter", last_name="Jones")
|
|
|
|
self.authors = [self.a1, self.a2]
|
2010-09-13 02:43:06 +08:00
|
|
|
|
2014-09-23 02:07:30 +08:00
|
|
|
self.article = Article.objects.create(headline="Django lets you build Web apps easily", primary_author=self.a1)
|
|
|
|
self.article.authors = self.authors
|
2010-09-13 02:43:06 +08:00
|
|
|
|
2013-03-19 06:09:26 +08:00
|
|
|
def test_query_all_available_authors(self):
|
2010-09-13 02:43:06 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Author.objects.all(), [
|
|
|
|
"Peter Jones", "John Smith",
|
|
|
|
],
|
2012-07-20 20:48:51 +08:00
|
|
|
six.text_type
|
2010-09-13 02:43:06 +08:00
|
|
|
)
|
2013-03-19 06:09:26 +08:00
|
|
|
|
|
|
|
def test_get_first_name(self):
|
|
|
|
self.assertEqual(
|
|
|
|
Author.objects.get(first_name__exact="John"),
|
|
|
|
self.a1,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_filter_first_name(self):
|
2010-09-13 02:43:06 +08:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Author.objects.filter(first_name__exact="John"), [
|
|
|
|
"John Smith",
|
|
|
|
],
|
2012-07-20 20:48:51 +08:00
|
|
|
six.text_type
|
2010-09-13 02:43:06 +08:00
|
|
|
)
|
|
|
|
|
2013-03-19 06:09:26 +08:00
|
|
|
def test_field_error(self):
|
2013-12-09 01:20:06 +08:00
|
|
|
self.assertRaises(
|
|
|
|
FieldError,
|
2010-09-13 02:43:06 +08:00
|
|
|
lambda: Author.objects.filter(firstname__exact="John")
|
|
|
|
)
|
|
|
|
|
2013-03-19 06:09:26 +08:00
|
|
|
def test_attribute_error(self):
|
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
self.a1.firstname
|
2010-09-13 02:43:06 +08:00
|
|
|
|
2013-03-19 06:09:26 +08:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
self.a1.last
|
2010-09-13 02:43:06 +08:00
|
|
|
|
2013-03-19 06:09:26 +08:00
|
|
|
def test_get_all_authors_for_an_article(self):
|
2010-09-13 02:43:06 +08:00
|
|
|
self.assertQuerysetEqual(
|
2014-09-23 02:07:30 +08:00
|
|
|
self.article.authors.all(), [
|
2010-09-13 02:43:06 +08:00
|
|
|
"Peter Jones",
|
|
|
|
"John Smith",
|
|
|
|
],
|
2012-07-20 20:48:51 +08:00
|
|
|
six.text_type
|
2010-09-13 02:43:06 +08:00
|
|
|
)
|
2013-03-19 06:09:26 +08:00
|
|
|
|
|
|
|
def test_get_all_articles_for_an_author(self):
|
2010-09-13 02:43:06 +08:00
|
|
|
self.assertQuerysetEqual(
|
2013-03-19 06:09:26 +08:00
|
|
|
self.a1.article_set.all(), [
|
2010-10-09 16:12:50 +08:00
|
|
|
"Django lets you build Web apps easily",
|
2010-09-13 02:43:06 +08:00
|
|
|
],
|
|
|
|
lambda a: a.headline
|
|
|
|
)
|
2013-03-19 06:09:26 +08:00
|
|
|
|
|
|
|
def test_get_author_m2m_relation(self):
|
2010-09-13 02:43:06 +08:00
|
|
|
self.assertQuerysetEqual(
|
2014-09-23 02:07:30 +08:00
|
|
|
self.article.authors.filter(last_name='Jones'), [
|
2010-09-13 02:43:06 +08:00
|
|
|
"Peter Jones"
|
|
|
|
],
|
2012-07-20 20:48:51 +08:00
|
|
|
six.text_type
|
2010-09-13 02:43:06 +08:00
|
|
|
)
|
2014-09-23 02:07:30 +08:00
|
|
|
|
|
|
|
def test_author_querying(self):
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Author.objects.all().order_by('last_name'),
|
|
|
|
['<Author: Peter Jones>', '<Author: John Smith>']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_author_filtering(self):
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Author.objects.filter(first_name__exact='John'),
|
|
|
|
['<Author: John Smith>']
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_author_get(self):
|
|
|
|
self.assertEqual(self.a1, Author.objects.get(first_name__exact='John'))
|
|
|
|
|
2014-11-04 06:48:03 +08:00
|
|
|
def test_filter_on_nonexistent_field(self):
|
2014-09-23 02:07:30 +08:00
|
|
|
self.assertRaisesMessage(
|
|
|
|
FieldError,
|
|
|
|
"Cannot resolve keyword 'firstname' into field. Choices are: Author_ID, article, first_name, last_name, primary_set",
|
|
|
|
Author.objects.filter,
|
|
|
|
firstname__exact='John'
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_author_get_attributes(self):
|
|
|
|
a = Author.objects.get(last_name__exact='Smith')
|
|
|
|
self.assertEqual('John', a.first_name)
|
|
|
|
self.assertEqual('Smith', a.last_name)
|
|
|
|
self.assertRaisesMessage(
|
|
|
|
AttributeError,
|
|
|
|
"'Author' object has no attribute 'firstname'",
|
|
|
|
getattr,
|
|
|
|
a, 'firstname'
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertRaisesMessage(
|
|
|
|
AttributeError,
|
|
|
|
"'Author' object has no attribute 'last'",
|
|
|
|
getattr,
|
|
|
|
a, 'last'
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_m2m_table(self):
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.article.authors.all().order_by('last_name'),
|
|
|
|
['<Author: Peter Jones>', '<Author: John Smith>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.a1.article_set.all(),
|
|
|
|
['<Article: Django lets you build Web apps easily>']
|
|
|
|
)
|
|
|
|
self.assertQuerysetEqual(
|
|
|
|
self.article.authors.filter(last_name='Jones'),
|
|
|
|
['<Author: Peter Jones>']
|
|
|
|
)
|