|
| 1 | +# SPDX-License-Identifier: MIT |
| 2 | +import inspect |
| 3 | +from pathlib import Path |
| 4 | +from typing import Any |
| 5 | +from xml.etree import ElementTree |
| 6 | + |
| 7 | +import jinja2 |
| 8 | + |
| 9 | +import odxtools |
| 10 | +from examples.somersaultecu import database as somersault_db |
| 11 | +from odxtools.nameditemlist import NamedItemList |
| 12 | +from odxtools.odxlink import DocType, OdxDocFragment |
| 13 | +from odxtools.specialdata import SpecialData |
| 14 | +from odxtools.writepdxfile import (jinja2_odxraise_helper, make_bool_xml_attrib, make_ref_attribs, |
| 15 | + make_xml_attrib, set_category_docfrag, set_layer_docfrag) |
| 16 | + |
| 17 | +doc_frags = (OdxDocFragment(doc_name="mecuj_test", doc_type=DocType.MULTIPLE_ECU_JOB_SPEC),) |
| 18 | + |
| 19 | +multiple_ecu_job_xml_str = """<?xml version="1.0" encoding="UTF-8" standalone="no" ?> |
| 20 | + <ODX MODEL-VERSION="2.2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="odx.xsd"> |
| 21 | + <MULTIPLE-ECU-JOB-SPEC ID="odx.mecuj_test"> |
| 22 | + <SHORT-NAME>mecuj_test</SHORT-NAME> |
| 23 | + <MULTIPLE-ECU-JOBS> |
| 24 | + <MULTIPLE-ECU-JOB ID="mecuj.MEJ.paloma" SEMANTIC="foo" IS-EXECUTABLE="false"> |
| 25 | + <SHORT-NAME>paloma</SHORT-NAME> |
| 26 | + <ADMIN-DATA> |
| 27 | + <LANGUAGE>py-PY</LANGUAGE> |
| 28 | + </ADMIN-DATA> |
| 29 | + <SDGS> |
| 30 | + <SDG SI="LABERTRASH"> |
| 31 | + <SDG-CAPTION ID="mecuj.SDGC.foo"> |
| 32 | + <SHORT-NAME>foo</SHORT-NAME> |
| 33 | + </SDG-CAPTION> |
| 34 | + <SD>bla bla</SD> |
| 35 | + </SDG> |
| 36 | + <SDG SI="MORE LABERTRASH"> |
| 37 | + <SDG-CAPTION-REF ID-REF="mecuj.SDGC.foo" /> |
| 38 | + <SD>blab blab</SD> |
| 39 | + </SDG> |
| 40 | + </SDGS> |
| 41 | + <FUNCT-CLASS-REFS> |
| 42 | + <FUNCT-CLASS-REF ID-REF="somersault.FNC.session" DOCREF="somersault" DOCTYPE="CONTAINER" /> |
| 43 | + <FUNCT-CLASS-REF ID-REF="mecuj.FNC.party" /> |
| 44 | + </FUNCT-CLASS-REFS> |
| 45 | + <PROG-CODES> |
| 46 | + <PROG-CODE> |
| 47 | + <CODE-FILE>jobs.py</CODE-FILE> |
| 48 | + <SYNTAX>PYTHON3</SYNTAX> |
| 49 | + <REVISION>4.141592</REVISION> |
| 50 | + <ENTRYPOINT>multi_ecu_job</ENTRYPOINT> |
| 51 | + </PROG-CODE> |
| 52 | + </PROG-CODES> |
| 53 | + <INPUT-PARAMS> |
| 54 | + <INPUT-PARAM> |
| 55 | + <SHORT-NAME>my_input_param</SHORT-NAME> |
| 56 | + <PHYSICAL-DEFAULT-VALUE>123</PHYSICAL-DEFAULT-VALUE> |
| 57 | + <DOP-BASE-REF ID-REF="somersault.DOP.uint8" DOCREF="somersault" DOCTYPE="CONTAINER" /> |
| 58 | + </INPUT-PARAM> |
| 59 | + </INPUT-PARAMS> |
| 60 | + <OUTPUT-PARAMS> |
| 61 | + <OUTPUT-PARAM ID="mecuj.outparams.my_success" SEMANTIC="GOSSIP"> |
| 62 | + <SHORT-NAME>my_success</SHORT-NAME> |
| 63 | + <DOP-BASE-REF ID-REF="somersault.DOP.uint8" DOCREF="somersault" DOCTYPE="CONTAINER" /> |
| 64 | + </OUTPUT-PARAM> |
| 65 | + </OUTPUT-PARAMS> |
| 66 | + <NEG-OUTPUT-PARAMS> |
| 67 | + <NEG-OUTPUT-PARAM> |
| 68 | + <SHORT-NAME>my_mistake</SHORT-NAME> |
| 69 | + <DOP-BASE-REF ID-REF="somersault.DOP.uint8" DOCREF="somersault" DOCTYPE="CONTAINER" /> |
| 70 | + </NEG-OUTPUT-PARAM> |
| 71 | + </NEG-OUTPUT-PARAMS> |
| 72 | + <DIAG-LAYER-REFS> |
| 73 | + <DIAG-LAYER-REF ID-REF="somersault_lazy" DOCREF="somersault" DOCTYPE="CONTAINER" /> |
| 74 | + </DIAG-LAYER-REFS> |
| 75 | + <AUDIENCE IS-DEVELOPMENT="true"> |
| 76 | + </AUDIENCE> |
| 77 | + </MULTIPLE-ECU-JOB> |
| 78 | + </MULTIPLE-ECU-JOBS> |
| 79 | + <DIAG-DATA-DICTIONARY-SPEC> |
| 80 | + <ADMIN-DATA> |
| 81 | + <LANGUAGE>hi-HO</LANGUAGE> |
| 82 | + </ADMIN-DATA> |
| 83 | + </DIAG-DATA-DICTIONARY-SPEC> |
| 84 | + <FUNCT-CLASSS> |
| 85 | + <FUNCT-CLASS ID="mecuj.FNC.party"> |
| 86 | + <SHORT-NAME>party</SHORT-NAME> |
| 87 | + </FUNCT-CLASS> |
| 88 | + </FUNCT-CLASSS> |
| 89 | + <ADDITIONAL-AUDIENCES> |
| 90 | + <ADDITIONAL-AUDIENCE ID="mecuj.AA.wizards"> |
| 91 | + <SHORT-NAME>wizards</SHORT-NAME> |
| 92 | + </ADDITIONAL-AUDIENCE> |
| 93 | + </ADDITIONAL-AUDIENCES> |
| 94 | + </MULTIPLE-ECU-JOB-SPEC> |
| 95 | +</ODX>""" |
| 96 | + |
| 97 | +multiple_ecu_job_et = ElementTree.fromstring(multiple_ecu_job_xml_str) |
| 98 | + |
| 99 | + |
| 100 | +def test_create_multiple_ecu_job_from_et() -> None: |
| 101 | + somersault_db._multiple_ecu_job_specs = NamedItemList() |
| 102 | + somersault_db.add_xml_tree(multiple_ecu_job_et) |
| 103 | + somersault_db.refresh() |
| 104 | + assert len(somersault_db.multiple_ecu_job_specs) == 1 |
| 105 | + |
| 106 | + mecujs = somersault_db.multiple_ecu_job_specs[0] |
| 107 | + assert len(mecujs._build_odxlinks()) == 6 |
| 108 | + assert len(mecujs.multiple_ecu_jobs) == 1 |
| 109 | + ddds = mecujs.diag_data_dictionary_spec |
| 110 | + assert ddds is not None |
| 111 | + assert ddds.admin_data is not None |
| 112 | + assert ddds.admin_data.language == "hi-HO" |
| 113 | + assert mecujs.functional_classes.party is not None |
| 114 | + assert mecujs.additional_audiences.wizards is not None |
| 115 | + |
| 116 | + mecuj = mecujs.multiple_ecu_jobs.paloma |
| 117 | + assert mecuj.admin_data is not None |
| 118 | + assert mecuj.admin_data.language == "py-PY" |
| 119 | + assert len(mecuj.sdgs) == 2 |
| 120 | + assert len(mecuj.sdgs[0].values) == 1 |
| 121 | + assert mecuj.sdgs[0].semantic_info == "LABERTRASH" |
| 122 | + assert mecuj.sdgs[0].sdg_caption_ref is None |
| 123 | + assert mecuj.sdgs[0].sdg_caption is not None |
| 124 | + assert mecuj.sdgs[0].sdg_caption.short_name == "foo" |
| 125 | + assert mecuj.sdgs[1].sdg_caption_ref is not None |
| 126 | + assert mecuj.sdgs[1].sdg_caption is not None |
| 127 | + assert mecuj.sdgs[1].sdg_caption.short_name == "foo" |
| 128 | + sd = mecuj.sdgs[0].values[0] |
| 129 | + assert isinstance(sd, SpecialData) |
| 130 | + assert sd.value == "bla bla" |
| 131 | + sd = mecuj.sdgs[1].values[0] |
| 132 | + assert isinstance(sd, SpecialData) |
| 133 | + assert sd.value == "blab blab" |
| 134 | + |
| 135 | + |
| 136 | +def test_write_multiple_ecu_job() -> None: |
| 137 | + somersault_db._multiple_ecu_job_specs = NamedItemList() |
| 138 | + somersault_db.add_xml_tree(multiple_ecu_job_et) |
| 139 | + somersault_db.refresh() |
| 140 | + assert len(somersault_db.multiple_ecu_job_specs) == 1 |
| 141 | + |
| 142 | + __module_filename = inspect.getsourcefile(odxtools) |
| 143 | + assert isinstance(__module_filename, str) |
| 144 | + test_jinja_vars: dict[str, Any] = {} |
| 145 | + test_jinja_vars["multiple_ecu_job_spec"] = somersault_db.multiple_ecu_job_specs[0] |
| 146 | + templates_dir = Path(__module_filename).parent / "templates" |
| 147 | + jinja_env = jinja2.Environment(loader=jinja2.FileSystemLoader(templates_dir)) |
| 148 | + jinja_env.globals["odxraise"] = jinja2_odxraise_helper |
| 149 | + jinja_env.globals["make_xml_attrib"] = make_xml_attrib |
| 150 | + jinja_env.globals["make_bool_xml_attrib"] = make_bool_xml_attrib |
| 151 | + jinja_env.globals["set_category_docfrag"] = lambda cname, ctype: set_category_docfrag( |
| 152 | + test_jinja_vars, cname, ctype) |
| 153 | + jinja_env.globals["set_layer_docfrag"] = lambda lname: set_layer_docfrag(test_jinja_vars, lname) |
| 154 | + jinja_env.globals["make_ref_attribs"] = lambda ref: make_ref_attribs(test_jinja_vars, ref) |
| 155 | + jinja_env.globals["getattr"] = getattr |
| 156 | + jinja_env.globals["hasattr"] = hasattr |
| 157 | + |
| 158 | + template = jinja_env.get_template("multiple-ecu-job-spec.odx-m.xml.jinja2") |
| 159 | + rawodx: str = template.render(test_jinja_vars) |
| 160 | + |
| 161 | + rawodx2 = '\n'.join(rawodx.split("\n")[0:1] + rawodx.split("\n")[2:]) |
| 162 | + expected_xml = multiple_ecu_job_xml_str.replace(" ", "").upper() |
| 163 | + actual_xml = rawodx2.replace(" ", "").upper() |
| 164 | + |
| 165 | + assert expected_xml == actual_xml |
0 commit comments