summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2014-05-18 12:40:25 +0200
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2014-05-18 14:18:49 +0200
commitd26f41368e7c8936306cf1c34d73fff40d958128 (patch)
treefa80cfba7b1ccfaddd34eefde096b38fe9c78c13
parent395744736691b3412dbedaffa8735e248a70c3c1 (diff)
downloadfactory-boy-d26f41368e7c8936306cf1c34d73fff40d958128.tar
factory-boy-d26f41368e7c8936306cf1c34d73fff40d958128.tar.gz
Remove containers.DeclarationsDict.
Now replaced with a simple dict.
-rw-r--r--factory/containers.py55
-rw-r--r--tests/test_containers.py101
2 files changed, 1 insertions, 155 deletions
diff --git a/factory/containers.py b/factory/containers.py
index 4537e44..c0c5e24 100644
--- a/factory/containers.py
+++ b/factory/containers.py
@@ -121,61 +121,6 @@ class LazyStub(object):
raise AttributeError('Setting of object attributes is not allowed')
-class DeclarationDict(dict):
- """Slightly extended dict to work with OrderedDeclaration."""
-
- def is_declaration(self, name, value):
- """Determines if a class attribute is a field value declaration.
-
- Based on the name and value of the class attribute, return ``True`` if
- it looks like a declaration of a default field value, ``False`` if it
- is private (name starts with '_') or a classmethod or staticmethod.
-
- """
- if isinstance(value, (classmethod, staticmethod)):
- return False
- elif isinstance(value, declarations.OrderedDeclaration):
- return True
- return (not name.startswith("_") and not name.startswith("FACTORY_"))
-
- def update_with_public(self, d):
- """Updates the DeclarationDict from a class definition dict.
-
- Takes into account all public attributes and OrderedDeclaration
- instances; ignores all class/staticmethods and private attributes
- (starting with '_').
-
- Returns a dict containing all remaining elements.
- """
- remaining = {}
- for k, v in d.items():
- if self.is_declaration(k, v):
- self[k] = v
- else:
- remaining[k] = v
- return remaining
-
- def copy(self, extra=None):
- """Copy this DeclarationDict into another one, including extra values.
-
- Args:
- extra (dict): additional attributes to include in the copy.
- """
- new = self.__class__()
- new.update(self)
- if extra:
- new.update(extra)
- return new
-
-
-class PostGenerationDeclarationDict(DeclarationDict):
- """Alternate DeclarationDict for PostGenerationDeclaration."""
-
- def is_declaration(self, name, value):
- """Captures instances of PostGenerationDeclaration."""
- return isinstance(value, declarations.PostGenerationDeclaration)
-
-
class LazyValue(object):
"""Some kind of "lazy evaluating" object."""
diff --git a/tests/test_containers.py b/tests/test_containers.py
index 8b78dc7..8a9e990 100644
--- a/tests/test_containers.py
+++ b/tests/test_containers.py
@@ -100,105 +100,6 @@ class LazyStubTestCase(unittest.TestCase):
self.assertIn('one', str(stub))
-class OrderedDeclarationMock(declarations.OrderedDeclaration):
- pass
-
-
-class DeclarationDictTestCase(unittest.TestCase):
- def test_basics(self):
- one = OrderedDeclarationMock()
- two = 2
- three = OrderedDeclarationMock()
-
- d = containers.DeclarationDict(dict(one=one, two=two, three=three))
-
- self.assertTrue('one' in d)
- self.assertTrue('two' in d)
- self.assertTrue('three' in d)
-
- self.assertEqual(one, d['one'])
- self.assertEqual(two, d['two'])
- self.assertEqual(three, d['three'])
-
- self.assertEqual(one, d.pop('one'))
- self.assertFalse('one' in d)
-
- d['one'] = one
- self.assertTrue('one' in d)
- self.assertEqual(one, d['one'])
-
- self.assertEqual(set(['one', 'two', 'three']),
- set(d))
-
- def test_insert(self):
- one = OrderedDeclarationMock()
- two = 2
- three = OrderedDeclarationMock()
- four = OrderedDeclarationMock()
-
- d = containers.DeclarationDict(dict(one=one, two=two, four=four))
-
- self.assertEqual(set(['two', 'one', 'four']), set(d))
-
- d['three'] = three
- self.assertEqual(set(['two', 'one', 'three', 'four']), set(d))
-
- def test_replace(self):
- one = OrderedDeclarationMock()
- two = 2
- three = OrderedDeclarationMock()
- four = OrderedDeclarationMock()
-
- d = containers.DeclarationDict(dict(one=one, two=two, three=three))
-
- self.assertEqual(set(['two', 'one', 'three']), set(d))
-
- d['three'] = four
- self.assertEqual(set(['two', 'one', 'three']), set(d))
- self.assertEqual(set([two, one, four]), set(d.values()))
-
- def test_copy(self):
- one = OrderedDeclarationMock()
- two = 2
- three = OrderedDeclarationMock()
- four = OrderedDeclarationMock()
-
- d = containers.DeclarationDict(dict(one=one, two=two, three=three))
- d2 = d.copy({'five': 5})
-
- self.assertEqual(5, d2['five'])
- self.assertFalse('five' in d)
-
- d.pop('one')
- self.assertEqual(one, d2['one'])
-
- d2['two'] = four
- self.assertEqual(four, d2['two'])
- self.assertEqual(two, d['two'])
-
- def test_update_with_public(self):
- d = containers.DeclarationDict()
- d.update_with_public({
- 'one': 1,
- '_two': 2,
- 'three': 3,
- 'classmethod': classmethod(lambda c: 1),
- 'staticmethod': staticmethod(lambda: 1),
- })
- self.assertEqual(set(['one', 'three']), set(d))
- self.assertEqual(set([1, 3]), set(d.values()))
-
- def test_update_with_public_ignores_factory_attributes(self):
- """Ensure that a DeclarationDict ignores FACTORY_ keys."""
- d = containers.DeclarationDict()
- d.update_with_public({
- 'one': 1,
- 'FACTORY_FOR': 2,
- 'FACTORY_ARG_PARAMETERS': 3,
- })
- self.assertEqual(['one'], list(d))
- self.assertEqual([1], list(d.values()))
-
class AttributeBuilderTestCase(unittest.TestCase):
def test_empty(self):
@@ -320,7 +221,7 @@ class AttributeBuilderTestCase(unittest.TestCase):
class FakeFactory(object):
@classmethod
def declarations(cls, extra):
- d = containers.DeclarationDict({'one': 1, 'two': la})
+ d = {'one': 1, 'two': la}
d.update(extra)
return d