2010-09-13 02:43:06 +08:00
|
|
|
from django.core.exceptions import FieldError
|
|
|
|
from django.test import TestCase
|
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from .models import Article, Author
|
2010-09-13 02:43:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
class CustomColumnsTests(TestCase):
|
2018-11-24 09:59:38 +08: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]
|
|
|
|
|
2021-07-23 14:48:16 +08:00
|
|
|
cls.article = Article.objects.create(
|
|
|
|
headline="Django lets you build web apps easily", primary_author=cls.a1
|
|
|
|
)
|
2018-11-24 09:59:38 +08:00
|
|
|
cls.article.authors.set(cls.authors)
|
2010-09-13 02:43:06 +08:00
|
|
|
|
2013-03-19 06:09:26 +08:00
|
|
|
def test_query_all_available_authors(self):
|
2022-10-07 19:05:35 +08:00
|
|
|
self.assertSequenceEqual(Author.objects.all(), [self.a2, self.a1])
|
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):
|
2022-10-07 19:05:35 +08:00
|
|
|
self.assertSequenceEqual(
|
2010-09-13 02:43:06 +08:00
|
|
|
Author.objects.filter(first_name__exact="John"),
|
2022-10-07 19:05:35 +08:00
|
|
|
[self.a1],
|
2010-09-13 02:43:06 +08:00
|
|
|
)
|
|
|
|
|
2013-03-19 06:09:26 +08:00
|
|
|
def test_field_error(self):
|
2017-05-29 03:37:21 +08: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 19:26:39 +08:00
|
|
|
Author.objects.filter(firstname__exact="John")
|
2010-09-13 02:43:06 +08:00
|
|
|
|
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):
|
2022-10-07 19:05:35 +08:00
|
|
|
self.assertSequenceEqual(self.article.authors.all(), [self.a2, self.a1])
|
2013-03-19 06:09:26 +08:00
|
|
|
|
|
|
|
def test_get_all_articles_for_an_author(self):
|
2022-09-24 18:29:58 +08:00
|
|
|
self.assertQuerySetEqual(
|
2013-03-19 06:09:26 +08:00
|
|
|
self.a1.article_set.all(),
|
|
|
|
[
|
2021-07-23 14:48:16 +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):
|
2022-10-07 19:05:35 +08:00
|
|
|
self.assertSequenceEqual(
|
|
|
|
self.article.authors.filter(last_name="Jones"), [self.a2]
|
2010-09-13 02:43:06 +08:00
|
|
|
)
|
2014-09-23 02:07:30 +08:00
|
|
|
|
|
|
|
def test_author_querying(self):
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
2022-02-22 17:29:38 +08:00
|
|
|
Author.objects.order_by("last_name"),
|
2020-10-19 00:29:52 +08:00
|
|
|
[self.a2, self.a1],
|
2014-09-23 02:07:30 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_author_filtering(self):
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
2014-09-23 02:07:30 +08:00
|
|
|
Author.objects.filter(first_name__exact="John"),
|
2020-10-19 00:29:52 +08:00
|
|
|
[self.a1],
|
2014-09-23 02:07:30 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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):
|
2016-01-04 16:50:08 +08:00
|
|
|
msg = (
|
2015-09-12 07:33:12 +08:00
|
|
|
"Cannot resolve keyword 'firstname' into field. Choices are: "
|
2016-01-04 16:50:08 +08:00
|
|
|
"Author_ID, article, first_name, last_name, primary_set"
|
2014-09-23 02:07:30 +08:00
|
|
|
)
|
2016-01-04 16:50:08 +08:00
|
|
|
with self.assertRaisesMessage(FieldError, msg):
|
|
|
|
Author.objects.filter(firstname__exact="John")
|
2014-09-23 02:07:30 +08: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 16:50:08 +08:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
AttributeError, "'Author' object has no attribute 'firstname'"
|
|
|
|
):
|
|
|
|
getattr(a, "firstname")
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2016-01-04 16:50:08 +08:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
AttributeError, "'Author' object has no attribute 'last'"
|
|
|
|
):
|
|
|
|
getattr(a, "last")
|
2014-09-23 02:07:30 +08:00
|
|
|
|
|
|
|
def test_m2m_table(self):
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(
|
2022-02-22 17:29:38 +08:00
|
|
|
self.article.authors.order_by("last_name"),
|
2020-10-19 00:29:52 +08:00
|
|
|
[self.a2, self.a1],
|
2014-09-23 02:07:30 +08:00
|
|
|
)
|
2020-10-19 00:29:52 +08:00
|
|
|
self.assertSequenceEqual(self.a1.article_set.all(), [self.article])
|
|
|
|
self.assertSequenceEqual(
|
2014-09-23 02:07:30 +08:00
|
|
|
self.article.authors.filter(last_name="Jones"),
|
2020-10-19 00:29:52 +08:00
|
|
|
[self.a2],
|
2014-09-23 02:07:30 +08:00
|
|
|
)
|