diff options
Diffstat (limited to 'tests/test_declarations.py')
-rw-r--r-- | tests/test_declarations.py | 52 |
1 files changed, 50 insertions, 2 deletions
diff --git a/tests/test_declarations.py b/tests/test_declarations.py index 7215a54..c0b3539 100644 --- a/tests/test_declarations.py +++ b/tests/test_declarations.py @@ -20,9 +20,12 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. -import unittest +import datetime -from factory.declarations import deepgetattr, OrderedDeclaration, Sequence +from factory.declarations import deepgetattr, CircularSubFactory, OrderedDeclaration, \ + PostGenerationDeclaration, Sequence + +from .compat import unittest class OrderedDeclarationTestCase(unittest.TestCase): def test_errors(self): @@ -54,6 +57,51 @@ class DigTestCase(unittest.TestCase): self.assertEqual(42, deepgetattr(obj, 'a.b.c.n.x', 42)) +class PostGenerationDeclarationTestCase(unittest.TestCase): + def test_extract_no_prefix(self): + decl = PostGenerationDeclaration() + + extracted, kwargs = decl.extract('foo', {'foo': 13, 'foo__bar': 42}) + self.assertEqual(extracted, 13) + self.assertEqual(kwargs, {'bar': 42}) + + def test_extract_with_prefix(self): + decl = PostGenerationDeclaration(extract_prefix='blah') + + extracted, kwargs = decl.extract('foo', + {'foo': 13, 'foo__bar': 42, 'blah': 42, 'blah__baz': 1}) + self.assertEqual(extracted, 42) + 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() |