Ejemplo n.º 1
0
def test_xml_compare():
    t1 = XML('<test />')
    t2 = XML('<test/>')
    assert dxml.xml_compare(t1, t2, sys.stdout.write)
    assert dxml.xml_compare(XML('''<hey>
    <you>!!</you>  </hey>'''), XML('<hey><you>!!</you></hey>'),
                            sys.stdout.write)
def test_xml_compare():
    t1 = XML('<test />')
    t2 = XML('<test/>')
    assert dxml.xml_compare(t1, t2, sys.stdout.write)
    assert dxml.xml_compare(XML('''<hey>
    <you>!!</you>  </hey>'''), XML('<hey><you>!!</you></hey>'),
                            sys.stdout.write)
Ejemplo n.º 3
0
 def assertXMLEqual(self, first, second):
     first = fromstring(first)
     second = fromstring(second)
     try:
         self.assertTrue(xml_compare(first, second))
     except AssertionError:
         print(tostring(first))
         print(tostring(second))
         xml_compare(first, second, print)
         raise
Ejemplo n.º 4
0
 def assertXMLEqual(self, first, second):
     from lxml.etree import fromstring, tostring
     from formencode.doctest_xml_compare import xml_compare
     # __import__('ipdb').set_trace()
     first = fromstring(first)
     second = fromstring(second)
     try:
         self.assertTrue(xml_compare(first, second))
     except AssertionError:
         print(tostring(first))
         print(tostring(second))
         xml_compare(first, second, print)
         raise
Ejemplo n.º 5
0
    def test_env_private(self):
        env = Environment(private=False)
        assert_true(xml_compare(etree.fromstring("""
<environment xmlns="http://www.eeml.org/xsd/0.5.1">
  <private>false</private>
</environment>"""), env.toeeml(), reporter=self.fail))

        env = Environment(private=True)
        assert_true(xml_compare(etree.fromstring("""
<environment xmlns="http://www.eeml.org/xsd/0.5.1">
  <private>true</private>
</environment>"""), env.toeeml(), reporter=self.fail))

        with self.assertRaises(ValueError):
            Environment(private='foobar')
Ejemplo n.º 6
0
def html_string_compare(astr, bstr):
    """
    compare to strings containing html based on html
    equivalence. Raises ValueError if the strings are
    not equivalent.
    """
    def reporter(x):
        print x
    a = None
    b = None
    try:
        a = etree.HTML(astr)
    except:
        print a
        raise
    try:
        b = etree.HTML(bstr)
    except:
        print b
        raise
    reporter = []
    result = xml_compare(a, b, reporter.append)
    if not result:
        raise ValueError("Comparison failed between actual:\n==================\n%s\n\nexpected:\n==================\n%s\n\nReport:\n%s"
            % (astr, bstr, '\n'.join(reporter)))
Ejemplo n.º 7
0
    def runTest(self):
        files_to_test = ["instance_xmlns_test.xls"]
        for file_to_test in files_to_test:
            path_to_excel_file = utils.path_to_text_fixture(file_to_test)

            #Get the xform output path:
            directory, filename = os.path.split(path_to_excel_file)
            root_filename, ext = os.path.splitext(filename)
            path_to_output_xform = os.path.join(directory,
                                                root_filename + "_output.xml")
            path_to_expected_xform = os.path.join(directory,
                                                  root_filename + ".xml")

            #Do the conversion:
            json_survey = xls2json.parse_file_to_json(path_to_excel_file)
            survey = pyxform.create_survey_element_from_dict(json_survey)
            survey.print_xform_to_file(path_to_output_xform)

            #Compare with the expected output:
            with codecs.open(path_to_expected_xform, 'rb',
                             encoding="utf-8") as expected_file:
                expected = etree.fromstring(expected_file.read())
                result = etree.fromstring(survey.to_xml())
                reporter = lambda x: sys.stdout.write(x + "\n")
                self.assertTrue(
                    xml_compare(expected, result, reporter=reporter))
            os.remove(path_to_output_xform)
Ejemplo n.º 8
0
    def runTest(self):
        files_to_test = ["instance_xmlns_test.xls"]
        for file_to_test in files_to_test:
            path_to_excel_file = utils.path_to_text_fixture(file_to_test)
            
            # Get the xform output path:
            directory, filename = os.path.split(path_to_excel_file)
            root_filename, ext = os.path.splitext(filename)
            path_to_output_xform = os.path.join(
                directory, root_filename + "_output.xml")
            path_to_expected_xform = os.path.join(
                directory, root_filename + ".xml")

            # Do the conversion:
            json_survey = xls2json.parse_file_to_json(path_to_excel_file)
            survey = pyxform.create_survey_element_from_dict(json_survey)
            survey.print_xform_to_file(path_to_output_xform)
            
            # Compare with the expected output:
            with codecs.open(path_to_expected_xform, 'rb', encoding="utf-8"
                             ) as expected_file:
                expected = ETree.fromstring(expected_file.read())
                result = ETree.fromstring(survey.to_xml())

                def write_line(x): sys.stdout.write(x + "\n")
                reporter = write_line
                self.assertTrue(xml_compare(
                    expected, result, reporter=reporter))
            os.remove(path_to_output_xform)
Ejemplo n.º 9
0
    def test_to_xml_string(self):
        """Test exporting in XML string format."""
        # read and parse sample file
        self.build = BuildJob(constants.TEST_SAMPLE_TIMESTAMP_FILE)

        # test xml string output
        self.assertEqual(
            b'<build>\n'
            b'  <stages>\n'
            b'    <stage duration="2.0" name="stage1"/>\n'
            b'    <stage duration="5.0" name="stage2"/>\n'
            b'    <stage duration="10.0" name="stage3"/>\n'
            b'  </stages>\n'
            b'</build>\n', self.build.to_xml_string())

        # add properties
        self.build.add_property('property1', 2)
        self.build.add_property('property2', 3)
        # test xml string output
        expected_xml = etree.fromstring(
            b'<build property1="2" property2="3">\n'
            b'  <stages>\n'
            b'    <stage duration="2.0" name="stage1"/>\n'
            b'    <stage duration="5.0" name="stage2"/>\n'
            b'    <stage duration="10.0" name="stage3"/>\n'
            b'  </stages>\n'
            b'</build>\n')
        self.assertTrue(
            xml_compare(expected_xml,
                        etree.fromstring(self.build.to_xml_string())))
Ejemplo n.º 10
0
    def test_multiple_update(self):
        pac = Cosm(1, 'ASDF')
        pac.update(
            [Data(1, 10),
             Data(2, 22, unit=RH()),
             Data(3, 44),
             Data(5, 65)])

        pac.update([Data(2, 476), Data(5, -1)])

        assert_true(
            xml_compare(etree.fromstring("""
            <eeml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.eeml.org/xsd/0.5.1" xsi:schemaLocation="http://www.eeml.org/xsd/0.5.1 http://www.eeml.org/xsd/0.5.1/0.5.1.xsd" version="0.5.1">
              <environment>
                <data id="1">
                  <current_value>10</current_value>
                </data>
                <data id="2">
                  <current_value>476</current_value>
                </data>
                <data id="3">
                  <current_value>44</current_value>
                </data>
                <data id="5">
                  <current_value>-1</current_value>
                </data>
              </environment>
            </eeml>"""),
                        etree.fromstring(pac.geteeml()),
                        reporter=self.fail))
