From 7ee56a4058ba3a91b54a1c3253c441bd06f82f29 Mon Sep 17 00:00:00 2001
From: Todd Dembrey <todd.dembrey@torchbox.com>
Date: Thu, 14 Dec 2017 17:33:04 +0000
Subject: [PATCH] Correctly handle phases which share a name

---
 opentech/apply/tests/test_workflow.py |  4 ++++
 opentech/apply/workflow.py            | 18 ++++++++++++------
 2 files changed, 16 insertions(+), 6 deletions(-)

diff --git a/opentech/apply/tests/test_workflow.py b/opentech/apply/tests/test_workflow.py
index 5300cc446..79d090c86 100644
--- a/opentech/apply/tests/test_workflow.py
+++ b/opentech/apply/tests/test_workflow.py
@@ -27,6 +27,10 @@ class TestWorkflowCreation(SimpleTestCase):
         workflow = WorkflowFactory(num_stages=2, stages__num_phases=1)
         self.assertEqual(workflow.next(workflow.stages[0].phases[0]), workflow.stages[1].phases[0])
 
+    def test_returns_next_phase_shared_name(self):
+        workflow = WorkflowFactory(num_stages=1, stages__num_phases=3, stages__phases__name='the_same')
+        self.assertEqual(workflow.next(workflow.stages[0].phases[0]), workflow.stages[0].phases[1])
+
 
 class TestStageCreation(SimpleTestCase):
     def test_can_create_stage(self):
diff --git a/opentech/apply/workflow.py b/opentech/apply/workflow.py
index 07aad2966..6476941d3 100644
--- a/opentech/apply/workflow.py
+++ b/opentech/apply/workflow.py
@@ -7,11 +7,16 @@ class Workflow:
     def __init__(self, name: str, stages: Sequence['Stage']) -> None:
         self.name = name
         self.stages = stages
-        self.mapping = {
-            str(phase): (i, j)
-            for i, stage in enumerate(stages)
-            for j, phase in enumerate(stage)
-        }
+        self.mapping = self.build_mapping(stages)
+
+    def build_mapping(self, stages):
+        mapping = {}
+        for i, stage in enumerate(stages):
+            for j, phase in enumerate(stage):
+                while str(phase) in mapping:
+                    phase.occurance += 1
+                mapping[str(phase)] = (i, j)
+        return mapping
 
     def current_index(self, phase: Union['Phase', str, None]):
         if isinstance(phase, Phase):
@@ -52,6 +57,7 @@ class Stage(Iterable['Phase']):
 class Phase:
     def __init__(self, name: str) -> None:
         self.name = name
+        self.occurance = 0
 
     def __str__(self):
-        return '__'.join([self.stage.name, self.name])
+        return '__'.join([self.stage.name, self.name, str(self.occurance)])
-- 
GitLab