From 39b1c73f7743f5a6d26d161db0d79f8de4402f78 Mon Sep 17 00:00:00 2001
From: Nate Bragg <jonathan.bragg@alum.rpi.edu>
Date: Tue, 17 Jan 2012 22:28:55 -0500
Subject: [PATCH] Modification of dgouldin's patch that fixes PEP8ness and
tests.
---
django/db/models/sql/query.py | 6 ++++++
tests/modeltests/many_to_many/tests.py | 10 ++++++++++
tests/modeltests/many_to_one/tests.py | 8 ++++++++
tests/modeltests/one_to_one/tests.py | 22 +++++++++++++++-------
4 files changed, 39 insertions(+), 7 deletions(-)
diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
index ed2bc06..f53bd4f 100644
a
|
b
|
from django.utils.encoding import force_unicode
|
14 | 14 | from django.utils.tree import Node |
15 | 15 | from django.db import connections, DEFAULT_DB_ALIAS |
16 | 16 | from django.db.models import signals |
| 17 | from django.db.models.base import Model |
17 | 18 | from django.db.models.expressions import ExpressionNode |
18 | 19 | from django.db.models.fields import FieldDoesNotExist |
| 20 | from django.db.models.fields.related import RelatedField |
19 | 21 | from django.db.models.query_utils import InvalidQuery |
20 | 22 | from django.db.models.sql import aggregates as base_aggregates_module |
21 | 23 | from django.db.models.sql.constants import * |
… |
… |
class Query(object):
|
1108 | 1110 | can_reuse) |
1109 | 1111 | return |
1110 | 1112 | |
| 1113 | if (isinstance(field, RelatedField) and isinstance(value, Model) and |
| 1114 | not isinstance(value, target.model)): |
| 1115 | raise TypeError, "'%s' instance expected" % target.model._meta.object_name |
| 1116 | |
1111 | 1117 | table_promote = False |
1112 | 1118 | join_promote = False |
1113 | 1119 | |
diff --git a/tests/modeltests/many_to_many/tests.py b/tests/modeltests/many_to_many/tests.py
index b00d7da..6370eb0 100644
a
|
b
|
class ManyToManyTests(TestCase):
|
199 | 199 | self.assertQuerysetEqual(Article.objects.exclude(publications=self.p2), |
200 | 200 | ['<Article: Django lets you build Web apps easily>']) |
201 | 201 | |
| 202 | # Filter values on related fields are checked to ensure the correct |
| 203 | # model class is being used. |
| 204 | self.assertRaises(TypeError, Article.objects.filter, |
| 205 | publications=self.a1) |
| 206 | |
202 | 207 | def test_reverse_selects(self): |
203 | 208 | # Reverse m2m queries are supported (i.e., starting at the table that |
204 | 209 | # doesn't have a ManyToManyField). |
… |
… |
class ManyToManyTests(TestCase):
|
249 | 254 | '<Publication: The Python Journal>', |
250 | 255 | ]) |
251 | 256 | |
| 257 | # Filter values on related fields are checked to ensure the correct |
| 258 | # model class is being used. |
| 259 | self.assertRaises(TypeError, Publication.objects.filter, |
| 260 | article=self.p1) |
| 261 | |
252 | 262 | def test_delete(self): |
253 | 263 | # If we delete a Publication, its Articles won't be able to access it. |
254 | 264 | self.p1.delete() |
diff --git a/tests/modeltests/many_to_one/tests.py b/tests/modeltests/many_to_one/tests.py
index 922506e..b43bf03 100644
a
|
b
|
class ManyToOneTests(TestCase):
|
232 | 232 | "<Article: This is a test>", |
233 | 233 | ]) |
234 | 234 | |
| 235 | # Filter values on related fields are checked to ensure the correct |
| 236 | # model class is being used. |
| 237 | self.assertRaises(TypeError, Article.objects.filter, reporter=self.a) |
| 238 | |
235 | 239 | def test_reverse_selects(self): |
236 | 240 | a3 = Article.objects.create(id=None, headline="Third article", |
237 | 241 | pub_date=datetime(2005, 7, 27), reporter_id=self.r.id) |
… |
… |
class ManyToOneTests(TestCase):
|
303 | 307 | list(Article.objects.filter(reporter=self.r).distinct().order_by() |
304 | 308 | .values('reporter__first_name', 'reporter__last_name'))) |
305 | 309 | |
| 310 | # Filter values on related fields are checked to ensure the correct |
| 311 | # model class is being used. |
| 312 | self.assertRaises(TypeError, Reporter.objects.filter, article=self.r) |
| 313 | |
306 | 314 | def test_select_related(self): |
307 | 315 | # Check that Article.objects.select_related().dates() works properly when |
308 | 316 | # there are multiple Articles with the same date but different foreign-key |
diff --git a/tests/modeltests/one_to_one/tests.py b/tests/modeltests/one_to_one/tests.py
index 6ee7852..6800aef 100644
a
|
b
|
class OneToOneTests(TestCase):
|
65 | 65 | assert_get_restaurant(place__pk=self.p1.pk) |
66 | 66 | assert_get_restaurant(place__name__startswith="Demon") |
67 | 67 | |
| 68 | # Filter values on related fields are checked to ensure the correct |
| 69 | # model class is being used. |
| 70 | self.assertRaises(TypeError, Restaurant.objects.get, place=self.r) |
| 71 | |
68 | 72 | def assert_get_place(**params): |
69 | 73 | self.assertEqual(repr(Place.objects.get(**params)), |
70 | 74 | '<Place: Demon Dogs the place>') |
… |
… |
class OneToOneTests(TestCase):
|
79 | 83 | assert_get_place(id__exact=self.p1.pk) |
80 | 84 | assert_get_place(pk=self.p1.pk) |
81 | 85 | |
| 86 | # Filter values on related fields are checked to ensure the correct |
| 87 | # model class is being used. |
| 88 | self.assertRaises(TypeError, Place.objects.get, restaurant=self.p1) |
| 89 | |
82 | 90 | def test_foreign_key(self): |
83 | 91 | # Add a Waiter to the Restaurant. |
84 | 92 | w = self.r.waiter_set.create(name='Joe') |
… |
… |
class OneToOneTests(TestCase):
|
92 | 100 | assert_filter_waiters(restaurant__place__exact=self.p1.pk) |
93 | 101 | assert_filter_waiters(restaurant__place__exact=self.p1) |
94 | 102 | assert_filter_waiters(restaurant__place__pk=self.p1.pk) |
95 | | assert_filter_waiters(restaurant__exact=self.p1.pk) |
96 | | assert_filter_waiters(restaurant__exact=self.p1) |
97 | | assert_filter_waiters(restaurant__pk=self.p1.pk) |
98 | | assert_filter_waiters(restaurant=self.p1.pk) |
| 103 | assert_filter_waiters(restaurant__exact=self.r.pk) |
| 104 | assert_filter_waiters(restaurant__exact=self.r) |
| 105 | assert_filter_waiters(restaurant__pk=self.r.pk) |
| 106 | assert_filter_waiters(restaurant=self.r.pk) |
99 | 107 | assert_filter_waiters(restaurant=self.r) |
100 | | assert_filter_waiters(id__exact=self.p1.pk) |
101 | | assert_filter_waiters(pk=self.p1.pk) |
| 108 | assert_filter_waiters(id__exact=self.r.pk) |
| 109 | assert_filter_waiters(pk=self.r.pk) |
102 | 110 | # Delete the restaurant; the waiter should also be removed |
103 | | r = Restaurant.objects.get(pk=self.p1.pk) |
| 111 | r = Restaurant.objects.get(pk=self.r.pk) |
104 | 112 | r.delete() |
105 | 113 | self.assertEqual(Waiter.objects.count(), 0) |
106 | 114 | |