Split iec.py into submodules; add iec_address submodule.
This commit is contained in:
@@ -10,11 +10,9 @@ desc: Unit tests for the AuDaGe IEC module.
|
||||
|
||||
import unittest
|
||||
import warnings
|
||||
from importlib import resources
|
||||
from importlib import resources # noqa: F401
|
||||
from math import ceil, floor
|
||||
|
||||
import output
|
||||
|
||||
from AuDaGe.iec import *
|
||||
|
||||
|
||||
@@ -49,6 +47,200 @@ class TestIEC(unittest.TestCase):
|
||||
)
|
||||
|
||||
|
||||
class TestAddress(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.good_specs = (
|
||||
(
|
||||
"%MW300.04",
|
||||
(AddressType.M, AddressLength.W, 300, 4),
|
||||
"%MW300.04",
|
||||
"%MW00300.04",
|
||||
"400300.04",
|
||||
),
|
||||
(
|
||||
"%MW300.4",
|
||||
(AddressType.M, AddressLength.W, 300, 4),
|
||||
"%MW300.04",
|
||||
"%MW00300.04",
|
||||
"400300.04",
|
||||
),
|
||||
(
|
||||
"%IW65535.15",
|
||||
(AddressType.I, AddressLength.W, 65535, 15),
|
||||
"%IW65535.15",
|
||||
"%IW65535.15",
|
||||
"365535.15",
|
||||
),
|
||||
(
|
||||
"%I12345",
|
||||
(AddressType.I, None, 12345, None),
|
||||
"%I12345",
|
||||
"%I12345",
|
||||
"112345",
|
||||
),
|
||||
(
|
||||
"%M6440",
|
||||
(AddressType.M, None, 6440, None),
|
||||
"%M6440",
|
||||
"%M06440",
|
||||
"006440",
|
||||
),
|
||||
(
|
||||
"%IX12345",
|
||||
(AddressType.I, AddressLength.X, 12345, None),
|
||||
"%IX12345",
|
||||
"%IX12345",
|
||||
"112345",
|
||||
),
|
||||
(
|
||||
"%MX6440",
|
||||
(AddressType.M, AddressLength.X, 6440, None),
|
||||
"%MX6440",
|
||||
"%MX06440",
|
||||
"006440",
|
||||
),
|
||||
)
|
||||
# Semi-good specs are valid IEC but not Modbus addresses.
|
||||
self.semi_good_specs = (
|
||||
("q03", (AddressType.Q, None, 3, None), "%Q3", "%Q00003"),
|
||||
(
|
||||
"%QW7676.12",
|
||||
(AddressType.Q, AddressLength.W, 7676, 12),
|
||||
"%QW7676.12",
|
||||
"%QW07676.12",
|
||||
),
|
||||
(
|
||||
"%kd0",
|
||||
(AddressType.K, AddressLength.D, 0, None),
|
||||
"%KD0",
|
||||
"%KD00000",
|
||||
),
|
||||
(
|
||||
"%sw751",
|
||||
(AddressType.S, AddressLength.W, 751, None),
|
||||
"%SW751",
|
||||
"%SW00751",
|
||||
),
|
||||
(
|
||||
"%s323",
|
||||
(AddressType.S, None, 323, None),
|
||||
"%S323",
|
||||
"%S00323",
|
||||
),
|
||||
)
|
||||
self.bad_specs = (
|
||||
"goobledygook",
|
||||
"%n300.4",
|
||||
"40012.3",
|
||||
"%M70000",
|
||||
"%sw1000.17",
|
||||
"%QF10",
|
||||
)
|
||||
self.bad_args = (("n", None, 300, 4), ("Q", "F", 10, None))
|
||||
self.good_modbus = (
|
||||
(
|
||||
"400001",
|
||||
6,
|
||||
False,
|
||||
(AddressType.M, AddressLength.W, 1, None),
|
||||
),
|
||||
(
|
||||
400001,
|
||||
6,
|
||||
False,
|
||||
(AddressType.M, AddressLength.W, 1, None),
|
||||
),
|
||||
(
|
||||
"400001",
|
||||
5,
|
||||
False,
|
||||
(AddressType.M, AddressLength.W, 1, None),
|
||||
),
|
||||
(
|
||||
"40001",
|
||||
5,
|
||||
False,
|
||||
(AddressType.M, AddressLength.W, 1, None),
|
||||
),
|
||||
(
|
||||
"40001",
|
||||
6,
|
||||
False,
|
||||
(AddressType.M, None, 40001, None),
|
||||
),
|
||||
(
|
||||
"40001",
|
||||
6,
|
||||
True,
|
||||
(AddressType.M, AddressLength.X, 40001, None),
|
||||
),
|
||||
("000012", 6, False, (AddressType.M, None, 12, None)),
|
||||
("12", 6, False, (AddressType.M, None, 12, None)),
|
||||
("12", 6, True, (AddressType.M, AddressLength.X, 12, None)),
|
||||
("100000", 6, False, (AddressType.I, None, 0, None)),
|
||||
(
|
||||
"100000",
|
||||
6,
|
||||
True,
|
||||
(AddressType.I, AddressLength.X, 0, None),
|
||||
),
|
||||
(
|
||||
"300123",
|
||||
6,
|
||||
False,
|
||||
(AddressType.I, AddressLength.W, 123, None),
|
||||
),
|
||||
)
|
||||
self.bad_modbus = ("200000", "goobledygook", 600000)
|
||||
|
||||
def test_eq(self):
|
||||
"""Test that the __eq__ method returns False for non-Address `other`."""
|
||||
self.assertNotEqual(
|
||||
Address(*self.good_specs[0][1]), IECType("test", IECClass.BINARY)
|
||||
)
|
||||
|
||||
def test_init(self):
|
||||
for args in self.bad_args:
|
||||
with self.assertRaises(InvalidSpecError, msg=args):
|
||||
Address(*args)
|
||||
|
||||
def test_from_string(self):
|
||||
for spec in self.good_specs + self.semi_good_specs:
|
||||
self.assertEqual(
|
||||
Address.from_string(spec[0]), Address(*spec[1]), spec[0]
|
||||
)
|
||||
for spec in self.bad_specs:
|
||||
with self.assertRaises(InvalidSpecError, msg=spec):
|
||||
Address.from_string(spec)
|
||||
|
||||
def test_from_modbus(self):
|
||||
for spec in self.good_modbus:
|
||||
self.assertEqual(
|
||||
Address.from_modbus(spec[0], spec[1], spec[2]),
|
||||
Address(*spec[3]),
|
||||
)
|
||||
for spec in self.bad_modbus:
|
||||
with self.assertRaises(InvalidSpecError, msg=spec):
|
||||
Address.from_modbus(spec)
|
||||
|
||||
def test_to_string(self):
|
||||
for spec in self.good_specs:
|
||||
self.assertEqual(str(Address(*spec[1])), spec[2], spec[0])
|
||||
self.assertEqual(Address(*spec[1]).to_string(5), spec[3], spec[0])
|
||||
|
||||
def test_to_modbus(self):
|
||||
for spec in self.good_specs:
|
||||
self.assertEqual(Address(*spec[1]).to_modbus(6), spec[4], spec[0])
|
||||
for spec in self.semi_good_specs:
|
||||
with self.assertRaises(ValueError, msg=spec[0]):
|
||||
Address(*spec[1]).to_modbus()
|
||||
# Test whether to_modbus errors when the address is to large to fit in
|
||||
# the specified number of digits.
|
||||
self.assertRaises(
|
||||
ValueError, Address(*self.good_specs[2][1]).to_modbus, 5
|
||||
)
|
||||
|
||||
|
||||
class TestIECType(unittest.TestCase):
|
||||
def test_set_length(self):
|
||||
test_type = IECType(
|
||||
@@ -125,7 +317,8 @@ class TestArray(unittest.TestCase):
|
||||
spec[1],
|
||||
)
|
||||
for spec in self.error_array_specs:
|
||||
self.assertRaises(InvalidSpecError, Array, spec)
|
||||
with self.assertRaises(InvalidSpecError, msg=spec):
|
||||
Array(spec)
|
||||
|
||||
def test_get_length(self):
|
||||
"""Test get_length for both good and warning array specs."""
|
||||
@@ -143,12 +336,12 @@ class TestArray(unittest.TestCase):
|
||||
warnings.simplefilter("ignore")
|
||||
for spec in self.good_array_specs + self.warning_array_specs:
|
||||
array = Array(spec[0])
|
||||
definition = array.make_definition("test_name", "test_comment")
|
||||
definition.to_csv(
|
||||
resources.files(output).joinpath(
|
||||
f"array_definition_{str.strip(spec[0])}.csv"
|
||||
)
|
||||
)
|
||||
definition = array.make_definition("test_name", "test_comment") # noqa: F841
|
||||
# definition.to_csv(
|
||||
# resources.files(output).joinpath(
|
||||
# f"array_definition_{str.strip(spec[0])}.csv"
|
||||
# )
|
||||
# )
|
||||
|
||||
|
||||
class TestString(unittest.TestCase):
|
||||
@@ -182,7 +375,8 @@ class TestString(unittest.TestCase):
|
||||
RuntimeWarning, String(spec[0], spec[2]).get_length, spec[1]
|
||||
)
|
||||
for spec in error_string_specs:
|
||||
self.assertRaises(InvalidSpecError, String, spec[0])
|
||||
with self.assertRaises(InvalidSpecError, msg=spec[0]):
|
||||
String(spec[0])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user