Repository: kangasta/fdbk

Issue:
Data tools should add unit inside value payload instead next to it
Currently seems to work as expected for values when value is not in table.

Discussion:

Patch:
diff --git a/fdbk/data_tools/_utils.py b/fdbk/data_tools/_utils.py
index 702e1dd..3b3f201 100644
--- a/fdbk/data_tools/_utils.py
+++ b/fdbk/data_tools/_utils.py
@@ -103,6 +103,12 @@ def _get_clean_payload(statistic):
         topic_name = payload.pop("topic_name")
         payload["payload"]["topic_name"] = topic_name
 
+        try:
+            unit = payload.pop("unit")
+            payload["payload"]["unit"] = unit
+        except KeyError:
+            pass
+
     return payload
 
 
diff --git a/fdbk/schemas/statistics-out.json b/fdbk/schemas/statistics-out.json
index d99aeef..e2a096b 100644
--- a/fdbk/schemas/statistics-out.json
+++ b/fdbk/schemas/statistics-out.json
@@ -18,6 +18,9 @@
             },
             "topic_name": {
               "type": "string"
+            },
+            "unit": {
+              "type": "string"
             }
           },
           "required": [


Test patch:
diff --git a/tst/test_data_tools.py b/tst/test_data_tools.py
index 25e8968..bdd1200 100644
--- a/tst/test_data_tools.py
+++ b/tst/test_data_tools.py
@@ -15,7 +15,7 @@ def _test_timestamp(i, timestamps=None):
 def generate_test_data(N=10, timestamps=None):
     return [dict(number=i, number2=i*2, letter=chr(ord('A') + i), timestamp=_test_timestamp(i, timestamps)) for i in range(N)]
 
-STATUS_TOPIC = dict(name='Test status', fields=["number", "number2", "letter"], units=[], data_tools=[])
+STATUS_TOPIC = dict(name='Test status', fields=["number", "number2", "letter"], units=[dict(field="number", unit="scalar")], data_tools=[])
 AGGREGATE_ALWAYS_TOPIC = dict(
     name="Aggregate test",
     fields=["number", "number2", "letter"],
@@ -66,6 +66,38 @@ class DataToolsTest(TestCase):
             self.assertEqual(result.get('payload').get('type'), type_)
             self.assertEqual(result.get('payload').get('value'), value)
 
+    def test_collection_functions_unit(self):
+        topic_d = STATUS_TOPIC
+        data = generate_test_data()
+
+        topic_d['data_tools'] = [
+            dict(field='number', method="average"),
+            dict(field='number', method='list_item', parameters=dict(
+                name="test_list", method="average")),
+            dict(field='number', method='table_item', parameters=dict(
+                name="test_table", method="average")),
+        ]
+
+        results, warnings = run_data_tools(topic_d, data)
+        self.assertEqual(len(warnings), 0)
+
+        results, warnings = post_process(results)
+        self.assertEqual(len(warnings), 0)
+        validate_statistics_array(results)
+
+        self.assertEqual(len(results), 3)
+
+        value = next(i for i in results if i["type"] == "value")
+        self.assertEqual(
+            value["payload"]["unit"], "scalar")
+        list_ = next(i for i in results if i["type"] == "list")
+        self.assertEqual(
+            list_["payload"]["data"][0]["payload"]["unit"], "scalar")
+        table = next(i for i in results if i["type"] == "table")
+        self.assertEqual(
+            table["payload"]["data"][0]["data"][0]["payload"]["unit"], "scalar")
+
+
     def test_status_functions(self):
         topic_d = STATUS_TOPIC
         data = generate_test_data()