summaryrefslogtreecommitdiff
path: root/tests/custom_columns
diff options
context:
space:
mode:
authorLoic Bistuer <loic.bistuer@gmail.com>2014-09-23 01:07:30 +0700
committerLoic Bistuer <loic.bistuer@gmail.com>2014-09-23 03:56:19 +0700
commitd42a45de407c7d7962aa7a2dd79f7d32b423e600 (patch)
tree0322ab45541729ecc3173b7ef8665b171c4b130f /tests/custom_columns
parent7fe554b2a3d72d0142e5c9e97efbd0a672c2d790 (diff)
downloaddjango-d42a45de407c7d7962aa7a2dd79f7d32b423e600.tar.gz
Merged custom_columns_regress into the custom_columns test package.
Diffstat (limited to 'tests/custom_columns')
-rw-r--r--tests/custom_columns/models.py3
-rw-r--r--tests/custom_columns/tests.py73
2 files changed, 66 insertions, 10 deletions
diff --git a/tests/custom_columns/models.py b/tests/custom_columns/models.py
index df1e743be9..6126183ca9 100644
--- a/tests/custom_columns/models.py
+++ b/tests/custom_columns/models.py
@@ -23,6 +23,7 @@ from django.utils.encoding import python_2_unicode_compatible
@python_2_unicode_compatible
class Author(models.Model):
+ Author_ID = models.AutoField(primary_key=True, db_column='Author ID')
first_name = models.CharField(max_length=30, db_column='firstname')
last_name = models.CharField(max_length=30, db_column='last')
@@ -36,8 +37,10 @@ class Author(models.Model):
@python_2_unicode_compatible
class Article(models.Model):
+ Article_ID = models.AutoField(primary_key=True, db_column='Article ID')
headline = models.CharField(max_length=100)
authors = models.ManyToManyField(Author, db_table='my_m2m_table')
+ primary_author = models.ForeignKey(Author, db_column='Author ID', related_name='primary_set', null=True)
def __str__(self):
return self.headline
diff --git a/tests/custom_columns/tests.py b/tests/custom_columns/tests.py
index aae3712e53..75bba4bf12 100644
--- a/tests/custom_columns/tests.py
+++ b/tests/custom_columns/tests.py
@@ -8,15 +8,14 @@ from .models import Author, Article
class CustomColumnsTests(TestCase):
- def setUp(self):
- a1 = Author.objects.create(first_name="John", last_name="Smith")
- a2 = Author.objects.create(first_name="Peter", last_name="Jones")
- art = Article.objects.create(headline="Django lets you build Web apps easily")
- art.authors = [a1, a2]
+ 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]
- self.a1 = a1
- self.art = art
+ self.article = Article.objects.create(headline="Django lets you build Web apps easily", primary_author=self.a1)
+ self.article.authors = self.authors
def test_query_all_available_authors(self):
self.assertQuerysetEqual(
@@ -38,7 +37,6 @@ class CustomColumnsTests(TestCase):
"John Smith",
],
six.text_type
-
)
def test_field_error(self):
@@ -56,7 +54,7 @@ class CustomColumnsTests(TestCase):
def test_get_all_authors_for_an_article(self):
self.assertQuerysetEqual(
- self.art.authors.all(), [
+ self.article.authors.all(), [
"Peter Jones",
"John Smith",
],
@@ -73,8 +71,63 @@ class CustomColumnsTests(TestCase):
def test_get_author_m2m_relation(self):
self.assertQuerysetEqual(
- self.art.authors.filter(last_name='Jones'), [
+ self.article.authors.filter(last_name='Jones'), [
"Peter Jones"
],
six.text_type
)
+
+ 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'))
+
+ def test_filter_on_nonexistant_field(self):
+ self.assertRaisesMessage(
+ FieldError,
+ "Cannot resolve keyword 'firstname' into field. Choices are: Author_ID, article, first_name, last_name, primary_set",
+ Author.objects.filter,
+ firstname__exact='John'
+ )
+
+ 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)
+ self.assertRaisesMessage(
+ AttributeError,
+ "'Author' object has no attribute 'firstname'",
+ getattr,
+ a, 'firstname'
+ )
+
+ self.assertRaisesMessage(
+ AttributeError,
+ "'Author' object has no attribute 'last'",
+ getattr,
+ a, 'last'
+ )
+
+ 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>']
+ )