From 184dd0516267c58370d6a88afb1c1ce894b2b7c1 Mon Sep 17 00:00:00 2001 From: Raphaël Barrois Date: Mon, 9 Apr 2012 13:31:13 +0200 Subject: Cleaner extract_dict: allow excluded keys. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Raphaël Barrois --- tests/test_utils.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/test_utils.py b/tests/test_utils.py index c3047d3..2c77c15 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -68,7 +68,17 @@ class ExtractDictTestCase(unittest.TestCase): self.assertEqual({'baz': 42}, utils.extract_dict('foo', d, pop=True)) self.assertNotIn('foo__baz', d) - def test_many_key(self): + def test_one_key_excluded(self): + d = {'foo': 13, 'foo__baz': 42, '__foo': 1} + self.assertEqual({}, + utils.extract_dict('foo', d, pop=False, exclude=('foo__baz',))) + self.assertEqual(42, d['foo__baz']) + + self.assertEqual({}, + utils.extract_dict('foo', d, pop=True, exclude=('foo__baz',))) + self.assertIn('foo__baz', d) + + def test_many_keys(self): d = {'foo': 13, 'foo__baz': 42, 'foo__foo__bar': 2, 'foo__bar': 3, '__foo': 1} self.assertEqual({'foo__bar': 2, 'bar': 3, 'baz': 42}, utils.extract_dict('foo', d, pop=False)) @@ -82,6 +92,20 @@ class ExtractDictTestCase(unittest.TestCase): self.assertNotIn('foo__bar', d) self.assertNotIn('foo__foo__bar', d) + def test_many_keys_excluded(self): + d = {'foo': 13, 'foo__baz': 42, 'foo__foo__bar': 2, 'foo__bar': 3, '__foo': 1} + self.assertEqual({'foo__bar': 2, 'baz': 42}, + utils.extract_dict('foo', d, pop=False, exclude=('foo__bar', 'bar'))) + self.assertEqual(42, d['foo__baz']) + self.assertEqual(3, d['foo__bar']) + self.assertEqual(2, d['foo__foo__bar']) + + self.assertEqual({'foo__bar': 2, 'baz': 42}, + utils.extract_dict('foo', d, pop=True, exclude=('foo__bar', 'bar'))) + self.assertNotIn('foo__baz', d) + self.assertIn('foo__bar', d) + self.assertNotIn('foo__foo__bar', d) + class MultiExtractDictTestCase(unittest.TestCase): def test_empty_dict(self): self.assertEqual({'foo': {}}, utils.multi_extract_dict(['foo'], {})) @@ -182,9 +206,9 @@ class MultiExtractDictTestCase(unittest.TestCase): self.assertEqual( { 'foo__foo': {'bar': 2}, - 'foo': {'bar': 3, 'baz': 42, 'foo__bar': 2}, + 'foo': {'bar': 3, 'baz': 42}, 'bar__bar': {'baz': 4}, - 'bar': {'foo': 1, 'bar__baz': 4}, + 'bar': {'foo': 1}, 'baz': {} }, utils.multi_extract_dict( -- cgit v1.2.3