Esempio n. 1
0
def assert_xml_equal(actual: Union[IO, str], expected: Union[IO, str]):
    """Checks whether the XML trees behind `actual` and `expected` are equal.

    Args:
        actual: The actual XML
        expected: The expected XML

    Throws:
        AssertionError when xml(actual) != xml(expected)
    """
    e1 = _to_etree(actual)
    e2 = _to_etree(expected)

    try:
        lxml_asserts.assert_xml_equal(e1, e2)
    except AssertionError as e:
        # For debugging purposes, the trees are saved to later inspect their contents
        s1 = etree.tostring(e1, pretty_print=True).decode("utf-8")
        s2 = etree.tostring(e2, pretty_print=True).decode("utf-8")

        with open("actual.xml", "w") as f:
            f.write(s1)

        with open("expected.xml", "w") as f:
            f.write(s2)

        with open("difference.diff", "w") as f:
            diff = difflib.unified_diff(s1.splitlines(),
                                        s2.splitlines(),
                                        fromfile="Actual",
                                        tofile="Expected")
            diff_string = "\n".join(diff)
            f.write(diff_string)

        raise e
Esempio n. 2
0
def test_serializing_small_cas_to_string(small_xmi, small_typesystem_xml):
    typesystem = load_typesystem(small_typesystem_xml)
    cas = load_cas_from_xmi(small_xmi, typesystem=typesystem)

    actual_xml = cas.to_xmi()

    assert_xml_equal(actual_xml, small_xmi.encode('utf-8'))
Esempio n. 3
0
def test_serializing_small_typesystem_to_file(tmpdir, small_typesystem_xml):
    typesystem = load_typesystem(small_typesystem_xml)
    path = tmpdir.join('typesystem.xml')

    with open(path, 'wb') as f:
        typesystem.to_xml(f)

    with open(path, 'rb') as actual:
        assert_xml_equal(actual.read(), small_typesystem_xml.encode('utf-8'))
Esempio n. 4
0
def test_serializing_small_cas_to_file_path(tmpdir, small_xmi,
                                            small_typesystem_xml):
    typesystem = load_typesystem(small_typesystem_xml)
    cas = load_cas_from_xmi(small_xmi, typesystem=typesystem)
    path = tmpdir.join('cas.xml')

    cas.to_xmi(path)

    with open(path, 'rb') as actual:
        assert_xml_equal(actual.read(), small_xmi.encode('utf-8'))
Esempio n. 5
0
    def assertXmlEqual(self, first, second, check_tags_order=False, msg=None):
        """
        Assert that two xml documents are equal.
        :param first: first etree object or xml string
        :param second: second etree object or xml string
        :param check_tags_order: if False, the order of children is ignored
        :param msg: custom error message
        :return: raises failureException if xml documents are not equal
        """
        if msg is None:
            msg = u'XML documents are not equal'

        try:
            assert_xml_equal(first, second, check_tags_order)
        except AssertionError as e:
            raise_exc_info(
                (self.failureException,
                 self.failureException(u'{} — {}'.format(msg,
                                                         unicode_type(e))),
                 sys.exc_info()[2]))
Esempio n. 6
0
def test_serializing_small_typesystem_to_string(small_typesystem_xml):
    typesystem = load_typesystem(small_typesystem_xml)

    actual_xml = typesystem.to_xml()

    assert_xml_equal(actual_xml, small_typesystem_xml.encode('utf-8'))