Ejemplo n.º 11
0
    def test_multiple_update(self):
        pac = Cosm(1, 'ASDF')
        pac.update([
                Data(1, 10),
                Data(2, 22, unit=RH()),
                Data(3, 44),
                Data(5, 65)])

        pac.update([
                Data(2, 476),
                Data(5, -1)])

        assert_true(xml_compare(etree.fromstring("""
            <eeml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.eeml.org/xsd/0.5.1" xsi:schemaLocation="http://www.eeml.org/xsd/0.5.1 http://www.eeml.org/xsd/0.5.1/0.5.1.xsd" version="0.5.1">
              <environment>
                <data id="1">
                  <current_value>10</current_value>
                </data>
                <data id="2">
                  <current_value>476</current_value>
                </data>
                <data id="3">
                  <current_value>44</current_value>
                </data>
                <data id="5">
                  <current_value>-1</current_value>
                </data>
              </environment>
            </eeml>"""),
                                etree.fromstring(pac.geteeml()), reporter=self.fail))
Ejemplo n.º 12
0
    def test_good_environment(self):
        env = Environment('A Room Somewhere',
                          'http://www.cosm.com/feeds/1.xml',
                          'frozen',
                          'This is a room somewhere',
                          'http://www.roomsomewhere/icon.png',
                          'http://www.roomsomewhere/',
                          'myemail@roomsomewhere',
                          updated='2007-05-04T18:13:51.0Z',
                          creator='http://www.somewhere',
                          id_=1)

        assert_true(
            xml_compare(etree.fromstring("""
            <environment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.eeml.org/xsd/0.5.1" creator="http://www.somewhere" id="1" updated="2007-05-04T18:13:51.0Z">
                <title>A Room Somewhere</title>
                <feed>http://www.cosm.com/feeds/1.xml</feed>
                <status>frozen</status>
                <description>This is a room somewhere</description>
                <icon>http://www.roomsomewhere/icon.png</icon>
                <website>http://www.roomsomewhere/</website>
                <email>myemail@roomsomewhere</email>
            </environment>"""),
                        env.toeeml(),
                        reporter=self.fail))
Ejemplo n.º 13
0
    def test_to_xml_string(self):
        """Test exporting in XML string format."""
        # read and parse sample file
        self.build = BuildJob(constants.TEST_SAMPLE_TIMESTAMP_FILE)

        # test xml string output
        self.assertEqual(
            b'<build>\n'
            b'  <stages>\n'
            b'    <stage duration="2.0" name="stage1"/>\n'
            b'    <stage duration="5.0" name="stage2"/>\n'
            b'    <stage duration="10.0" name="stage3"/>\n'
            b'  </stages>\n'
            b'</build>\n',
            self.build.to_xml_string())

        # add properties
        self.build.add_property('property1', 2)
        self.build.add_property('property2', 3)
        # test xml string output
        expected_xml = etree.fromstring(
            b'<build property1="2" property2="3">\n'
            b'  <stages>\n'
            b'    <stage duration="2.0" name="stage1"/>\n'
            b'    <stage duration="5.0" name="stage2"/>\n'
            b'    <stage duration="10.0" name="stage3"/>\n'
            b'  </stages>\n'
            b'</build>\n'
        )
        self.assertTrue(
            xml_compare(
                expected_xml, etree.fromstring(self.build.to_xml_string())
            )
        )
Ejemplo n.º 14
0
    def test_good_unit(self):
        unit = Unit("Celzius", 'basicSI', "C")

        assert_true(xml_compare(etree.fromstring(
            """
            <unit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.eeml.org/xsd/0.5.1" type="basicSI" symbol="C">Celzius</unit>
            """.strip()), unit.toeeml(), reporter=self.fail))
Ejemplo n.º 15
0
def _cmpXml(got, ref):
    print "- " * 5 + "GOT:" + " -" * 20
    print got
    print "- " * 28
    tree1 = etree.fromstring(str(got))
    tree2 = etree.fromstring(str(ref))
    assert xml_compare(tree1, tree2, lambda x: sys.stdout.write(x + "\n"))
Ejemplo n.º 16
0
 def test_new_widgets(self):
     survey = utils.build_survey('widgets.xls')
     path = utils.path_to_text_fixture('widgets.xml')
     survey.to_xml
     with open(path) as f:
         expected = etree.fromstring(survey.to_xml())
         result = etree.fromstring(f.read())
         self.assertTrue(xml_compare(expected, result))
Ejemplo n.º 17
0
 def test_parse(self):
     for xml0 in [GET]:
         o = ofc.OFCapableSwitchType.from_xml(xml0)
         xml1 = o.to_xml('capable-switch')
         ok_(
             xml_compare(ET.fromstring(xml0),
                         ET.fromstring(xml1),
                         reporter=sys.stderr.write))
Ejemplo n.º 18
0
 def test_new_widgets(self):
     survey = utils.build_survey('widgets.xls')
     path = utils.path_to_text_fixture('widgets.xml')
     survey.to_xml
     with open(path) as f:
         expected = etree.fromstring(survey.to_xml())
         result = etree.fromstring(f.read())
         self.assertTrue(xml_compare(expected, result))
Ejemplo n.º 19
0
    def test_good_unit(self):
        unit = Unit("Celzius", 'basicSI', "C")

        assert_true(
            xml_compare(etree.fromstring("""
            <unit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.eeml.org/xsd/0.5.1" type="basicSI" symbol="C">Celzius</unit>
            """),
                        unit.toeeml(),
                        reporter=self.fail))
Ejemplo n.º 20
0
def run_filename(filename):
    f = open(filename)
    content = f.read()
    f.close()
    parts = re.split(r"---*", content)
    template = parts[0]
    expected = parts[1]
    if len(parts) == 3:
        data_content = parts[2].strip()
    elif len(parts) > 3:
        print parts[3:]
        assert 0, "Too many sections"
    else:
        data_content = ""
    namespace = {}
    if data_content:
        exec data_content in namespace
    data = namespace.copy()
    data["defaults"] = data.get("defaults", {})
    if data.has_key("check"):
        checker = data["check"]
        del data["check"]
    else:

        def checker(p, s):
            pass

    for name in data.keys():
        if name.startswith("_") or hasattr(__builtin__, name):
            del data[name]
    listener = htmlfill_schemabuilder.SchemaBuilder()
    p = htmlfill.FillingParser(listener=listener, **data)
    p.feed(template)
    p.close()
    output = p.text()

    def reporter(v):
        print v

    try:
        output_xml = ET.XML(output)
        expected_xml = ET.XML(expected)
    except ExpatError:
        comp = output.strip() == expected.strip()
    else:
        comp = xml_compare(output_xml, expected_xml, reporter)
    if not comp:
        print "---- Output:   ----"
        print output
        print "---- Expected: ----"
        print expected
        assert 0
    checker(p, listener.schema())
    checker(p, htmlfill_schemabuilder.parse_schema(template))
