From 3dfc022b0854ac7c6cd0de45d3b5dc86080832fc Mon Sep 17 00:00:00 2001 From: Christian Wolf Date: Tue, 15 Nov 2022 19:37:08 +0100 Subject: [PATCH] Map individual results onto common table rows --- src/solo_turnier/tests/test_worker.py | 11 +++++++++++ src/solo_turnier/worker.py | 12 ++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/solo_turnier/tests/test_worker.py b/src/solo_turnier/tests/test_worker.py index 5875e1d..5a8c18b 100644 --- a/src/solo_turnier/tests/test_worker.py +++ b/src/solo_turnier/tests/test_worker.py @@ -383,3 +383,14 @@ def test_sortPersons_withoutId(mocker): sorted, showIds = dataWorker.sortPersonsInGroup(persons) assert sorted == [persons[3], persons[1], persons[0], persons[2]] assert showIds == False + +def test_mapPersonResultsToDanceList(mocker): + def mockResult(dance): + mock = mocker.patch('solo_turnier.worker.CompetitionResult') + mock.dance = dance + return mock + dances = ['Cha Cha', 'Rumba', 'Langs. Walzer', 'Quickstep'] + results = [mockResult('Rumba'), mockResult('Quickstep'), mockResult('Cha Cha')] + dataWorker = worker.DataWorker() + mappedResults = dataWorker.mapPersonResultsToDanceList(results, dances) + assert mappedResults == [results[2], results[0], None, results[1]] diff --git a/src/solo_turnier/worker.py b/src/solo_turnier/worker.py index 9d940c2..2332fd1 100644 --- a/src/solo_turnier/worker.py +++ b/src/solo_turnier/worker.py @@ -264,3 +264,15 @@ class DataWorker: return ([d[1] for d in decorated], showIds) + def mapPersonResultsToDanceList(self, results: list[CompetitionResult], dances: list[str]) -> list[CompetitionResult|None]: + ret = [] + for dance in dances: + competitions = [c for c in results if c.dance == dance] + if len(competitions) == 0: + ret.append(None) + elif len(competitions) > 1: + raise Exception(f'Multiple competitions with the same dance "{dance}" found.') + else: + ret.append(competitions[0]) + + return ret