modified to use pytest

This commit is contained in:
Emmanuel Viennet 2021-08-01 16:33:08 +03:00
parent 704bb2c170
commit 5db32a80ee
2 changed files with 109 additions and 106 deletions

View File

@ -71,11 +71,14 @@ def simple_dictlist2xml(dictlist, tagname=None, quote=False, pretty=True):
if pretty: if pretty:
# solution peu satisfaisante car on doit reparser le XML # solution peu satisfaisante car on doit reparser le XML
# de plus, on encode/decode pour avoir le tag <?xml version="1.0" encoding="utf-8"?> # de plus, on encode/decode pour avoir le tag <?xml version="1.0" encoding="utf-8"?>
ans = ( try:
minidom.parseString(ans) ans = (
.toprettyxml(indent="\t", encoding="utf-8") minidom.parseString(ans)
.decode("utf-8") .toprettyxml(indent="\t", encoding="utf-8")
) .decode("utf-8")
)
except xml.parsers.expat.ExpatError:
pass
return ans return ans

View File

@ -2,21 +2,18 @@
"""Unit tests for XML exports """Unit tests for XML exports
Usage: python -m unittest tests.test_export_xml Usage: pytest tests/unit/test_export_xml.py
""" """
# ScoDoc7 utilisait jaxml, obsolete et non portée en python3 # ScoDoc7 utilisait jaxml, obsolete et non portée en python3
# On teste ici les fionctions de remplacement, fournies par # On teste ici les fonctions de remplacement, fournies par
# notre nouveau module sco_xml.py # notre nouveau module sco_xml.py
from __future__ import print_function
import os import os
import re import re
import sys import sys
import unittest import unittest
sys.path.append("/mac/ScoDoc")
from app.scodoc import sco_xml from app.scodoc import sco_xml
from app.scodoc.gen_tables import GenTable from app.scodoc.gen_tables import GenTable
@ -36,106 +33,109 @@ def xmls_compare(x, y):
return xml_normalize(x) == xml_normalize(y) return xml_normalize(x) == xml_normalize(y)
# expected_result est le résultat de l'ancienne fonction ScoDoc7: def test_export_xml(test_client):
for (data, expected_result) in ( """exports XML compatibles ScoDoc 7"""
( # expected_result est le résultat de l'ancienne fonction ScoDoc7:
[{"id": 1, "ues": [{"note": 10}, {}, {"valeur": 25}]}, {"bis": 2}], for (data, expected_result) in (
"""<?xml version="1.0" encoding="utf-8"?> (
<infos id="1"> [{"id": 1, "ues": [{"note": 10}, {}, {"valeur": 25}]}, {"bis": 2}],
<ues note="10" /> """<?xml version="1.0" encoding="utf-8"?>
<ues /> <infos id="1">
<ues valeur="25" /> <ues note="10" />
</infos> <ues />
<infos bis="2" /> <ues valeur="25" />
""", </infos>
), <infos bis="2" />
([], """"""), """,
( ),
["allo"], ([], """"""),
"""<?xml version="1.0" encoding="utf-8"?> (
<infos code="allo" /> ["allo"],
""", """<?xml version="1.0" encoding="utf-8"?>
), <infos code="allo" />
( """,
[{}], ),
"""<?xml version="1.0" encoding="utf-8"?> (
<infos /> [{}],
""", """<?xml version="1.0" encoding="utf-8"?>
), <infos />
( """,
[{"x": 1}], ),
"""<?xml version="1.0" encoding="utf-8"?> (
<infos x="1" /> [{"x": 1}],
""", """<?xml version="1.0" encoding="utf-8"?>
), <infos x="1" />
( """,
[{"y": [1, 2, 3], "x": 1}], ),
"""<?xml version="1.0" encoding="utf-8"?> (
<infos x="1"> [{"y": [1, 2, 3], "x": 1}],
<y code="1" /> """<?xml version="1.0" encoding="utf-8"?>
<y code="2" /> <infos x="1">
<y code="3" />
</infos>
""",
),
(
[{"y": [{"x": 1}, {"y": [1, 2, 3]}], "x": 1}],
"""<?xml version="1.0" encoding="utf-8"?>
<infos x="1">
<y x="1" />
<y>
<y code="1" /> <y code="1" />
<y code="2" /> <y code="2" />
<y code="3" /> <y code="3" />
</y> </infos>
</infos> """,
""", ),
), (
): [{"y": [{"x": 1}, {"y": [1, 2, 3]}], "x": 1}],
# x = scu.simple_dictlist2xml(data, tagname="infos") """<?xml version="1.0" encoding="utf-8"?>
y = sco_xml.simple_dictlist2xml(data, tagname="infos") <infos x="1">
assert xmls_compare(expected_result, y) <y x="1" />
# print("""({}, '''{}'''),""".format(data, str(x))) <y>
<y code="1" />
<y code="2" />
<y code="3" />
</y>
</infos>
""",
),
):
# x = scu.simple_dictlist2xml(data, tagname="infos")
y = sco_xml.simple_dictlist2xml(data, tagname="infos")
assert xmls_compare(expected_result, y)
# print("""({}, '''{}'''),""".format(data, str(x)))
# test du sendXML compatible ScoDoc7 # test du sendXML compatible ScoDoc7
etuds = [{"x": 1, "etuds": ["allo", "mama"]}, {"x": 2, "etuds": ["un", "deux"]}] etuds = [{"x": 1, "etuds": ["allo", "mama"]}, {"x": 2, "etuds": ["un", "deux"]}]
# Le résultat de l'ancien print(sendXML(None, etuds, tagname="etudiants")) # Le résultat de l'ancien print(sendXML(None, etuds, tagname="etudiants"))
expected_result = """ expected_result = """
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<etudiants_list> <etudiants_list>
<etudiants x="1"> <etudiants x="1">
<etuds code="allo" /> <etuds code="allo" />
<etuds code="mama" /> <etuds code="mama" />
</etudiants> </etudiants>
<etudiants x="2"> <etudiants x="2">
<etuds code="un" /> <etuds code="un" />
<etuds code="deux" /> <etuds code="deux" />
</etudiants> </etudiants>
</etudiants_list> </etudiants_list>
""" """
assert xmls_compare( assert xmls_compare(
expected_result, expected_result,
sco_xml.simple_dictlist2xml([{"etudiant": etuds}], tagname="etudiant_list"), sco_xml.simple_dictlist2xml([{"etudiant": etuds}], tagname="etudiant_list"),
) )
# ---- Tables # ---- Tables
T = GenTable( table = GenTable(
rows=[{"nom": "Toto", "age": 26}, {"nom": "Titi", "age": 21}], rows=[{"nom": "Toto", "age": 26}, {"nom": "Titi", "age": 21}],
columns_ids=("nom", "age"), columns_ids=("nom", "age"),
) )
print(T.xml()) table_xml = table.xml()
expected_result = """ expected_result = """
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<table origin="" caption="" id="gt_806883"> <table origin="" caption="" id="gt_806883">
<row> <row>
<nom value="Toto" /> <nom value="Toto" />
<age value="26" /> <age value="26" />
</row> </row>
<row> <row>
<nom value="Titi" /> <nom value="Titi" />
<age value="21" /> <age value="21" />
</row> </row>
</table> </table>
""" """
assert xmls_compare(table_xml, expected_result)