Write unit tests for converter module.

This commit is contained in:
2026-10-29 17:23:47 -05:00
parent a152732c7d
commit 529545b688
2 changed files with 106 additions and 1 deletions

View File

@@ -27,11 +27,14 @@ from numpy import nan
import assets
import AuDaGe
from AuDaGe import iec
from AuDaGe.formats.controlexpert.xsy import XSYReader
from AuDaGe.frame import AuDaGeFrame, Frame
def regex_mapping(string: str, **mapping: dict[str, Any]) -> Any:
"""Return the first value from `mapping` whose key matches `string`."""
"""Return the first value from `mapping` whose key (interpreted as a regular
expression) matches `string`.
"""
for key, value in mapping.items():
if re.search(key, string) is not None:
return value
@@ -247,6 +250,7 @@ class Converter:
"csv": pd.read_csv,
"xml": pd.read_xml,
"xlsx": pd.read_excel,
"xsy": XSYReader().read,
}
format = deepcopy(self.spec[YN.FORMAT])
if isinstance(format, str):
@@ -293,6 +297,7 @@ class Converter:
"csv": frame.to_csv,
"xml": frame.to_xml,
"xlsx": frame.to_excel,
"xsy": XSYReader().write,
}
format = deepcopy(self.spec[YN.FORMAT])
if isinstance(format, str):

100
tests/test_converter.py Normal file
View File

@@ -0,0 +1,100 @@
"""
AuDaGe Converter tests
name: test_converter.py
by: Josh Lucas
date: 2026-10-29
desc: Unit tests for the AuDaGe Converter module.
"""
import unittest
from importlib import resources
from AuDaGe.converter import Converter
try:
from tests import assets, output
except ImportError: # pragma: no cover
import assets # noqa: I001
import output # ty: ignore[unresolved-import]
class TestConverter(unittest.TestCase):
plcs = ("WA3252_PLC_001_1", "WA3252_PLC_002_1")
tags = ("caption", "desc", "message", "tagname", "unit")
def setUp(self):
self.assets_path = resources.files(assets)
self.output_path = resources.files(output)
def test_init(self):
self.converters = {}
for plc in self.plcs:
for tag in self.tags:
substitutions = {"$extension$": tag.title(), "$plc$": plc}
self.converters[(plc, tag)] = Converter(
f"extensions/kepserver/strings/{tag}_csv.yaml",
substitutions=substitutions,
)
def test_from_AF(self):
pass
def test_from_file(self):
self.kep = {}
for key, converter in self.converters.items():
self.kep[key] = converter.from_file(
str(self.assets_path.joinpath(f"{key[0]}.csv"))
)
def test_to_AF(self):
self.af = {}
for (kep_key, kep), (converter_key, converter) in zip(
self.kep.items(), self.converters.items()
):
self.af[kep_key] = converter.to_AF(kep)
def test_to_file(self):
pass
if __name__ == "__main__":
unittest.main(failfast=True)
# try:
# kep = kep_converter.from_file(
# f"C:/Users/j.lucas/OneDrive - Prime Controls/Customers/NTMWD/"
# f"Engineering/Leonard (North) Water Treatment Plant/Programs/PLC/"
# f"WA3252-PLC-001-1 Raw Water PS/{plc}.csv"
# )
# except FileNotFoundError:
# kep = kep_converter.from_file(
# f"D:/OneDrive - Prime Controls/Customers/NTMWD/"
# f"Engineering/Leonard (North) Water Treatment Plant/Programs/PLC/"
# f"WA3252-PLC-001-1 Raw Water PS/{plc}.csv"
# )
# af = kep_converter.to_AF(kep)
# kep2 = kep_converter.from_AF(af)
# kep_converter.to_file(kep2, f"tests/output/{plc}_strings_{tag}.csv")
# af2 = kep_converter.to_AF(kep2)
# macro_converter = Converter(
# f"extensions/factorytalk/macro/{tag}_macro.yaml",
# substitutions=substitutions,
# )
# macro = macro_converter.from_AF(af2)
# macro_converter.to_file(macro, f"tests/output/{plc}_{tag}.mcr")
# kep_converter = Converter("extensions/kepserver/modbus/csv.yaml")
# kep = kep_converter.from_file(
# f"C:/Users/j.lucas/OneDrive - Prime Controls/Customers/NTMWD/"
# f"Engineering/Leonard (North) Water Treatment Plant/Programs/PLC/"
# f"WA3252-PLC-001-1 Raw Water PS/{plc}.csv"
# )
# af = kep_converter.to_AF(kep)
# macro_converter = Converter(
# "extensions/factorytalk/macro/alarm_severity_macro.yaml",
# substitutions={"$plc$": plc},
# )
# macro = macro_converter.from_AF(af)
# macro_converter.to_file(
# macro, f"tests/output/{plc}_alarm_severities.mcr"
# )