summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2014-11-02 16:32:16 +0100
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2014-11-02 16:32:16 +0100
commit70412551c545e94b27bef468cd248fce7a9cdd96 (patch)
tree62c237a950c4c2cc9a935deba34fad4f4d1dbc65 /tests
parentf81aba229a7492b11a331407f60fba1bd4d6522c (diff)
downloadfactory-boy-70412551c545e94b27bef468cd248fce7a9cdd96.tar
factory-boy-70412551c545e94b27bef468cd248fce7a9cdd96.tar.gz
Add tests for self-referential models (See #173).
Diffstat (limited to 'tests')
-rw-r--r--tests/cyclic/self_ref.py37
-rw-r--r--tests/test_using.py17
2 files changed, 54 insertions, 0 deletions
diff --git a/tests/cyclic/self_ref.py b/tests/cyclic/self_ref.py
new file mode 100644
index 0000000..f18c989
--- /dev/null
+++ b/tests/cyclic/self_ref.py
@@ -0,0 +1,37 @@
+# -*- coding: utf-8 -*-
+# Copyright (c) 2011-2013 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 TreeElement(object):
+ def __init__(self, name, parent):
+ self.parent = parent
+ self.name = name
+
+
+class TreeElementFactory(factory.Factory):
+ class Meta:
+ model = TreeElement
+
+ name = factory.Sequence(lambda n: "tree%s" % n)
+ parent = factory.SubFactory('tests.cyclic.self_ref.TreeElementFactory')
diff --git a/tests/test_using.py b/tests/test_using.py
index f18df4d..8d78789 100644
--- a/tests/test_using.py
+++ b/tests/test_using.py
@@ -1938,6 +1938,23 @@ class CircularTestCase(unittest.TestCase):
self.assertIsNone(b.foo.bar.foo.bar)
+class SelfReferentialTests(unittest.TestCase):
+ def test_no_parent(self):
+ from .cyclic import self_ref
+
+ obj = self_ref.TreeElementFactory(parent=None)
+ self.assertIsNone(obj.parent)
+
+ def test_deep(self):
+ from .cyclic import self_ref
+
+ obj = self_ref.TreeElementFactory(parent__parent__parent__parent=None)
+ self.assertIsNotNone(obj.parent)
+ self.assertIsNotNone(obj.parent.parent)
+ self.assertIsNotNone(obj.parent.parent.parent)
+ self.assertIsNone(obj.parent.parent.parent.parent)
+
+
class DictTestCase(unittest.TestCase):
def test_empty_dict(self):
class TestObjectFactory(factory.Factory):