File: test_xml.py

package info (click to toggle)
jc 1.25.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 101,104 kB
  • sloc: python: 70,400; sh: 724; xml: 278; makefile: 5
file content (102 lines) | stat: -rw-r--r-- 4,104 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import os
import unittest
import json
import jc.parsers.xml

# fix for whether tests are run directly or via runtests.sh
try:
    from ._vendor.packaging import version
except:
    from _vendor.packaging import version  # type: ignore

# check the version of installed xmltodict library
try:
    import xmltodict
    XMLTODICT_INSTALLED = True
    XMLTODICT_0_13_0_OR_HIGHER = version.parse(xmltodict.__version__) >= version.parse('0.13.0')  # type: ignore
except:
    XMLTODICT_INSTALLED = False


THIS_DIR = os.path.dirname(os.path.abspath(__file__))

@unittest.skipIf(not XMLTODICT_INSTALLED, 'xmltodict library not installed')
class MyTests(unittest.TestCase):

    # input
    with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/xml-cd_catalog.xml'), 'r', encoding='utf-8') as f:
        generic_xml_cd_catalog = f.read()

    with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/xml-foodmenu.xml'), 'r', encoding='utf-8') as f:
        generic_xml_foodmenu = f.read()

    with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/xml-nmap.xml'), 'r', encoding='utf-8') as f:
        generic_xml_nmap = f.read()

    # output
    with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/xml-cd_catalog.json'), 'r', encoding='utf-8') as f:
        generic_xml_cd_catalog_json = json.loads(f.read())

    with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/xml-foodmenu.json'), 'r', encoding='utf-8') as f:
        generic_xml_foodmenu_json = json.loads(f.read())

    with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/xml-nmap.json'), 'r', encoding='utf-8') as f:
        generic_xml_nmap_json = json.loads(f.read())

    with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/xml-nmap-nocomment.json'), 'r', encoding='utf-8') as f:
        generic_xml_nmap_nocomment_json = json.loads(f.read())

    with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/xml-nmap-raw.json'), 'r', encoding='utf-8') as f:
        generic_xml_nmap_r_json = json.loads(f.read())

    with open(os.path.join(THIS_DIR, os.pardir, 'tests/fixtures/generic/xml-nmap-raw-nocomment.json'), 'r', encoding='utf-8') as f:
        generic_xml_nmap_r_nocomment_json = json.loads(f.read())


    def test_xml_nodata(self):
        """
        Test xml parser with no data
        """
        self.assertEqual(jc.parsers.xml.parse('', quiet=True), [])

    def test_xml_nodata_r(self):
        """
        Test xml parser with no data and raw output
        """
        self.assertEqual(jc.parsers.xml.parse('', raw=True, quiet=True), [])

    def test_xml_cd_catalog(self):
        """
        Test the cd catalog xml file
        """
        self.assertEqual(jc.parsers.xml.parse(self.generic_xml_cd_catalog, quiet=True), self.generic_xml_cd_catalog_json)

    def test_xml_foodmenu(self):
        """
        Test the food menu xml file
        """
        self.assertEqual(jc.parsers.xml.parse(self.generic_xml_foodmenu, quiet=True), self.generic_xml_foodmenu_json)

    def test_xml_nmap(self):
        """
        Test nmap xml output
        """
        if XMLTODICT_0_13_0_OR_HIGHER:
            self.assertEqual(jc.parsers.xml.parse(self.generic_xml_nmap, quiet=True), self.generic_xml_nmap_json)
        else:
            print('\n### Using older version of xmltodict library. Testing without comment support. ### ... ')
            self.assertEqual(jc.parsers.xml.parse(self.generic_xml_nmap, quiet=True), self.generic_xml_nmap_nocomment_json)

    def test_xml_nmap_r(self):
        """
        Test nmap xml raw output
        """
        if XMLTODICT_0_13_0_OR_HIGHER:
            self.assertEqual(jc.parsers.xml.parse(self.generic_xml_nmap, raw=True, quiet=True), self.generic_xml_nmap_r_json)
        else:
            print('\n### Using older version of xmltodict library. Testing without comment support. ### ... ')
            self.assertEqual(jc.parsers.xml.parse(self.generic_xml_nmap, raw=True, quiet=True), self.generic_xml_nmap_r_nocomment_json)


if __name__ == '__main__':
    unittest.main()