Browse Source

Add append_field function

pull/1310/head
Hironsan 3 years ago
parent
commit
ca8cddf55a
2 changed files with 38 additions and 0 deletions
  1. 32
      app/api/tests/upload/test_utils.py
  2. 6
      app/api/views/upload/utils.py

32
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)

6
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
Loading…
Cancel
Save