summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2012-08-16 17:29:59 +0200
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2012-08-16 22:38:42 +0200
commitc34d01278bd9cadff3f1ebea4579ef556cf75480 (patch)
tree730d40aed20ad27e665329cd34694a7cdc5861d2
parentbab73174ff9c6c8ef4bfa24142d18f04411cb342 (diff)
downloadfactory-boy-c34d01278bd9cadff3f1ebea4579ef556cf75480.tar
factory-boy-c34d01278bd9cadff3f1ebea4579ef556cf75480.tar.gz
Add full tests for CircularSubFactory.
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
-rw-r--r--tests/cyclic/__init__.py0
-rw-r--r--tests/cyclic/bar.py37
-rw-r--r--tests/cyclic/foo.py38
-rw-r--r--tests/test_using.py22
-rw-r--r--tests/test_utils.py3
5 files changed, 100 insertions, 0 deletions
diff --git a/tests/cyclic/__init__.py b/tests/cyclic/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/cyclic/__init__.py
diff --git a/tests/cyclic/bar.py b/tests/cyclic/bar.py
new file mode 100644
index 0000000..cc90930
--- /dev/null
+++ b/tests/cyclic/bar.py
@@ -0,0 +1,37 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2011-2012 Raphaël Barrois
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+
+"""Helper to test circular factory dependencies."""
+
+import factory
+
+class Bar(object):
+ def __init__(self, foo, y):
+ self.foo = foo
+ self.y = y
+
+
+class BarFactory(factory.Factory):
+ FACTORY_FOR = Bar
+
+ y = 13
+ foo = factory.CircularSubFactory('cyclic.foo', 'FooFactory')
+
diff --git a/tests/cyclic/foo.py b/tests/cyclic/foo.py
new file mode 100644
index 0000000..e6f8896
--- /dev/null
+++ b/tests/cyclic/foo.py
@@ -0,0 +1,38 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2011-2012 Raphaël Barrois
+#
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in
+# all copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+# THE SOFTWARE.
+
+"""Helper to test circular factory dependencies."""
+
+import factory
+
+from cyclic import bar
+
+class Foo(object):
+ def __init__(self, bar, x):
+ self.bar = bar
+ self.x = x
+
+
+class FooFactory(factory.Factory):
+ FACTORY_FOR = Foo
+
+ x = 42
+ bar = factory.SubFactory(bar.BarFactory)
diff --git a/tests/test_using.py b/tests/test_using.py
index f4d5440..1d0b875 100644
--- a/tests/test_using.py
+++ b/tests/test_using.py
@@ -22,6 +22,8 @@
import factory
+import os
+import sys
from .compat import unittest
@@ -953,5 +955,25 @@ class PostGenerationTestCase(unittest.TestCase):
self.assertEqual(obj, obj.related.three)
+class CircularTestCase(unittest.TestCase):
+ def test_example(self):
+ sys.path.insert(0, os.path.abspath(os.path.dirname(__file__)))
+
+ from cyclic import foo
+ f = foo.FooFactory.build(bar__foo=None)
+ self.assertEqual(42, f.x)
+ self.assertEqual(13, f.bar.y)
+ self.assertIsNone(f.bar.foo)
+
+ from cyclic import bar
+ b = bar.BarFactory.build(foo__bar__foo__bar=None)
+ self.assertEqual(13, b.y)
+ self.assertEqual(42, b.foo.x)
+ self.assertEqual(13, b.foo.bar.y)
+ self.assertEqual(42, b.foo.bar.foo.x)
+ self.assertIsNone(b.foo.bar.foo.bar)
+
+
+
if __name__ == '__main__':
unittest.main()
diff --git a/tests/test_utils.py b/tests/test_utils.py
index dbc357b..9aaafc1 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -25,6 +25,7 @@ from factory import utils
from .compat import unittest
+
class ExtractDictTestCase(unittest.TestCase):
def test_empty_dict(self):
self.assertEqual({}, utils.extract_dict('foo', {}))
@@ -84,6 +85,7 @@ class ExtractDictTestCase(unittest.TestCase):
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'], {}))
@@ -213,6 +215,7 @@ class MultiExtractDictTestCase(unittest.TestCase):
self.assertNotIn('bar__foo', d)
self.assertNotIn('bar__bar__baz', d)
+
class ImportObjectTestCase(unittest.TestCase):
def test_datetime(self):
imported = utils.import_object('datetime', 'date')