diff options
-rw-r--r-- | docs/changelog.rst | 1 | ||||
-rw-r--r-- | factory/base.py | 2 | ||||
-rw-r--r-- | tests/test_base.py | 31 |
3 files changed, 31 insertions, 3 deletions
diff --git a/docs/changelog.rst b/docs/changelog.rst index c2da698..a6ca79e 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -16,6 +16,7 @@ ChangeLog *Bugfix:* - Avoid issues when using :meth:`factory.django.mute_signals` on a base factory class (see :issue:`183`). + - Fix limitations of :class:`factory.StubFactory`, that can now use :class:`factory.SubFactory` and co (see :issue:`131`). *Deprecation:* diff --git a/factory/base.py b/factory/base.py index 7215f00..4c3e0ad 100644 --- a/factory/base.py +++ b/factory/base.py @@ -683,7 +683,7 @@ class StubFactory(Factory): @classmethod def build(cls, **kwargs): - raise UnsupportedStrategy() + return cls.stub(**kwargs) @classmethod def create(cls, **kwargs): diff --git a/tests/test_base.py b/tests/test_base.py index d1df58e..12031b9 100644 --- a/tests/test_base.py +++ b/tests/test_base.py @@ -403,7 +403,7 @@ class FactoryDefaultStrategyTestCase(unittest.TestCase): self.assertRaises(base.Factory.UnknownStrategy, TestModelFactory) - def test_stub_with_non_stub_strategy(self): + def test_stub_with_create_strategy(self): class TestModelFactory(base.StubFactory): class Meta: model = TestModel @@ -414,8 +414,18 @@ class FactoryDefaultStrategyTestCase(unittest.TestCase): self.assertRaises(base.StubFactory.UnsupportedStrategy, TestModelFactory) + def test_stub_with_build_strategy(self): + class TestModelFactory(base.StubFactory): + class Meta: + model = TestModel + + one = 'one' + TestModelFactory._meta.strategy = base.BUILD_STRATEGY - self.assertRaises(base.StubFactory.UnsupportedStrategy, TestModelFactory) + obj = TestModelFactory() + + # For stubs, build() is an alias of stub(). + self.assertFalse(isinstance(obj, TestModel)) def test_change_strategy(self): @base.use_strategy(base.CREATE_STRATEGY) @@ -454,6 +464,23 @@ class FactoryCreationTestCase(unittest.TestCase): self.assertEqual(TestFactory._meta.strategy, base.STUB_STRATEGY) + def test_stub_and_subfactory(self): + class StubA(base.StubFactory): + class Meta: + model = TestObject + + one = 'blah' + + class StubB(base.StubFactory): + class Meta: + model = TestObject + + stubbed = declarations.SubFactory(StubA, two='two') + + b = StubB() + self.assertEqual('blah', b.stubbed.one) + self.assertEqual('two', b.stubbed.two) + def test_custom_creation(self): class TestModelFactory(FakeModelFactory): class Meta: |