From 80d0269cb23c8b87d5f1357de41068b1717323f3 Mon Sep 17 00:00:00 2001 From: Christian Wolf Date: Tue, 15 Nov 2022 13:00:17 +0100 Subject: [PATCH] Extract some commin data from raw imports --- src/solo_turnier/tests/test_worker.py | 68 +++++++++++++++++++++++++++ src/solo_turnier/worker.py | 42 +++++++++++++++++ 2 files changed, 110 insertions(+) diff --git a/src/solo_turnier/tests/test_worker.py b/src/solo_turnier/tests/test_worker.py index ea749f4..228f1a5 100644 --- a/src/solo_turnier/tests/test_worker.py +++ b/src/solo_turnier/tests/test_worker.py @@ -101,3 +101,71 @@ def test_checkUniqueIds_True(): assert dataWorker.checkUniqueIds(data) == False assert person1.id == None assert person2.id == 1 + +@pytest.fixture(params=range(4)) +def fixture_consolidateGroups(request): + person1 = worker.ResultPerson('Max 1', 'Mustermann', 'TSC Entenhausen') + person2 = worker.ResultPerson('Max 2', 'Mustermann', 'TSC Entenhausen') + person3 = worker.ResultPerson('Max 3', 'Mustermann', 'TSC Entenhausen') + person4 = worker.ResultPerson('Max 4', 'Mustermann', 'TSC Entenhausen') + # persons = (person1, person2, person3, person4) + + dict1 = { + person1: [ + worker.CompetitionResult('Rumba', 'Kin.', 'Adv.', '2', '2', 2), + worker.CompetitionResult('Jive', 'Kin.', 'Beg.', '1', '1', 3) + ] + } + dict2 = { + person2: [ + worker.CompetitionResult('Rumba', 'Kin.', 'Adv.', '2', '2', 2), + worker.CompetitionResult('Jive', 'Kin./Jun.', 'Beg.', '1', '1', 3) + ] + } + dict3 = { + person3: [ + worker.CompetitionResult('Rumba', 'Kin.', 'Adv.', '2', '2', 2) + ] + } + dict4 = { + person4: [ + worker.CompetitionResult('Rumba', 'Kin./Jun.', 'Adv.', '2', '2', 2) + ] + } + + cases = ( + (dict1|dict3, (True, False), {}, False), + (dict1|dict2|dict3, (True, True), {}, False), + (dict4, (False, False), {person4: 'Kin./Jun.'}, False), + (dict1|dict2|dict3|dict4, (False, True), {person4: 'Kin./Jun.'}, False), + ) + return cases[request.param] + +@pytest.fixture +def fixture_consolidateGroups_fail(): + person5 = worker.ResultPerson('Max 5', 'Mustermann', 'TSC Entenhausen') + + dict5 = { + person5: [ + worker.CompetitionResult('Rumba', 'Kin.', 'Adv.', '2', '2', 2), + worker.CompetitionResult('Jive', 'Jun.', 'Beg.', '1', '1', 3) + ] + } + + return (dict5, person5) + +def test_consolidateGroups(fixture_consolidateGroups): + data = fixture_consolidateGroups[0] + dataWorker = worker.DataWorker() + + assert dataWorker.consolidateGroups(data) == fixture_consolidateGroups[1] + + for person in data: + assert person.group == fixture_consolidateGroups[2].get(person, 'Kin.') + +def test_consolidateGroups_failing(fixture_consolidateGroups, fixture_consolidateGroups_fail): + data = fixture_consolidateGroups[0] | fixture_consolidateGroups_fail[0] + dataWorker = worker.DataWorker() + + with pytest.raises(Exception): + dataWorker.consolidateGroups(data) diff --git a/src/solo_turnier/worker.py b/src/solo_turnier/worker.py index 2ab42a2..58abf4a 100644 --- a/src/solo_turnier/worker.py +++ b/src/solo_turnier/worker.py @@ -140,3 +140,45 @@ class DataWorker: unique = False return unique + + """ + Return a tuple + The first one is True, if all persons could be unambiguously identified a group + The second one is True if there was the need to override a group but it was possible to extract from other data + The second one can be seen as a warning + """ + def consolidateGroups(self, data:dict[ResultPerson, list[CompetitionResult]]) -> tuple[bool, bool]: + ambiguous = False + warnChange = False + + unambiguousGroups = set(['Kin.', 'Jun.', 'Jug.']) + combinations = set(['Kin./Jun.', 'Jun./Jug.']) + + for person in data: + groupsRaw = set([c.group for c in data[person]]) + + unknown = groupsRaw.difference(unambiguousGroups).difference(combinations) + if len(unknown) > 0: + raise Exception(f'There were unknown groups found for {person}: {unknown}') + + numUnambiguousGroups = len(groupsRaw.intersection(unambiguousGroups)) + + if numUnambiguousGroups == 0: + if len(groupsRaw) == 2: + warnChange = True + person.group = 'Jun.' + else: + ambiguous = True + if len(groupsRaw) == 1: + person.group = list(groupsRaw)[0] + + elif numUnambiguousGroups == 1: + if len(groupsRaw.intersection(combinations)) > 0: + warnChange = True + + person.group = list(groupsRaw.intersection(unambiguousGroups))[0] + + else: + raise Exception(f'{person} cannot have different groups.') + + return (not ambiguous, warnChange)