From 59d127e45f5ed31e346162a0ac312f672c096821 Mon Sep 17 00:00:00 2001 From: Anton Baklanov Date: Wed, 17 Apr 2013 18:20:31 +0300 Subject: Fixed #20276 -- Implemented __bool__ for MergeDict MergeDict evaluates now to False if all contained dicts are empty. Thanks til for the report and the initial patch. --- tests/utils_tests/test_datastructures.py | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'tests/utils_tests') diff --git a/tests/utils_tests/test_datastructures.py b/tests/utils_tests/test_datastructures.py index 3161c04f97..91111cc2ed 100644 --- a/tests/utils_tests/test_datastructures.py +++ b/tests/utils_tests/test_datastructures.py @@ -203,6 +203,13 @@ class MergeDictTests(SimpleTestCase): ('key2', ['value2', 'value3']), ('key4', ['value5', 'value6'])]) + def test_bool_casting(self): + empty = MergeDict({}, {}, {}) + not_empty = MergeDict({}, {}, {"key": "value"}) + self.assertFalse(empty) + self.assertTrue(not_empty) + + class MultiValueDictTests(SimpleTestCase): def test_multivaluedict(self): -- cgit v1.3 From 714161c8642646f1f617436479313ca49c71b5c8 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Fri, 19 Apr 2013 10:58:29 -0700 Subject: Fix != operations on lazy objects. --- django/utils/functional.py | 1 + tests/utils_tests/test_simplelazyobject.py | 9 +++++++++ 2 files changed, 10 insertions(+) (limited to 'tests/utils_tests') diff --git a/django/utils/functional.py b/django/utils/functional.py index 13aec9cb82..1592828c7f 100644 --- a/django/utils/functional.py +++ b/django/utils/functional.py @@ -346,6 +346,7 @@ class SimpleLazyObject(LazyObject): # care about this (especially in equality tests) __class__ = property(new_method_proxy(operator.attrgetter("__class__"))) __eq__ = new_method_proxy(operator.eq) + __ne__ = new_method_proxy(operator.ne) __hash__ = new_method_proxy(hash) __bool__ = new_method_proxy(bool) # Python 3 __nonzero__ = __bool__ # Python 2 diff --git a/tests/utils_tests/test_simplelazyobject.py b/tests/utils_tests/test_simplelazyobject.py index 883e60aa81..f925e01eb6 100644 --- a/tests/utils_tests/test_simplelazyobject.py +++ b/tests/utils_tests/test_simplelazyobject.py @@ -152,3 +152,12 @@ class TestUtilsSimpleLazyObject(TestCase): SimpleLazyObject(None) finally: sys.settrace(old_trace_func) + + def test_not_equal(self): + lazy1 = SimpleLazyObject(lambda: 2) + lazy2 = SimpleLazyObject(lambda: 2) + lazy3 = SimpleLazyObject(lambda: 3) + self.assertEqual(lazy1, lazy2) + self.assertNotEqual(lazy1, lazy3) + self.assertTrue(lazy1 != lazy3) + self.assertFalse(lazy1 != lazy2) -- cgit v1.3 From 4769db6b5fddaed93a8d8d03d0c36f7262e9ac8b Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Fri, 26 Apr 2013 08:57:39 +0200 Subject: Fixed #20321 -- Added missing key name in MergeDict KeyError message Thanks mark.harviston et gmail.com for the report. --- django/utils/datastructures.py | 2 +- tests/utils_tests/test_datastructures.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) (limited to 'tests/utils_tests') diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py index b3060202be..a211323320 100644 --- a/django/utils/datastructures.py +++ b/django/utils/datastructures.py @@ -26,7 +26,7 @@ class MergeDict(object): return dict_[key] except KeyError: pass - raise KeyError + raise KeyError(key) def __copy__(self): return self.__class__(*self.dicts) diff --git a/tests/utils_tests/test_datastructures.py b/tests/utils_tests/test_datastructures.py index 91111cc2ed..5829e7c2d7 100644 --- a/tests/utils_tests/test_datastructures.py +++ b/tests/utils_tests/test_datastructures.py @@ -209,6 +209,14 @@ class MergeDictTests(SimpleTestCase): self.assertFalse(empty) self.assertTrue(not_empty) + def test_key_error(self): + """ + Test that the message of KeyError contains the missing key name. + """ + d1 = MergeDict({'key1': 42}) + with six.assertRaisesRegex(self, KeyError, 'key2'): + d1['key2'] + class MultiValueDictTests(SimpleTestCase): -- cgit v1.3