Ejemplo n.º 21
0
    def test_to_xml(self):
        """Test to_xml()"""
        # read and parse sample file
        self.stages.read_csv(constants.TEST_SAMPLE_TIMESTAMP_FILE)

        # test xml output
        expected_xml = etree.fromstring(
            b'<stages><stage duration="2.0" name="stage1"/>'
            b'<stage duration="5.0" name="stage2"/>'
            b'<stage duration="10.0" name="stage3"/></stages>')
        self.assertTrue(xml_compare(expected_xml, self.stages.to_xml()))
Ejemplo n.º 22
0
    def test_env_private(self):
        env = Environment(private=False)
        assert_true(
            xml_compare(etree.fromstring("""
<environment xmlns="http://www.eeml.org/xsd/0.5.1">
  <private>false</private>
</environment>"""),
                        env.toeeml(),
                        reporter=self.fail))

        env = Environment(private=True)
        assert_true(
            xml_compare(etree.fromstring("""
<environment xmlns="http://www.eeml.org/xsd/0.5.1">
  <private>true</private>
</environment>"""),
                        env.toeeml(),
                        reporter=self.fail))

        with self.assertRaises(ValueError):
            Environment(private='foobar')
Ejemplo n.º 23
0
def assert_equal_xml(generated_xml, expected_xml):
    """
    :type generated_xml: str
    :type expected_xml: str
    """
    __tracebackhide__ = True  # noqa

    differences = []

    def append_to_messages(message):
        """
        :type message: str
        """
        differences.append(' * {}'.format(message))

    generated_root_element = etree.fromstring(generated_xml.encode('utf-8'))
    expected_root_element = etree.fromstring(expected_xml.encode('utf-8'))

    xml_compare(expected_root_element, generated_root_element, reporter=append_to_messages)
    if differences:
        raise AssertionError('\n{}'.format('\n'.join(differences)))
Ejemplo n.º 24
0
    def test_good_location(self):
        loc = Location('My Room', 32.4, 22.7, 0.2, 'indoor', 'physical', 'fixed')

        assert_true(xml_compare(etree.fromstring(
            """
            <location xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.eeml.org/xsd/0.5.1" disposition="fixed" domain="physical" exposure="indoor">
            <name>My Room</name>
            <lat>32.4</lat>
            <lon>22.7</lon>
            <ele>0.2</ele>
            </location>
            """.strip()), loc.toeeml(), reporter=self.fail))
Ejemplo n.º 25
0
    def test_to_xml(self):
        """Test to_xml()"""
        # read and parse sample file
        self.stages.read_csv(constants.TEST_SAMPLE_TIMESTAMP_FILE)

        # test xml output
        expected_xml = etree.fromstring(
            b'<stages><stage duration="2.0" name="stage1"/>'
            b'<stage duration="5.0" name="stage2"/>'
            b'<stage duration="10.0" name="stage3"/></stages>'
        )
        self.assertTrue(xml_compare(expected_xml, self.stages.to_xml()))
Ejemplo n.º 26
0
def run_filename(filename):
    f = open(filename)
    content = f.read()
    f.close()
    parts = re.split(r"---*", content)
    template = parts[0]
    expected = parts[1]
    if len(parts) == 3:
        data_content = parts[2].strip()
    elif len(parts) > 3:
        assert False, "Too many sections: %s" % parts[3:]
    else:
        data_content = ""
    namespace = {}
    if data_content:
        six.exec_(data_content, namespace)
    data = namespace.copy()
    data["defaults"] = data.get("defaults", {})
    if "check" in data:
        checker = data.pop("check")
    else:

        def checker(p, s):
            pass

    for name in list(data.keys()):
        if name.startswith("_") or hasattr("__builtin__", name):
            del data[name]
    listener = htmlfill_schemabuilder.SchemaBuilder()
    p = htmlfill.FillingParser(listener=listener, **data)
    p.feed(template)
    p.close()
    output = p.text()

    def reporter(v):
        print(v)

    try:
        output_xml = ET.XML(output)
        expected_xml = ET.XML(expected)
    except XMLParseError:
        comp = output.strip() == expected.strip()
    else:
        comp = xml_compare(output_xml, expected_xml, reporter)
    if not comp:
        print("---- Output:   ----")
        print(output)
        print("---- Expected: ----")
        print(expected)
        assert False
    checker(p, listener.schema())
    checker(p, htmlfill_schemabuilder.parse_schema(template))
Ejemplo n.º 27
0
def run_filename(filename):
    f = open(filename)
    content = f.read()
    f.close()
    parts = re.split(r'---*', content)
    template = parts[0]
    expected = parts[1]
    if len(parts) == 3:
        data_content = parts[2].strip()
    elif len(parts) > 3:
        assert False, "Too many sections: %s" % parts[3:]
    else:
        data_content = ''
    namespace = {}
    if data_content:
        exec data_content in namespace
    data = namespace.copy()
    data['defaults'] = data.get('defaults', {})
    if 'check' in data:
        checker = data.pop('check')
    else:

        def checker(p, s):
            pass

    for name in data.keys():
        if name.startswith('_') or hasattr('__builtin__', name):
            del data[name]
    listener = htmlfill_schemabuilder.SchemaBuilder()
    p = htmlfill.FillingParser(listener=listener, **data)
    p.feed(template)
    p.close()
    output = p.text()

    def reporter(v):
        print v

    try:
        output_xml = ET.XML(output)
        expected_xml = ET.XML(expected)
    except XMLParseError:
        comp = output.strip() == expected.strip()
    else:
        comp = xml_compare(output_xml, expected_xml, reporter)
    if not comp:
        print '---- Output:   ----'
        print output
        print '---- Expected: ----'
        print expected
        assert False
    checker(p, listener.schema())
    checker(p, htmlfill_schemabuilder.parse_schema(template))
Ejemplo n.º 28
0
 def test_novalue(self):
     """Test initial state the function and class instances."""
     # number of stages should be zero
     self.assertEqual(0, len(self.stages.stages))
     # test total duration
     self.assertEqual(0, self.stages.total_duration())
     # test started_at and finished_at
     self.assertEqual(None, self.stages.started_at)
     self.assertEqual(None, self.stages.finished_at)
     # xml shouldn't contain items
     self.assertTrue(
         xml_compare(etree.fromstring(b"<stages/>"), self.stages.to_xml()))
     self.assertEqual(b"<stages/>\n", self.stages.to_xml_string())
