From: Antonio Valentino <antonio.valentino@tiscali.it>
Date: Sun, 15 May 2022 06:42:52 +0000
Subject: fix-tests

Forwarded: no

This patch implements 2 main changes:
* fixes XML comparison
* remove parts of the test that make access to the internet
  (not allowed during the build of debian packages)

The patch is not forwarded upstream because:
* the upstream development branch already has an improved code for XML
  comparison
* excluding test code that access the internet is a debian specific need
---
 src/pykml/test/test_factory.py | 61 +++++++++++++++++++++---------------------
 src/pykml/test/test_helpers.py |  4 +--
 src/pykml/test/test_parser.py  | 27 ++++++++++++-------
 src/pykml/test/test_util.py    | 16 +++++------
 4 files changed, 58 insertions(+), 50 deletions(-)

diff --git a/src/pykml/test/test_factory.py b/src/pykml/test/test_factory.py
index ebd85ab..3ccf5bf 100644
--- a/src/pykml/test/test_factory.py
+++ b/src/pykml/test/test_factory.py
@@ -1,5 +1,6 @@
 import unittest
 from os import path
+import sys
 from lxml import etree
 from pykml.parser import Schema
 from pykml.parser import parse
@@ -43,14 +44,14 @@ class KmlFactoryTestCase(unittest.TestCase):
     def test_trivial_kml_document(self):
         """Tests the creation of a trivial OGC KML document."""
         doc = KML.kml()
-        schema = Schema("ogckml22.xsd")
-        self.assertTrue(schema.validate(doc))
+        # schema = Schema("ogckml22.xsd")
+        # self.assertTrue(schema.validate(doc))
         self.assertEqual(
             etree.tostring(doc).decode(),
             '<kml '
+                 'xmlns="http://www.opengis.net/kml/2.2" '
                  'xmlns:atom="http://www.w3.org/2005/Atom" '
-                 'xmlns:gx="http://www.google.com/kml/ext/2.2" '
-                 'xmlns="http://www.opengis.net/kml/2.2"/>'
+                 'xmlns:gx="http://www.google.com/kml/ext/2.2"/>'
         )
 
     def test_basic_kml_document_2(self):
@@ -67,16 +68,16 @@ class KmlFactoryTestCase(unittest.TestCase):
             )
         )
         # validate against a local schema
-        self.assertTrue(Schema("ogckml22.xsd").validate(doc))
+        # self.assertTrue(Schema("ogckml22.xsd").validate(doc))
         # validate against a remote schema
-        self.assertTrue(Schema("http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd").validate(doc))
+        # self.assertTrue(Schema("http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd").validate(doc))
 
         self.assertEqual(
             etree.tostring(doc).decode(),
             '<kml '
+                 'xmlns="http://www.opengis.net/kml/2.2" '
                  'xmlns:atom="http://www.w3.org/2005/Atom" '
-                 'xmlns:gx="http://www.google.com/kml/ext/2.2" '
-                 'xmlns="http://www.opengis.net/kml/2.2">'
+                 'xmlns:gx="http://www.google.com/kml/ext/2.2">'
               '<Document>'
                 '<name>KmlFile</name>'
                 '<Placemark>'
@@ -116,13 +117,13 @@ class KmlFactoryTestCase(unittest.TestCase):
                 )
             )
         )
-        self.assertTrue(Schema("kml22gx.xsd").validate(doc))
+        # self.assertTrue(Schema("kml22gx.xsd").validate(doc))
         self.assertEqual(
             etree.tostring(doc).decode(),
             '<kml '
+                 'xmlns="http://www.opengis.net/kml/2.2" '
                  'xmlns:atom="http://www.w3.org/2005/Atom" '
-                 'xmlns:gx="http://www.google.com/kml/ext/2.2" '
-                 'xmlns="http://www.opengis.net/kml/2.2">'
+                 'xmlns:gx="http://www.google.com/kml/ext/2.2">'
               '<gx:Tour>'
                 '<gx:Playlist>'
                   '<gx:SoundCue>'
@@ -165,13 +166,13 @@ class KmlFactoryTestCase(unittest.TestCase):
                 )
             )
         )
