1
from __future__ import absolute_import
3
from django.core.exceptions import FieldError
4
from django.test import TestCase
6
from .models import Author, Article
10
""" Return pks to be able to compare lists"""
11
return [o.pk for o in objects]
13
class CustomColumnRegression(TestCase):
16
self.a1 = Author.objects.create(first_name='John', last_name='Smith')
17
self.a2 = Author.objects.create(first_name='Peter', last_name='Jones')
18
self.authors = [self.a1, self.a2]
20
def test_basic_creation(self):
21
art = Article(headline='Django lets you build Web apps easily', primary_author=self.a1)
23
art.authors = [self.a1, self.a2]
25
def test_author_querying(self):
26
self.assertQuerysetEqual(
27
Author.objects.all().order_by('last_name'),
28
['<Author: Peter Jones>', '<Author: John Smith>']
31
def test_author_filtering(self):
32
self.assertQuerysetEqual(
33
Author.objects.filter(first_name__exact='John'),
34
['<Author: John Smith>']
37
def test_author_get(self):
38
self.assertEqual(self.a1, Author.objects.get(first_name__exact='John'))
40
def test_filter_on_nonexistant_field(self):
41
self.assertRaisesMessage(
43
"Cannot resolve keyword 'firstname' into field. Choices are: Author_ID, article, first_name, last_name, primary_set",
44
Author.objects.filter,
45
firstname__exact='John'
48
def test_author_get_attributes(self):
49
a = Author.objects.get(last_name__exact='Smith')
50
self.assertEqual('John', a.first_name)
51
self.assertEqual('Smith', a.last_name)
52
self.assertRaisesMessage(
54
"'Author' object has no attribute 'firstname'",
59
self.assertRaisesMessage(
61
"'Author' object has no attribute 'last'",
66
def test_m2m_table(self):
67
art = Article.objects.create(headline='Django lets you build Web apps easily', primary_author=self.a1)
68
art.authors = self.authors
69
self.assertQuerysetEqual(
70
art.authors.all().order_by('last_name'),
71
['<Author: Peter Jones>', '<Author: John Smith>']
73
self.assertQuerysetEqual(
74
self.a1.article_set.all(),
75
['<Article: Django lets you build Web apps easily>']
77
self.assertQuerysetEqual(
78
art.authors.filter(last_name='Jones'),
79
['<Author: Peter Jones>']