Write unit tests for converter module.
This commit is contained in:
@@ -27,11 +27,14 @@ from numpy import nan
|
|||||||
import assets
|
import assets
|
||||||
import AuDaGe
|
import AuDaGe
|
||||||
from AuDaGe import iec
|
from AuDaGe import iec
|
||||||
|
from AuDaGe.formats.controlexpert.xsy import XSYReader
|
||||||
from AuDaGe.frame import AuDaGeFrame, Frame
|
from AuDaGe.frame import AuDaGeFrame, Frame
|
||||||
|
|
||||||
|
|
||||||
def regex_mapping(string: str, **mapping: dict[str, Any]) -> Any:
|
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():
|
for key, value in mapping.items():
|
||||||
if re.search(key, string) is not None:
|
if re.search(key, string) is not None:
|
||||||
return value
|
return value
|
||||||
@@ -247,6 +250,7 @@ class Converter:
|
|||||||
"csv": pd.read_csv,
|
"csv": pd.read_csv,
|
||||||
"xml": pd.read_xml,
|
"xml": pd.read_xml,
|
||||||
"xlsx": pd.read_excel,
|
"xlsx": pd.read_excel,
|
||||||
|
"xsy": XSYReader().read,
|
||||||
}
|
}
|
||||||
format = deepcopy(self.spec[YN.FORMAT])
|
format = deepcopy(self.spec[YN.FORMAT])
|
||||||
if isinstance(format, str):
|
if isinstance(format, str):
|
||||||
@@ -293,6 +297,7 @@ class Converter:
|
|||||||
"csv": frame.to_csv,
|
"csv": frame.to_csv,
|
||||||
"xml": frame.to_xml,
|
"xml": frame.to_xml,
|
||||||
"xlsx": frame.to_excel,
|
"xlsx": frame.to_excel,
|
||||||
|
"xsy": XSYReader().write,
|
||||||
}
|
}
|
||||||
format = deepcopy(self.spec[YN.FORMAT])
|
format = deepcopy(self.spec[YN.FORMAT])
|
||||||
if isinstance(format, str):
|
if isinstance(format, str):
|
||||||
|
|||||||
100
tests/test_converter.py
Normal file
100
tests/test_converter.py
Normal 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"
|
||||||
|
# )
|
||||||
Reference in New Issue
Block a user