-        self.assertTrue(Schema("kml22gx.xsd").validate(doc))
+        # self.assertTrue(Schema("kml22gx.xsd").validate(doc))
         self.assertEqual(
             etree.tostring(doc).decode(),
             '<kml '
+                 'xmlns="http://www.opengis.net/kml/2.2" '
                  'xmlns:atom="http://www.w3.org/2005/Atom" '
-                 'xmlns:gx="http://www.google.com/kml/ext/2.2" '
-                 'xmlns="http://www.opengis.net/kml/2.2">'
+                 'xmlns:gx="http://www.google.com/kml/ext/2.2">'
               '<Document>'
                 '<atom:author>'
                   '<atom:name>J. K. Rowling</atom:name>'
@@ -198,9 +199,9 @@ class KmlFactoryTestCase(unittest.TestCase):
         self.assertEqual(
             etree.tostring(doc).decode(),
               '<description '
+                    'xmlns="http://www.opengis.net/kml/2.2" '
                     'xmlns:atom="http://www.w3.org/2005/Atom" '
-                    'xmlns:gx="http://www.google.com/kml/ext/2.2" '
-                    'xmlns="http://www.opengis.net/kml/2.2">'
+                    'xmlns:gx="http://www.google.com/kml/ext/2.2">'
                   '&lt;h1&gt;CDATA Tags are useful!&lt;/h1&gt;'
               '</description>'
         )
@@ -231,9 +232,9 @@ class KmlFactoryTestCase(unittest.TestCase):
         self.assertEqual(
             etree.tostring(doc).decode(),
             '<kml '
+                 'xmlns="http://www.opengis.net/kml/2.2" '
                  'xmlns:atom="http://www.w3.org/2005/Atom" '
-                 'xmlns:gx="http://www.google.com/kml/ext/2.2" '
-                 'xmlns="http://www.opengis.net/kml/2.2">'
+                 'xmlns:gx="http://www.google.com/kml/ext/2.2">'
               '<Document>'
                 '<Placemark>'
                   '<name>CDATA example</name>'
@@ -359,9 +360,9 @@ class GeneratePythonScriptTestCase(unittest.TestCase):
             'google_kml_tutorial/using_the_cdata_element.kml'
         )
         file = 'test/testfiles/'
-        schema = Schema('kml22gx.xsd')
+        # schema = Schema('kml22gx.xsd')
         with open(test_datafile) as f:
-            doc = parse(f, schema=schema)
+            doc = parse(f)  # , schema=schema)
         script = write_python_script_for_kml_document(doc)
         self.assertEqual(
             script,
@@ -403,14 +404,14 @@ class GeneratePythonScriptTestCase(unittest.TestCase):
         handle, temp_kml_file = tempfile.mkstemp(suffix='.kml')
         with open(temp_kml_file, 'w') as f:
             exit_code = subprocess.call(
-                    ["python",tfile],
+                    [sys.executable, tfile],
                     stdout=f,
                     env=current_env
             )
         self.assertEqual(exit_code, 0)
 
         # parse and validate the KML generated by the temporary script
-        doc2 = parse(temp_kml_file, schema=schema)
+        doc2 = parse(temp_kml_file)  # , schema=schema)
         # test that the root element is as expected
         self.assertEqual(doc2.docinfo.root_name, 'kml')
 
@@ -426,9 +427,9 @@ class GeneratePythonScriptTestCase(unittest.TestCase):
             'testfiles',
             'google_kml_developers_guide/complete_tour_example.kml'
         )
-        schema = Schema('kml22gx.xsd')
+        # schema = Schema('kml22gx.xsd')
         with open(test_datafile) as f:
-            doc = parse(f, schema=schema)
+            doc = parse(f)  # , schema=schema)
         script = write_python_script_for_kml_document(doc)
 
         # create a temporary python file
@@ -448,11 +449,11 @@ class GeneratePythonScriptTestCase(unittest.TestCase):
         handle, temp_kml_file = tempfile.mkstemp(suffix='.kml')
         #print(temp_kml_file)
         with open(temp_kml_file, 'w') as f:
-            exit_code = subprocess.call(["python",tfile], stdout=f, env=current_env)
+            exit_code = subprocess.call([sys.executable, tfile], stdout=f, env=current_env)
         self.assertEqual(exit_code, 0)
 
         # parse and validate the KML generated by the temporary script
-        doc2 = parse(temp_kml_file, schema=schema)
+        doc2 = parse(temp_kml_file)  # , schema=schema)
         # test that the root element is as expected
         self.assertEqual(doc2.docinfo.root_name, 'kml')
 
@@ -468,9 +469,9 @@ class GeneratePythonScriptTestCase(unittest.TestCase):
             'testfiles',
             'simple_file_with_comments.kml'
         )
