From 867b18576fc77ec87ad40c930e151fcf5ae63fdb Mon Sep 17 00:00:00 2001 From: Christian Wolf Date: Tue, 15 Nov 2022 16:52:19 +0100 Subject: [PATCH] Extract data from HTML to allow for simpler extraction --- src/solo_turnier/tests/test_worker.py | 26 ++++++++++++++++++++++++++ src/solo_turnier/worker.py | 11 +++++++++++ 2 files changed, 37 insertions(+) diff --git a/src/solo_turnier/tests/test_worker.py b/src/solo_turnier/tests/test_worker.py index 3080506..9fbe0d2 100644 --- a/src/solo_turnier/tests/test_worker.py +++ b/src/solo_turnier/tests/test_worker.py @@ -2,6 +2,7 @@ from solo_turnier import worker import os import json import pytest +import pytest_mock def __importJSONData(name): path = os.path.join(os.path.dirname(__file__), 'worker', name) @@ -190,3 +191,28 @@ def test_consolidateGroups_failing(fixture_consolidateGroups_fail): with pytest.raises(Exception): dataWorker.consolidateGroups(data) + +def test_createHtmlLUT(mocker): + mock = mocker.patch('solo_turnier.html_parser.HtmlParser.guessDataFromHtmlTitle') + mock.side_effect= [ + {'group': 'group1', 'class_': 'class1', 'dance': 'dance1'}, + {'group': 'group2', 'class_': 'class2', 'dance': 'dance2'}, + {'group': 'group3', 'class_': 'class3', 'dance': 'dance3'}, + ] + + importMock1 = mocker.patch('solo_turnier.html_parser.HtmlImport') + importMock2 = mocker.patch('solo_turnier.html_parser.HtmlImport') + importMock3 = mocker.patch('solo_turnier.html_parser.HtmlImport') + importMock1.title = 'Fake title 1' + importMock2.title = 'Fake title 2' + importMock3.title = 'Fake title 3' + + dataWorker = worker.DataWorker() + structure = dataWorker._createHtmlLUT([importMock1, importMock2, importMock3]) + + expected = { + ('group1', 'class1', 'dance1'): importMock1, + ('group2', 'class2', 'dance2'): importMock2, + ('group3', 'class3', 'dance3'): importMock3, + } + assert expected == structure diff --git a/src/solo_turnier/worker.py b/src/solo_turnier/worker.py index 12efd75..1ecd835 100644 --- a/src/solo_turnier/worker.py +++ b/src/solo_turnier/worker.py @@ -1,5 +1,6 @@ import logging +from solo_turnier import html_parser class ResultRow: def __init__(self, firstName, lastName, club, id, group, class_, dance, place, placeTo, competitionGroup, competitionClass): self.firstName = firstName @@ -196,3 +197,13 @@ class DataWorker: raise Exception(f'{person} cannot have different groups.') return (not ambiguous, warnChange) + + def _createHtmlLUT(self, htmlImports: list[html_parser.HtmlImport]): + ret = {} + parser = html_parser.HtmlParser() + for imp in htmlImports: + parsed = parser.guessDataFromHtmlTitle(imp.title) + key = (parsed['group'], parsed['class_'], parsed['dance']) + ret[key] = imp + return ret +