def main():

    # TODO: specify requirements and check inputs.
    module=AnsibleModule(
        argument_spec=dict(
            host=dict(),
            state=dict(),
            name=dict(),
            template=dict(),
        )
    )

    host = module.params['host']
    state = module.params['state']
    name = module.params['name']
    template = module.params['template']

    # TODO: try catch here
    J = Jenkins(host)

    # TODO: try catch here
    # TODO: how to handle paths?
    if (template):
        txt = open(template)
        job_config = txt.read()
        job_config_xml = etree.fromstring(job_config)

    # There are 4 possible outcomes: created, updated, deleted, nochange.
    if (state == 'present'):
        # If the desired state is present we must check if the job exists and then check if the job has changed.
        if (J.has_job(name)):
            # The job exists so we must compare the XMLs for changes and update if they don't match.
            current_conf = J[name].get_config()
            current_conf = current_conf.encode('ascii','replace')
            current_conf_xml = etree.fromstring(current_conf)
            if (xml_compare(job_config_xml, current_conf_xml)):
                module.exit_json(msg="job exists", changed=False)
            else:
                J[name].update_config(job_config)
                module.exit_json(msg="job updated", changed=True)
        else:
            new_job = J.create_job(name, job_config)
            module.exit_json(msg="job created", changed=True)
    elif (state == 'absent'):
        if (J.has_job(name)):
            J.delete_job(name)
            module.exit_json(msg="job deleted", changed=True)
        else:
            module.exit_json(msg="job does not exist", changed=False)

    module.exit_json(msg=J.version, changed=False)
Ejemplo n.º 30
0
    def test_extension(self):
        xmlstring = """
        <Extension url="http://example.com/ExtensionThingy">
            <valueString value="Hurray for me"/>
        </Extension>
        """

        ext = fhir.model.Extension()
        ext.url = 'http://example.com/ExtensionThingy'
        ext.value = fhir.model.string('Hurray for me')

        x1 = ET.fromstring(xmlstring)
        x2 = ET.fromstring(ext.toXML())
        self.assertTrue(xml_compare(x1, x2))
Ejemplo n.º 31
0
    def test_multipleBoolean(self):
        xmlstring = """
        <Multiple xmlns="http://hl7.org/fhir">
            <multiBoolean value="true"/>
        </Multiple>
        """
        m = Multiple()
        m.multi = fhir.model.boolean(True)

        x1 = ET.fromstring(xmlstring)
        x2 = ET.fromstring(m.toXML())

        msg = 'Serialized Resource does not match expected XML representation!'
        self.assertTrue(xml_compare(x1, x2), msg)
Ejemplo n.º 32
0
def run_filename(filename):
    f = open(filename)
    content = f.read()
    f.close()
    parts = re.split(r'---*', content)
    template = parts[0]
    expected = parts[1]
    if len(parts) == 3:
        data_content = parts[2].strip()
    elif len(parts) > 3:
        assert False, "Too many sections: %s" % parts[3:]
    else:
        data_content = ''
    namespace = {}
    if data_content:
        exec data_content in namespace
    data = namespace.copy()
    data['defaults'] = data.get('defaults', {})
    if 'check' in data:
        checker = data.pop('check')
    else:
        def checker(p, s):
            pass
    for name in data.keys():
        if name.startswith('_') or hasattr('__builtin__', name):
            del data[name]
    listener = htmlfill_schemabuilder.SchemaBuilder()
    p = htmlfill.FillingParser(listener=listener, **data)
    p.feed(template)
    p.close()
    output = p.text()

    def reporter(v):
        print v

    try:
        output_xml = ET.XML(output)
        expected_xml = ET.XML(expected)
    except XMLParseError:
        comp = output.strip() == expected.strip()
    else:
        comp = xml_compare(output_xml, expected_xml, reporter)
    if not comp:
        print '---- Output:   ----'
        print output
        print '---- Expected: ----'
        print expected
        assert False
    checker(p, listener.schema())
    checker(p, htmlfill_schemabuilder.parse_schema(template))
Ejemplo n.º 33
0
def assert_equal_xml(generated_xml, expected_xml):
    """
    :type generated_xml: str
    :type expected_xml: str
    """
    __tracebackhide__ = True  # noqa

    differences = []

    def append_to_messages(message):
        """
        :type message: str
        """
        differences.append(' * {}'.format(message))

    generated_root_element = etree.fromstring(generated_xml.encode('utf-8'))
    expected_root_element = etree.fromstring(expected_xml.encode('utf-8'))

    xml_compare(expected_root_element,
                generated_root_element,
                reporter=append_to_messages)
    if differences:
        raise AssertionError('\n{}'.format('\n'.join(differences)))
Ejemplo n.º 34
0
 def test_novalue(self):
     """Test initial state the function and class instances."""
     # number of stages should be zero
     self.assertEqual(0, len(self.stages.stages))
     # test total duration
     self.assertEqual(0, self.stages.total_duration())
     # test started_at and finished_at
     self.assertEqual(None, self.stages.started_at)
     self.assertEqual(None, self.stages.finished_at)
     # xml shouldn't contain items
     self.assertTrue(
         xml_compare(etree.fromstring(b"<stages/>"), self.stages.to_xml())
     )
     self.assertEqual(b"<stages/>\n", self.stages.to_xml_string())
Ejemplo n.º 35
0
def _xml_semantic_compare(left_xml_str, right_xml_str):
    """
    Compare two XML strings semantically to ensure the content is the same.
    """
    # Reasons for using lxml.etree instead of Python's xml.etree.ElementTree:
    # - The XML strings do not contain an XML directive specifying the
    #   encoding, and the encoding parameter of ElementTree.XMLParser() does
    #   not seem to work.
    # - ET.fromstring() and ET.XML() do not handle byte strings in Python 2.x
    #   (see bugs.python.org/issue11033), so a manual conversion to unicode
    #   would be needed.
    left_xml = etree.fromstring(left_xml_str)
    right_xml = etree.fromstring(right_xml_str)
    return doctest_xml_compare.xml_compare(left_xml, right_xml)
Ejemplo n.º 36
0
def main():

    # TODO: specify requirements and check inputs.
    module = AnsibleModule(argument_spec=dict(
        host=dict(),
        state=dict(),
        name=dict(),
        template=dict(),
    ))

    host = module.params['host']
    state = module.params['state']
    name = module.params['name']
    template = module.params['template']

    # TODO: try catch here
    J = Jenkins(host)

    # TODO: try catch here
    # TODO: how to handle paths?
    if (template):
        txt = open(template)
        job_config = txt.read()
        job_config_xml = etree.fromstring(job_config)

    # There are 4 possible outcomes: created, updated, deleted, nochange.
    if (state == 'present'):
        # If the desired state is present we must check if the job exists and then check if the job has changed.
        if (J.has_job(name)):
            # The job exists so we must compare the XMLs for changes and update if they don't match.
            current_conf = J[name].get_config()
            current_conf = current_conf.encode('ascii', 'replace')
            current_conf_xml = etree.fromstring(current_conf)
            if (xml_compare(job_config_xml, current_conf_xml)):
                module.exit_json(msg="job exists", changed=False)
            else:
                J[name].update_config(job_config)
                module.exit_json(msg="job updated", changed=True)
        else:
            new_job = J.create_job(name, job_config)
            module.exit_json(msg="job created", changed=True)
    elif (state == 'absent'):
        if (J.has_job(name)):
            J.delete_job(name)
            module.exit_json(msg="job deleted", changed=True)
        else:
            module.exit_json(msg="job does not exist", changed=False)

    module.exit_json(msg=J.version, changed=False)