-        schema = Schema('kml22gx.xsd')
+        # schema = Schema('kml22gx.xsd')
         with open(test_datafile) as f:
-            doc = parse(f, schema=schema)
+            doc = parse(f)  # , schema=schema)
         script = write_python_script_for_kml_document(doc)
 
         # create a temporary python file
@@ -488,11 +489,11 @@ class GeneratePythonScriptTestCase(unittest.TestCase):
         handle, temp_kml_file = tempfile.mkstemp(suffix='.kml')
         #print(temp_kml_file)  # Useful for debugging
         with open(temp_kml_file, 'w') as f:
-            exit_code = subprocess.call(["python",tfile], stdout=f, env=current_env)
+            exit_code = subprocess.call([sys.executable, tfile], stdout=f, env=current_env)
         self.assertEqual(exit_code, 0)
 
         # parse and validate the KML generated by the temporary script
-        doc2 = parse(temp_kml_file, schema=schema)
+        doc2 = parse(temp_kml_file)  # , schema=schema)
         # test that the root element is as expected
         self.assertEqual(doc2.docinfo.root_name, 'kml')
 
diff --git a/src/pykml/test/test_helpers.py b/src/pykml/test/test_helpers.py
index 195cd3b..ad93cb4 100644
--- a/src/pykml/test/test_helpers.py
+++ b/src/pykml/test/test_helpers.py
@@ -72,7 +72,7 @@ class KmlHelpersTestCase(unittest.TestCase):
                 '</Document>'
             '</kml>'
         )
