Skip to content

Commit db62e12

Browse files
authored
Merge pull request #512 from maykinmedia/issue/#503-sorting
🐛 - fix: sorting the zaaktypen
2 parents 217ace4 + 60cf60a commit db62e12

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

backend/src/openarchiefbeheer/zaken/tests/test_utils.py

+76-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88

99
from openarchiefbeheer.destruction.tests.factories import DestructionListItemFactory
1010
from openarchiefbeheer.utils.results_store import ResultStore
11-
from openarchiefbeheer.zaken.utils import delete_zaak_and_related_objects
11+
from openarchiefbeheer.zaken.utils import (
12+
delete_zaak_and_related_objects,
13+
format_zaaktype_choices,
14+
)
1215

1316

1417
class DeletingZakenWithErrorsTests(TestCase):
@@ -143,3 +146,75 @@ def test_failure_on_deleting_zaak_relation_is_handled(self, m):
143146

144147
with self.assertRaises(ConnectTimeout):
145148
delete_zaak_and_related_objects(destruction_list_item.zaak, result_store)
149+
150+
151+
class FormatZaaktypeChoicesTests(TestCase):
152+
def test_format_zaaktype_choices_with_multiple_versions(self):
153+
# Test that the function picks the latest version of each zaaktype based on versiedatum
154+
# And test that the output is sorted by label
155+
zaaktypen = [
156+
{
157+
"identificatie": "ZKT-001",
158+
"url": "http://localhost:8000/api/v1/zaaktypen/1",
159+
"omschrijving": "Zaaktype 1",
160+
"versiedatum": "2023-01-01",
161+
},
162+
{
163+
"identificatie": "ZKT-001",
164+
"url": "http://localhost:8000/api/v1/zaaktypen/2",
165+
"omschrijving": "Zaaktype 1 Updated",
166+
"versiedatum": "2023-06-01",
167+
},
168+
{
169+
"identificatie": "ZKT-002",
170+
"url": "http://localhost:8000/api/v1/zaaktypen/3",
171+
"omschrijving": "Zaaktype 2",
172+
"versiedatum": "2022-12-01",
173+
},
174+
]
175+
176+
# Expecting the most recent version for each zaaktype, with URLs aggregated
177+
expected_result = [
178+
{
179+
"label": "Zaaktype 1 Updated (ZKT-001)",
180+
"value": "http://localhost:8000/api/v1/zaaktypen/1,http://localhost:8000/api/v1/zaaktypen/2",
181+
},
182+
{
183+
"label": "Zaaktype 2 (ZKT-002)",
184+
"value": "http://localhost:8000/api/v1/zaaktypen/3",
185+
},
186+
]
187+
188+
result = format_zaaktype_choices(zaaktypen)
189+
self.assertEqual(result, expected_result)
190+
191+
def test_format_zaaktype_choices_with_no_identificatie(self):
192+
# Test how the function handles zaaktypen with no identificatie
193+
zaaktypen = [
194+
{
195+
"identificatie": None,
196+
"url": "http://localhost:8000/api/v1/zaaktypen/1",
197+
"omschrijving": "Zaaktype without ID",
198+
"versiedatum": "2023-01-01",
199+
}
200+
]
201+
202+
# Should fall back to using "no identificatie" in the label
203+
expected_result = [
204+
{
205+
"label": "Zaaktype without ID (no identificatie)",
206+
"value": "http://localhost:8000/api/v1/zaaktypen/1",
207+
}
208+
]
209+
210+
result = format_zaaktype_choices(zaaktypen)
211+
self.assertEqual(result, expected_result)
212+
213+
def test_format_zaaktype_choices_with_empty_input(self):
214+
# Test the behavior with an empty input list
215+
zaaktypen = []
216+
expected_result = []
217+
218+
# Should return an empty list
219+
result = format_zaaktype_choices(zaaktypen)
220+
self.assertEqual(result, expected_result)

backend/src/openarchiefbeheer/zaken/utils.py

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ def format_zaaktype_choices(zaaktypen: Iterable[dict]) -> list[DropDownChoice]:
109109
label = f"{omschrijving} ({identificatie or _("no identificatie")})"
110110
value = ",".join(urls)
111111
formatted_zaaktypen.append({"label": label, "value": value})
112+
formatted_zaaktypen = sorted(formatted_zaaktypen, key=lambda x: x["label"])
112113
return formatted_zaaktypen
113114

114115

0 commit comments

Comments
 (0)