Ejemplo n.º 37
0
def assert_xml_compare(a, b):
    report = []

    def reporter(problem):
        report.append(problem)

    if not xml_compare(etree.XML(a), etree.XML(b), reporter=reporter):
        print(a)
        print()
        print(b)
        print()
        print('XML compare report:')
        for r_line in report:
            print(r_line)
        assert False
Ejemplo n.º 38
0
def assert_xml_equals(actual, expected):
    '''
    Assert xml files/strings are equivalent.

    Differences in formatting or attribute order are ignored. Comments are
    ignored as well. Differences in element order are significant though!

    Parameters
    ----------
    actual : ~typing.BinaryIO or ~pathlib.Path or str
        Actual XML, as file object or Path to XML file, or XML contents as
        string.
    expected : ~typing.BinaryIO or ~pathlib.Path or str
        Expected XML, as file object or Path to XML file, or XML contents as
        string.
    '''

    # Note: if this ever breaks, there is a way to write out XML to file
    # canonicalised. StringIO may help in not having to use any temp files
    # http://lxml.de/api/lxml.etree._ElementTree-class.html#write_c14n
    def tree(xml):
        if isinstance(xml, str):
            xml = etree.fromstring(str(xml))
        else:
            if isinstance(xml, Path):
                xml = str(xml)
            xml = etree.parse(xml)
        return xml.getroot()

    def raise_assert(msg):
        assert False, (f'XMLs differ\n'
                       f'Actual XML: {actual}\n'
                       f'Expected XML: {expected}\n'
                       f'Difference: {msg}')

    xml_compare(tree(actual), tree(expected), reporter=raise_assert)
Ejemplo n.º 39
0
    def test_good_location(self):
        loc = Location('physical', 'My Room', 32.4, 22.7, 0.2, 'indoor',
                       'fixed')

        assert_true(
            xml_compare(etree.fromstring("""
            <location xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.eeml.org/xsd/0.5.1" disposition="fixed" domain="physical" exposure="indoor">
            <name>My Room</name>
            <lat>32.4</lat>
            <lon>22.7</lon>
            <ele>0.2</ele>
            </location>
            """),
                        loc.toeeml(),
                        reporter=self.fail))
Ejemplo n.º 40
0
    def test_multipleDateTime(self):
        xmlstring = """
        <Multiple xmlns="http://hl7.org/fhir">
            <multiDateTime value="2080-01-01T00:00:00Z"/>
        </Multiple>
        """

        m = Multiple()
        m.multi = fhir.model.dateTime("2080-01-01T00:00:00Z")

        x1 = ET.fromstring(xmlstring)
        x2 = ET.fromstring(m.toXML())

        msg = 'Serialized Resource does not match expected XML representation!'
        self.assertTrue(xml_compare(x1, x2), msg)
Ejemplo n.º 41
0
    def assertXFormEqual(self, xform1, xform2):
        xform1 = ETree.fromstring(xform1.encode('utf-8'))
        xform2 = ETree.fromstring(xform2.encode('utf-8'))

        # Sort tags under <model> section in each form
        self.sort_model(xform1)
        self.sort_model(xform2)

        # Report any errors returned from xform_compare
        errs = []

        def reporter(msg):
            errs.append(msg)

        self.assertTrue(xml_compare(xform1, xform2, reporter),
                        "\n\n" + "\n".join(reversed(errs)))
Ejemplo n.º 42
0
    def assertXFormEqual(self, xform1, xform2):
        xform1 = etree.fromstring(xform1)
        xform2 = etree.fromstring(xform2)

        # Sort tags under <model> section in each form
        self.sort_model(xform1)
        self.sort_model(xform2)

        # Report any errors returned from xform_compare
        errs = []
        def reporter(msg):
            errs.append(msg)

        self.assertTrue(
            xml_compare(xform1, xform2, reporter),
            "\n\n" + "\n".join(reversed(errs))
        )
Ejemplo n.º 43
0
    def test_good_data(self):
        u = Unit('Celsius', 'derivedSI', 'C')
        test_data = Data(
            id=0,
            value=10.0, 
            tags=['length'], 
            minValue=0, 
            maxValue=100, 
            unit=u)

        assert_true(xml_compare(etree.fromstring(
            """
            <data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.eeml.org/xsd/0.5.1" id="0">
            <tag>length</tag>
            <current_value maxValue="100" minValue="0">10.0</current_value>
            <unit symbol="C" type="derivedSI">Celsius</unit>
            </data>
            """.strip()), test_data.toeeml(), reporter=self.fail))
Ejemplo n.º 44
0
def _xml_semantic_compare(left_xml_str, right_xml_str):
    """
    Compare two XML strings semantically to ensure the content is the same.
    """
    # Reasons for using lxml.etree instead of Python's xml.etree.ElementTree
    # (ET):
    # - The XML strings do not contain an XML directive specifying the
    #   encoding, and the encoding parameter of ET.XMLParser() does not seem
    #   to work.
    # - ET.fromstring() and ET.XML() do not handle byte strings in Python 2.x
    #   (see bugs.python.org/issue11033), so a manual conversion to unicode
    #   would be needed.
    # Note: lxml.etree.fromstring() has issues with unicode strings as input,
    # so we pass UTF-8 encoded byte strings. See lxml bug
    # https://bugs.launchpad.net/lxml/+bug/1902364.
    left_xml = etree.fromstring(_ensure_bytes(left_xml_str))
    right_xml = etree.fromstring(_ensure_bytes(right_xml_str))
    return doctest_xml_compare.xml_compare(left_xml, right_xml)
Ejemplo n.º 45
0
    def test_good_datapoints(self):
        env = Environment('A Room Somewhere',
                          'http://www.cosm.com/feeds/1.xml',
                          'frozen',
                          'This is a room somewhere',
                          'http://www.roomsomewhere/icon.png',
                          'http://www.roomsomewhere/',
                          'myemail@roomsomewhere',
                          updated=datetime(2007, 5, 4, 18, 13, 51, 0,
                                           pytz.utc),
                          creator='http://www.somewhere',
                          id_=1)

        datapoints = DataPoints(
            1, [(0, ), (1, ),
                (2, datetime(2007, 5, 4, 18, 13, 51, 0, pytz.utc))])

        result = create_eeml(env, None, datapoints).toeeml()

        assert_true(
            xml_compare(etree.fromstring("""
<eeml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.eeml.org/xsd/0.5.1" xsi:schemaLocation="http://www.eeml.org/xsd/0.5.1 http://www.eeml.org/xsd/0.5.1/0.5.1.xsd" version="0.5.1">
  <environment updated="2007-05-04T18:13:51+00:00" creator="http://www.somewhere" id="1">
    <title>A Room Somewhere</title>
    <feed>http://www.cosm.com/feeds/1.xml</feed>
    <status>frozen</status>
    <description>This is a room somewhere</description>
    <icon>http://www.roomsomewhere/icon.png</icon>
    <website>http://www.roomsomewhere/</website>
    <email>myemail@roomsomewhere</email>
    <data id="1">
      <datapoints xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.eeml.org/xsd/0.5.1">
        <value>0</value>
        <value>1</value>
        <value at="2007-05-04T18:13:51+00:00">2</value>
      </datapoints>
    </data>
  </environment>
</eeml>
"""),
                        result,
                        reporter=self.fail))
