Create importer to objects

This commit is contained in:
Christian Wolf 2022-11-14 20:01:32 +01:00
parent d17379e254
commit a4e9020ebd
5 changed files with 196 additions and 0 deletions

View File

@ -2,4 +2,7 @@
from . import cli from . import cli
from . import reader from . import reader
from . import participant from . import participant
from . import worker
from . import batch from . import batch

View File

@ -0,0 +1,47 @@
from solo_turnier import worker
import os
import json
import pytest
def __importJSONData(name):
path = os.path.join(os.path.dirname(__file__), 'worker', name)
with open(path, 'r') as fp:
return json.load(fp)
@pytest.fixture
def fixture_csvExtractor():
data = __importJSONData('csvImport.json')
expected = __importJSONData('csvImportResult.json')
return (data, expected)
def test_csvExtractor(fixture_csvExtractor):
extractor = worker.CSVExtractor()
mapped = extractor.mapCSVImport(fixture_csvExtractor[0])
assert len(mapped) == len(fixture_csvExtractor[1])
for i,elem in enumerate(fixture_csvExtractor[1]):
assert mapped[i].__dict__ == elem
def test_extractPersonFromRow():
row = worker.ResultRow('Max', 'Mustermann', 'TSC Entenhausen', '2', 'Kin', 'Adv.', 'Rumba', '2', '2')
person = worker.ResultPerson.extractFromResultRow(row)
expected = {
'firstName': 'Max',
'lastName': 'Mustermann',
'name': 'Max Mustermann',
'club': 'TSC Entenhausen',
'id': None
}
assert person.__dict__ == expected
def test_extractCompetitionFromRow():
row = worker.ResultRow('Max', 'Mustermann', 'TSC Entenhausen', '2', 'Kin', 'Adv.', 'Rumba', '2', '2')
person = worker.CompetitionResult.extractFromResultRow(row)
expected = {
'dance': 'Rumba',
'class_': 'Adv.',
'place': '2',
'placeTo': '2',
'id': 2
}
assert person.__dict__ == expected

View File

@ -0,0 +1,49 @@
{
"header": [
"\ufeffTurniernr", "Datum", "Gruppe", "Klasse", "Art", "StNr", "VNamePartner", "NNamePartner",
"VNamePartnerin", "NNamePartnerin", "Club", "LTV", "Platzvon", "Platzbis", "Punkte", "GruppePaar", "KlassePaar", "Hinweis"
],
"data": [
[
"",
"19.06.2022",
"Kin.",
"Newc./Beg.",
"Cha Cha",
"2",
"Max",
"Mustermann 1",
"",
"",
"Club A",
"TRP",
"1",
"1",
"0",
"Kin.",
"Beginner",
""
],
[
"",
"19.06.2022",
"Kin.",
"Newc./Beg.",
"Cha Cha",
"3",
"Max",
"Mustermann 2",
"",
"",
"Club B",
"SLT",
"3",
"3",
"0",
"Kin.",
"Beginner",
""
]
]
}

View File

@ -0,0 +1,27 @@
[
{
"dance": "Cha Cha",
"id": "2",
"firstName": "Max",
"lastName": "Mustermann 1",
"name": "Max Mustermann 1",
"club": "Club A",
"place": "1",
"placeTo": "1",
"group": "Kin.",
"class_": "Beginner"
},
{
"dance": "Cha Cha",
"id": "3",
"firstName": "Max",
"lastName": "Mustermann 2",
"name": "Max Mustermann 2",
"club": "Club B",
"place": "3",
"placeTo": "3",
"group": "Kin.",
"class_": "Beginner"
}
]

View File

@ -0,0 +1,70 @@
import logging
class ResultRow:
def __init__(self, firstName, lastName, club, id, group, class_, dance, place, placeTo):
self.firstName = firstName
self.lastName = lastName
self.name = f'{firstName} {lastName}'
self.club = club
self.id = id
self.group = group
self.class_ = class_
self.dance = dance
self.place = place
self.placeTo = placeTo
class ResultPerson:
def __init__(self, firstName, lastName, club, id = None):
self.firstName = firstName
self.lastName = lastName
self.name = f'{firstName} {lastName}'
self.club = club
self.id = id
@staticmethod
def extractFromResultRow(row: ResultRow):
return ResultPerson(
firstName=row.firstName,
lastName=row.lastName,
club=row.club
)
class CompetitionResult:
def __init__(self, dance, class_, place, placeTo, id):
self.dance = dance
self.class_ = class_
self.place = place
self.placeTo = placeTo
self.id = int(id)
@staticmethod
def extractFromResultRow(row: ResultRow):
return CompetitionResult(
dance=row.dance,
class_=row.class_,
place=row.place, placeTo=row.placeTo,
id=row.id
)
class CSVExtractor:
def __init__(self):
self.l = logging.getLogger('solo_turnier.worker')
def mapCSVImport(self, imported) -> list[ResultRow]:
ret = []
def __processRow(row):
result = ResultRow(
dance=row[4],
id=row[5],
firstName=row[6], lastName=row[7],
club=row[10],
place=row[12], placeTo=row[13],
group=row[15], class_=row[16]
)
ret.append(result)
for row in imported['data']:
__processRow(row)
return ret