diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2012-04-09 13:31:13 +0200 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polyconseil.fr> | 2012-04-15 10:05:19 +0200 |
commit | 184dd0516267c58370d6a88afb1c1ce894b2b7c1 (patch) | |
tree | 939be03111e6bbf31ebae88763178df5b4d5ed43 /tests | |
parent | 223c31ce7dd962f43c580c89fa1966939557bc5c (diff) | |
download | factory-boy-184dd0516267c58370d6a88afb1c1ce894b2b7c1.tar factory-boy-184dd0516267c58370d6a88afb1c1ce894b2b7c1.tar.gz |
Cleaner extract_dict: allow excluded keys.
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_utils.py | 30 |
1 files changed, 27 insertions, 3 deletions
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( |