Ejemplo n.º 46
0
    def test_good_data(self):
        u = Unit('Celsius', 'derivedSI', 'C')
        test_data = Data(id_=0,
                         value=10.0,
                         tags=['length', 'foo'],
                         minValue=0,
                         maxValue=100,
                         unit=u)

        assert_true(
            xml_compare(etree.fromstring("""
            <data xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.eeml.org/xsd/0.5.1" id="0">
            <tag>length</tag>
            <tag>foo</tag>
            <current_value maxValue="100" minValue="0">10.0</current_value>
            <unit symbol="C" type="derivedSI">Celsius</unit>
            </data>
            """),
                        test_data.toeeml(),
                        reporter=self.fail))
Ejemplo n.º 47
0
    def test_multiple_datapoints(self):
        env = Environment()
        env.updateData(DataPoints(1, [(4,)]))
        env.updateData(DataPoints(4, [(5,)]))
        env.updateData(DataPoints(1, [(6,), (7,)]))

        assert_true(xml_compare(etree.fromstring("""
<environment xmlns="http://www.eeml.org/xsd/0.5.1">
  <data id="1">
    <datapoints>
      <value>6</value>
      <value>7</value>
    </datapoints>
  </data>
  <data id="4">
    <datapoints>
      <value>5</value>
    </datapoints>
  </data>
</environment>"""), env.toeeml(), reporter=self.fail))
Ejemplo n.º 48
0
    def runTest(self):
       
        path_to_excel_file = utils.path_to_text_fixture("xlsform_spec_test.xls")
        
        #Get the xform output path:
        directory, filename = os.path.split(path_to_excel_file)
        root_filename, ext = os.path.splitext(filename)
        path_to_xform = os.path.join(directory, root_filename + ".xml")

        #Do the conversion:
        json_survey = xls2json.parse_file_to_json(path_to_excel_file)
        survey = pyxform.create_survey_element_from_dict(json_survey)
        survey.print_xform_to_file(path_to_xform)
        
        #Compare with the expected output:
        expected_path = utils.path_to_text_fixture("spec_test_expected_output.xml")
        with codecs.open(expected_path, 'rb', encoding="utf-8") as expected_file:
            expected = etree.fromstring(expected_file.read())
            result = etree.fromstring(survey.to_xml())
            self.assertTrue(xml_compare(expected, result))
Ejemplo n.º 49
0
    def test_multiple_datapoints(self):
        env = Environment()
        env.updateData(DataPoints(1, [(4, )]))
        env.updateData(DataPoints(4, [(5, )]))
        env.updateData(DataPoints(1, [(6, ), (7, )]))

        assert_true(
            xml_compare(etree.fromstring("""
<environment xmlns="http://www.eeml.org/xsd/0.5.1">
  <data id="1">
    <datapoints>
      <value>6</value>
      <value>7</value>
    </datapoints>
  </data>
  <data id="4">
    <datapoints>
      <value>5</value>
    </datapoints>
  </data>
</environment>"""),
                        env.toeeml(),
                        reporter=self.fail))
Ejemplo n.º 50
0
    def test_good_environment(self):
        env = Environment('A Room Somewhere',
            'http://www.cosm.com/feeds/1.xml',
            'frozen',
            'This is a room somewhere',
            'http://www.roomsomewhere/icon.png',
            'http://www.roomsomewhere/',
            'myemail@roomsomewhere',
            updated='2007-05-04T18:13:51.0Z',
            creator='http://www.somewhere',
            id=1)

        assert_true(xml_compare(etree.fromstring(
            """
            <environment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.eeml.org/xsd/0.5.1" creator="http://www.somewhere" id="1" updated="2007-05-04T18:13:51.0Z">
                <title>A Room Somewhere</title>
                <feed />
                <status>frozen</status>
                <description>This is a room somewhere</description>
                <icon>http://www.roomsomewhere/icon.png</icon>
                <website>http://www.roomsomewhere/</website>
                <email>myemail@roomsomewhere</email>
            </environment>""".strip()), env.toeeml(), reporter=self.fail))
Ejemplo n.º 51
0
    def test_good_datapoints(self):
        env = Environment('A Room Somewhere',
                          'http://www.cosm.com/feeds/1.xml',
                          'frozen',
                          'This is a room somewhere',
                          'http://www.roomsomewhere/icon.png',
                          'http://www.roomsomewhere/',
                          'myemail@roomsomewhere',
                          updated='2007-05-04T18:13:51.0Z',
                          creator='http://www.somewhere',
                          id_=1)

        datapoints = DataPoints([(0,), (1,), (2, datetime(2007, 5, 4, 18, 13, 51))])

        result = create_eeml(env, None, datapoints).toeeml()

        assert_true(xml_compare(etree.fromstring(
            """
<eeml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.eeml.org/xsd/0.5.1" xsi:schemaLocation="http://www.eeml.org/xsd/0.5.1 http://www.eeml.org/xsd/0.5.1/0.5.1.xsd" version="0.5.1">
  <environment updated="2007-05-04T18:13:51.0Z" creator="http://www.somewhere" id="1">
    <title>A Room Somewhere</title>
    <feed>http://www.cosm.com/feeds/1.xml</feed>
    <status>frozen</status>
    <description>This is a room somewhere</description>
    <icon>http://www.roomsomewhere/icon.png</icon>
    <website>http://www.roomsomewhere/</website>
    <email>myemail@roomsomewhere</email>
    <data>
      <datapoints xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.eeml.org/xsd/0.5.1">
        <value>0</value>
        <value>1</value>
        <value at="2007-05-04T18:13:51">2</value>
      </datapoints>
    </data>
  </environment>
</eeml>
""".strip()), result, reporter=self.fail))
Ejemplo n.º 52
0
    def testAll(self):
        self.errors = BytesIO()
        config = configparser.ConfigParser()
        config.read([defaultsfn, os.path.join(self.testdir, "options.cfg")])

        themefn = None
        if config.get('diazotest', 'theme'):
            themefn = os.path.join(self.testdir, config.get('diazotest',
                                                            'theme'))
        contentfn = os.path.join(self.testdir, "content.html")
        rulesfn = os.path.join(self.testdir, "rules.xml")
        xpathsfn = os.path.join(self.testdir, "xpaths.txt")
        xslfn = os.path.join(self.testdir, "compiled.xsl")
        outputfn = os.path.join(self.testdir, "output.html")

        xsl_params = {}
        extra_params = config.get('diazotest', 'extra-params')
        if extra_params:
            for token in extra_params.split(' '):
                token_split = token.split(':')
                xsl_params[token_split[0]] = len(token_split) > 1 and \
                    token_split[1] or None

        if not os.path.exists(rulesfn):
            return

        contentdoc = etree.parse(source=contentfn, base_url=contentfn,
                                 parser=etree.HTMLParser())

        # Make a compiled version
        theme_parser = etree.HTMLParser()
        ct = diazo.compiler.compile_theme(
            rules=rulesfn,
            theme=themefn,
            parser=theme_parser,
            absolute_prefix=config.get('diazotest', 'absolute-prefix'),
            indent=config.getboolean('diazotest', 'pretty-print'),
            xsl_params=xsl_params,
        )

        # Serialize / parse the theme - this can catch problems with escaping.
        cts = etree.tostring(ct, encoding='unicode')
        parser = etree.XMLParser()
        etree.fromstring(cts, parser=parser)

        # Compare to previous version
        if os.path.exists(xslfn):
            with open(xslfn) as f:
                old = f.read()
            new = cts
            if old != new:
                if self.writefiles:
                    with open(xslfn + '.old', 'w') as f:
                        f.write(old)
                if self.warnings:
                    print("WARNING:", "compiled.xsl has CHANGED")
                    for line in difflib.unified_diff(old.split(u'\n'),
                                                     new.split(u'\n'),
                                                     xslfn, 'now'):
                        print(line)

        # Write the compiled xsl out to catch unexpected changes
        if self.writefiles:
            with open(xslfn, 'w') as f:
                f.write(cts)

        # Apply the compiled version, then test against desired output
        theme_parser.resolvers.add(diazo.run.RunResolver(self.testdir))
        processor = etree.XSLT(ct)
        params = {}
        params['path'] = "'%s'" % config.get('diazotest', 'path')

        for key in xsl_params:
            try:
                params[key] = quote_param(config.get('diazotest', key))
            except configparser.NoOptionError:
                pass

        result = processor(contentdoc, **params)

        # Read the whole thing to strip off xhtml namespace.
        # If we had xslt 2.0 then we could use xpath-default-namespace.
        self.themed_string = str(result)
        self.themed_content = etree.ElementTree(
            file=StringIO(self.themed_string), parser=etree.HTMLParser())

        # remove the extra meta content type

        metas = self.themed_content.xpath(
            "/html/head/meta[@http-equiv='Content-Type']")
        if metas:
            meta = metas[0]
            meta.getparent().remove(meta)

        if os.path.exists(xpathsfn):
            with open(xpathsfn) as f:
                for xpath in f.readlines():
                    # Read the XPaths from the file, skipping blank lines and
                    # comments
                    this_xpath = xpath.strip()
                    if not this_xpath or this_xpath[0] == '#':
                        continue
                    assert self.themed_content.xpath(this_xpath), "%s: %s" % (
                        xpathsfn, this_xpath)

        # Compare to previous version
        if os.path.exists(outputfn):
            with open(outputfn) as f:
                old = f.read()
            new = self.themed_string
            if not xml_compare(
                    etree.fromstring(old.strip()),
                    etree.fromstring(new.strip())):
                # if self.writefiles:
                #    open(outputfn + '.old', 'w').write(old)
                for line in difflib.unified_diff(old.split(u'\n'),
                                                 new.split(u'\n'),
                                                 outputfn, 'now'):
                    print(line)
                assert old == new, "output.html has CHANGED"

        # Write out the result to catch unexpected changes
        if self.writefiles:
            with open(outputfn, 'w') as f:
                f.write(self.themed_string)
