1 |
bh |
1245 |
# Copyright (C) 2003 by Intevation GmbH |
2 |
|
|
# Authors: |
3 |
|
|
# Bernhard Herzog <[email protected]> |
4 |
|
|
# |
5 |
|
|
# This program is free software under the GPL (>=v2) |
6 |
|
|
# Read the file COPYING coming with the software for details. |
7 |
|
|
|
8 |
|
|
"""Test for the xmlsupport.py module |
9 |
|
|
""" |
10 |
|
|
|
11 |
|
|
__version__ = "$Revision$" |
12 |
|
|
# $Source$ |
13 |
|
|
# $Id$ |
14 |
|
|
|
15 |
|
|
import unittest |
16 |
|
|
|
17 |
|
|
import xmlsupport |
18 |
|
|
import support |
19 |
|
|
|
20 |
|
|
class TestValidation(unittest.TestCase, xmlsupport.ValidationTest): |
21 |
|
|
|
22 |
|
|
def test_simple(self): |
23 |
|
|
"""test xmlsupport validating valid XML |
24 |
|
|
|
25 |
|
|
The test succeeds if validate_data doesn't raise any exception |
26 |
|
|
""" |
27 |
|
|
data = ('<?xml version="1.0" encoding="UTF-8"?>\n' |
28 |
|
|
'<!DOCTYPE session SYSTEM "thuban.dtd">\n' |
29 |
|
|
'<session title="empty session">\n</session>\n') |
30 |
|
|
self.validate_data(data) |
31 |
|
|
|
32 |
|
|
def test_invalid(self): |
33 |
|
|
"""test xmlsupport validating invalid XML |
34 |
|
|
|
35 |
|
|
The test succeeds if validate_data raises an assertion error |
36 |
|
|
""" |
37 |
|
|
data = ('<?xml version="1.0" encoding="UTF-8"?>\n' |
38 |
|
|
'<!DOCTYPE session SYSTEM "thuban.dtd">\n' |
39 |
|
|
'<session foo="bar">\n</session>\n') |
40 |
|
|
# only really run this test when pyRXP is available |
41 |
|
|
if xmlsupport.pyRXP is not None: |
42 |
|
|
self.assertRaises(AssertionError, self.validate_data, data) |
43 |
|
|
|
44 |
|
|
|
45 |
bh |
1257 |
|
46 |
|
|
class TestEventList(unittest.TestCase): |
47 |
|
|
|
48 |
|
|
"""Test cases for sax_eventlist""" |
49 |
|
|
|
50 |
|
|
def test_even_list_simple(self): |
51 |
|
|
"""Test sax_eventlist on very simple XML""" |
52 |
|
|
data = "\n".join(['<?xml version="1.0" encoding="UTF-8"?>' |
53 |
|
|
'<!DOCTYPE session SYSTEM "thuban.dtd">' |
54 |
|
|
'<session title="single map&layer">' |
55 |
|
|
'</session>']) |
56 |
|
|
|
57 |
|
|
self.assertEquals(xmlsupport.sax_eventlist(data = data), |
58 |
bh |
1280 |
[('start', (None, u'session'), |
59 |
bh |
1257 |
[((None, u'title'), u'single map&layer')]), |
60 |
bh |
1280 |
('end', (None, u'session'))]) |
61 |
bh |
1257 |
|
62 |
|
|
def test_even_list_namespace(self): |
63 |
|
|
"""Test sax_eventlist on XML with a default namespace""" |
64 |
|
|
data = "\n".join(['<?xml version="1.0" encoding="UTF-8"?>' |
65 |
|
|
'<!DOCTYPE session SYSTEM "thuban.dtd">' |
66 |
|
|
'<session title="single map&layer"' |
67 |
|
|
' xmlns="http://example.com/example.dtd">' |
68 |
|
|
'</session>']) |
69 |
|
|
|
70 |
|
|
self.assertEquals(xmlsupport.sax_eventlist(data = data), |
71 |
|
|
[('start', (u'http://example.com/example.dtd', |
72 |
|
|
u'session'), |
73 |
bh |
1280 |
[((None, u'title'), u'single map&layer')]), |
74 |
bh |
1257 |
('end', (u'http://example.com/example.dtd', |
75 |
bh |
1280 |
u'session'))]) |
76 |
bh |
1257 |
|
77 |
bh |
1268 |
def test_even_list_id_normalization(self): |
78 |
bh |
1280 |
"""Test sax_eventlist id normalization""" |
79 |
bh |
1268 |
data1 = "\n".join(['<?xml version="1.0" encoding="UTF-8"?>' |
80 |
|
|
'<!DOCTYPE session SYSTEM "thuban.dtd">' |
81 |
|
|
'<session title="bla">' |
82 |
|
|
' <table id="foo"/>' |
83 |
|
|
' <tableref id="foo"/>' |
84 |
|
|
'</session>']) |
85 |
|
|
|
86 |
|
|
data2 = "\n".join(['<?xml version="1.0" encoding="UTF-8"?>' |
87 |
|
|
'<!DOCTYPE session SYSTEM "thuban.dtd">' |
88 |
|
|
'<session title="bla">' |
89 |
|
|
' <table id="bar"/>' |
90 |
|
|
' <tableref id="bar"/>' |
91 |
|
|
'</session>']) |
92 |
|
|
ids = [((None, "table"), (None, "id"))] |
93 |
|
|
idrefs = [((None, "tableref"), (None, "id"))] |
94 |
|
|
self.assertEquals(xmlsupport.sax_eventlist(data = data1, ids = ids, |
95 |
|
|
idrefs = idrefs), |
96 |
|
|
xmlsupport.sax_eventlist(data = data2, ids = ids, |
97 |
|
|
idrefs = idrefs)) |
98 |
|
|
|
99 |
bh |
1245 |
if __name__ == "__main__": |
100 |
|
|
# Fake the __file__ global because it's needed by a test |
101 |
|
|
import sys |
102 |
|
|
__file__ = sys.argv[0] |
103 |
|
|
support.run_tests() |