|
| 1 | +""" |
| 2 | +we try to fill a database using kohlrahbi[sqlmodels] and the data from the machine-readable AHB submodule |
| 3 | +""" |
| 4 | + |
| 5 | +import json |
| 6 | +from pathlib import Path |
| 7 | +from typing import Generator |
| 8 | + |
| 9 | +import pytest |
| 10 | +from efoli import EdifactFormat, EdifactFormatVersion |
| 11 | +from sqlmodel import Session, SQLModel, create_engine, select |
| 12 | + |
| 13 | +from kohlrahbi.ahb import process_pruefi |
| 14 | +from kohlrahbi.enums.ahbexportfileformat import AhbExportFileFormat |
| 15 | +from kohlrahbi.models.sqlmodels.anwendungshandbuch import AhbLine, AhbMetaInformation, FlatAnwendungshandbuch |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture() |
| 19 | +def sqlite_session(tmp_path: Path) -> Generator[Session, None, None]: |
| 20 | + database_path = tmp_path / "test.db" |
| 21 | + engine = create_engine(f"sqlite:///{database_path}") |
| 22 | + SQLModel.metadata.drop_all(engine) |
| 23 | + SQLModel.metadata.create_all(engine) |
| 24 | + with Session(bind=engine) as session: |
| 25 | + yield session |
| 26 | + |
| 27 | + |
| 28 | +def _load_flat_ahb_to_db( |
| 29 | + session: Session, json_path: Path, edifact_format: EdifactFormat, edifact_format_version: EdifactFormatVersion |
| 30 | +) -> str: |
| 31 | + """returns the pruefi""" |
| 32 | + # you may use this kind of function to eventually load _all_ AHBs into a DB |
| 33 | + # if you e.g. check out our machine-readable AHB repository, |
| 34 | + # for json_path in _mr_ahb_submodule_path.rglob("FV*/**/*.json"): |
| 35 | + # with open(json_path, "r", encoding="utf-8") as json_file: |
| 36 | + # edifact_format = EdifactFormat(json_path.parent.parent.name) |
| 37 | + # edifact_format_version = EdifactFormatVersion(json_path.parent.parent.parent.name) |
| 38 | + # _load_flat_ahb_to_db(...) |
| 39 | + with open(json_path, "r", encoding="utf-8") as json_file: |
| 40 | + file_body = json.loads(json_file.read()) |
| 41 | + flat_ahb = FlatAnwendungshandbuch.model_validate(file_body) |
| 42 | + edifact_format = EdifactFormat.UTILMD |
| 43 | + edifact_format_version = EdifactFormatVersion.FV2504 |
| 44 | + file_body["meta"]["edifact_format"] = str(edifact_format) |
| 45 | + file_body["meta"]["edifact_format_version"] = str(edifact_format_version) |
| 46 | + meta = AhbMetaInformation.model_validate(file_body["meta"]) |
| 47 | + session.add(meta) |
| 48 | + flat_ahb.meta = meta |
| 49 | + for line_index, raw_line in enumerate(file_body["lines"]): |
| 50 | + raw_line["position_inside_ahb"] = line_index |
| 51 | + line = AhbLine.model_validate(raw_line) |
| 52 | + flat_ahb.lines.append(line) |
| 53 | + session.add(line) |
| 54 | + session.add(flat_ahb) |
| 55 | + session.commit() |
| 56 | + session.flush() |
| 57 | + return meta.pruefidentifikator |
| 58 | + |
| 59 | + |
| 60 | +def test_sqlmodels(sqlite_session: Session, tmp_path: Path) -> None: |
| 61 | + flat_ahb_dir_path = tmp_path |
| 62 | + docx_file_path = ( |
| 63 | + Path(__file__).parent.parent |
| 64 | + / "edi_energy_mirror" |
| 65 | + / "edi_energy_de" |
| 66 | + / "FV2504" |
| 67 | + / "UTILMDAHBStrom-informatorischeLesefassung2.1_99991231_20250404.docx" |
| 68 | + ) |
| 69 | + process_pruefi("55001", docx_file_path, flat_ahb_dir_path, (AhbExportFileFormat.FLATAHB,)) |
| 70 | + flat_ahb_path = flat_ahb_dir_path / "UTILMD" / "flatahb" / "55001.json" |
| 71 | + assert flat_ahb_path.exists() |
| 72 | + pruefi = _load_flat_ahb_to_db(sqlite_session, flat_ahb_path, EdifactFormat.UTILMD, EdifactFormatVersion.FV2504) |
| 73 | + ahbs_from_db = sqlite_session.exec( |
| 74 | + select(FlatAnwendungshandbuch) |
| 75 | + .join(AhbMetaInformation, FlatAnwendungshandbuch.id == AhbMetaInformation.ahb_id) # type:ignore[arg-type] |
| 76 | + .join(AhbLine, FlatAnwendungshandbuch.id == AhbLine.ahb_id) # type:ignore[arg-type] |
| 77 | + .where(AhbMetaInformation.pruefidentifikator == "55001") |
| 78 | + ).all() |
| 79 | + ahb_from_db: FlatAnwendungshandbuch = ahbs_from_db[0] |
| 80 | + assert isinstance(ahb_from_db, FlatAnwendungshandbuch) |
| 81 | + assert any(ahb_from_db.lines) |
| 82 | + assert ahb_from_db.meta is not None |
| 83 | + assert ahb_from_db.meta.pruefidentifikator == pruefi |
0 commit comments