From 9836e013b4494b5e320c81ec4e3f766522639be2 Mon Sep 17 00:00:00 2001 From: Thomas Goirand Date: Sun, 14 Oct 2012 13:19:59 +0000 Subject: Back to upstream version 1.1.5 --- tests/cyclic/__init__.py | 0 tests/cyclic/bar.py | 37 ----- tests/cyclic/foo.py | 38 ----- tests/test_base.py | 59 ++----- tests/test_containers.py | 11 -- tests/test_declarations.py | 31 +--- tests/test_using.py | 374 ++++++--------------------------------------- tests/test_utils.py | 34 ++--- 8 files changed, 77 insertions(+), 507 deletions(-) delete mode 100644 tests/cyclic/__init__.py delete mode 100644 tests/cyclic/bar.py delete mode 100644 tests/cyclic/foo.py (limited to 'tests') diff --git a/tests/cyclic/__init__.py b/tests/cyclic/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/cyclic/bar.py b/tests/cyclic/bar.py deleted file mode 100644 index cc90930..0000000 --- a/tests/cyclic/bar.py +++ /dev/null @@ -1,37 +0,0 @@ -# -*- 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 deleted file mode 100644 index e6f8896..0000000 --- a/tests/cyclic/foo.py +++ /dev/null @@ -1,38 +0,0 @@ -# -*- 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_base.py b/tests/test_base.py index 7ec3d0e..7575ee2 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -35,25 +35,19 @@ class TestObject(object): self.four = four class FakeDjangoModel(object): - @classmethod - def create(cls, **kwargs): - instance = cls(**kwargs) - instance.id = 1 - return instance + class FakeDjangoManager(object): + def create(self, **kwargs): + fake_model = FakeDjangoModel(**kwargs) + fake_model.id = 1 + return fake_model + + objects = FakeDjangoManager() def __init__(self, **kwargs): for name, value in kwargs.iteritems(): setattr(self, name, value) self.id = None -class FakeModelFactory(base.Factory): - ABSTRACT_FACTORY = True - - @classmethod - def _create(cls, target_class, *args, **kwargs): - return target_class.create(**kwargs) - - class TestModel(FakeDjangoModel): pass @@ -73,8 +67,6 @@ class FactoryTestCase(unittest.TestCase): def testLazyAttributeNonExistentParam(self): class TestObjectFactory(base.Factory): - FACTORY_FOR = TestObject - one = declarations.LazyAttribute(lambda a: a.does_not_exist ) self.assertRaises(AttributeError, TestObjectFactory) @@ -82,13 +74,9 @@ class FactoryTestCase(unittest.TestCase): def testInheritanceWithSequence(self): """Tests that sequence IDs are shared between parent and son.""" class TestObjectFactory(base.Factory): - FACTORY_FOR = TestObject - one = declarations.Sequence(lambda a: a) class TestSubFactory(TestObjectFactory): - FACTORY_FOR = TestObject - pass parent = TestObjectFactory.build() @@ -109,8 +97,6 @@ class FactoryDefaultStrategyTestCase(unittest.TestCase): base.Factory.default_strategy = base.BUILD_STRATEGY class TestModelFactory(base.Factory): - FACTORY_FOR = TestModel - one = 'one' test_model = TestModelFactory() @@ -120,9 +106,7 @@ class FactoryDefaultStrategyTestCase(unittest.TestCase): def testCreateStrategy(self): # Default default_strategy - class TestModelFactory(FakeModelFactory): - FACTORY_FOR = TestModel - + class TestModelFactory(base.Factory): one = 'one' test_model = TestModelFactory() @@ -133,8 +117,6 @@ class FactoryDefaultStrategyTestCase(unittest.TestCase): base.Factory.default_strategy = base.STUB_STRATEGY class TestModelFactory(base.Factory): - FACTORY_FOR = TestModel - one = 'one' test_model = TestModelFactory() @@ -145,16 +127,12 @@ class FactoryDefaultStrategyTestCase(unittest.TestCase): base.Factory.default_strategy = 'unknown' class TestModelFactory(base.Factory): - FACTORY_FOR = TestModel - one = 'one' self.assertRaises(base.Factory.UnknownStrategy, TestModelFactory) def testStubWithNonStubStrategy(self): class TestModelFactory(base.StubFactory): - FACTORY_FOR = TestModel - one = 'one' TestModelFactory.default_strategy = base.CREATE_STRATEGY @@ -167,8 +145,6 @@ class FactoryDefaultStrategyTestCase(unittest.TestCase): def test_change_strategy(self): @base.use_strategy(base.CREATE_STRATEGY) class TestModelFactory(base.StubFactory): - FACTORY_FOR = TestModel - one = 'one' self.assertEqual(base.CREATE_STRATEGY, TestModelFactory.default_strategy) @@ -182,10 +158,8 @@ class FactoryCreationTestCase(unittest.TestCase): self.assertTrue(isinstance(TestFactory.build(), TestObject)) def testAutomaticAssociatedClassDiscovery(self): - with warnings.catch_warnings(): - warnings.simplefilter('ignore') - class TestObjectFactory(base.Factory): - pass + class TestObjectFactory(base.Factory): + pass self.assertTrue(isinstance(TestObjectFactory.build(), TestObject)) @@ -194,7 +168,8 @@ class FactoryCreationTestCase(unittest.TestCase): with warnings.catch_warnings(record=True) as w: # Clear the warning registry. - __warningregistry__.clear() + if hasattr(base, '__warningregistry__'): + base.__warningregistry__.clear() warnings.simplefilter('always') class TestObjectFactory(base.Factory): @@ -211,8 +186,6 @@ class FactoryCreationTestCase(unittest.TestCase): def testInheritanceWithStub(self): class TestObjectFactory(base.StubFactory): - FACTORY_FOR = TestObject - pass class TestFactory(TestObjectFactory): @@ -221,9 +194,7 @@ class FactoryCreationTestCase(unittest.TestCase): self.assertEqual(TestFactory.default_strategy, base.STUB_STRATEGY) def testCustomCreation(self): - class TestModelFactory(FakeModelFactory): - FACTORY_FOR = TestModel - + class TestModelFactory(base.Factory): @classmethod def _prepare(cls, create, **kwargs): kwargs['four'] = 4 @@ -262,16 +233,12 @@ class PostGenerationParsingTestCase(unittest.TestCase): def test_extraction(self): class TestObjectFactory(base.Factory): - FACTORY_FOR = TestObject - foo = declarations.PostGenerationDeclaration() self.assertIn('foo', TestObjectFactory._postgen_declarations) def test_classlevel_extraction(self): class TestObjectFactory(base.Factory): - FACTORY_FOR = TestObject - foo = declarations.PostGenerationDeclaration() foo__bar = 42 diff --git a/tests/test_containers.py b/tests/test_containers.py index b1ed6ed..797c480 100644 --- a/tests/test_containers.py +++ b/tests/test_containers.py @@ -178,17 +178,6 @@ class DeclarationDictTestCase(unittest.TestCase): self.assertEqual(set(['one', 'three']), set(d)) self.assertEqual(set([1, 3]), set(d.values())) - def test_update_with_public_ignores_factory_attributes(self): - """Ensure that a DeclarationDict ignores FACTORY_ keys.""" - d = containers.DeclarationDict() - d.update_with_public({ - 'one': 1, - 'FACTORY_FOR': 2, - 'FACTORY_ARG_PARAMETERS': 3, - }) - self.assertEqual(['one'], list(d)) - self.assertEqual([1], list(d.values())) - class AttributeBuilderTestCase(unittest.TestCase): def test_empty(self): diff --git a/tests/test_declarations.py b/tests/test_declarations.py index c0b3539..1c0502b 100644 --- a/tests/test_declarations.py +++ b/tests/test_declarations.py @@ -20,9 +20,8 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. -import datetime -from factory.declarations import deepgetattr, CircularSubFactory, OrderedDeclaration, \ +from factory.declarations import deepgetattr, OrderedDeclaration, \ PostGenerationDeclaration, Sequence from .compat import unittest @@ -74,34 +73,6 @@ 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_using.py b/tests/test_using.py index 7e141eb..f4d5440 100644 --- a/tests/test_using.py +++ b/tests/test_using.py @@ -21,11 +21,6 @@ """Tests using factory.""" -import functools -import os -import sys -import warnings - import factory from .compat import unittest @@ -39,49 +34,24 @@ class TestObject(object): self.four = four self.five = five - -class FakeModel(object): - @classmethod - def create(cls, **kwargs): - instance = cls(**kwargs) - instance.id = 1 - return instance - - class FakeModelManager(object): +class FakeDjangoModel(object): + class FakeDjangoManager(object): def create(self, **kwargs): - instance = FakeModel.create(**kwargs) - instance.id = 2 - return instance + fake_model = FakeDjangoModel(**kwargs) + fake_model.id = 1 + return fake_model - objects = FakeModelManager() + objects = FakeDjangoManager() def __init__(self, **kwargs): for name, value in kwargs.iteritems(): setattr(self, name, value) self.id = None - -class FakeModelFactory(factory.Factory): - ABSTRACT_FACTORY = True - - @classmethod - def _create(cls, target_class, *args, **kwargs): - return target_class.create(**kwargs) - - -class TestModel(FakeModel): +class TestModel(FakeDjangoModel): pass -def disable_warnings(fun): - @functools.wraps(fun) - def decorated(*args, **kwargs): - with warnings.catch_warnings(): - warnings.simplefilter('ignore') - return fun(*args, **kwargs) - return decorated - - class SimpleBuildTestCase(unittest.TestCase): """Tests the minimalist 'factory.build/create' functions.""" @@ -112,21 +82,19 @@ class SimpleBuildTestCase(unittest.TestCase): self.assertEqual(obj.three, 3) self.assertEqual(obj.four, None) - @disable_warnings def test_create(self): - obj = factory.create(FakeModel, foo='bar') - self.assertEqual(obj.id, 2) + obj = factory.create(FakeDjangoModel, foo='bar') + self.assertEqual(obj.id, 1) self.assertEqual(obj.foo, 'bar') - @disable_warnings def test_create_batch(self): - objs = factory.create_batch(FakeModel, 4, foo='bar') + objs = factory.create_batch(FakeDjangoModel, 4, foo='bar') self.assertEqual(4, len(objs)) self.assertEqual(4, len(set(objs))) for obj in objs: - self.assertEqual(obj.id, 2) + self.assertEqual(obj.id, 1) self.assertEqual(obj.foo, 'bar') def test_stub(self): @@ -135,7 +103,7 @@ class SimpleBuildTestCase(unittest.TestCase): self.assertFalse(hasattr(obj, 'two')) def test_stub_batch(self): - objs = factory.stub_batch(FakeModel, 4, foo='bar') + objs = factory.stub_batch(FakeDjangoModel, 4, foo='bar') self.assertEqual(4, len(objs)) self.assertEqual(4, len(set(objs))) @@ -145,23 +113,22 @@ class SimpleBuildTestCase(unittest.TestCase): self.assertEqual(obj.foo, 'bar') def test_generate_build(self): - obj = factory.generate(FakeModel, factory.BUILD_STRATEGY, foo='bar') + obj = factory.generate(FakeDjangoModel, factory.BUILD_STRATEGY, foo='bar') self.assertEqual(obj.id, None) self.assertEqual(obj.foo, 'bar') - @disable_warnings def test_generate_create(self): - obj = factory.generate(FakeModel, factory.CREATE_STRATEGY, foo='bar') - self.assertEqual(obj.id, 2) + obj = factory.generate(FakeDjangoModel, factory.CREATE_STRATEGY, foo='bar') + self.assertEqual(obj.id, 1) self.assertEqual(obj.foo, 'bar') def test_generate_stub(self): - obj = factory.generate(FakeModel, factory.STUB_STRATEGY, foo='bar') + obj = factory.generate(FakeDjangoModel, factory.STUB_STRATEGY, foo='bar') self.assertFalse(hasattr(obj, 'id')) self.assertEqual(obj.foo, 'bar') def test_generate_batch_build(self): - objs = factory.generate_batch(FakeModel, factory.BUILD_STRATEGY, 20, foo='bar') + objs = factory.generate_batch(FakeDjangoModel, factory.BUILD_STRATEGY, 20, foo='bar') self.assertEqual(20, len(objs)) self.assertEqual(20, len(set(objs))) @@ -170,19 +137,18 @@ class SimpleBuildTestCase(unittest.TestCase): self.assertEqual(obj.id, None) self.assertEqual(obj.foo, 'bar') - @disable_warnings def test_generate_batch_create(self): - objs = factory.generate_batch(FakeModel, factory.CREATE_STRATEGY, 20, foo='bar') + objs = factory.generate_batch(FakeDjangoModel, factory.CREATE_STRATEGY, 20, foo='bar') self.assertEqual(20, len(objs)) self.assertEqual(20, len(set(objs))) for obj in objs: - self.assertEqual(obj.id, 2) + self.assertEqual(obj.id, 1) self.assertEqual(obj.foo, 'bar') def test_generate_batch_stub(self): - objs = factory.generate_batch(FakeModel, factory.STUB_STRATEGY, 20, foo='bar') + objs = factory.generate_batch(FakeDjangoModel, factory.STUB_STRATEGY, 20, foo='bar') self.assertEqual(20, len(objs)) self.assertEqual(20, len(set(objs))) @@ -192,18 +158,17 @@ class SimpleBuildTestCase(unittest.TestCase): self.assertEqual(obj.foo, 'bar') def test_simple_generate_build(self): - obj = factory.simple_generate(FakeModel, False, foo='bar') + obj = factory.simple_generate(FakeDjangoModel, False, foo='bar') self.assertEqual(obj.id, None) self.assertEqual(obj.foo, 'bar') - @disable_warnings def test_simple_generate_create(self): - obj = factory.simple_generate(FakeModel, True, foo='bar') - self.assertEqual(obj.id, 2) + obj = factory.simple_generate(FakeDjangoModel, True, foo='bar') + self.assertEqual(obj.id, 1) self.assertEqual(obj.foo, 'bar') def test_simple_generate_batch_build(self): - objs = factory.simple_generate_batch(FakeModel, False, 20, foo='bar') + objs = factory.simple_generate_batch(FakeDjangoModel, False, 20, foo='bar') self.assertEqual(20, len(objs)) self.assertEqual(20, len(set(objs))) @@ -212,15 +177,14 @@ class SimpleBuildTestCase(unittest.TestCase): self.assertEqual(obj.id, None) self.assertEqual(obj.foo, 'bar') - @disable_warnings def test_simple_generate_batch_create(self): - objs = factory.simple_generate_batch(FakeModel, True, 20, foo='bar') + objs = factory.simple_generate_batch(FakeDjangoModel, True, 20, foo='bar') self.assertEqual(20, len(objs)) self.assertEqual(20, len(set(objs))) for obj in objs: - self.assertEqual(obj.id, 2) + self.assertEqual(obj.id, 1) self.assertEqual(obj.foo, 'bar') def test_make_factory(self): @@ -242,8 +206,6 @@ class SimpleBuildTestCase(unittest.TestCase): class UsingFactoryTestCase(unittest.TestCase): def testAttribute(self): class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - one = 'one' test_object = TestObjectFactory.build() @@ -262,8 +224,6 @@ class UsingFactoryTestCase(unittest.TestCase): def testSequence(self): class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - one = factory.Sequence(lambda n: 'one' + n) two = factory.Sequence(lambda n: 'two' + n) @@ -277,8 +237,6 @@ class UsingFactoryTestCase(unittest.TestCase): def testSequenceCustomBegin(self): class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - @classmethod def _setup_next_sequence(cls): return 42 @@ -296,8 +254,6 @@ class UsingFactoryTestCase(unittest.TestCase): def test_sequence_batch(self): class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - one = factory.Sequence(lambda n: 'one' + n) two = factory.Sequence(lambda n: 'two' + n) @@ -311,8 +267,6 @@ class UsingFactoryTestCase(unittest.TestCase): def testLazyAttribute(self): class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - one = factory.LazyAttribute(lambda a: 'abc' ) two = factory.LazyAttribute(lambda a: a.one + ' xyz') @@ -322,8 +276,6 @@ class UsingFactoryTestCase(unittest.TestCase): def testLazyAttributeSequence(self): class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - one = factory.LazyAttributeSequence(lambda a, n: 'abc' + n) two = factory.LazyAttributeSequence(lambda a, n: a.one + ' xyz' + n) @@ -337,8 +289,6 @@ class UsingFactoryTestCase(unittest.TestCase): def testLazyAttributeDecorator(self): class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - @factory.lazy_attribute def one(a): return 'one' @@ -351,8 +301,6 @@ class UsingFactoryTestCase(unittest.TestCase): n = 3 class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - one = 'xx' two = factory.SelfAttribute('one') three = TmpObj() @@ -367,8 +315,6 @@ class UsingFactoryTestCase(unittest.TestCase): def testSequenceDecorator(self): class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - @factory.sequence def one(n): return 'one' + n @@ -378,8 +324,6 @@ class UsingFactoryTestCase(unittest.TestCase): def testLazyAttributeSequenceDecorator(self): class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - @factory.lazy_attribute_sequence def one(a, n): return 'one' + n @@ -393,8 +337,6 @@ class UsingFactoryTestCase(unittest.TestCase): def testBuildWithParameters(self): class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - one = factory.Sequence(lambda n: 'one' + n) two = factory.Sequence(lambda n: 'two' + n) @@ -408,9 +350,7 @@ class UsingFactoryTestCase(unittest.TestCase): self.assertEqual(test_object1.two, 'two1') def testCreate(self): - class TestModelFactory(FakeModelFactory): - FACTORY_FOR = TestModel - + class TestModelFactory(factory.Factory): one = 'one' test_model = TestModelFactory.create() @@ -418,9 +358,7 @@ class UsingFactoryTestCase(unittest.TestCase): self.assertTrue(test_model.id) def test_create_batch(self): - class TestModelFactory(FakeModelFactory): - FACTORY_FOR = TestModel - + class TestModelFactory(factory.Factory): one = 'one' objs = TestModelFactory.create_batch(20, two=factory.Sequence(int)) @@ -434,9 +372,7 @@ class UsingFactoryTestCase(unittest.TestCase): self.assertTrue(obj.id) def test_generate_build(self): - class TestModelFactory(FakeModelFactory): - FACTORY_FOR = TestModel - + class TestModelFactory(factory.Factory): one = 'one' test_model = TestModelFactory.generate(factory.BUILD_STRATEGY) @@ -444,9 +380,7 @@ class UsingFactoryTestCase(unittest.TestCase): self.assertFalse(test_model.id) def test_generate_create(self): - class TestModelFactory(FakeModelFactory): - FACTORY_FOR = TestModel - + class TestModelFactory(factory.Factory): one = 'one' test_model = TestModelFactory.generate(factory.CREATE_STRATEGY) @@ -454,9 +388,7 @@ class UsingFactoryTestCase(unittest.TestCase): self.assertTrue(test_model.id) def test_generate_stub(self): - class TestModelFactory(FakeModelFactory): - FACTORY_FOR = TestModel - + class TestModelFactory(factory.Factory): one = 'one' test_model = TestModelFactory.generate(factory.STUB_STRATEGY) @@ -464,9 +396,7 @@ class UsingFactoryTestCase(unittest.TestCase): self.assertFalse(hasattr(test_model, 'id')) def test_generate_batch_build(self): - class TestModelFactory(FakeModelFactory): - FACTORY_FOR = TestModel - + class TestModelFactory(factory.Factory): one = 'one' objs = TestModelFactory.generate_batch(factory.BUILD_STRATEGY, 20, two='two') @@ -480,9 +410,7 @@ class UsingFactoryTestCase(unittest.TestCase): self.assertFalse(obj.id) def test_generate_batch_create(self): - class TestModelFactory(FakeModelFactory): - FACTORY_FOR = TestModel - + class TestModelFactory(factory.Factory): one = 'one' objs = TestModelFactory.generate_batch(factory.CREATE_STRATEGY, 20, two='two') @@ -496,9 +424,7 @@ class UsingFactoryTestCase(unittest.TestCase): self.assertTrue(obj.id) def test_generate_batch_stub(self): - class TestModelFactory(FakeModelFactory): - FACTORY_FOR = TestModel - + class TestModelFactory(factory.Factory): one = 'one' objs = TestModelFactory.generate_batch(factory.STUB_STRATEGY, 20, two='two') @@ -512,9 +438,7 @@ class UsingFactoryTestCase(unittest.TestCase): self.assertFalse(hasattr(obj, 'id')) def test_simple_generate_build(self): - class TestModelFactory(FakeModelFactory): - FACTORY_FOR = TestModel - + class TestModelFactory(factory.Factory): one = 'one' test_model = TestModelFactory.simple_generate(False) @@ -522,9 +446,7 @@ class UsingFactoryTestCase(unittest.TestCase): self.assertFalse(test_model.id) def test_simple_generate_create(self): - class TestModelFactory(FakeModelFactory): - FACTORY_FOR = TestModel - + class TestModelFactory(factory.Factory): one = 'one' test_model = TestModelFactory.simple_generate(True) @@ -532,9 +454,7 @@ class UsingFactoryTestCase(unittest.TestCase): self.assertTrue(test_model.id) def test_simple_generate_batch_build(self): - class TestModelFactory(FakeModelFactory): - FACTORY_FOR = TestModel - + class TestModelFactory(factory.Factory): one = 'one' objs = TestModelFactory.simple_generate_batch(False, 20, two='two') @@ -548,9 +468,7 @@ class UsingFactoryTestCase(unittest.TestCase): self.assertFalse(obj.id) def test_simple_generate_batch_create(self): - class TestModelFactory(FakeModelFactory): - FACTORY_FOR = TestModel - + class TestModelFactory(factory.Factory): one = 'one' objs = TestModelFactory.simple_generate_batch(True, 20, two='two') @@ -565,8 +483,6 @@ class UsingFactoryTestCase(unittest.TestCase): def test_stub_batch(self): class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - one = 'one' two = factory.LazyAttribute(lambda a: a.one + ' two') three = factory.Sequence(lambda n: int(n)) @@ -584,8 +500,6 @@ class UsingFactoryTestCase(unittest.TestCase): def testInheritance(self): class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - one = 'one' two = factory.LazyAttribute(lambda a: a.one + ' two') @@ -606,8 +520,6 @@ class UsingFactoryTestCase(unittest.TestCase): def testInheritanceWithInheritedClass(self): class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - one = 'one' two = factory.LazyAttribute(lambda a: a.one + ' two') @@ -623,8 +535,6 @@ class UsingFactoryTestCase(unittest.TestCase): def testDualInheritance(self): class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - one = 'one' class TestOtherFactory(factory.Factory): @@ -641,13 +551,12 @@ class UsingFactoryTestCase(unittest.TestCase): self.assertEqual('three', obj.three) self.assertEqual('four', obj.four) - @disable_warnings def testSetCreationFunction(self): def creation_function(class_to_create, **kwargs): return "This doesn't even return an instance of {0}".format(class_to_create.__name__) - class TestModelFactory(FakeModelFactory): - FACTORY_FOR = TestModel + class TestModelFactory(factory.Factory): + pass TestModelFactory.set_creation_function(creation_function) @@ -656,8 +565,6 @@ class UsingFactoryTestCase(unittest.TestCase): def testClassMethodAccessible(self): class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - @classmethod def alt_create(cls, **kwargs): return kwargs @@ -666,8 +573,6 @@ class UsingFactoryTestCase(unittest.TestCase): def testStaticMethodAccessible(self): class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - @staticmethod def alt_create(**kwargs): return kwargs @@ -675,65 +580,16 @@ class UsingFactoryTestCase(unittest.TestCase): self.assertEqual(TestObjectFactory.alt_create(foo=1), {"foo": 1}) -class NonKwargParametersTestCase(unittest.TestCase): - def test_build(self): - class TestObject(object): - def __init__(self, *args, **kwargs): - self.args = args - self.kwargs = kwargs - - class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - FACTORY_ARG_PARAMETERS = ('one', 'two',) - - one = 1 - two = 2 - three = 3 - - obj = TestObjectFactory.build() - self.assertEqual((1, 2), obj.args) - self.assertEqual({'three': 3}, obj.kwargs) - - def test_create(self): - class TestObject(object): - def __init__(self, *args, **kwargs): - self.args = None - self.kwargs = None - - @classmethod - def create(cls, *args, **kwargs): - inst = cls() - inst.args = args - inst.kwargs = kwargs - return inst - - class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - FACTORY_ARG_PARAMETERS = ('one', 'two') - - one = 1 - two = 2 - three = 3 - - @classmethod - def _create(cls, target_class, *args, **kwargs): - return target_class.create(*args, **kwargs) - - obj = TestObjectFactory.create() - self.assertEqual((1, 2), obj.args) - self.assertEqual({'three': 3}, obj.kwargs) - - class SubFactoryTestCase(unittest.TestCase): def testSubFactory(self): - class TestModel2(FakeModel): + class TestModel2(FakeDjangoModel): pass - class TestModelFactory(FakeModelFactory): + class TestModelFactory(factory.Factory): FACTORY_FOR = TestModel one = 3 - class TestModel2Factory(FakeModelFactory): + class TestModel2Factory(factory.Factory): FACTORY_FOR = TestModel2 two = factory.SubFactory(TestModelFactory, one=1) @@ -743,13 +599,13 @@ class SubFactoryTestCase(unittest.TestCase): self.assertEqual(1, test_model.two.id) def testSubFactoryWithLazyFields(self): - class TestModel2(FakeModel): + class TestModel2(FakeDjangoModel): pass - class TestModelFactory(FakeModelFactory): + class TestModelFactory(factory.Factory): FACTORY_FOR = TestModel - class TestModel2Factory(FakeModelFactory): + class TestModel2Factory(factory.Factory): FACTORY_FOR = TestModel2 two = factory.SubFactory(TestModelFactory, one=factory.Sequence(lambda n: 'x%sx' % n), @@ -941,85 +797,11 @@ class SubFactoryTestCase(unittest.TestCase): self.assertEqual(outer.side_a.inner_from_a.a, outer.foo * 2) self.assertEqual(outer.side_a.inner_from_a.b, 4) - def test_nonstrict_container_attribute(self): - class TestModel2(FakeModel): - pass - - class TestModelFactory(FakeModelFactory): - FACTORY_FOR = TestModel - one = 3 - two = factory.ContainerAttribute(lambda obj, containers: len(containers or []), strict=False) - - class TestModel2Factory(FakeModelFactory): - FACTORY_FOR = TestModel2 - one = 1 - two = factory.SubFactory(TestModelFactory, one=1) - - obj = TestModel2Factory.build() - self.assertEqual(1, obj.one) - self.assertEqual(1, obj.two.one) - self.assertEqual(1, obj.two.two) - - obj = TestModelFactory() - self.assertEqual(3, obj.one) - self.assertEqual(0, obj.two) - - def test_strict_container_attribute(self): - class TestModel2(FakeModel): - pass - - class TestModelFactory(FakeModelFactory): - FACTORY_FOR = TestModel - one = 3 - two = factory.ContainerAttribute(lambda obj, containers: len(containers or []), strict=True) - - class TestModel2Factory(FakeModelFactory): - FACTORY_FOR = TestModel2 - one = 1 - two = factory.SubFactory(TestModelFactory, one=1) - - obj = TestModel2Factory.build() - self.assertEqual(1, obj.one) - self.assertEqual(1, obj.two.one) - self.assertEqual(1, obj.two.two) - - self.assertRaises(TypeError, TestModelFactory.build) - - def test_function_container_attribute(self): - class TestModel2(FakeModel): - pass - - class TestModelFactory(FakeModelFactory): - FACTORY_FOR = TestModel - one = 3 - - @factory.container_attribute - def two(self, containers): - if containers: - return len(containers) - return 42 - - class TestModel2Factory(FakeModelFactory): - FACTORY_FOR = TestModel2 - one = 1 - two = factory.SubFactory(TestModelFactory, one=1) - - obj = TestModel2Factory.build() - self.assertEqual(1, obj.one) - self.assertEqual(1, obj.two.one) - self.assertEqual(1, obj.two.two) - - obj = TestModelFactory() - self.assertEqual(3, obj.one) - self.assertEqual(42, obj.two) - class IteratorTestCase(unittest.TestCase): def test_iterator(self): class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - one = factory.Iterator(xrange(10, 30)) objs = TestObjectFactory.build_batch(20) @@ -1029,8 +811,6 @@ class IteratorTestCase(unittest.TestCase): def test_infinite_iterator(self): class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - one = factory.InfiniteIterator(xrange(5)) objs = TestObjectFactory.build_batch(20) @@ -1040,8 +820,6 @@ class IteratorTestCase(unittest.TestCase): def test_infinite_iterator_list_comprehension(self): class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - one = factory.InfiniteIterator([j * 3 for j in xrange(5)]) # Scope bleeding: j will end up in TestObjectFactory's scope. @@ -1050,8 +828,6 @@ class IteratorTestCase(unittest.TestCase): def test_infinite_iterator_list_comprehension_protected(self): class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - one = factory.InfiniteIterator([_j * 3 for _j in xrange(5)]) # Scope bleeding : _j will end up in TestObjectFactory's scope. @@ -1063,8 +839,6 @@ class IteratorTestCase(unittest.TestCase): def test_iterator_decorator(self): class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - @factory.iterator def one(): for i in xrange(10, 50): @@ -1077,8 +851,6 @@ class IteratorTestCase(unittest.TestCase): def test_infinite_iterator_decorator(self): class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - @factory.infinite_iterator def one(): for i in xrange(5): @@ -1093,8 +865,6 @@ class IteratorTestCase(unittest.TestCase): class PostGenerationTestCase(unittest.TestCase): def test_post_generation(self): class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - one = 1 @factory.post_generation() @@ -1111,8 +881,6 @@ class PostGenerationTestCase(unittest.TestCase): def test_post_generation_extraction(self): class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - one = 1 @factory.post_generation() @@ -1136,40 +904,10 @@ class PostGenerationTestCase(unittest.TestCase): self.assertEqual(kwargs, {'foo': 13}) class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - bar = factory.PostGeneration(my_lambda) obj = TestObjectFactory.build(bar=42, bar__foo=13) - def test_post_generation_method_call(self): - calls = [] - - class TestObject(object): - def __init__(self, one=None, two=None): - self.one = one - self.two = two - self.extra = None - - def call(self, *args, **kwargs): - self.extra = (args, kwargs) - - class TestObjectFactory(factory.Factory): - FACTORY_FOR = TestObject - one = 3 - two = 2 - post_call = factory.PostGenerationMethodCall('call', one=1) - - obj = TestObjectFactory.build() - self.assertEqual(3, obj.one) - self.assertEqual(2, obj.two) - self.assertEqual(((), {'one': 1}), obj.extra) - - obj = TestObjectFactory.build(post_call__one=2, post_call__two=3) - self.assertEqual(3, obj.one) - self.assertEqual(2, obj.two) - self.assertEqual(((), {'one': 2, 'two': 3}), obj.extra) - def test_related_factory(self): class TestRelatedObject(object): def __init__(self, obj=None, one=None, two=None): @@ -1215,25 +953,5 @@ 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 9aaafc1..6fd6ee2 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -25,6 +25,23 @@ from factory import utils from .compat import unittest +class DecLengthCompareTestCase(unittest.TestCase): + def test_reciprocity(self): + self.assertEqual(1, utils.declength_compare('a', 'bb')) + self.assertEqual(-1, utils.declength_compare('aa', 'b')) + + def test_not_lexical(self): + self.assertEqual(1, utils.declength_compare('abc', 'aaaa')) + self.assertEqual(-1, utils.declength_compare('aaaa', 'abc')) + + def test_same_length(self): + self.assertEqual(-1, utils.declength_compare('abc', 'abd')) + self.assertEqual(1, utils.declength_compare('abe', 'abd')) + + def test_equality(self): + self.assertEqual(0, utils.declength_compare('abc', 'abc')) + self.assertEqual(0, utils.declength_compare([1, 2, 3], [1, 2, 3])) + class ExtractDictTestCase(unittest.TestCase): def test_empty_dict(self): @@ -85,7 +102,6 @@ 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'], {})) @@ -214,19 +230,3 @@ 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__') -- cgit v1.2.3