2013-07-29 17:19:04 +00:00
|
|
|
from __future__ import unicode_literals
|
2011-10-13 18:04:12 +00:00
|
|
|
|
2010-09-12 18:43:06 +00:00
|
|
|
from django.core.exceptions import FieldError
|
|
|
|
from django.test import TestCase
|
2012-07-20 12:48:51 +00:00
|
|
|
from django.utils import six
|
2010-09-12 18:43:06 +00:00
|
|
|
|
2015-01-28 12:35:27 +00:00
|
|
|
from .models import Article, Author
|
2010-09-12 18:43:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CustomColumnsTests(TestCase):
|
|
|
|
|
2014-09-22 18:07:30 +00: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-12 18:43:06 +00:00
|
|
|
|
2014-09-22 18:07:30 +00:00
|
|
|
self.article = Article.objects.create(headline="Django lets you build Web apps easily", primary_author=self.a1)
|
2015-10-08 21:17:10 +00:00
|
|
|
self.article.authors.set(self.authors)
|
2010-09-12 18:43:06 +00:00
|
|
|
|
2013-03-18 22:09:26 +00:00
|
|
|
def test_query_all_available_authors(self):
|
2010-09-12 18:43:06 +00:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Author.objects.all(), [
|
|
|
|
"Peter Jones", "John Smith",
|
|
|
|
],
|
2012-07-20 12:48:51 +00:00
|
|
|
six.text_type
|
2010-09-12 18:43:06 +00:00
|
|
|
)
|
2013-03-18 22:09:26 +00: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-12 18:43:06 +00:00
|
|
|
self.assertQuerysetEqual(
|
|
|
|
Author.objects.filter(first_name__exact="John"), [
|
|
|
|
"John Smith",
|
|
|
|
],
|
2012-07-20 12:48:51 +00:00
|
|
|
six.text_type
|
2010-09-12 18:43:06 +00:00
|
|
|
)
|
|
|
|
|
2013-03-18 22:09:26 +00:00
|
|
|
def test_field_error(self):
|
2016-01-17 11:26:39 +00:00
|
|
|
with self.assertRaises(FieldError):
|
|
|
|
Author.objects.filter(firstname__exact="John")
|
2010-09-12 18:43:06 +00:00
|
|
|
|
2013-03-18 22:09:26 +00:00
|
|
|
def test_attribute_error(self):
|
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
self.a1.firstname
|
2010-09-12 18:43:06 +00:00
|
|
|
|
2013-03-18 22:09:26 +00:00
|
|
|
with self.assertRaises(AttributeError):
|
|
|
|
self.a1.last
|
2010-09-12 18:43:06 +00:00
|
|
|
|
2013-03-18 22:09:26 +00:00
|
|
|
def test_get_all_authors_for_an_article(self):
|
2010-09-12 18:43:06 +00:00
|
|
|
self.assertQuerysetEqual(
|
2014-09-22 18:07:30 +00:00
|
|
|
self.article.authors.all(), [
|
2010-09-12 18:43:06 +00:00
|
|
|
"Peter Jones",
|
|
|
|
"John Smith",
|
|
|
|
],
|
2012-07-20 12:48:51 +00:00
|
|
|
six.text_type
|
2010-09-12 18:43:06 +00:00
|
|
|
)
|
2013-03-18 22:09:26 +00:00
|
|
|
|
|
|
|
def test_get_all_articles_for_an_author(self):
|
2010-09-12 18:43:06 +00:00
|
|
|
self.assertQuerysetEqual(
|
2013-03-18 22:09:26 +00:00
|
|
|
self.a1.article_set.all(), [
|
2010-10-09 08:12:50 +00:00
|
|
|
"Django lets you build Web apps easily",
|
2010-09-12 18:43:06 +00:00
|
|
|
],
|
|
|
|
lambda a: a.headline
|
|
|
|
)
|
2013-03-18 22:09:26 +00:00
|
|
|
|
|
|
|
def test_get_author_m2m_relation(self):
|
2010-09-12 18:43:06 +00:00
|
|
|
self.assertQuerysetEqual(
|
2014-09-22 18:07:30 +00:00
|
|
|
self.article.authors.filter(last_name='Jones'), [
|
2010-09-12 18:43:06 +00:00
|
|
|
"Peter Jones"
|
|
|
|
],
|
2012-07-20 12:48:51 +00:00
|
|
|
six.text_type
|
2010-09-12 18:43:06 +00:00
|
|
|
)
|
2014-09-22 18:07:30 +00: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-03 22:48:03 +00:00
|
|
|
def test_filter_on_nonexistent_field(self):
|
2016-01-04 08:50:08 +00:00
|
|
|
msg = (
|
2015-09-11 23:33:12 +00:00
|
|
|
"Cannot resolve keyword 'firstname' into field. Choices are: "
|
2016-01-04 08:50:08 +00:00
|
|
|
"Author_ID, article, first_name, last_name, primary_set"
|
2014-09-22 18:07:30 +00:00
|
|
|
)
|
2016-01-04 08:50:08 +00:00
|
|
|
with self.assertRaisesMessage(FieldError, msg):
|
|
|
|
Author.objects.filter(firstname__exact='John')
|
2014-09-22 18:07:30 +00:00
|
|
|
|
|
|
|
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)
|
2016-01-04 08:50:08 +00:00
|
|
|
with self.assertRaisesMessage(AttributeError, "'Author' object has no attribute 'firstname'"):
|
|
|
|
getattr(a, 'firstname')
|
2014-09-22 18:07:30 +00:00
|
|
|
|
2016-01-04 08:50:08 +00:00
|
|
|
with self.assertRaisesMessage(AttributeError, "'Author' object has no attribute 'last'"):
|
|
|
|
getattr(a, 'last')
|
2014-09-22 18:07:30 +00:00
|
|
|
|
|
|
|
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>']
|
|
|
|
)
|