diff options
| author | Simon Charette <charette.s@gmail.com> | 2013-12-08 14:12:01 -0500 |
|---|---|---|
| committer | Simon Charette <charette.s@gmail.com> | 2013-12-11 12:49:28 -0500 |
| commit | 75924cfa6dca95aa1f02e38802df285271dc7c14 (patch) | |
| tree | 32c333713fbab97a3040850fe59a3725127ad487 /tests | |
| parent | c7c647419cb857fe53cf1368c10223c6e042c216 (diff) | |
Fixed #21563 -- Single related object descriptors should work with `hasattr`.
Thanks to Aymeric Augustin for the review and Trac alias monkut for the report.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/one_to_one/tests.py | 4 | ||||
| -rw-r--r-- | tests/reverse_single_related/tests.py | 13 |
2 files changed, 15 insertions, 2 deletions
diff --git a/tests/one_to_one/tests.py b/tests/one_to_one/tests.py index 84ada2cc51..d5d955b08e 100644 --- a/tests/one_to_one/tests.py +++ b/tests/one_to_one/tests.py @@ -25,6 +25,10 @@ class OneToOneTests(TestCase): # p2 doesn't have an associated restaurant. with self.assertRaisesMessage(Restaurant.DoesNotExist, 'Place has no restaurant'): self.p2.restaurant + # The exception raised on attribute access when a related object + # doesn't exist should be an instance of a subclass of `AttributeError` + # refs #21563 + self.assertFalse(hasattr(self.p2, 'restaurant')) def test_setter(self): # Set the place using assignment notation. Because place is the primary diff --git a/tests/reverse_single_related/tests.py b/tests/reverse_single_related/tests.py index fd1a5196fe..2a8b96ddbc 100644 --- a/tests/reverse_single_related/tests.py +++ b/tests/reverse_single_related/tests.py @@ -33,5 +33,14 @@ class ReverseSingleRelatedTests(TestCase): # of the "bare" queryset. Usually you'd define this as a property on the class, # but this approximates that in a way that's easier in tests. Source.objects.use_for_related_fields = True - private_item = Item.objects.get(pk=private_item.pk) - self.assertRaises(Source.DoesNotExist, lambda: private_item.source) + try: + private_item = Item.objects.get(pk=private_item.pk) + self.assertRaises(Source.DoesNotExist, lambda: private_item.source) + finally: + Source.objects.use_for_related_fields = False + + def test_hasattr_single_related(self): + # The exception raised on attribute access when a related object + # doesn't exist should be an instance of a subclass of `AttributeError` + # refs #21563 + self.assertFalse(hasattr(Item(), 'source')) |
