diff options
author | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-03-03 22:10:42 +0100 |
---|---|---|
committer | Raphaël Barrois <raphael.barrois@polytechnique.org> | 2013-03-03 22:10:42 +0100 |
commit | 9422cf12516143650f1014f34f996260c00d4c0a (patch) | |
tree | 879113f8d65560dc9fd435872ffbf1cc44af01de /tests | |
parent | f8708d936be1aa53a8b61f95cda6edcdbd8fc00a (diff) | |
download | factory-boy-9422cf12516143650f1014f34f996260c00d4c0a.tar factory-boy-9422cf12516143650f1014f34f996260c00d4c0a.tar.gz |
Allow symbol names in RelatedFactory (Closes #30).
This works exactly as for SubFactory.
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_declarations.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/tests/test_declarations.py b/tests/test_declarations.py index c57e77d..cc921d4 100644 --- a/tests/test_declarations.py +++ b/tests/test_declarations.py @@ -260,6 +260,41 @@ class SubFactoryTestCase(unittest.TestCase): datetime.date = orig_date +class RelatedFactoryTestCase(unittest.TestCase): + + def test_arg(self): + self.assertRaises(ValueError, declarations.RelatedFactory, 'UnqualifiedSymbol') + + def test_lazyness(self): + f = declarations.RelatedFactory('factory.declarations.Sequence', x=3) + self.assertEqual(None, f.factory) + + self.assertEqual({'x': 3}, f.defaults) + + factory_class = f.get_factory() + self.assertEqual(declarations.Sequence, factory_class) + + def test_cache(self): + """Ensure that RelatedFactory tries to import only once.""" + orig_date = datetime.date + f = declarations.RelatedFactory('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 + + class CircularSubFactoryTestCase(unittest.TestCase): def test_circularsubfactory_deprecated(self): |