diff --git a/app/scodoc/sco_xml.py b/app/scodoc/sco_xml.py index 5677e00d7d..633d271420 100644 --- a/app/scodoc/sco_xml.py +++ b/app/scodoc/sco_xml.py @@ -71,11 +71,14 @@ def simple_dictlist2xml(dictlist, tagname=None, quote=False, pretty=True): if pretty: # solution peu satisfaisante car on doit reparser le XML # de plus, on encode/decode pour avoir le tag - ans = ( - minidom.parseString(ans) - .toprettyxml(indent="\t", encoding="utf-8") - .decode("utf-8") - ) + try: + ans = ( + minidom.parseString(ans) + .toprettyxml(indent="\t", encoding="utf-8") + .decode("utf-8") + ) + except xml.parsers.expat.ExpatError: + pass return ans diff --git a/tests/unit/test_export_xml.py b/tests/unit/test_export_xml.py index 11c438ce19..dd9bc7acd5 100644 --- a/tests/unit/test_export_xml.py +++ b/tests/unit/test_export_xml.py @@ -2,21 +2,18 @@ """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 -# On teste ici les fionctions de remplacement, fournies par +# On teste ici les fonctions de remplacement, fournies par # notre nouveau module sco_xml.py -from __future__ import print_function import os import re import sys import unittest -sys.path.append("/mac/ScoDoc") - from app.scodoc import sco_xml from app.scodoc.gen_tables import GenTable @@ -36,106 +33,109 @@ def xmls_compare(x, y): return xml_normalize(x) == xml_normalize(y) -# expected_result est le résultat de l'ancienne fonction ScoDoc7: -for (data, expected_result) in ( - ( - [{"id": 1, "ues": [{"note": 10}, {}, {"valeur": 25}]}, {"bis": 2}], - """ - - - - - - -""", - ), - ([], """"""), - ( - ["allo"], - """ - -""", - ), - ( - [{}], - """ - -""", - ), - ( - [{"x": 1}], - """ - -""", - ), - ( - [{"y": [1, 2, 3], "x": 1}], - """ - - - - - -""", - ), - ( - [{"y": [{"x": 1}, {"y": [1, 2, 3]}], "x": 1}], - """ - - - +def test_export_xml(test_client): + """exports XML compatibles ScoDoc 7""" + # expected_result est le résultat de l'ancienne fonction ScoDoc7: + for (data, expected_result) in ( + ( + [{"id": 1, "ues": [{"note": 10}, {}, {"valeur": 25}]}, {"bis": 2}], + """ + + + + + + + """, + ), + ([], """"""), + ( + ["allo"], + """ + + """, + ), + ( + [{}], + """ + + """, + ), + ( + [{"x": 1}], + """ + + """, + ), + ( + [{"y": [1, 2, 3], "x": 1}], + """ + - - -""", - ), -): - # 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))) + + """, + ), + ( + [{"y": [{"x": 1}, {"y": [1, 2, 3]}], "x": 1}], + """ + + + + + + + + + """, + ), + ): + # 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 -etuds = [{"x": 1, "etuds": ["allo", "mama"]}, {"x": 2, "etuds": ["un", "deux"]}] -# Le résultat de l'ancien print(sendXML(None, etuds, tagname="etudiants")) -expected_result = """ - - - - - - - - - - - -""" + # test du sendXML compatible ScoDoc7 + etuds = [{"x": 1, "etuds": ["allo", "mama"]}, {"x": 2, "etuds": ["un", "deux"]}] + # Le résultat de l'ancien print(sendXML(None, etuds, tagname="etudiants")) + expected_result = """ + + + + + + + + + + + + """ -assert xmls_compare( - expected_result, - sco_xml.simple_dictlist2xml([{"etudiant": etuds}], tagname="etudiant_list"), -) + assert xmls_compare( + expected_result, + sco_xml.simple_dictlist2xml([{"etudiant": etuds}], tagname="etudiant_list"), + ) -# ---- Tables -T = GenTable( - rows=[{"nom": "Toto", "age": 26}, {"nom": "Titi", "age": 21}], - columns_ids=("nom", "age"), -) -print(T.xml()) + # ---- Tables + table = GenTable( + rows=[{"nom": "Toto", "age": 26}, {"nom": "Titi", "age": 21}], + columns_ids=("nom", "age"), + ) + table_xml = table.xml() -expected_result = """ - - - - - - - - - - -
-""" + expected_result = """ + + + + + + + + + + +
+ """ + assert xmls_compare(table_xml, expected_result) \ No newline at end of file