summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2012-08-09 02:16:42 +0200
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2012-08-09 02:16:42 +0200
commita31e87f19b7c193b980d0f54971c12a60e8c7263 (patch)
tree2ccf86db6921f19363176db6de561a452a212aea /tests
parente0e628ec54b8a4d7f41f86f9f535d791a437b13c (diff)
downloadfactory-boy-a31e87f19b7c193b980d0f54971c12a60e8c7263.tar
factory-boy-a31e87f19b7c193b980d0f54971c12a60e8c7263.tar.gz
Introduce 'CircularSubFactory.
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
Diffstat (limited to 'tests')
-rw-r--r--tests/test_declarations.py31
-rw-r--r--tests/test_utils.py15
2 files changed, 45 insertions, 1 deletions
diff --git a/tests/test_declarations.py b/tests/test_declarations.py
index 1c0502b..c0b3539 100644
--- a/tests/test_declarations.py
+++ b/tests/test_declarations.py
@@ -20,8 +20,9 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
+import datetime
-from factory.declarations import deepgetattr, OrderedDeclaration, \
+from factory.declarations import deepgetattr, CircularSubFactory, OrderedDeclaration, \
PostGenerationDeclaration, Sequence
from .compat import unittest
@@ -73,6 +74,34 @@ class PostGenerationDeclarationTestCase(unittest.TestCase):
self.assertEqual(kwargs, {'baz': 1})
+class CircularSubFactoryTestCase(unittest.TestCase):
+ def test_lazyness(self):
+ f = CircularSubFactory('factory.declarations', 'Sequence', x=3)
+ self.assertEqual(None, f.factory)
+
+ self.assertEqual({'x': 3}, f.defaults)
+
+ factory_class = f.get_factory()
+ self.assertEqual(Sequence, factory_class)
+
+ def test_cache(self):
+ orig_date = datetime.date
+ f = CircularSubFactory('datetime', 'date')
+ self.assertEqual(None, f.factory)
+
+ factory_class = f.get_factory()
+ self.assertEqual(orig_date, factory_class)
+
+ try:
+ # Modify original value
+ datetime.date = None
+ # Repeat import
+ factory_class = f.get_factory()
+ self.assertEqual(orig_date, factory_class)
+
+ finally:
+ # IMPORTANT: restore attribute.
+ datetime.date = orig_date
if __name__ == '__main__':
unittest.main()
diff --git a/tests/test_utils.py b/tests/test_utils.py
index f30c0e3..dbc357b 100644
--- a/tests/test_utils.py
+++ b/tests/test_utils.py
@@ -212,3 +212,18 @@ class MultiExtractDictTestCase(unittest.TestCase):
self.assertNotIn('foo__foo__bar', d)
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')
+ import datetime
+ d = datetime.date
+ self.assertEqual(d, imported)
+
+ def test_unknown_attribute(self):
+ self.assertRaises(AttributeError, utils.import_object,
+ 'datetime', 'foo')
+
+ def test_invalid_module(self):
+ self.assertRaises(ImportError, utils.import_object,
+ 'this-is-an-invalid-module', '__name__')