Skip to content

Commit

Permalink
[#93] Re-write all the tests and make them simple (#97)
Browse files Browse the repository at this point in the history
* [#93] Re-write and simplify all the tests
  • Loading branch information
javrasya committed Sep 4, 2019
1 parent 1e04fdf commit 84a3737
Show file tree
Hide file tree
Showing 17 changed files with 881 additions and 779 deletions.
3 changes: 2 additions & 1 deletion requirements.txt
@@ -1,3 +1,4 @@
django-mptt
factory-boy
mock
mock
pyhamcrest
33 changes: 12 additions & 21 deletions river/core/classworkflowobject.py
@@ -1,8 +1,6 @@
from django.contrib.contenttypes.models import ContentType

from river.models import State, TransitionApprovalMeta, Workflow
from river.utils.error_code import ErrorCode
from river.utils.exceptions import RiverException


class ClassWorkflowObject(object):
Expand All @@ -23,30 +21,23 @@ def workflow(self):
def _content_type(self):
return ContentType.objects.get_for_model(self.workflow_class)

def get_on_approval_objects(self, as_user):
object_pks = []
def get_available_approvals(self, as_user):
transition_approvals = None
for workflow_object in self.workflow_class.objects.all():
instance_workflow = getattr(workflow_object.river, self.name)
transition_approvals = instance_workflow.get_available_approvals(as_user=as_user)
if transition_approvals.count():
object_pks.append(workflow_object.pk)
return self.workflow_class.objects.filter(pk__in=object_pks)
available_approvals = instance_workflow.get_available_approvals(as_user=as_user)
transition_approvals = available_approvals.union(available_approvals) if transition_approvals else available_approvals

return transition_approvals

def get_on_approval_objects(self, as_user):
available_approvals = self.get_available_approvals(as_user)
return self.workflow_class.objects.filter(pk__in=available_approvals.values_list("object_id", flat=True))

@property
def initial_state(self):
initial_states = State.objects.filter(
pk__in=TransitionApprovalMeta.objects.filter(
workflow=self.workflow,
parents__isnull=True
).values_list("source_state", flat=True)
)
if initial_states.count() == 0:
raise RiverException(ErrorCode.NO_AVAILABLE_INITIAL_STATE, 'There is no available initial state for the content type %s. ' % self._content_type)
elif initial_states.count() > 1:
raise RiverException(ErrorCode.MULTIPLE_INITIAL_STATE,
'There are multiple initial state for the content type %s. Have only one initial state' % self._content_type)

return initial_states[0]
workflow = Workflow.objects.filter(content_type=self._content_type, field_name=self.name).first()
return workflow.initial_state if workflow else None

@property
def final_states(self):
Expand Down
60 changes: 33 additions & 27 deletions river/core/instanceworkflowobject.py
Expand Up @@ -154,7 +154,7 @@ def process(action, next_state=None, god_mod=False):
available_transition_approvals = self.get_available_approvals(as_user=as_user, god_mod=god_mod)
c = available_transition_approvals.count()
if c == 0:
raise RiverException(ErrorCode.NO_AVAILABLE_NEXT_STATE_FOR_USER, "There is no available state for destination for the user.")
raise RiverException(ErrorCode.NO_AVAILABLE_NEXT_STATE_FOR_USER, "There is no available approval for the user")
if c > 1:
if next_state:
available_transition_approvals = available_transition_approvals.filter(destination_state=next_state)
Expand Down Expand Up @@ -188,7 +188,8 @@ def process(action, next_state=None, god_mod=False):
transition_status = True

# Next states should be PENDING back again if there is circle.
self._cycle_proceedings()
if self._check_if_it_cycled(transition_approval.destination_state):
self._re_create_cycled_path(transition_approval.destination_state)
# ProceedingService.get_next_proceedings(workflow_object).update(status=PENDING)

with ProceedingSignal(self.workflow_object, self.field_name, transition_approval), \
Expand Down Expand Up @@ -224,33 +225,38 @@ def _get_next_approvals(self, transition_approval_pks=None, current_states=None,

return proceedings

@atomic
def _cycle_proceedings(self):
"""
Finds next proceedings and clone them for cycling if it exists.
"""
next_approvals = self._get_next_approvals().exclude(
status=PENDING).exclude(cloned=True)
for ta in next_approvals:
clone_transition_approval, c = TransitionApproval.objects.get_or_create(
source_state=ta.source_state,
destination_state=ta.destination_state,
content_type=ta.content_type,
object_id=ta.object_id,
def _check_if_it_cycled(self, new_state):
return TransitionApproval.objects.filter(
workflow_object=self.workflow_object,
workflow=self.class_workflow.workflow,
source_state=new_state,
status=APPROVED
).count() > 0

def _re_create_cycled_path(self, from_state):
approvals = TransitionApproval.objects.filter(workflow_object=self.workflow_object, workflow=self.class_workflow.workflow, source_state=from_state)
cycle_ended = False
while not cycle_ended:
for old_approval in approvals:
if old_approval.enabled:
TransitionApproval.objects.get_or_create(
source_state=old_approval.source_state,
destination_state=old_approval.destination_state,
workflow=old_approval.workflow,
object_id=old_approval.workflow_object.pk,
content_type=old_approval.content_type,
skip=False,
priority=old_approval.priority,
enabled=True,
status=PENDING,
meta=old_approval.meta
)
approvals = TransitionApproval.objects.filter(
workflow_object=self.workflow_object,
workflow=self.class_workflow.workflow,
skip=ta.skip,
priority=ta.priority,
enabled=ta.enabled,
status=PENDING,
meta=ta.meta
source_state__in=approvals.values_list("destination_state", flat=TransitionApproval)
)

if c:
clone_transition_approval.permissions.add(*ta.permissions.all())
clone_transition_approval.groups.add(*ta.groups.all())
next_approvals.update(cloned=True)

return True if next_approvals.count() else False
cycle_ended = approvals.filter(source_state=from_state).count() > 0

def get_state(self):
return getattr(self.workflow_object, self.field_name)
Expand Down
160 changes: 0 additions & 160 deletions river/tests/base_test.py

This file was deleted.

File renamed without changes.

0 comments on commit 84a3737

Please sign in to comment.