Skip to content

Commit

Permalink
Suppression de la fonction de convertion, plus mis à jours des test u…
Browse files Browse the repository at this point in the history
…nitaire
  • Loading branch information
VIALLY Antoine (Externe) committed Apr 12, 2024
1 parent bbb7396 commit ab2ba2b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 76 deletions.
46 changes: 5 additions & 41 deletions src/scl_loader/scl_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,42 +127,6 @@ class SCLLoaderError(AttributeError):
pass


def _safe_convert_value(value: str) -> any:
"""
Convert a string value in typed value une valeur string en valeur typée.
Parameters
----------
value
La string contenant la valeur à convertir
Returns
-------
any
la valeur convertie
"""
if value is None:
return None

p_num = re.compile(r'^-?([0-9]+)(\.[0-9]+)?$')
value = value.strip()
low_val = str.lower(value)
if low_val == 'false':
return False
elif low_val == 'true':
return True
elif p_num.match(value) is not None:
if p_num.match(value).group(2):
return float(value)
else:
return int(value)
else:
val = value.strip()
if len(val) > 0:
return val
return None


def _get_tag_without_ns(nstag: str) -> str:
"""
Get the xml tag without namespace
Expand Down Expand Up @@ -361,7 +325,7 @@ def add_subnode_by_elem(self, elem: etree.Element):
attributes['Val'] = val

if _tag == 'Val' and 'Val' in attributes:
setattr(self, 'Val', _safe_convert_value(attributes['Val']) or '')
setattr(self, 'Val', attributes['Val']or '')
return
elif re.fullmatch(REG_DA, elem.tag):
new_node = DA(self._datatypes, elem, self._fullattrs, **attributes)
Expand Down Expand Up @@ -522,7 +486,7 @@ def _create_node(self, node_elem: etree.Element, **kwargs):
self._create_from_etree_element(dt_node_elem)

for key, value in kwargs.items():
setattr(self, key, _safe_convert_value(value))
setattr(self, key, value)

def _create_from_etree_element(self, node_elem: etree.Element):
"""
Expand Down Expand Up @@ -739,10 +703,10 @@ def _set_attributes_from_elem(self, node: etree.Element):
"""

for key, value in node.attrib.items():
setattr(self, key, _safe_convert_value(value))
setattr(self, key, value)

if node.text and len(node.text.strip()) > 0:
setattr(self, 'Val', _safe_convert_value(node.text))
setattr(self, 'Val', node.text)

self.name = str(self.name)

Expand Down Expand Up @@ -789,7 +753,7 @@ def _set_instances(self, elem: etree.Element):
if not elem.get('id'): # No instances in datatypes
tag = _get_tag_without_ns(elem.tag)
if tag == 'Val' and elem.text:
setattr(self, 'Val', _safe_convert_value(elem.text))
setattr(self, 'Val', elem.text)
elif re.fullmatch(REG_SDI, tag):
self._manage_SDI(elem)
else:
Expand Down
50 changes: 15 additions & 35 deletions tests/test_scl_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,26 +83,6 @@ def _get_node_list_by_tag(scd, tag: str) -> list:
return result


def test_safe_convert_value():
"""
I should be able to convert a value in
string format to typed format.
Typed formats supported : bool, int, float
"""
assert scdl._safe_convert_value('abc123') == 'abc123'
assert scdl._safe_convert_value('false') is False
assert scdl._safe_convert_value('False') is False
assert scdl._safe_convert_value('true') is True
assert scdl._safe_convert_value('TRUE') is True
assert scdl._safe_convert_value('123') == 123
assert scdl._safe_convert_value('-123') == -123
assert scdl._safe_convert_value('.123') == '.123'
assert scdl._safe_convert_value('01.23') == 1.23
assert scdl._safe_convert_value('-1.23') == -1.23
assert scdl._safe_convert_value('01b23') == '01b23'
assert scdl._safe_convert_value('#{~[~]{@^|`@`~\\/') == '#{~[~]{@^|`@`~\\/'


def test_valid_scd():
assert SCD_handler(SCD_OPEN_PATH)
with pytest.raises(AttributeError):
Expand Down Expand Up @@ -160,32 +140,32 @@ def test_create_DA_by_kwargs(self):
DA(self.scd.datatypes)
simple_da_inst = DA(self.scd.datatypes, None, None, **simple_da)
assert getattr(simple_da_inst, 'fc') == 'ST'
assert getattr(simple_da_inst, 'dchg') is False
assert getattr(simple_da_inst, 'qchg') is True
assert getattr(simple_da_inst, 'dupd') is False
assert getattr(simple_da_inst, 'dchg') == 'false'
assert getattr(simple_da_inst, 'qchg') == 'true'
assert getattr(simple_da_inst, 'dupd') == 'false'
assert getattr(simple_da_inst, 'name') == 'q'
assert getattr(simple_da_inst, 'bType') == 'Quality'

simple2_da_inst = DA(self.scd.datatypes, None, None, **simple2_da)
assert getattr(simple2_da_inst, 'fc') == 'DC'
assert getattr(simple2_da_inst, 'dchg') is False
assert getattr(simple2_da_inst, 'qchg') is False
assert getattr(simple2_da_inst, 'dupd') is False
assert getattr(simple2_da_inst, 'dchg') == 'false'
assert getattr(simple2_da_inst, 'qchg') == 'false'
assert getattr(simple2_da_inst, 'dupd') == 'false'
assert getattr(simple2_da_inst, 'name') == 'd'
assert getattr(simple2_da_inst, 'bType') == 'VisString255'
assert getattr(simple2_da_inst, 'valKind') == 'RO'
assert getattr(simple2_da_inst, 'valImport') is False
assert getattr(simple2_da_inst, 'valImport') == 'false'

enum_da_inst = DA(self.scd.datatypes, None, None, **enum_da)
assert getattr(enum_da_inst, 'fc') == 'CF'
assert getattr(enum_da_inst, 'dchg') is True
assert getattr(enum_da_inst, 'qchg') is False
assert getattr(enum_da_inst, 'dupd') is False
assert getattr(enum_da_inst, 'dchg') == 'true'
assert getattr(enum_da_inst, 'qchg') == 'false'
assert getattr(enum_da_inst, 'dupd') == 'false'
assert getattr(enum_da_inst, 'name') == 'ctlModel'
assert getattr(enum_da_inst, 'bType') == 'Enum'
assert getattr(enum_da_inst, 'valKind') == 'RO'
assert getattr(enum_da_inst, 'type') == 'CtlModelKind'
assert getattr(enum_da_inst, 'valImport') is False
assert getattr(enum_da_inst, 'valImport') == 'false'

def test_create_struct_DA_by_kwargs(self):
struct_da = {'name': 'originSrc', 'fc': 'ST', 'bType': 'Struct', 'type': 'Originator'}
Expand Down Expand Up @@ -236,7 +216,7 @@ def test_create_LN_by_dtid(self):
ln_inst = LN(self.scd.datatypes, None, **kwargs)
assert getattr(ln_inst, 'lnType') == 'GAPC'
assert getattr(ln_inst, 'lnClass') == 'GAPC'
assert getattr(ln_inst, 'inst') == 0
assert getattr(ln_inst, 'inst') == '0'
assert getattr(ln_inst, 'name') == 'GAPC0'
assert getattr(ln_inst, 'desc') == 'This is a GAPC'
assert isinstance(getattr(ln_inst, 'Alm1'), DO)
Expand Down Expand Up @@ -393,7 +373,7 @@ def test_get_all_GSE(self):
all_gse = self.SCD_HANDLER.get_GSEs("AUT1A_SITE_1")
assert (self.SCD_HANDLER.get_GSEs("AUT1A_SITE_1")[0].cbName == "PVR_LLN0_CB_GSE_INT")
assert (self.SCD_HANDLER.get_GSEs("AUT1A_SITE_1")[0].Address.P[0].type == "VLAN-PRIORITY")
assert (self.SCD_HANDLER.get_GSEs("AUT1A_SITE_1")[0].Address.P[0].Val == 4)
assert (self.SCD_HANDLER.get_GSEs("AUT1A_SITE_1")[0].Address.P[0].Val == '4')
assert (len(self.SCD_HANDLER.get_GSEs("AUT1A_SITE_1")) == 11)
assert (self.SCD_HANDLER.get_GSEs("AUT1A_SITE_666") == [])

Expand Down Expand Up @@ -537,15 +517,15 @@ def test_LD_get_reportcontrols(self):

result = ld.get_reportcontrols()
assert len(result) == 1
assert result[0].buffered is True
assert result[0].buffered == 'true'
assert result[0].datSet == 'PVR_LLN0_DS_RPT_DQCHG_EXT'

def test_LD_get_reportcontrol_by_name(self):
ied = self.SCD_HANDLER.get_IED_by_name('BCU_4LINE2_1')
ld = ied.get_children_LDs()[0]

assert ld.get_reportcontrol_by_name("toto") is None
assert ld.get_reportcontrol_by_name("PVR_LLN0_CB_RPT_DQCHG_EXT").buffered is True
assert ld.get_reportcontrol_by_name("PVR_LLN0_CB_RPT_DQCHG_EXT").buffered == 'true'
assert ld.get_reportcontrol_by_name("PVR_LLN0_CB_RPT_DQCHG_EXT").datSet == 'PVR_LLN0_DS_RPT_DQCHG_EXT'

def test_LD_get_datasets(self):
Expand Down

0 comments on commit ab2ba2b

Please sign in to comment.