Ticket #16955: 16955-2.patch

File 16955-2.patch, 6.5 KB (added by Nate Bragg, 13 years ago)

Fixed PEP8ness and tests, per my previous comment

  • django/db/models/sql/query.py

    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  
    1414from django.utils.tree import Node
    1515from django.db import connections, DEFAULT_DB_ALIAS
    1616from django.db.models import signals
     17from django.db.models.base import Model
    1718from django.db.models.expressions import ExpressionNode
    1819from django.db.models.fields import FieldDoesNotExist
     20from django.db.models.fields.related import RelatedField
    1921from django.db.models.query_utils import InvalidQuery
    2022from django.db.models.sql import aggregates as base_aggregates_module
    2123from django.db.models.sql.constants import *
    class Query(object):  
    11081110                    can_reuse)
    11091111            return
    11101112
     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
    11111117        table_promote = False
    11121118        join_promote = False
    11131119
  • tests/modeltests/many_to_many/tests.py

    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):  
    199199        self.assertQuerysetEqual(Article.objects.exclude(publications=self.p2),
    200200                                 ['<Article: Django lets you build Web apps easily>'])
    201201
     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
    202207    def test_reverse_selects(self):
    203208        # Reverse m2m queries are supported (i.e., starting at the table that
    204209        # doesn't have a ManyToManyField).
    class ManyToManyTests(TestCase):  
    249254                '<Publication: The Python Journal>',
    250255            ])
    251256
     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
    252262    def test_delete(self):
    253263        # If we delete a Publication, its Articles won't be able to access it.
    254264        self.p1.delete()
  • tests/modeltests/many_to_one/tests.py

    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):  
    232232                "<Article: This is a test>",
    233233            ])
    234234
     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
    235239    def test_reverse_selects(self):
    236240        a3 = Article.objects.create(id=None, headline="Third article",
    237241                                    pub_date=datetime(2005, 7, 27), reporter_id=self.r.id)
    class ManyToOneTests(TestCase):  
    303307            list(Article.objects.filter(reporter=self.r).distinct().order_by()
    304308                 .values('reporter__first_name', 'reporter__last_name')))
    305309
     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
    306314    def test_select_related(self):
    307315        # Check that Article.objects.select_related().dates() works properly when
    308316        # there are multiple Articles with the same date but different foreign-key
  • tests/modeltests/one_to_one/tests.py

    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):  
    6565        assert_get_restaurant(place__pk=self.p1.pk)
    6666        assert_get_restaurant(place__name__startswith="Demon")
    6767
     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
    6872        def assert_get_place(**params):
    6973            self.assertEqual(repr(Place.objects.get(**params)),
    7074                             '<Place: Demon Dogs the place>')
    class OneToOneTests(TestCase):  
    7983        assert_get_place(id__exact=self.p1.pk)
    8084        assert_get_place(pk=self.p1.pk)
    8185
     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
    8290    def test_foreign_key(self):
    8391        # Add a Waiter to the Restaurant.
    8492        w = self.r.waiter_set.create(name='Joe')
    class OneToOneTests(TestCase):  
    92100        assert_filter_waiters(restaurant__place__exact=self.p1.pk)
    93101        assert_filter_waiters(restaurant__place__exact=self.p1)
    94102        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)
    99107        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)
    102110        # 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)
    104112        r.delete()
    105113        self.assertEqual(Waiter.objects.count(), 0)
    106114
Back to Top