Raise parsing exception and check it in tests

This commit is contained in:
Jean-Louis Dupond 2023-04-12 09:52:10 +02:00
parent ae8a2baaab
commit 6398c06a7a
7 changed files with 31 additions and 8 deletions

View File

@ -33,7 +33,7 @@ class TelegramParser(object):
for signature in self.telegram_specification['objects'].keys() for signature in self.telegram_specification['objects'].keys()
} }
def parse(self, telegram_data, encryption_key="", authentication_key=""): # noqa: C901 def parse(self, telegram_data, encryption_key="", authentication_key="", throw_ex=False): # noqa: C901
""" """
Parse telegram from string to dict. Parse telegram from string to dict.
The telegram str type makes python 2.x integration easier. The telegram str type makes python 2.x integration easier.
@ -93,9 +93,14 @@ class TelegramParser(object):
for match in matches: for match in matches:
try: try:
dsmr_object = parser.parse(match) dsmr_object = parser.parse(match)
except Exception: except ParseError:
logger.error("ignore line with signature {}, because parsing failed.".format(signature), logger.error("ignore line with signature {}, because parsing failed.".format(signature),
exc_info=True) exc_info=True)
if throw_ex:
raise
except Exception as err:
logger.error("Unexpected {}: {}".format(type(err), err))
raise
else: else:
telegram.add(obis_reference=signature, dsmr_object=dsmr_object) telegram.add(obis_reference=signature, dsmr_object=dsmr_object)

View File

@ -18,7 +18,10 @@ class TelegramParserFluviusTest(unittest.TestCase):
def test_parse(self): def test_parse(self):
parser = TelegramParser(telegram_specifications.BELGIUM_FLUVIUS) parser = TelegramParser(telegram_specifications.BELGIUM_FLUVIUS)
result = parser.parse(TELEGRAM_FLUVIUS_V171) try:
result = parser.parse(TELEGRAM_FLUVIUS_V171, throw_ex=True)
except Exception as ex:
assert False, f"parse trigged an exception {ex}"
# BELGIUM_VERSION_INFORMATION (0-0:96.1.4) # BELGIUM_VERSION_INFORMATION (0-0:96.1.4)
assert isinstance(result[obis.BELGIUM_VERSION_INFORMATION], CosemObject) assert isinstance(result[obis.BELGIUM_VERSION_INFORMATION], CosemObject)

View File

@ -15,7 +15,10 @@ class TelegramParserIskraIETest(unittest.TestCase):
def test_parse(self): def test_parse(self):
parser = TelegramParser(telegram_specifications.ISKRA_IE) parser = TelegramParser(telegram_specifications.ISKRA_IE)
result = parser.parse(TELEGRAM_ISKRA_IE) try:
result = parser.parse(TELEGRAM_ISKRA_IE, throw_ex=True)
except Exception as ex:
assert False, f"parse trigged an exception {ex}"
# EQUIPMENT_IDENTIFIER_GAS (0-0:96.1.0) # EQUIPMENT_IDENTIFIER_GAS (0-0:96.1.0)
assert isinstance(result[obis.EQUIPMENT_IDENTIFIER_GAS], CosemObject) assert isinstance(result[obis.EQUIPMENT_IDENTIFIER_GAS], CosemObject)

View File

@ -14,7 +14,10 @@ class TelegramParserV2_2Test(unittest.TestCase):
def test_parse(self): def test_parse(self):
parser = TelegramParser(telegram_specifications.V2_2) parser = TelegramParser(telegram_specifications.V2_2)
result = parser.parse(TELEGRAM_V2_2) try:
result = parser.parse(TELEGRAM_V2_2, throw_ex=True)
except Exception as ex:
assert False, f"parse trigged an exception {ex}"
# ELECTRICITY_USED_TARIFF_1 (1-0:1.8.1) # ELECTRICITY_USED_TARIFF_1 (1-0:1.8.1)
assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_1], CosemObject) assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_1], CosemObject)

View File

@ -14,7 +14,10 @@ class TelegramParserV3Test(unittest.TestCase):
def test_parse(self): def test_parse(self):
parser = TelegramParser(telegram_specifications.V3) parser = TelegramParser(telegram_specifications.V3)
result = parser.parse(TELEGRAM_V3) try:
result = parser.parse(TELEGRAM_V3, throw_ex=True)
except Exception as ex:
assert False, f"parse trigged an exception {ex}"
# ELECTRICITY_USED_TARIFF_1 (1-0:1.8.1) # ELECTRICITY_USED_TARIFF_1 (1-0:1.8.1)
assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_1], CosemObject) assert isinstance(result[obis.ELECTRICITY_USED_TARIFF_1], CosemObject)

View File

@ -17,7 +17,10 @@ class TelegramParserV4_2Test(unittest.TestCase):
def test_parse(self): def test_parse(self):
parser = TelegramParser(telegram_specifications.V4) parser = TelegramParser(telegram_specifications.V4)
result = parser.parse(TELEGRAM_V4_2) try:
result = parser.parse(TELEGRAM_V4_2, throw_ex=True)
except Exception as ex:
assert False, f"parse trigged an exception {ex}"
# P1_MESSAGE_HEADER (1-3:0.2.8) # P1_MESSAGE_HEADER (1-3:0.2.8)
assert isinstance(result[obis.P1_MESSAGE_HEADER], CosemObject) assert isinstance(result[obis.P1_MESSAGE_HEADER], CosemObject)

View File

@ -17,7 +17,10 @@ class TelegramParserV5Test(unittest.TestCase):
def test_parse(self): def test_parse(self):
parser = TelegramParser(telegram_specifications.V5) parser = TelegramParser(telegram_specifications.V5)
telegram = parser.parse(TELEGRAM_V5) try:
telegram = parser.parse(TELEGRAM_V5, throw_ex=True)
except Exception as ex:
assert False, f"parse trigged an exception {ex}"
# P1_MESSAGE_HEADER (1-3:0.2.8) # P1_MESSAGE_HEADER (1-3:0.2.8)
assert isinstance(telegram.P1_MESSAGE_HEADER, CosemObject) assert isinstance(telegram.P1_MESSAGE_HEADER, CosemObject)