diff --git a/src/solo_turnier/tests/test_worker.py b/src/solo_turnier/tests/test_worker.py index 0bde14b..5875e1d 100644 --- a/src/solo_turnier/tests/test_worker.py +++ b/src/solo_turnier/tests/test_worker.py @@ -358,3 +358,28 @@ def test_collectPersonsInGroups(mocker): assert groups['Jun.'] == [persons[2], persons[8]] assert groups['Jug.'] == [persons[5]] assert groups['Sonst'] == [persons[4], persons[6], persons[7]] + +def test_sortPersons_withId(mocker): + def mockPerson(id): + mock = mocker.patch('solo_turnier.worker.ResultPerson') + mock.id = id + return mock + persons = [mockPerson(2), mockPerson(1), mockPerson(5), mockPerson(3)] + dataWorker = worker.DataWorker() + sorted, showIds = dataWorker.sortPersonsInGroup(persons) + assert sorted == [persons[1], persons[0], persons[3], persons[2]] + assert showIds == True + +def test_sortPersons_withoutId(mocker): + def mockPerson(name): + mock = mocker.patch('solo_turnier.worker.ResultPerson') + mock.id = 3 + mock.name = name + mock.club = 'TSC Entenhausen' + return mock + persons = [mockPerson('Max'), mockPerson('Isabel'), mockPerson('Reimund'), mockPerson('Anna')] + persons[2].id = None + dataWorker = worker.DataWorker() + sorted, showIds = dataWorker.sortPersonsInGroup(persons) + assert sorted == [persons[3], persons[1], persons[0], persons[2]] + assert showIds == False diff --git a/src/solo_turnier/worker.py b/src/solo_turnier/worker.py index ab511c4..9d940c2 100644 --- a/src/solo_turnier/worker.py +++ b/src/solo_turnier/worker.py @@ -243,3 +243,24 @@ class DataWorker: found = groups['Kin.'] + groups['Jun.'] + groups['Jug.'] groups['Sonst'] = [p for p in data.keys() if p not in found] return groups + + def sortPersonsInGroup(self, persons: list[ResultPerson]) -> list[ResultPerson]: + ids = [p.id for p in persons] + + def decorateByName(p: ResultPerson): + return (f'{p.name} ({p.club})', p) + def decorateById(p: ResultPerson): + return (p.id, p) + + if any([id == None for id in ids]): + # We need to sort by name + decorated = [decorateByName(p) for p in persons] + showIds = False + else: + decorated = [decorateById(p) for p in persons] + showIds = True + + decorated.sort() + + return ([d[1] for d in decorated], showIds) +