From ed87795dd8d939a4152038c10b210798f9d8350a Mon Sep 17 00:00:00 2001
From: "niehues.mark@gmail.com" <niehues.mark@gmail.com>
Date: Fri, 20 Mar 2020 19:12:29 +0100
Subject: [PATCH] tests runnging

---
 evrouting/charge/routing.py         | 32 ++++++++++-------------------
 tests/charge/test_charge_routing.py | 14 ++++++-------
 2 files changed, 18 insertions(+), 28 deletions(-)

diff --git a/evrouting/charge/routing.py b/evrouting/charge/routing.py
index 92be48c..37ce574 100644
--- a/evrouting/charge/routing.py
+++ b/evrouting/charge/routing.py
@@ -63,9 +63,8 @@ def shortest_path(G: nx.Graph, charging_stations: Set[Node], s: Node, t: Node,
 
         if node_min == t:
             return _result(
-                label_node_min,
-                f_soc_factory(label_node_min).minimum,
-                node_min)
+                label_node_min, f_soc_factory(label_node_min).minimum
+            )
 
         # Handle charging stations
         if node_min in charging_stations and node_min != label_node_min.last_cs:
@@ -205,8 +204,7 @@ def _setup(G: nx.Graph, charging_stations: Set[Node], capacity: SoC,
             )
 
 
-def _result(label: Label = None, f_soc_min: Time = None,
-            node: Node = None) -> Dict:
+def _result(label: Label = None, f_soc_min: Time = None) -> Dict:
     """
     Returns a dict with two fields, as described below.
 
@@ -215,21 +213,22 @@ def _result(label: Label = None, f_soc_min: Time = None,
     :param node: The final node.
 
     :return Time result['trip_time']: The overall trip time ```f_soc_min```
-    :return List[Tuple[Time, Node]] result['path']: List of Nodes passed
-        along the path. It is modelled as Tuples of Trip Time and Node.
-        If a Node appears twice in a row, it means, the battery got
-        charged. The charging time is the difference of the trip times.
+    :return List[Tuple[Node, Time]] result['path']: List of Nodes and their
+        according charging time along the path.
     """
-    if any(arg is None for arg in [label, f_soc_min, node]):
+    if any(arg is None for arg in [label, f_soc_min]):
         return {'trip_time': None, 'path': []}
 
+    # Remember where charging time applies
+    # First entry comes from the time necessary to charge at the last
+    # charging stop to reach the goal.
     t_charge_map = {label.last_cs: f_soc_min - label.t_trip}
-    path = []
 
     # Skip inserted extra node
     node = label.parent_node
     label = label.parent_label
-    time = f_soc_min
+
+    path = []
     while label is not None:
         if node == label.parent_node:
             # Label got spawned at fixing t_charge of the parent's label
@@ -237,15 +236,6 @@ def _result(label: Label = None, f_soc_min: Time = None,
             t_charge_map[label.parent_label.last_cs] = label.t_trip - label.parent_label.t_trip
         else:
             path.append((node, t_charge_map.get(node, 0)))
-            """
-            if node not in t_charge_map:
-                t_trip = label.t_trip - label.parent_label.t_trip
-                time -= t_trip
-                path.append((node, time))
-            else:
-                time -= t_charge_map[node]
-                path.append((node, t_charge_map[node]))
-            """
         node = label.parent_node
         label = label.parent_label
 
diff --git a/tests/charge/test_charge_routing.py b/tests/charge/test_charge_routing.py
index def061f..8156973 100644
--- a/tests/charge/test_charge_routing.py
+++ b/tests/charge/test_charge_routing.py
@@ -15,14 +15,14 @@ class TestRoutes:
         result = shortest_path(**init_config(edge_case))
 
         assert result['trip_time'] == 3.5
-        assert result['path'] == [(0, 0), (0, 1), (1, 2), (1, 2.5), (2, 3.5)]
+        assert result['path'] == [(0, 1), (1, 0.5), (2, 0)]
 
     def test_shortest_path_charge_at_s_only(self):
         """Charging at s."""
         result = shortest_path(**init_config(edge_case_a_slow))
 
         assert result['trip_time'] == 3
-        assert result['path'] == [(0, 0), (0, 2), (2, 3)]
+        assert result['path'] == [(0, 1), (1, 0), (2, 0)]
 
     def test_shortest_path_no_charge_s_path_t(self):
         """No charging at s but enough initial SoC to go to t directly."""
@@ -31,7 +31,7 @@ class TestRoutes:
         result = shortest_path(**conf)
 
         assert result['trip_time'] == 1
-        assert result['path'] == [(0, 0), (2, 1)]
+        assert result['path'] == [(0, 0), (2, 0)]
 
     def test_shortest_path_no_charge_s_path_a(self):
         """No charging at s but just enough SoC to go to t via a."""
@@ -40,7 +40,7 @@ class TestRoutes:
         result = shortest_path(**conf)
 
         assert result['trip_time'] == 2
-        assert result['path'] == [(0, 0), (1, 1), (2, 2)]
+        assert result['path'] == [(0, 0), (1, 0), (2, 0)]
 
 
 class TestWithFinalSoC:
@@ -52,7 +52,7 @@ class TestWithFinalSoC:
         result = shortest_path(**conf)
 
         assert result['trip_time'] == 5
-        assert result['path'] == [(0, 0), (0, 1), (1, 2), (1, 4), (2, 5)]
+        assert result['path'] == [(0, 1), (1, 2), (2, 0)]
 
     def test_path_impossilbe(self):
         """Not possible to end with full battery."""
@@ -70,7 +70,7 @@ class TestWithFinalSoC:
         result = shortest_path(**conf)
 
         assert result['trip_time'] == 5
-        assert result['path'] == [(0, 0), (0, 2), (1, 3), (1, 4), (2, 5)]
+        assert result['path'] == [(0, 2), (1, 1), (2, 0)]
 
     def test_shortest_path_no_charge_s_path_t(self):
         """No charging at s but initial soc."""
@@ -80,5 +80,5 @@ class TestWithFinalSoC:
         result = shortest_path(**conf)
 
         assert result['trip_time'] == 2.5
-        assert result['path'] == [(0, 0), (1, 1), (1, 1.5), (2, 2.5)]
+        assert result['path'] == [(0, 0), (1, .5), (2, 0)]
 
-- 
GitLab