Skip to content

Commit

Permalink
Define test factories strategy in FactoryOptions
Browse files Browse the repository at this point in the history
Instead of assigning to Factory._meta.strategy. Keeps overrides focused
on a single class and remove need to save and restore global default
strategy.
  • Loading branch information
francoisfreitag committed Nov 4, 2020
1 parent 80f66e9 commit 5f8330f
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,18 +362,11 @@ class SubTestObjectFactory(self.TestObjectFactory):


class FactoryDefaultStrategyTestCase(unittest.TestCase):
def setUp(self):
self.default_strategy = base.Factory._meta.strategy

def tearDown(self):
base.Factory._meta.strategy = self.default_strategy

def test_build_strategy(self):
base.Factory._meta.strategy = enums.BUILD_STRATEGY

class TestModelFactory(base.Factory):
class Meta:
model = TestModel
strategy = enums.BUILD_STRATEGY

one = 'one'

Expand All @@ -395,11 +388,10 @@ class Meta:
self.assertTrue(test_model.id)

def test_stub_strategy(self):
base.Factory._meta.strategy = enums.STUB_STRATEGY

class TestModelFactory(base.Factory):
class Meta:
model = TestModel
strategy = enums.STUB_STRATEGY

one = 'one'

Expand All @@ -408,11 +400,10 @@ class Meta:
self.assertFalse(hasattr(test_model, 'id')) # We should have a plain old object

def test_unknown_strategy(self):
base.Factory._meta.strategy = 'unknown'

class TestModelFactory(base.Factory):
class Meta:
model = TestModel
strategy = 'unknown'

one = 'one'

Expand All @@ -423,32 +414,31 @@ def test_stub_with_create_strategy(self):
class TestModelFactory(base.StubFactory):
class Meta:
model = TestModel
strategy = enums.CREATE_STRATEGY

one = 'one'

TestModelFactory._meta.strategy = enums.CREATE_STRATEGY

with self.assertRaises(base.StubFactory.UnsupportedStrategy):
TestModelFactory()

def test_stub_with_build_strategy(self):
class TestModelFactory(base.StubFactory):
class Meta:
model = TestModel
strategy = enums.BUILD_STRATEGY

one = 'one'

TestModelFactory._meta.strategy = enums.BUILD_STRATEGY
obj = TestModelFactory()

# For stubs, build() is an alias of stub().
self.assertFalse(isinstance(obj, TestModel))

def test_change_strategy(self):
@base.use_strategy(enums.CREATE_STRATEGY)
class TestModelFactory(base.StubFactory):
class Meta:
model = TestModel
strategy = enums.CREATE_STRATEGY

one = 'one'

Expand Down

0 comments on commit 5f8330f

Please sign in to comment.