From c34d01278bd9cadff3f1ebea4579ef556cf75480 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Thu, 16 Aug 2012 17:29:59 +0200 Subject: Add full tests for CircularSubFactory. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Raphaël Barrois --- tests/cyclic/__init__.py | 0 tests/cyclic/bar.py | 37 +++++++++++++++++++++++++++++++++++++ tests/cyclic/foo.py | 38 ++++++++++++++++++++++++++++++++++++++ tests/test_using.py | 22 ++++++++++++++++++++++ tests/test_utils.py | 3 +++ 5 files changed, 100 insertions(+) create mode 100644 tests/cyclic/__init__.py create mode 100644 tests/cyclic/bar.py create mode 100644 tests/cyclic/foo.py diff --git a/tests/cyclic/__init__.py b/tests/cyclic/__init__.py new file mode 100644 index 0000000..e69de29 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') -- cgit v1.2.3