Extract data from HTML to allow for simpler extraction

This commit is contained in:
Christian Wolf 2022-11-15 16:52:19 +01:00
parent 8301ae206f
commit 867b18576f
2 changed files with 37 additions and 0 deletions

View File

@ -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

View File

@ -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