diff --git a/app/api/tests/upload/test_utils.py b/app/api/tests/upload/test_utils.py new file mode 100644 index 00000000..b8a02fee --- /dev/null +++ b/app/api/tests/upload/test_utils.py @@ -0,0 +1,32 @@ +import unittest + +from ...views.upload.utils import append_field + + +class TestDatasetUtils(unittest.TestCase): + + def test_can_append_field(self): + data = [ + {'label': 'A'}, + {'label': 'B'} + ] + append_field(data, project=1) + expected = [ + {'label': 'A', 'project': 1}, + {'label': 'B', 'project': 1} + ] + self.assertEqual(data, expected) + + def test_can_append_field_to_nested_list(self): + annotation = [ + [{'label': '18'}], + [{'label': '7'}, {'label': '4'}] + ] + docs = list(range(len(annotation))) + for a, d in zip(annotation, docs): + append_field(a, document=d) + expected = [ + [{'label': '18', 'document': 0}], + [{'label': '7', 'document': 1}, {'label': '4', 'document': 1}] + ] + self.assertEqual(annotation, expected) diff --git a/app/api/views/upload/utils.py b/app/api/views/upload/utils.py new file mode 100644 index 00000000..da53389d --- /dev/null +++ b/app/api/views/upload/utils.py @@ -0,0 +1,6 @@ +from typing import Dict, List + + +def append_field(data: List[Dict], **kwargs): + [d.update(kwargs) for d in data] + return data