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
|
#!/usr/bin/env python3
# Copyright 2024 The Chromium Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import unittest
import dwa_model
PRETTY_XML = """
<!-- Comment1 -->
<dwa-configuration>
<event name="Event1">
<owner>owner@chromium.org</owner>
<owner>anotherowner@chromium.org</owner>
<summary>
Event1 summary.
</summary>
<study name="Study1"/>
<study name="Study2"/>
<metric name="Metric1">
<summary>
Metric1 summary.
</summary>
</metric>
<metric name="Metric2"/>
<metric name="Metric3"/>
</event>
</dwa-configuration>
""".strip()
UNPRETTIFIED_XML = """
<!-- Comment1 -->
<dwa-configuration>
<event name="Event1">
<metric name="Metric3"/>
<metric name="Metric1">
<summary>
Metric1 summary.
</summary>
</metric>
<metric name="Metric2">
</metric>
<summary>Event1 summary.</summary>
<owner>owner@chromium.org</owner>
<owner>anotherowner@chromium.org</owner>
<study name="Study2"/>
<study name="Study1"/>
</event>
</dwa-configuration>
""".strip()
CONFIG_EVENT_NAMES_SORTED = """
<dwa-configuration>
<event name="Event1"/>
<event name="Event2"/>
<event name="Event3"/>
</dwa-configuration>
""".strip()
CONFIG_EVENT_NAMES_UNSORTED = """
<dwa-configuration>
<event name="Event2"/>
<event name="Event3"/>
<event name="Event1"/>
</dwa-configuration>
""".strip()
class DwaXmlTest(unittest.TestCase):
def __init__(self, *args, **kwargs) -> None:
super(DwaXmlTest, self).__init__(*args, **kwargs)
self.maxDiff = None
def testPrettify(self) -> None:
result = dwa_model.PrettifyXML(PRETTY_XML)
self.assertMultiLineEqual(PRETTY_XML, result.strip())
result = dwa_model.PrettifyXML(UNPRETTIFIED_XML)
self.assertMultiLineEqual(PRETTY_XML, result.strip())
def testHasBadEventName(self) -> None:
# Name containing illegal character.
bad_xml = PRETTY_XML.replace('Event1', 'Event:1')
with self.assertRaises(ValueError) as context:
dwa_model.PrettifyXML(bad_xml)
self.assertIn('Event:1', str(context.exception))
self.assertIn('does not match regex', str(context.exception))
# Name starting with a digit.
bad_event_name_xml = PRETTY_XML.replace('Event1', '1Event')
with self.assertRaises(ValueError) as context:
dwa_model.PrettifyXML(bad_event_name_xml)
self.assertIn('1Event', str(context.exception))
self.assertIn('does not match regex', str(context.exception))
def testHasBadMetricName(self) -> None:
# Name containing illegal character.
bad_xml = PRETTY_XML.replace('Metric1', 'Metric:1')
with self.assertRaises(ValueError) as context:
dwa_model.PrettifyXML(bad_xml)
self.assertIn('Metric:1', str(context.exception))
self.assertIn('does not match regex', str(context.exception))
# Name starting with a digit.
bad_metric_name_xml = PRETTY_XML.replace('Metric3', '3rdPartyCookie')
with self.assertRaises(ValueError) as context:
dwa_model.PrettifyXML(bad_metric_name_xml)
self.assertIn('3rdPartyCookie', str(context.exception))
self.assertIn('does not match regex', str(context.exception))
def testHasBadStudyName(self) -> None:
# Name containing illegal character.
bad_xml = PRETTY_XML.replace('Study1', 'Study:1')
with self.assertRaises(ValueError) as context:
dwa_model.PrettifyXML(bad_xml)
self.assertIn('Study:1', str(context.exception))
self.assertIn('does not match regex', str(context.exception))
# Name starting with a digit.
bad_study_name_xml = PRETTY_XML.replace('Study2', '3rdPartyCookie')
with self.assertRaises(ValueError) as context:
dwa_model.PrettifyXML(bad_study_name_xml)
self.assertIn('3rdPartyCookie', str(context.exception))
self.assertIn('does not match regex', str(context.exception))
def testSortByEventName(self) -> None:
result = dwa_model.PrettifyXML(CONFIG_EVENT_NAMES_SORTED)
self.assertMultiLineEqual(CONFIG_EVENT_NAMES_SORTED, result.strip())
result = dwa_model.PrettifyXML(CONFIG_EVENT_NAMES_UNSORTED)
self.assertMultiLineEqual(CONFIG_EVENT_NAMES_SORTED, result.strip())
if __name__ == '__main__':
unittest.main()
|