summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2013-04-15 02:22:01 +0200
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2013-04-15 02:22:01 +0200
commit2b661e6eae3187c05c4eb8e1c3790cee6a9e3032 (patch)
treefb23ae16274eb071639b64792dff07f1d1302898 /tests
parente7a9a87320c78ec05a5d548516fe17c258e6d4c7 (diff)
downloadfactory-boy-2b661e6eae3187c05c4eb8e1c3790cee6a9e3032.tar
factory-boy-2b661e6eae3187c05c4eb8e1c3790cee6a9e3032.tar.gz
Add Dict/List declarations (Closes #18).
Diffstat (limited to 'tests')
-rw-r--r--tests/test_base.py10
-rw-r--r--tests/test_using.py166
2 files changed, 176 insertions, 0 deletions
diff --git a/tests/test_base.py b/tests/test_base.py
index 969ef13..73e59fa 100644
--- a/tests/test_base.py
+++ b/tests/test_base.py
@@ -63,6 +63,15 @@ class SafetyTestCase(unittest.TestCase):
self.assertRaises(base.FactoryError, base.BaseFactory)
+class AbstractFactoryTestCase(unittest.TestCase):
+ def test_factory_for_optional(self):
+ """Ensure that FACTORY_FOR is optional for ABSTRACT_FACTORY."""
+ class TestObjectFactory(base.Factory):
+ ABSTRACT_FACTORY = True
+
+ # Passed
+
+
class FactoryTestCase(unittest.TestCase):
def test_factory_for(self):
class TestObjectFactory(base.Factory):
@@ -106,6 +115,7 @@ class FactoryTestCase(unittest.TestCase):
ones = set([x.one for x in (parent, alt_parent, sub, alt_sub)])
self.assertEqual(4, len(ones))
+
class FactoryDefaultStrategyTestCase(unittest.TestCase):
def setUp(self):
self.default_strategy = base.Factory.FACTORY_STRATEGY
diff --git a/tests/test_using.py b/tests/test_using.py
index def49e4..c46bf2f 100644
--- a/tests/test_using.py
+++ b/tests/test_using.py
@@ -1591,6 +1591,172 @@ class CircularTestCase(unittest.TestCase):
self.assertIsNone(b.foo.bar.foo.bar)
+class DictTestCase(unittest.TestCase):
+ def test_empty_dict(self):
+ class TestObjectFactory(factory.Factory):
+ FACTORY_FOR = TestObject
+ one = factory.Dict({})
+
+ o = TestObjectFactory()
+ self.assertEqual({}, o.one)
+
+ def test_naive_dict(self):
+ class TestObjectFactory(factory.Factory):
+ FACTORY_FOR = TestObject
+ one = factory.Dict({'a': 1})
+
+ o = TestObjectFactory()
+ self.assertEqual({'a': 1}, o.one)
+
+ def test_sequence_dict(self):
+ class TestObjectFactory(factory.Factory):
+ FACTORY_FOR = TestObject
+ one = factory.Dict({'a': factory.Sequence(lambda n: n + 2)})
+
+ o1 = TestObjectFactory()
+ o2 = TestObjectFactory()
+
+ self.assertEqual({'a': 2}, o1.one)
+ self.assertEqual({'a': 3}, o2.one)
+
+ def test_dict_override(self):
+ class TestObjectFactory(factory.Factory):
+ FACTORY_FOR = TestObject
+ one = factory.Dict({'a': 1})
+
+ o = TestObjectFactory(one__a=2)
+ self.assertEqual({'a': 2}, o.one)
+
+ def test_dict_extra_key(self):
+ class TestObjectFactory(factory.Factory):
+ FACTORY_FOR = TestObject
+ one = factory.Dict({'a': 1})
+
+ o = TestObjectFactory(one__b=2)
+ self.assertEqual({'a': 1, 'b': 2}, o.one)
+
+ def test_dict_merged_fields(self):
+ class TestObjectFactory(factory.Factory):
+ FACTORY_FOR = TestObject
+ two = 13
+ one = factory.Dict({
+ 'one': 1,
+ 'two': 2,
+ 'three': factory.SelfAttribute('two'),
+ })
+
+ o = TestObjectFactory(one__one=42)
+ self.assertEqual({'one': 42, 'two': 2, 'three': 2}, o.one)
+
+ def test_nested_dicts(self):
+ class TestObjectFactory(factory.Factory):
+ FACTORY_FOR = TestObject
+ one = 1
+ two = factory.Dict({
+ 'one': 3,
+ 'two': factory.SelfAttribute('one'),
+ 'three': factory.Dict({
+ 'one': 5,
+ 'two': factory.SelfAttribute('..one'),
+ 'three': factory.SelfAttribute('...one'),
+ }),
+ })
+
+ o = TestObjectFactory()
+ self.assertEqual(1, o.one)
+ self.assertEqual({
+ 'one': 3,
+ 'two': 3,
+ 'three': {
+ 'one': 5,
+ 'two': 3,
+ 'three': 1,
+ },
+ }, o.two)
+
+
+class ListTestCase(unittest.TestCase):
+ def test_empty_list(self):
+ class TestObjectFactory(factory.Factory):
+ FACTORY_FOR = TestObject
+ one = factory.List([])
+
+ o = TestObjectFactory()
+ self.assertEqual([], o.one)
+
+ def test_naive_list(self):
+ class TestObjectFactory(factory.Factory):
+ FACTORY_FOR = TestObject
+ one = factory.List([1])
+
+ o = TestObjectFactory()
+ self.assertEqual([1], o.one)
+
+ def test_sequence_list(self):
+ class TestObjectFactory(factory.Factory):
+ FACTORY_FOR = TestObject
+ one = factory.List([factory.Sequence(lambda n: n + 2)])
+
+ o1 = TestObjectFactory()
+ o2 = TestObjectFactory()
+
+ self.assertEqual([2], o1.one)
+ self.assertEqual([3], o2.one)
+
+ def test_list_override(self):
+ class TestObjectFactory(factory.Factory):
+ FACTORY_FOR = TestObject
+ one = factory.List([1])
+
+ o = TestObjectFactory(one__0=2)
+ self.assertEqual([2], o.one)
+
+ def test_list_extra_key(self):
+ class TestObjectFactory(factory.Factory):
+ FACTORY_FOR = TestObject
+ one = factory.List([1])
+
+ o = TestObjectFactory(one__1=2)
+ self.assertEqual([1, 2], o.one)
+
+ def test_list_merged_fields(self):
+ class TestObjectFactory(factory.Factory):
+ FACTORY_FOR = TestObject
+ two = 13
+ one = factory.List([
+ 1,
+ 2,
+ factory.SelfAttribute('1'),
+ ])
+
+ o = TestObjectFactory(one__0=42)
+ self.assertEqual([42, 2, 2], o.one)
+
+ def test_nested_lists(self):
+ class TestObjectFactory(factory.Factory):
+ FACTORY_FOR = TestObject
+ one = 1
+ two = factory.List([
+ 3,
+ factory.SelfAttribute('0'),
+ factory.List([
+ 5,
+ factory.SelfAttribute('..0'),
+ factory.SelfAttribute('...one'),
+ ]),
+ ])
+
+ o = TestObjectFactory()
+ self.assertEqual(1, o.one)
+ self.assertEqual([
+ 3,
+ 3,
+ [
+ 5,
+ 3,
+ 1,
+ ],
+ ], o.two)
if __name__ == '__main__':
unittest.main()