forked from Exirel/python-xmlunittest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
887 lines (684 loc) · 30.9 KB
/
test.py
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
# -*- conding: utf-8 -*-
from __future__ import unicode_literals
import io
import os
import unittest
from lxml import etree
from xmlunittest import XmlTestCase
class TestXmlTestCase(unittest.TestCase):
"""Test the XmlTestCase.
The issue of testing TestCase can be tough as only error case can be
tested. So it is important to know what you really want to test.
XmlTestCase is developped using TDD: tests are written before functionnal
code. For each successful case a related error case is tested too.
"""
def test_assertXmlDocument(self):
"""Asserts assertXmlDocument raises when data is invalid.
At this time, assertXmlDocument only test XML data is a valid XML
document, but it can be a fragment of XML only. This does not test
the XML declaration nor any doctype declaration.
"""
test_case = XmlTestCase(methodName='assertXmlDocument')
data = b"""<?xml version="1.0" encoding="UTF-8" ?>
<root/>"""
root = test_case.assertXmlDocument(data)
self.assertIsInstance(root, etree._Element)
with self.assertRaises(test_case.failureException):
test_case.assertXmlDocument('not an XML document')
# -------------------------------------------------------------------------
def test_assertXmlPartial(self):
"""Asserts assertXmlPartial raises when data is invalid.
Method assertXmlPartial must be able to take a partial XML formated
string and returns a valid XML document, or raise an error.
"""
test_case = XmlTestCase(methodName='assertXmlPartial')
data = b"""<partial>1</partial>
<partial>2</partial>"""
root = test_case.assertXmlPartial(data)
self.assertIsInstance(root, etree._Element)
self.assertEqual(root.tag, test_case.default_partial_tag)
self.assertEqual(len(root), 2)
with self.assertRaises(test_case.failureException):
test_case.assertXmlPartial(b'<invalidChar>&</invalidChar>')
with self.assertRaises(test_case.failureException):
test_case.assertXmlPartial(b'not even a partial XML document')
with self.assertRaises(test_case.failureException):
test_case.assertXmlPartial(b'<missingEndTag>')
def test_assertXmlPartial_name(self):
"""Asserts assertXmlPartial raises when data is invalid.
Method assertXmlPartial accept a `root_tag` parameter to tell
method the root element's tag name.
"""
test_case = XmlTestCase(methodName='assertXmlPartial')
data = b"""<partial>1</partial>
<partial>2</partial>"""
root = test_case.assertXmlPartial(data, root_tag='customTag')
self.assertIsInstance(root, etree._Element)
self.assertEqual(root.tag, 'customTag')
self.assertEqual(len(root), 2)
with self.assertRaises(test_case.failureException):
test_case.assertXmlPartial(b'<invalidChar>&</invalidChar>',
root_tag='customTag')
with self.assertRaises(test_case.failureException):
test_case.assertXmlPartial(b'not even a partial XML document',
root_tag='customTag')
with self.assertRaises(test_case.failureException):
test_case.assertXmlPartial(b'<missingEndTag>',
root_tag='customTag')
# -------------------------------------------------------------------------
def test_assertXmlNamespace(self):
"""Asserts assertXmlNamespace raises namespace is invalid.
When an element declare an xml namespace, this element and each child
reference this namespace, and thus it can be tested.
"""
test_case = XmlTestCase(methodName='assertXmlNamespace')
data = b"""<?xml version="1.0" encoding="UTF-8" ?>
<root xmlns:ns="uri"/>"""
root = test_case.assertXmlDocument(data)
test_case.assertXmlNamespace(root, 'ns', 'uri')
with self.assertRaises(test_case.failureException):
test_case.assertXmlNamespace(root, 'wrong_ns', 'uri')
with self.assertRaises(test_case.failureException):
test_case.assertXmlNamespace(root, 'ns', 'wrong_uri')
# -------------------------------------------------------------------------
def test_assertXmlHasAttribute(self):
"""Asserts assertXmlHasAttribute raises when attribute does not exist.
Method assertXmlHasAttribute can test if attribute exists or not, and
more - see other tests for that.
"""
test_case = XmlTestCase(methodName='assertXmlHasAttribute')
data = b"""<?xml version="1.0" encoding="UTF-8" ?>
<root att="value" />"""
root = test_case.assertXmlDocument(data)
test_case.assertXmlHasAttribute(root, 'att')
with self.assertRaises(test_case.failureException):
test_case.assertXmlHasAttribute(root, 'no_att')
def test_assertXmlHasAttribute_value(self):
"""Asserts assertXmlHasAttribute raises when value is invalid.
With optional argument `expected_value`, assertXmlHasAttribute can
assert if attribute's value is the given expected value.
"""
test_case = XmlTestCase(methodName='assertXmlHasAttribute')
data = b"""<?xml version="1.0" encoding="UTF-8" ?>
<root att="value" />"""
root = test_case.assertXmlDocument(data)
test_case.assertXmlHasAttribute(root, 'att', expected_value='value')
with self.assertRaises(test_case.failureException):
test_case.assertXmlHasAttribute(root, 'att',
expected_value='invalid')
def test_assertXmlHasAttribute_values(self):
"""Asserts assertXmlHasAttribute raises when value is invalid.
With optional argument `expected_values`, assertXmlHasAttribute can
assert if attribute's value is one of the given expected values.
"""
test_case = XmlTestCase(methodName='assertXmlHasAttribute')
data = b"""<?xml version="1.0" encoding="UTF-8" ?>
<root>
<child att="1"/>
<child att="3"/>
</root>"""
root = test_case.assertXmlDocument(data)
for node in root.xpath('//child'):
test_case.assertXmlHasAttribute(node, 'att',
expected_values=['1', '3'])
with self.assertRaises(test_case.failureException):
test_case.assertXmlHasAttribute(node, 'att',
expected_values=['2', '4'])
# -------------------------------------------------------------------------
def test_assertXmlNode(self):
"""Asserts assertXmlNode raises when node is invalid.
Method assertXmlNode raise if node does not exists (None) or is not
an XML Element.
"""
test_case = XmlTestCase(methodName='assertXmlNode')
data = b"""<?xml version="1.0" encoding="UTF-8" ?>
<root>text_value</root>"""
root = test_case.assertXmlDocument(data)
test_case.assertXmlNode(root)
with self.assertRaises(test_case.failureException):
test_case.assertXmlNode(None)
# Text data is not handled
with self.assertRaises(test_case.failureException):
test_case.assertXmlNode('<root>text_value</root>')
def test_assertXmlNode_tag(self):
"""Asserts assertXmlNode raises when node is invalid.
Method assertXmlNode raise if node has not the expected tag name.
"""
test_case = XmlTestCase(methodName='assertXmlNode')
data = b"""<?xml version="1.0" encoding="UTF-8" ?>
<root>text_value</root>"""
root = test_case.assertXmlDocument(data)
test_case.assertXmlNode(root, tag='root')
with self.assertRaises(test_case.failureException):
test_case.assertXmlNode(root, tag='noRoot')
def test_assertXmlNode_text(self):
"""Asserts assertXmlNode raises when node is invalid.
Method assertXmlNode raise if node has not the expected text value.
"""
test_case = XmlTestCase(methodName='assertXmlNode')
data = b"""<?xml version="1.0" encoding="UTF-8" ?>
<root>text_value</root>"""
root = test_case.assertXmlDocument(data)
test_case.assertXmlNode(root, text='text_value')
with self.assertRaises(test_case.failureException):
test_case.assertXmlNode(root, text='invalid')
def test_assertXmlNode_tag_text(self):
"""Asserts assertXmlNode raises when node is invalid.
Method assertXmlNode raises if node has not the expected tag name
or the expected text value.
"""
test_case = XmlTestCase(methodName='assertXmlNode')
data = b"""<?xml version="1.0" encoding="UTF-8" ?>
<root>text_value</root>"""
root = test_case.assertXmlDocument(data)
test_case.assertXmlNode(root, tag='root', text='text_value')
with self.assertRaises(test_case.failureException):
test_case.assertXmlNode(root, tag='root', text='invalid')
with self.assertRaises(test_case.failureException):
test_case.assertXmlNode(root, tag='noRoot', text='text_value')
with self.assertRaises(test_case.failureException):
test_case.assertXmlNode(root, tag='noRoot', text='invalid')
def test_assertXmlNode_text_in(self):
"""Asserts assertXmlNode raises when node is invalid.
Method assertXmlNode raises if node's text value is not in the list
of valid values.
"""
test_case = XmlTestCase(methodName='assertXmlNode')
data = b"""<?xml version="1.0" encoding="UTF-8" ?>
<root>valid</root>"""
root = test_case.assertXmlDocument(data)
test_case.assertXmlNode(root, text_in=['valid', 'ok'])
with self.assertRaises(test_case.failureException):
test_case.assertXmlNode(root, text_in=['invalid', 'ok'])
# -------------------------------------------------------------------------
def test_assertXpathsExist(self):
"""Asserts assertXpathsExist raises when validation failed.
Method assertXpathsExist raises when any xpath does not select a least
one result.
"""
test_case = XmlTestCase(methodName='assertXpathsExist')
data = b"""<?xml version="1.0" encoding="UTF-8" ?>
<root att="exists">
<sub subAtt="input"/>
<sub/>
</root>"""
root = test_case.assertXmlDocument(data)
xpaths = ['@att', './sub', './sub[@subAtt="input"]']
test_case.assertXpathsExist(root, xpaths)
with self.assertRaises(test_case.failureException):
test_case.assertXpathsExist(root, ['@invalidAtt'])
with self.assertRaises(test_case.failureException):
test_case.assertXpathsExist(root, ['./invalidChild'])
with self.assertRaises(test_case.failureException):
test_case.assertXpathsExist(root, ['./sub[@subAtt="invalid"]'])
# -------------------------------------------------------------------------
def test_assertXpathsOnlyOne(self):
"""Asserts assertXpathsOnlyOne raises when validation failed.
Method assertXpathsOnlyOne raises when one of XPath
expressions does not select one and exactly one result.
"""
test_case = XmlTestCase(methodName='assertXpathsOnlyOne')
data = b"""<?xml version="1.0" encoding="UTF-8" ?>
<root>
<sub subAtt="unique" id="1" />
<sub subAtt="notUnique" id="2"/>
<sub subAtt="notUnique" id="3"/>
<uniqueSub/>
</root>"""
root = test_case.assertXmlDocument(data)
unique_for_each = ['./uniqueSub',
'./sub[@subAtt="unique"]']
test_case.assertXpathsOnlyOne(root, unique_for_each)
with self.assertRaises(test_case.failureException):
test_case.assertXpathsOnlyOne(root, ['./invalidChild'])
with self.assertRaises(test_case.failureException):
test_case.assertXpathsOnlyOne(root, ['./sub[@subAtt="notUnique"]'])
def test_assertXpathsUniqueValue(self):
"""Asserts assertXpathsUniqueValue raises when validation failed.
Method assertXpathsUniqueValue raises when one of XPath expression
select does not returns unique results.
"""
test_case = XmlTestCase(methodName='assertXpathsUniqueValue')
data = b"""<?xml version="1.0" encoding="UTF-8" ?>
<root>
<sub subAtt="unique" id="1">unique 1</sub>
<sub subAtt="notUnique" id="2">unique 2</sub>
<sub subAtt="notUnique" id="3">unique 3</sub>
<multiple>twice</multiple>
<multiple>twice</multiple>
</root>"""
root = test_case.assertXmlDocument(data)
test_case.assertXpathsUniqueValue(root, ['./sub/@id',
'./sub/text()'])
with self.assertRaises(test_case.failureException):
test_case.assertXpathsUniqueValue(root, ['./sub/@subAtt'])
with self.assertRaises(test_case.failureException):
test_case.assertXpathsUniqueValue(root, ['./multiple/text()'])
def test_assertXpathValues(self):
"""Asserts assertXpathValues raises when validation failed.
Method assertXpathValues raises when not each XPath expression's result
is in the expected values.
"""
test_case = XmlTestCase(methodName='assertXpathValues')
data = b"""<?xml version="1.0" encoding="UTF-8" ?>
<root>
<sub id="1">a</sub>
<sub id="2">a</sub>
<sub id="3">b</sub>
<sub id="4">c</sub>
</root>"""
root = test_case.assertXmlDocument(data)
test_case.assertXpathValues(root, './sub/@id', ['1', '2', '3', '4'])
test_case.assertXpathValues(root, './sub/text()', ['a', 'b', 'c'])
with self.assertRaises(test_case.failureException):
test_case.assertXpathValues(root, './sub/@id', ['1', '2'])
with self.assertRaises(test_case.failureException):
test_case.assertXpathValues(root, './sub/text()', ['a', 'b'])
# -------------------------------------------------------------------------
def test_assertXmlValidDTD(self):
"""Asserts assertXmlValidDTD raises when DTD does not valid XML."""
test_case = XmlTestCase(methodName='assertXmlValidDTD')
dtd = """<!ELEMENT root (child)>
<!ELEMENT child EMPTY>
<!ATTLIST child id ID #REQUIRED>
"""
data = b"""<?xml version="1.0" encoding="utf-8"?>
<root>
<child id="child1"/>
</root>
"""
root = test_case.assertXmlDocument(data)
# Document is valid according to DTD
test_case.assertXmlValidDTD(root, dtd)
data_invalid = b"""<?xml version="1.0" encoding="utf-8"?>
<root>
<child id="child1"/>
<child id="child1"/>
</root>
"""
root = test_case.assertXmlDocument(data_invalid)
# Document is invalid according to DTD (multiple child element)
with self.assertRaises(test_case.failureException):
test_case.assertXmlValidDTD(root, dtd)
def test_assertXmlValidDTD_filename(self):
"""Asserts assertXmlValidDTD accepts a filename as DTD."""
test_case = XmlTestCase(methodName='assertXmlValidDTD')
filename = 'test_assertXmlValidDTD_filename.dtd'
dtd = """<!ELEMENT root (child)>
<!ELEMENT child EMPTY>
<!ATTLIST child id ID #REQUIRED>
"""
with open(filename, 'w') as dtd_file:
dtd_file.write(dtd)
data = b"""<?xml version="1.0" encoding="utf-8"?>
<root>
<child id="child1"/>
</root>
"""
root = test_case.assertXmlDocument(data)
# Document is valid according to DTD
try:
test_case.assertXmlValidDTD(root, filename=filename)
except:
os.unlink(filename)
raise
data_invalid = b"""<?xml version="1.0" encoding="utf-8"?>
<root>
<child id="child1"/>
<child id="child1"/>
</root>
"""
root = test_case.assertXmlDocument(data_invalid)
try:
# Document is invalid according to DTD (multiple child element)
with self.assertRaises(test_case.failureException):
test_case.assertXmlValidDTD(root, filename=filename)
finally:
os.unlink(filename)
def test_assertXmlValidDTD_DTD(self):
"""Asserts assertXmlValidDTD accepts an LXML DTD object."""
test_case = XmlTestCase(methodName='assertXmlValidDTD')
dtd = """<!ELEMENT root (child)>
<!ELEMENT child EMPTY>
<!ATTLIST child id ID #REQUIRED>
"""
schema = etree.DTD(io.StringIO(dtd))
data = b"""<?xml version="1.0" encoding="utf-8"?>
<root>
<child id="child1"/>
</root>
"""
root = test_case.assertXmlDocument(data)
# Document is valid according to DTD
test_case.assertXmlValidDTD(root, schema)
data_invalid = b"""<?xml version="1.0" encoding="utf-8"?>
<root>
<child id="child1"/>
<child id="child1"/>
</root>
"""
root = test_case.assertXmlDocument(data_invalid)
# Document is invalid according to DTD (multiple child element)
with self.assertRaises(test_case.failureException):
test_case.assertXmlValidDTD(root, schema)
def test_assertXmlValidDTD_no_dtd(self):
"""Asserts assertXmlValidDTD raises ValueError without any DTD."""
test_case = XmlTestCase(methodName='assertXmlValidDTD')
data = b"""<?xml version="1.0" encoding="utf-8"?>
<root>
<child id="child1"/>
</root>
"""
root = test_case.assertXmlDocument(data)
# No DTD: ValueError
with self.assertRaises(ValueError):
test_case.assertXmlValidDTD(root)
# -------------------------------------------------------------------------
def test_assertXmlValidXSchema(self):
"""Asserts assertXmlValidXSchema raises when schema does not valid XML.
"""
test_case = XmlTestCase(methodName='assertXmlValidXSchema')
xschema = b"""<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="child" minOccurs="1" maxOccurs="1">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="id" type="xsd:string" use="required" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
"""
data = b"""<?xml version="1.0" encoding="utf-8"?>
<root>
<child id="valid"/>
</root>
"""
root = test_case.assertXmlDocument(data)
test_case.assertXmlValidXSchema(root, xschema)
data_invalid = b"""<?xml version="1.0" encoding="utf-8"?>
<root>
<child id="valid"/>
<child id="tooManyChild"/>
</root>
"""
root = test_case.assertXmlDocument(data_invalid)
with self.assertRaises(test_case.failureException):
test_case.assertXmlValidXSchema(root, xschema)
def test_assertXmlValidXSchema_filename(self):
"""Asserts assertXmlValidXSchema raises when schema does not valid XML.
"""
test_case = XmlTestCase(methodName='assertXmlValidXSchema')
xschema = """<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="child" minOccurs="1" maxOccurs="1">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="id" type="xsd:string" use="required" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
"""
filename = 'test_assertXmlValidXSchema_filename.xml'
with open(filename, 'w') as xchema_file:
xchema_file.write(xschema)
data = b"""<?xml version="1.0" encoding="utf-8"?>
<root>
<child id="valid"/>
</root>
"""
root = test_case.assertXmlDocument(data)
try:
test_case.assertXmlValidXSchema(root, filename=filename)
except:
os.unlink(filename)
raise
data_invalid = b"""<?xml version="1.0" encoding="utf-8"?>
<root>
<child id="valid"/>
<child id="tooManyChild"/>
</root>
"""
root = test_case.assertXmlDocument(data_invalid)
try:
with self.assertRaises(test_case.failureException):
test_case.assertXmlValidXSchema(root, filename=filename)
finally:
os.unlink(filename)
def test_assertXmlValidXSchema_xschema(self):
"""Asserts assertXmlValidXSchema raises when schema does not valid XML.
"""
test_case = XmlTestCase(methodName='assertXmlValidXSchema')
xschema = b"""<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="child" minOccurs="1" maxOccurs="1">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="id" type="xsd:string" use="required" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
"""
xml_schema = etree.XMLSchema(etree.XML(xschema))
data = b"""<?xml version="1.0" encoding="utf-8"?>
<root>
<child id="valid"/>
</root>
"""
root = test_case.assertXmlDocument(data)
test_case.assertXmlValidXSchema(root, xml_schema)
data_invalid = b"""<?xml version="1.0" encoding="utf-8"?>
<root>
<child id="valid"/>
<child id="tooManyChild"/>
</root>
"""
root = test_case.assertXmlDocument(data_invalid)
with self.assertRaises(test_case.failureException):
test_case.assertXmlValidXSchema(root, xml_schema)
def test_assertXmlValidXSchema_no_xchema(self):
"""Asserts assertXmlValidXSchema raises ValueError without any schema.
"""
test_case = XmlTestCase(methodName='assertXmlValidXSchema')
data = b"""<?xml version="1.0" encoding="utf-8"?>
<root>
<child id="child1"/>
</root>
"""
root = test_case.assertXmlDocument(data)
# No DTD: ValueError
with self.assertRaises(ValueError):
test_case.assertXmlValidXSchema(root)
# -------------------------------------------------------------------------
def test_assertXmlValidRelaxNG(self):
"""Asserts assertXmlValidRelaxNG raises when schema does not valid XML.
"""
test_case = XmlTestCase(methodName='assertXmlValidRelaxNG')
relaxng = b"""<?xml version="1.0" encoding="utf-8"?>
<rng:element name="root" xmlns:rng="http://relaxng.org/ns/structure/1.0">
<rng:element name="child">
<rng:attribute name="id">
<rng:text />
</rng:attribute>
</rng:element>
</rng:element>
"""
data = b"""<?xml version="1.0" encoding="utf-8"?>
<root>
<child id="valid"/>
</root>
"""
root = test_case.assertXmlDocument(data)
test_case.assertXmlValidRelaxNG(root, relaxng)
data_invalid = b"""<?xml version="1.0" encoding="utf-8"?>
<root>
<child id="valid"/>
<child id="tooManyChild"/>
</root>
"""
root = test_case.assertXmlDocument(data_invalid)
with self.assertRaises(test_case.failureException):
test_case.assertXmlValidRelaxNG(root, relaxng)
def test_assertXmlValidRelaxNG_filename(self):
"""Asserts assertXmlValidRelaxNG raises when schema does not valid XML.
"""
test_case = XmlTestCase(methodName='assertXmlValidRelaxNG')
relaxng = """<?xml version="1.0" encoding="utf-8"?>
<rng:element name="root" xmlns:rng="http://relaxng.org/ns/structure/1.0">
<rng:element name="child">
<rng:attribute name="id">
<rng:text />
</rng:attribute>
</rng:element>
</rng:element>
"""
filename = 'test_assertXmlValidRelaxNG_filename.xml'
with open(filename, 'w') as relaxng_file:
relaxng_file.write(relaxng)
data = b"""<?xml version="1.0" encoding="utf-8"?>
<root>
<child id="valid"/>
</root>
"""
root = test_case.assertXmlDocument(data)
try:
test_case.assertXmlValidRelaxNG(root, filename=filename)
except:
os.unlink(filename)
raise
data_invalid = b"""<?xml version="1.0" encoding="utf-8"?>
<root>
<child id="valid"/>
<child id="tooManyChild"/>
</root>
"""
root = test_case.assertXmlDocument(data_invalid)
try:
with self.assertRaises(test_case.failureException):
test_case.assertXmlValidRelaxNG(root, filename=filename)
finally:
os.unlink(filename)
def test_assertXmlValidRelaxNG_relaxng(self):
"""Asserts assertXmlValidRelaxNG raises when schema does not valid XML.
"""
test_case = XmlTestCase(methodName='assertXmlValidRelaxNG')
relaxng = b"""<?xml version="1.0" encoding="utf-8"?>
<rng:element name="root" xmlns:rng="http://relaxng.org/ns/structure/1.0">
<rng:element name="child">
<rng:attribute name="id">
<rng:text />
</rng:attribute>
</rng:element>
</rng:element>
"""
xml_relaxng = etree.RelaxNG(etree.XML(relaxng))
data = b"""<?xml version="1.0" encoding="utf-8"?>
<root>
<child id="valid"/>
</root>
"""
root = test_case.assertXmlDocument(data)
test_case.assertXmlValidRelaxNG(root, xml_relaxng)
data_invalid = b"""<?xml version="1.0" encoding="utf-8"?>
<root>
<child id="valid"/>
<child id="tooManyChild"/>
</root>
"""
root = test_case.assertXmlDocument(data_invalid)
with self.assertRaises(test_case.failureException):
test_case.assertXmlValidRelaxNG(root, xml_relaxng)
def test_assertXmlValidRelaxNG_no_relaxng(self):
"""Asserts assertXmlValidRelaxNG raises ValueError without any RelaxNG.
"""
test_case = XmlTestCase(methodName='assertXmlValidRelaxNG')
data = b"""<?xml version="1.0" encoding="utf-8"?>
<root>
<child id="child1"/>
</root>
"""
root = test_case.assertXmlDocument(data)
# No DTD: ValueError
with self.assertRaises(ValueError):
test_case.assertXmlValidRelaxNG(root)
# -------------------------------------------------------------------------
def test_assertXmlEquivalentOutputs(self):
"""Asserts assertXmlEquivalentOutputs raises when comparison failed.
Basic assertion: same document, with different order of attributes,
text with useless spaces, etc.
"""
test_case = XmlTestCase(methodName='assertXmlEquivalentOutputs')
# Same XML (with different spacings placements and attrs order)
data = b"""<?xml version="1.0" encoding="UTF-8" ?>
<root>
<tag foo="bar" bar="foo">foo</tag>
</root>"""
expected = b"""<?xml version="1.0" encoding="UTF-8" ?>
<root><tag bar="foo" foo="bar"> foo </tag></root>"""
test_case.assertXmlEquivalentOutputs(data, expected)
# Not the right element given
wrong_element = b"""<?xml version="1.0" encoding="UTF-8" ?>
<root>
<notTag foo="bar" bar="foo">foo</notTag>
</root>"""
with self.assertRaises(test_case.failureException):
test_case.assertXmlEquivalentOutputs(wrong_element, expected)
# Too many tag elements
data = b"""<?xml version="1.0" encoding="UTF-8" ?>
<root>
<tag foo="bar" bar="foo">foo</tag>
<tag foo="bar" bar="foo">foo</tag>
</root>"""
with self.assertRaises(test_case.failureException):
test_case.assertXmlEquivalentOutputs(wrong_element, expected)
def test_assertXmlEquivalentOutputs_namespaces(self):
"""Asserts assertXmlEquivalentOutputs raises when comparison failed.
Assertion with different namespaces: the namespace URI is the same,
but the prefix is different. In this case, the two XML are equivalents.
"""
test_case = XmlTestCase(methodName='assertXmlEquivalentOutputs')
# Same XML, but with different namespace prefixes
data = b"""<?xml version="1.0" encoding="UTF-8" ?>
<root xmlns:foo="mynamespace">
<foo:tag>foo</foo:tag>
</root>"""
expected = b"""<?xml version="1.0" encoding="UTF-8" ?>
<root xmlns:bar="mynamespace">
<bar:tag>foo</bar:tag>
</root>"""
test_case.assertXmlEquivalentOutputs(data, expected)
wrong_namespace = b"""<?xml version="1.0" encoding="UTF-8" ?>
<root xmlns:foo="not_the_same_namespace">
<foo:tag>foo</foo:tag>
</root>
"""
with self.assertRaises(test_case.failureException):
test_case.assertXmlEquivalentOutputs(wrong_namespace, expected)
if __name__ == "__main__":
unittest.main()