Ejemplo n.º 53
0
    def test_good_create_doc(self):
        env = Environment('A Room Somewhere',
                          'http://www.cosm.com/feeds/1.xml',
                          'frozen',
                          'This is a room somewhere',
                          'http://www.roomsomewhere/icon.png',
                          'http://www.roomsomewhere/',
                          'myemail@roomsomewhere',
                          updated='2007-05-04T18:13:51.0Z',
                          creator='http://www.somewhere',
                          id_=1)
        loc = Location('physical', 'My Room', 32.4, 22.7, 0.2, 'indoor',
                       'fixed')
        u = Unit('Celsius', 'derivedSI', 'C')
        dat = []
        dat.append(
            Data(0,
                 36.2,
                 minValue=23.8,
                 maxValue=48.0,
                 unit=u,
                 tags=['temperature']))
        u = Unit('blushesPerHour', 'contextDependentUnits')
        dat.append(
            Data(1,
                 84.2,
                 minValue=0,
                 maxValue=100,
                 unit=u,
                 tags=['blush', 'redness', 'embarrasement']))
        u = Unit('meter', 'basicSI', 'm')
        dat.append(
            Data(2,
                 12.3,
                 minValue=0,
                 unit=u,
                 tags=['length', 'distance', 'extension']))

        intermed = etree.tostring(create_eeml(
            env, loc, dat).toeeml())  # Broken down to help with error-checking
        final = etree.fromstring(intermed)

        assert_true(
            xml_compare(etree.fromstring("""
            <eeml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.eeml.org/xsd/0.5.1" xsi:schemaLocation="http://www.eeml.org/xsd/0.5.1 http://www.eeml.org/xsd/0.5.1/0.5.1.xsd" version="0.5.1">
                <environment creator="http://www.somewhere" id="1" updated="2007-05-04T18:13:51.0Z">
                    <title>A Room Somewhere</title>
                    <feed>http://www.cosm.com/feeds/1.xml</feed>                    
                    <status>frozen</status>
                    <description>This is a room somewhere</description>
                    <icon>http://www.roomsomewhere/icon.png</icon>
                    <website>http://www.roomsomewhere/</website>
                    <email>myemail@roomsomewhere</email>
                    <location disposition="fixed" domain="physical" exposure="indoor">
                        <name>My Room</name>
                        <lat>32.4</lat>
                        <lon>22.7</lon>
                        <ele>0.2</ele>
                    </location>
                    <data id="0">
                        <tag>temperature</tag>
                        <current_value maxValue="48.0" minValue="23.8">36.2</current_value>
                        <unit symbol="C" type="derivedSI">Celsius</unit>
                    </data>
                    <data id="1">
                        <tag>blush</tag>
                        <tag>redness</tag>
                        <tag>embarrasement</tag>
                        <current_value maxValue="100" minValue="0">84.2</current_value>
                        <unit type="contextDependentUnits">blushesPerHour</unit>
                    </data>
                    <data id="2">
                        <tag>length</tag>
                        <tag>distance</tag>
                        <tag>extension</tag>
                        <current_value minValue="0">12.3</current_value>
                        <unit symbol="m" type="basicSI">meter</unit>
                    </data>
                </environment>
            </eeml>
            """),
                        final,
                        reporter=self.fail))
