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