From 2cd51ce32224597542b1d1f0296b1c9d6f30513d Mon Sep 17 00:00:00 2001 From: Todd Dembrey <todd.dembrey@torchbox.com> Date: Mon, 12 Feb 2018 10:11:50 +0000 Subject: [PATCH] split workflow related factories into their own file --- .../apply/funds/tests/factories/__init__.py | 3 + .../apply/funds/tests/factories/models.py | 111 ----------------- .../apply/funds/tests/factories/workflows.py | 116 ++++++++++++++++++ 3 files changed, 119 insertions(+), 111 deletions(-) create mode 100644 opentech/apply/funds/tests/factories/workflows.py diff --git a/opentech/apply/funds/tests/factories/__init__.py b/opentech/apply/funds/tests/factories/__init__.py index c9d2b8eba..415433a81 100644 --- a/opentech/apply/funds/tests/factories/__init__.py +++ b/opentech/apply/funds/tests/factories/__init__.py @@ -2,8 +2,11 @@ from . import models from .models import * # noqa from . import blocks from .blocks import * # noqa +from . import workflows +from .workflows import * # noqa __all__ = [] __all__.extend(blocks.__all__) __all__.extend(models.__all__) +__all__.extend(workflows.__all__) diff --git a/opentech/apply/funds/tests/factories/models.py b/opentech/apply/funds/tests/factories/models.py index 97984c061..c42a9cc41 100644 --- a/opentech/apply/funds/tests/factories/models.py +++ b/opentech/apply/funds/tests/factories/models.py @@ -1,6 +1,5 @@ import datetime -from django.forms import Form import factory import wagtail_factories @@ -14,16 +13,11 @@ from opentech.apply.funds.models import ( Round, RoundForm, ) -from opentech.apply.funds.workflow import Action, Phase, Stage, Workflow from . import blocks __all__ = [ - 'ActionFactory', - 'PhaseFactory', - 'StageFactory', - 'WorkflowFactory', 'FundTypeFactory', 'FundFormFactory', 'ApplicationFormFactory', @@ -34,111 +28,6 @@ __all__ = [ ] -class ListSubFactory(factory.SubFactory): - def __init__(self, *args, count=0, **kwargs): - self.count = count - super().__init__(*args, **kwargs) - - def evaluate(self, *args, **kwargs): - if isinstance(self.count, factory.declarations.BaseDeclaration): - self.evaluated_count = self.count.evaluate(*args, **kwargs) - else: - self.evaluated_count = self.count - - return super().evaluate(*args, **kwargs) - - def generate(self, step, params): - subfactory = self.get_factory() - force_sequence = step.sequence if self.FORCE_SEQUENCE else None - return [ - step.recurse(subfactory, params, force_sequence=force_sequence) - for _ in range(self.evaluated_count) - ] - - -class ActionFactory(factory.Factory): - class Meta: - model = Action - - name = factory.Faker('word') - - -class PhaseFactory(factory.Factory): - class Meta: - model = Phase - - class Params: - num_actions = factory.Faker('random_int', min=1, max=5) - - name = factory.Faker('word') - actions = ListSubFactory(ActionFactory, count=factory.SelfAttribute('num_actions')) - stage = factory.PostGeneration( - lambda obj, create, extracted, **kwargs: StageFactory.build(phases=[obj]) - ) - - @classmethod - def _create(cls, model_class, *args, **kwargs): - actions = kwargs.pop('actions') - new_class = type(model_class.__name__, (model_class,), {'actions': actions}) - return new_class(*args, **kwargs) - - @classmethod - def _build(cls, model_class, *args, **kwargs): - # defer to create because parent uses build - return cls._create(model_class, *args, **kwargs) - - -class StageFactory(factory.Factory): - class Meta: - model = Stage - inline_args = ('form',) - - class Params: - num_phases = factory.Faker('random_int', min=1, max=3) - - name = factory.Faker('word') - form = factory.LazyFunction(Form) - phases = ListSubFactory(PhaseFactory, count=factory.SelfAttribute('num_phases')) - - @classmethod - def _create(cls, model_class, *args, **kwargs): - # Returns a new class - phases = kwargs.pop('phases') - name = kwargs.pop('name') - return type(model_class.__name__, (model_class,), {'phases': phases, 'name': name}) - - @classmethod - def _build(cls, model_class, *args, **kwargs): - # returns an instance of the stage class - phases = kwargs.pop('phases') - name = kwargs.pop('name') - new_class = type(model_class.__name__, (model_class,), {'phases': phases, 'name': name}) - return new_class(*args, **kwargs) - - -class WorkflowFactory(factory.Factory): - class Meta: - model = Workflow - rename = {'stages': 'stage_classes'} - - class Params: - num_stages = factory.Faker('random_int', min=1, max=3) - - name = factory.Faker('word') - stages = ListSubFactory(StageFactory, count=factory.SelfAttribute('num_stages')) - - @factory.LazyAttribute - def forms(self): - return [Form() for _ in range(self.num_stages)] - - @classmethod - def _create(cls, model_class, *args, **kwargs): - name = kwargs.pop('name') - stages = kwargs.pop('stage_classes') - new_class = type(model_class.__name__, (model_class,), {'name': name, 'stage_classes': stages}) - return new_class(*args, **kwargs) - - class FundTypeFactory(wagtail_factories.PageFactory): class Meta: model = FundType diff --git a/opentech/apply/funds/tests/factories/workflows.py b/opentech/apply/funds/tests/factories/workflows.py new file mode 100644 index 000000000..009922275 --- /dev/null +++ b/opentech/apply/funds/tests/factories/workflows.py @@ -0,0 +1,116 @@ +import factory +from django.forms import Form + +from opentech.apply.funds.workflow import Action, Phase, Stage, Workflow + +__all__ = [ + 'ActionFactory', + 'PhaseFactory', + 'StageFactory', + 'WorkflowFactory', +] + + +class ListSubFactory(factory.SubFactory): + def __init__(self, *args, count=0, **kwargs): + self.count = count + super().__init__(*args, **kwargs) + + def evaluate(self, *args, **kwargs): + if isinstance(self.count, factory.declarations.BaseDeclaration): + self.evaluated_count = self.count.evaluate(*args, **kwargs) + else: + self.evaluated_count = self.count + + return super().evaluate(*args, **kwargs) + + def generate(self, step, params): + subfactory = self.get_factory() + force_sequence = step.sequence if self.FORCE_SEQUENCE else None + return [ + step.recurse(subfactory, params, force_sequence=force_sequence) + for _ in range(self.evaluated_count) + ] + + +class ActionFactory(factory.Factory): + class Meta: + model = Action + + name = factory.Faker('word') + + +class PhaseFactory(factory.Factory): + class Meta: + model = Phase + + class Params: + num_actions = factory.Faker('random_int', min=1, max=5) + + name = factory.Faker('word') + actions = ListSubFactory(ActionFactory, count=factory.SelfAttribute('num_actions')) + stage = factory.PostGeneration( + lambda obj, create, extracted, **kwargs: StageFactory.build(phases=[obj]) + ) + + @classmethod + def _create(cls, model_class, *args, **kwargs): + actions = kwargs.pop('actions') + new_class = type(model_class.__name__, (model_class,), {'actions': actions}) + return new_class(*args, **kwargs) + + @classmethod + def _build(cls, model_class, *args, **kwargs): + # defer to create because parent uses build + return cls._create(model_class, *args, **kwargs) + + +class StageFactory(factory.Factory): + class Meta: + model = Stage + inline_args = ('form',) + + class Params: + num_phases = factory.Faker('random_int', min=1, max=3) + + name = factory.Faker('word') + form = factory.LazyFunction(Form) + phases = ListSubFactory(PhaseFactory, count=factory.SelfAttribute('num_phases')) + + @classmethod + def _create(cls, model_class, *args, **kwargs): + # Returns a new class + phases = kwargs.pop('phases') + name = kwargs.pop('name') + return type(model_class.__name__, (model_class,), {'phases': phases, 'name': name}) + + @classmethod + def _build(cls, model_class, *args, **kwargs): + # returns an instance of the stage class + phases = kwargs.pop('phases') + name = kwargs.pop('name') + new_class = type(model_class.__name__, (model_class,), {'phases': phases, 'name': name}) + return new_class(*args, **kwargs) + + +class WorkflowFactory(factory.Factory): + class Meta: + model = Workflow + rename = {'stages': 'stage_classes'} + + class Params: + num_stages = factory.Faker('random_int', min=1, max=3) + + name = factory.Faker('word') + stages = ListSubFactory(StageFactory, count=factory.SelfAttribute('num_stages')) + + @factory.LazyAttribute + def forms(self): + return [Form() for _ in range(self.num_stages)] + + @classmethod + def _create(cls, model_class, *args, **kwargs): + name = kwargs.pop('name') + stages = kwargs.pop('stage_classes') + new_class = type(model_class.__name__, (model_class,), {'name': name, 'stage_classes': stages}) + return new_class(*args, **kwargs) -- GitLab