-        doc = fromstring(test_kml.encode(), schema=Schema("ogckml22.xsd"))
+        doc = fromstring(test_kml.encode())  # , schema=Schema("ogckml22.xsd"))
         set_max_decimal_places(
             doc,
             max_decimals={
@@ -154,7 +154,7 @@ class KmlHelpersTestCase(unittest.TestCase):
             '</Folder>'
             '</kml>'
         )
-        doc = fromstring(test_kml.encode(), schema=Schema("kml22gx.xsd"))
+        doc = fromstring(test_kml.encode())  # , schema=Schema("kml22gx.xsd"))
         set_max_decimal_places(
             doc,
             max_decimals={
diff --git a/src/pykml/test/test_parser.py b/src/pykml/test/test_parser.py
index a36fae4..7eb3840 100644
--- a/src/pykml/test/test_parser.py
+++ b/src/pykml/test/test_parser.py
@@ -19,11 +19,13 @@ from pykml.parser import parse
 
 class ValidatorTestCase(unittest.TestCase):
 
+    @unittest.skip("access to unavailable resources")
     def test_initialize_schema(self):
         """Tests the creation Schema instance"""
         schema = Schema("ogckml22.xsd")
         self.assertTrue(isinstance(schema.schema, etree.XMLSchema))
 
+    @unittest.skip("access to remote resources")
     def test_initialize_schema_remote_url(self):
         schema = Schema("http://code.google.com/apis/kml/schema/kml22gx.xsd")
         self.assertTrue(isinstance(schema.schema, etree.XMLSchema))
@@ -35,11 +37,12 @@ class ParseKmlOgcTestCase(unittest.TestCase):
     def test_fromstring_kml_document(self):
         "Tests the parsing of an valid KML string"
         test_kml = '<kml xmlns="http://www.opengis.net/kml/2.2"/>'
-        tree = fromstring(test_kml, schema=Schema("ogckml22.xsd"))
-        self.assertEqual(etree.tostring(tree).decode(), test_kml)
+        # tree = fromstring(test_kml, schema=Schema("ogckml22.xsd"))
+        # self.assertEqual(etree.tostring(tree).decode(), test_kml)
         tree = fromstring(test_kml)
         self.assertEqual(etree.tostring(tree).decode(), test_kml)
 
+    @unittest.skip("access to unavailable resources")
     def test_fromstring_invalid_kml_document(self):
         "Tests the parsing of an invalid KML string"
         test_kml = '<bad_element />'
@@ -55,12 +58,13 @@ class ParseKmlOgcTestCase(unittest.TestCase):
         "Tests the parsing of an valid KML file object"
         test_kml = '<kml xmlns="http://www.opengis.net/kml/2.2"/>'
         fileobject = StringIO(test_kml)
-        schema = Schema("ogckml22.xsd")
-        tree = parse(fileobject, schema=schema)
+        # schema = Schema("ogckml22.xsd")
+        tree = parse(fileobject)  # , schema=schema)
         self.assertEqual(etree.tostring(tree).decode(), test_kml)
-        tree = parse(fileobject, schema=schema)
+        tree = parse(fileobject)  # , schema=schema)
         self.assertEqual(etree.tostring(tree).decode(), test_kml)
 
+    @unittest.skip("access to unavailable resources")
     def test_parse_invalid_kml_document(self):
         "Tests the parsing of an invalid KML document"
         fileobject = StringIO('<bad_element />')
@@ -80,7 +84,7 @@ class ParseKmlOgcTestCase(unittest.TestCase):
         #url = 'http://code.google.com/apis/kml/documentation/kmlfiles/animatedupdate_example.kml'
         try:
             fileobject = urllib2.urlopen(url)
-            tree = parse(fileobject, schema=Schema("ogckml22.xsd"))
+            tree = parse(fileobject)  # , schema=Schema("ogckml22.xsd"))
             self.assertEqual(
                 etree.tostring(tree)[:78].decode(),
                 '<kml xmlns="http://www.opengis.net/kml/2.2">'
@@ -99,7 +103,7 @@ class ParseKmlOgcTestCase(unittest.TestCase):
         )
         # parse with validation
         with open(test_datafile) as f:
-            doc = parse(f, schema=Schema('ogckml22.xsd'))
+            doc = parse(f)  # , schema=Schema('ogckml22.xsd'))
         self.assertEqual(
             etree.tostring(doc).decode(),
             '<kml xmlns="http://www.opengis.net/kml/2.2">'
@@ -146,6 +150,7 @@ class ParseKmlOgcTestCase(unittest.TestCase):
           '</kml>'
         )
 
+    @unittest.skip("access to unavailable resources")
     def test_parse_invalid_ogc_kml_document(self):
         """Tests the parsing of an invalid KML document.  Note that this KML
         document uses elements that are not in the OGC KML spec.
@@ -166,6 +171,7 @@ class ParseKmlOgcTestCase(unittest.TestCase):
 class ParseKmlGxTestCase(unittest.TestCase):
     "A collection of tests related to parsing KML Google Extension documents"
 
+    @unittest.skip("access to remote resources")
     def test_parse_kml_url(self):
         "Tests the parsing of a KML URL"
         url = 'http://code.google.com/apis/kml/documentation/kmlfiles/altitudemode_reference.kml'
@@ -195,12 +201,13 @@ class ParseKmlGxTestCase(unittest.TestCase):
             doc = parse(f)
         # parse with validation (local schema file)
         with open(test_datafile) as f:
-            doc = parse(f, schema=Schema('kml22gx.xsd'))
+            doc = parse(f)  # , schema=Schema('kml22gx.xsd'))
         # parse with validation (remote schema file)
-        with open(test_datafile) as f:
-            doc = parse(f, schema=Schema('http://code.google.com/apis/kml/schema/kml22gx.xsd'))
+        # with open(test_datafile) as f:
+        #     doc = parse(f, schema=Schema('http://code.google.com/apis/kml/schema/kml22gx.xsd'))
         self.assertTrue(True)
 
+    @unittest.skip("access to remote resources")
     def test_parse_kml_url_2(self):
         "Tests the parsing of a KML URL"
         url = 'http://code.google.com/apis/kml/documentation/kmlfiles/animatedupdate_example.kml'
diff --git a/src/pykml/test/test_util.py b/src/pykml/test/test_util.py
index a1cafa8..f002e45 100644
--- a/src/pykml/test/test_util.py
+++ b/src/pykml/test/test_util.py
@@ -17,7 +17,7 @@ class KmlUtilTestCase(unittest.TestCase):
             'google_kml_developers_guide/complete_tour_example.kml'
         )
         with open(test_datafile) as f:
-            doc = parse(f, schema=Schema('kml22gx.xsd'))
+            doc = parse(f)  # , schema=Schema('kml22gx.xsd'))
         summary = count_elements(doc)
 
         self.assertTrue('http://www.opengis.net/kml/2.2' in summary)
@@ -156,9 +156,9 @@ class KmlUtilTestCase(unittest.TestCase):
         self.assertEqual(
             etree.tostring(format_xml_with_cdata(kmlobj)).decode(),
             '<kml'
+              ' xmlns="http://www.opengis.net/kml/2.2"'
               ' xmlns:atom="http://www.w3.org/2005/Atom"'
-              ' xmlns:gx="http://www.google.com/kml/ext/2.2"'
-              ' xmlns="http://www.opengis.net/kml/2.2">'
+              ' xmlns:gx="http://www.google.com/kml/ext/2.2">'
               '<Document>'
                 '<Placemark>'
                   '<name>foobar</name>'
@@ -192,9 +192,9 @@ class KmlUtilTestCase(unittest.TestCase):
         self.assertEqual(
             etree.tostring(format_xml_with_cdata(kmldoc)).decode(),
             '<kml '
+                 'xmlns="http://www.opengis.net/kml/2.2" '
                  'xmlns:atom="http://www.w3.org/2005/Atom" '
-                 'xmlns:gx="http://www.google.com/kml/ext/2.2" '
-                 'xmlns="http://www.opengis.net/kml/2.2">'
+                 'xmlns:gx="http://www.google.com/kml/ext/2.2">'
                  '<Document>'
                     '<Folder>'
                         '<name>KmlFile</name>'
@@ -204,10 +204,10 @@ class KmlUtilTestCase(unittest.TestCase):
                             '<description>'
                                 '<![CDATA['
                                   '<table border="1"'
+                                    '<tr><th>name</th><td>first</td></tr>'
                                     '<tr><th>snippet</th><td>The first one</td></tr>'
                                     '<tr><th>lat</th><td>45.0</td></tr>'
                                     '<tr><th>lon</th><td>-90.0</td></tr>'
-                                    '<tr><th>name</th><td>first</td></tr>'
                                   '</table>'
                                 ']]>'
                             '</description>'
@@ -218,7 +218,7 @@ class KmlUtilTestCase(unittest.TestCase):
                         '<Placemark>'
                             '<name>second</name>'
                             '<Snippet maxLines="2">The second one</Snippet>'
-                            '<description><![CDATA[<table border="1"<tr><th>snippet</th><td>The second one</td></tr><tr><th>lat</th><td>46.0</td></tr><tr><th>lon</th><td>-89.0</td></tr><tr><th>name</th><td>second</td></tr></table>]]></description>'
+                            '<description><![CDATA[<table border="1"<tr><th>name</th><td>second</td></tr><tr><th>snippet</th><td>The second one</td></tr><tr><th>lat</th><td>46.0</td></tr><tr><th>lon</th><td>-89.0</td></tr></table>]]></description>'
                             '<Point>'
                                 '<coordinates>-89.0,46.0</coordinates>'
                             '</Point>'
@@ -226,7 +226,7 @@ class KmlUtilTestCase(unittest.TestCase):
                         '<Placemark>'
                             '<name>third</name>'
                             '<Snippet maxLines="2">The third one (with quotes)</Snippet>'
-                            '<description><![CDATA[<table border="1"<tr><th>snippet</th><td>The third one (with quotes)</td></tr><tr><th>lat</th><td>45.0</td></tr><tr><th>lon</th><td>-88.0</td></tr><tr><th>name</th><td>third</td></tr></table>]]></description>'
+                            '<description><![CDATA[<table border="1"<tr><th>name</th><td>third</td></tr><tr><th>snippet</th><td>The third one (with quotes)</td></tr><tr><th>lat</th><td>45.0</td></tr><tr><th>lon</th><td>-88.0</td></tr></table>]]></description>'
                             '<Point>'
                                 '<coordinates>-88.0,45.0</coordinates>'
                             '</Point>'
