2010-09-12 18:43:06 +00:00
|
|
|
from django.core.exceptions import FieldError
|
|
|
|
from django.test import TestCase
|
|
|
|
|
2015-01-28 12:35:27 +00:00
|
|
|
from .models import Article, Author
|
2010-09-12 18:43:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CustomColumnsTests(TestCase):
|
|
|
|
|
2018-11-24 01:59:38 +00:00
|
|
|
@classmethod
|
|
|
|
def setUpTestData(cls):
|
|
|
|
cls.a1 = Author.objects.create(first_name="John", last_name="Smith")
|
|
|
|
cls.a2 = Author.objects.create(first_name="Peter", last_name="Jones")
|
|
|
|
cls.authors = [cls.a1, cls.a2]
|
|
|
|
|
|
|
|
cls.article = Article.objects.create(headline="Django lets you build Web apps easily", primary_author=cls.a1)
|
|
|
|
cls.article.authors.set(cls.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",
|
|
|
|
],
|
2016-12-29 15:27:49 +00:00
|
|
|
str
|
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",
|
|
|
|
],
|
2016-12-29 15:27:49 +00:00
|
|
|
str
|
2010-09-12 18:43:06 +00:00
|
|
|
)
|
|
|
|
|
2013-03-18 22:09:26 +00:00
|
|
|
def test_field_error(self):
|
2017-05-28 19:37:21 +00:00
|
|
|
msg = (
|
|
|
|
"Cannot resolve keyword 'firstname' into field. Choices are: "
|
|
|
|
"Author_ID, article, first_name, last_name, primary_set"
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(FieldError, msg):
|
2016-01-17 11:26:39 +00:00
|
|
|
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",
|
|
|
|
],
|
2016-12-29 15:27:49 +00:00
|
|
|
str
|
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"
|
|
|
|
],
|
2016-12-29 15:27:49 +00:00
|
|
|
str
|
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>']
|
|
|
|
)
|