Ejemplo n.º 54
0
    def testAll(self):
        self.errors = BytesIO()
        config = configparser.ConfigParser()
        config.read([defaultsfn, os.path.join(self.testdir, "options.cfg")])

        themefn = None
        if config.get('diazotest', 'theme'):
            themefn = os.path.join(self.testdir,
                                   config.get('diazotest', 'theme'))
        contentfn = os.path.join(self.testdir, "content.html")
        rulesfn = os.path.join(self.testdir, "rules.xml")
        xpathsfn = os.path.join(self.testdir, "xpaths.txt")
        xslfn = os.path.join(self.testdir, "compiled.xsl")
        outputfn = os.path.join(self.testdir, "output.html")

        xsl_params = {}
        extra_params = config.get('diazotest', 'extra-params')
        if extra_params:
            for token in extra_params.split(' '):
                token_split = token.split(':')
                xsl_params[token_split[0]] = len(token_split) > 1 and \
                    token_split[1] or None

        if not os.path.exists(rulesfn):
            return

        contentdoc = etree.parse(source=contentfn,
                                 base_url=contentfn,
                                 parser=etree.HTMLParser())

        # Make a compiled version
        theme_parser = etree.HTMLParser()
        ct = diazo.compiler.compile_theme(
            rules=rulesfn,
            theme=themefn,
            parser=theme_parser,
            absolute_prefix=config.get('diazotest', 'absolute-prefix'),
            indent=config.getboolean('diazotest', 'pretty-print'),
            xsl_params=xsl_params,
        )

        # Serialize / parse the theme - this can catch problems with escaping.
        cts = etree.tostring(ct, encoding='unicode')
        parser = etree.XMLParser()
        etree.fromstring(cts, parser=parser)

        # Compare to previous version
        if os.path.exists(xslfn):
            with open(xslfn) as f:
                old = f.read()
            new = cts
            if old != new:
                if self.writefiles:
                    with open(xslfn + '.old', 'w') as f:
                        f.write(old)
                if self.warnings:
                    print("WARNING:", "compiled.xsl has CHANGED")
                    for line in difflib.unified_diff(old.split(u'\n'),
                                                     new.split(u'\n'), xslfn,
                                                     'now'):
                        print(line)

        # Write the compiled xsl out to catch unexpected changes
        if self.writefiles:
            with open(xslfn, 'w') as f:
                f.write(cts)

        # Apply the compiled version, then test against desired output
        theme_parser.resolvers.add(diazo.run.RunResolver(self.testdir))
        processor = etree.XSLT(ct)
        params = {}
        params['path'] = "'%s'" % config.get('diazotest', 'path')

        for key in xsl_params:
            try:
                params[key] = quote_param(config.get('diazotest', key))
            except configparser.NoOptionError:
                pass

        result = processor(contentdoc, **params)

        # Read the whole thing to strip off xhtml namespace.
        # If we had xslt 2.0 then we could use xpath-default-namespace.
        self.themed_string = str(result)
        self.themed_content = etree.ElementTree(file=StringIO(
            self.themed_string),
                                                parser=etree.HTMLParser())

        # remove the extra meta content type

        metas = self.themed_content.xpath(
            "/html/head/meta[@http-equiv='Content-Type']")
        if metas:
            meta = metas[0]
            meta.getparent().remove(meta)

        if os.path.exists(xpathsfn):
            with open(xpathsfn) as f:
                for xpath in f.readlines():
                    # Read the XPaths from the file, skipping blank lines and
                    # comments
                    this_xpath = xpath.strip()
                    if not this_xpath or this_xpath[0] == '#':
                        continue
                    assert self.themed_content.xpath(
                        this_xpath), "%s: %s" % (xpathsfn, this_xpath)

        # Compare to previous version
        if os.path.exists(outputfn):
            with open(outputfn) as f:
                old = f.read()
            new = self.themed_string
            if not xml_compare(etree.fromstring(old.strip()),
                               etree.fromstring(new.strip())):
                # if self.writefiles:
                #    open(outputfn + '.old', 'w').write(old)
                for line in difflib.unified_diff(old.split(u'\n'),
                                                 new.split(u'\n'), outputfn,
                                                 'now'):
                    print(line)
                assert old == new, "output.html has CHANGED"

        # Write out the result to catch unexpected changes
        if self.writefiles:
            with open(outputfn, 'w') as f:
                f.write(self.themed_string)
 def test_parse(self):
     for xml0 in [GET]:
         o = ofc.OFCapableSwitchType.from_xml(xml0)
         xml1 = o.to_xml('capable-switch')
         ok_(xml_compare(ET.fromstring(xml0), ET.fromstring(xml1),
                         reporter=sys.stderr.write))
Ejemplo n.º 56
0
    def test_good_create_doc(self):
        env = Environment('A Room Somewhere',
            'http://www.cosm.com/feeds/1.xml',
            'frozen',
            'This is a room somewhere',
            'http://www.roomsomewhere/icon.png',
            'http://www.roomsomewhere/',
            'myemail@roomsomewhere',
            updated='2007-05-04T18:13:51.0Z',
            creator='http://www.somewhere',
            id=1)
        loc = Location('My Room', 32.4, 22.7, 0.2, 'indoor', 'physical', 'fixed')
        u = Unit('Celsius', 'derivedSI', 'C')
        dat = []
        dat.append(Data(0, 36.2, minValue=23.8, maxValue=48.0, unit = u, tags=['temperature']))
        u = Unit('blushesPerHour', 'contextDependentUnits')
        dat.append(Data(1, 84.2, minValue=0, maxValue=100, unit = u, tags=['blush', 'redness', 'embarrasement']))
        u = Unit('meter', 'basicSI', 'm')
        dat.append(Data(2, 12.3, minValue=0, unit = u, tags=['length', 'distance', 'extension']))


        intermed = etree.tostring(
            create_eeml(env, loc, dat).toeeml()) # Broken down to help with error-checking
        final = etree.fromstring(intermed)

        assert_true(xml_compare(etree.fromstring(
            """
            <eeml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.eeml.org/xsd/0.5.1" xsi:schemaLocation="http://www.eeml.org/xsd/0.5.1 http://www.eeml.org/xsd/0.5.1/0.5.1.xsd" version="0.5.1">
                <environment creator="http://www.somewhere" id="1" updated="2007-05-04T18:13:51.0Z">
                    <title>A Room Somewhere</title>
                    <feed />
                    <status>frozen</status>
                    <description>This is a room somewhere</description>
                    <icon>http://www.roomsomewhere/icon.png</icon>
                    <website>http://www.roomsomewhere/</website>
                    <email>myemail@roomsomewhere</email>
                    <location disposition="fixed" domain="physical" exposure="indoor">
                        <name>My Room</name>
                        <lat>32.4</lat>
                        <lon>22.7</lon>
                        <ele>0.2</ele>
                    </location>
                    <data id="0">
                        <tag>temperature</tag>
                        <current_value maxValue="48.0" minValue="23.8">36.2</current_value>
                        <unit symbol="C" type="derivedSI">Celsius</unit>
                    </data>
                    <data id="1">
                        <tag>blush</tag>
                        <tag>redness</tag>
                        <tag>embarrasement</tag>
                        <current_value maxValue="100" minValue="0">84.2</current_value>
                        <unit type="contextDependentUnits">blushesPerHour</unit>
                    </data>
                    <data id="2">
                        <tag>length</tag>
                        <tag>distance</tag>
                        <tag>extension</tag>
                        <current_value minValue="0">12.3</current_value>
                        <unit symbol="m" type="basicSI">meter</unit>
                    </data>
                </environment>
            </eeml>
            """.strip()), final, reporter=self.fail))
Ejemplo n.º 57
0
 def test_xml_read_write(self):
     xml_content = utils.get_config(self.test_file)
     utils.update_config(self.test_file, xml_content)
     new_xml_tree = self.get_xml_tree()
     # pass element to xml_compare, not ElementTree
     self.assertEqual(True, xml_compare(self.old_xml_tree, new_xml_tree))