Skip to content

Commit

Permalink
Add default param to various enum.from_str functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ankohanse committed Dec 25, 2024
1 parent 6a28abc commit f7eb5ce
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "aioxcom"
version = "1.6.3"
version = "1.7.0"
authors = [
{ name="Anko Hanse", email="[email protected]" },
]
Expand Down
24 changes: 18 additions & 6 deletions src/aioxcom/xcom_const.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ class VOLTAGE(StrEnum):
AC240 = "240 Vac"

@staticmethod
def from_str(s: str):
def from_str(s: str, default: str|None = None):
match s.upper():
case '120 VAC' | '120_VAC': return VOLTAGE.AC120
case '240 VAC' | '240_VAC': return VOLTAGE.AC240
case _: raise Exception(f"Unknown voltage: '{s}'")
case _:
if default is not None:
return default
else:
raise Exception(f"Unknown voltage: '{s}'")


### data types
Expand All @@ -30,15 +34,19 @@ class LEVEL(IntEnum):
QSP = 0x0040 # Qualified Service Person

@staticmethod
def from_str(s: str):
def from_str(s: str, default: int|None = None):
match s.upper():
case 'INFO': return LEVEL.INFO
case 'VO' | 'V.O.': return LEVEL.VO
case 'BASIC': return LEVEL.BASIC
case 'EXPERT': return LEVEL.EXPERT
case 'INST' | 'INST.': return LEVEL.INST
case 'QSP': return LEVEL.QSP
case _: raise Exception(f"Unknown level: '{s}'")
case _:
if default is not None:
return default
else:
raise Exception(f"Unknown level: '{s}'")

def __str__(self):
return self.name
Expand All @@ -61,7 +69,7 @@ class FORMAT(StrEnum):
INVALID = "INVALID" # n.a.

@staticmethod
def from_str(s: str):
def from_str(s: str, default: str|None = None):
match s.upper():
case 'BOOL': return FORMAT.BOOL
case 'FORMAT': return FORMAT.FORMAT
Expand All @@ -75,7 +83,11 @@ def from_str(s: str):
case 'BYTES': return FORMAT.BYTES
case 'MENU' | 'ONLY_LEVEL' | 'ONLY LEVEL': return FORMAT.MENU
case 'NOT SUPPORTED': return FORMAT.INVALID
case _: raise Exception(f"Unknown format: '{s}'")
case _:
if default is not None:
return default
else:
raise Exception(f"Unknown format: '{s}'")

def __str__(self):
return self.name
Expand Down
92 changes: 92 additions & 0 deletions tests/test_const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from typing import Literal
import pytest
import pytest_asyncio
from aioxcom import VOLTAGE, LEVEL, FORMAT


@pytest.mark.parametrize(
"fixture, inp_str, inp_def, exp_val, exp_except",
[
("120 VAC", "120 VAC", None, VOLTAGE.AC120, None),
("120_VAC", "120_VAC", None, VOLTAGE.AC120, None),
("240 VAC", "240 VAC", None, VOLTAGE.AC240, None),
("240_VAC", "240_VAC", None, VOLTAGE.AC240, None),
("value", "120 VAC", VOLTAGE.AC240, VOLTAGE.AC120, None),
("default", "xxxxxxx", VOLTAGE.AC240, VOLTAGE.AC240, None),
("except", "xxxxxxx", None, None, Exception),
]
)
def test_voltage(fixture:str, inp_str:str, inp_def: VOLTAGE|None, exp_val: VOLTAGE|None, exp_except: type[Exception]|None):

if exp_except is None:
val = VOLTAGE.from_str(inp_str, inp_def)
assert val == exp_val
assert type(val) is VOLTAGE
else:
with pytest.raises(exp_except):
val = VOLTAGE.from_str(inp_str, inp_def)


@pytest.mark.parametrize(
"fixture, inp_str, inp_def, exp_val, exp_except",
[
("INFO", "INFO", None, LEVEL.INFO, None),
("VO", "VO", None, LEVEL.VO, None),
("V.O.", "V.O.", None, LEVEL.VO, None),
("BASIC", "BASIC", None, LEVEL.BASIC, None),
("EXPERT", "EXPERT", None, LEVEL.EXPERT, None),
("INST", "INST", None, LEVEL.INST, None),
("INST.", "INST.", None, LEVEL.INST, None),
("QSP", "QSP", None, LEVEL.QSP, None),
("value", "EXPERT", LEVEL.BASIC, LEVEL.EXPERT, None),
("default", "xxxxxx", LEVEL.BASIC, LEVEL.BASIC, None),
("except", "xxxxxx", None, None, Exception),
]
)
def test_level(fixture:str, inp_str:str, inp_def: LEVEL|None, exp_val: LEVEL|None, exp_except: type[Exception]|None):

if exp_except is None:
val = LEVEL.from_str(inp_str, inp_def)
assert val == exp_val
assert type(val) is LEVEL
else:
with pytest.raises(exp_except):
val = LEVEL.from_str(inp_str, inp_def)


@pytest.mark.parametrize(
"fixture, inp_str, inp_def, exp_val, exp_except",
[
("BOOL", "BOOL", None, FORMAT.BOOL, None),
("FORMAT", "FORMAT", None, FORMAT.FORMAT, None),
("SHORT_ENUM", "SHORT_ENUM", None, FORMAT.SHORT_ENUM, None),
("SHORT ENUM", "SHORT ENUM", None, FORMAT.SHORT_ENUM, None),
("ERROR", "ERROR", None, FORMAT.ERROR, None),
("INT32", "INT32", None, FORMAT.INT32, None),
("FLOAT", "FLOAT", None, FORMAT.FLOAT, None),
("LONG_ENUM", "LONG_ENUM", None, FORMAT.LONG_ENUM, None),
("LONG ENUM", "LONG ENUM", None, FORMAT.LONG_ENUM, None),
("STRING", "STRING", None, FORMAT.STRING, None),
("DYNAMIC", "DYNAMIC", None, FORMAT.DYNAMIC, None),
("BYTES", "BYTES", None, FORMAT.BYTES, None),
("MENU", "MENU", None, FORMAT.MENU, None),
("ONLY_LEVEL", "ONLY_LEVEL", None, FORMAT.MENU, None),
("ONLY LEVEL", "ONLY LEVEL", None, FORMAT.MENU, None),
("NOT SUPPORTED", "NOT SUPPORTED", None, FORMAT.INVALID, None),
("value", "INT32", FORMAT.FLOAT, FORMAT.INT32, None),
("default", "xxxxx", FORMAT.FLOAT, FORMAT.FLOAT, None),
("except", "xxxxx", None, None, Exception),
]
)
def test_format(fixture:str, inp_str:str, inp_def: FORMAT|None, exp_val: FORMAT|None, exp_except: type[Exception]|None):

if exp_except is None:
val = FORMAT.from_str(inp_str, inp_def)
assert val == exp_val
assert type(val) is FORMAT
else:
with pytest.raises(exp_except):
val = FORMAT.from_str(inp_str, inp_def)

0 comments on commit f7eb5ce

Please sign in to comment.