# -*- coding: utf-8 -*- # This program is free software; you can redistribute it and/or modify it under # the terms of the (LGPL) GNU Lesser General Public License as published by the # Free Software Foundation; either version 3 of the License, or (at your # option) any later version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU Library Lesser General Public License # for more details at ( http://www.gnu.org/licenses/lgpl.html ). # # You should have received a copy of the GNU Lesser General Public License # along with this program; if not, write to the Free Software Foundation, Inc., # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # written by: Jurko Gospodnetić ( jurko.gospodnetic@pke.hr ) """ CompareSAX testing utility unit tests. """ if __name__ == "__main__": import testutils testutils.run_using_pytest(globals()) import suds import suds.sax.document import suds.sax.parser from testutils.assertion import assert_no_output from testutils.compare_sax import CompareSAX import pytest import xml.sax # CompareSAX class uses Python assertions to report failed comparison results # so we need to skip the tests in this module if Python assertions have been # disabled in the CompareSAX implementation module. skip_test_if_CompareSAX_assertions_disabled = pytest.mark.skipif( not CompareSAX.assertions_enabled(), reason="CompareSAX assertions disabled") @skip_test_if_CompareSAX_assertions_disabled @pytest.mark.parametrize("data", ( "", "", '', "xml")) def test_failed_parsing(data, capsys): pytest.raises(xml.sax.SAXParseException, CompareSAX.data2data, data, data) assert_no_output(capsys) class TestMatched: """Successful CompareSAX matching tests.""" @skip_test_if_CompareSAX_assertions_disabled def test_empty_document(self, capsys): a = suds.sax.document.Document() b = suds.sax.document.Document() CompareSAX.document2document(a, b) assert_no_output(capsys) @skip_test_if_CompareSAX_assertions_disabled @pytest.mark.parametrize(("data1", "data2"), ( # Simple matches. ('', ''), ('', ''), ('', ''), # Extra namespace declarations. ('', ''), ('', ''), ('', ''), # Mismatched namespace prefixes. ('', ''), ('', ''), # Numeric unicode character references. ("\u2606", "&#%d;" % (0x2606,)))) def test_data2data(self, data1, data2, capsys): CompareSAX.data2data(data1, data2) assert_no_output(capsys) @skip_test_if_CompareSAX_assertions_disabled @pytest.mark.parametrize("type1", (suds.byte_str, str)) @pytest.mark.parametrize("type2", (suds.byte_str, str)) def test_string_input_types(self, type1, type2, capsys): xml = "" CompareSAX.data2data(type1(xml), type2(xml)) assert_no_output(capsys) @skip_test_if_CompareSAX_assertions_disabled def test_xml_encoding(self, capsys): """Test that the encoding listed in the XML declaration is honored.""" xml_format = '\u00D8' data1 = (xml_format % ("UTF-8",)).encode('utf-8') data2 = (xml_format % ("latin1",)).encode('latin1') CompareSAX.data2data(data1, data2) assert_no_output(capsys) class TestMismatched: """Failed CompareSAX matching tests.""" @skip_test_if_CompareSAX_assertions_disabled @pytest.mark.parametrize(("data1", "data2", "expected_context"), ( # Different element namespaces. ("", '', "data2data..namespace"), ('', '', "data2data..namespace"), ('', '', "data2data...namespace"), ('', '', "data2data....namespace"), # Different textual content in text only nodes. ("one", "two", "data2data..text"), ("x", "x ", "data2data..text"), ("x", "x ", "data2data..text"), ("x ", "x ", "data2data..text"), (" x", "x", "data2data..text"), (" x", "x", "data2data..text"), (" x", " x", "data2data..text"), ("x", "X", "data2data....text"), ("xy", "xY", "data2data....text"), # Different textual content in mixed content nodes with children. ("4242", "42 42", "data2data..text"), # Differently named elements. ("", "", "data2data."), ("", "", "data2data.."), ("", "", "data2data.."), ("", "", "data2data.."), ("", "", "data2data..."), ("", "", "data2data..."), # Extra/missing non-root element. ("", "", "data2data."), ("", "", "data2data."), ("", "", "data2data."), ("", "", "data2data."), ("", "", "data2data."), ("", "", "data2data."), # Multiple differences. ("", "", "data2data."), ("", '', "data2data..namespace"), ("", "", "data2data.."), ("", '', "data2data...namespace"))) def test_data2data(self, data1, data2, expected_context, capsys): pytest.raises(AssertionError, CompareSAX.data2data, data1, data2) _assert_context_output(capsys, expected_context) @skip_test_if_CompareSAX_assertions_disabled def test_document2document_context(self, capsys): a = suds.sax.document.Document() b = suds.sax.parser.Parser().parse(string=suds.byte_str("")) pytest.raises(AssertionError, CompareSAX.document2document, a, b) _assert_context_output(capsys, "document2document") @skip_test_if_CompareSAX_assertions_disabled def test_document2element_context(self, capsys): a = suds.sax.parser.Parser().parse(string=suds.byte_str("1")) b = suds.sax.parser.Parser().parse(string=suds.byte_str("2")) pytest.raises(AssertionError, CompareSAX.document2element, a, b.root()) _assert_context_output(capsys, "document2element..text") @skip_test_if_CompareSAX_assertions_disabled def test_element2element_context(self, capsys): Parser = suds.sax.parser.Parser e1 = Parser().parse(string=suds.byte_str("")).root() e2 = Parser().parse(string=suds.byte_str("")).root() pytest.raises(AssertionError, CompareSAX.element2element, e1, e2) _assert_context_output(capsys, "element2element.") @skip_test_if_CompareSAX_assertions_disabled def test_element2element_context_invalid_name__left(self, capsys): Parser = suds.sax.parser.Parser e = Parser().parse(string=suds.byte_str("")).root() e_invalid = object() pytest.raises(AssertionError, CompareSAX.element2element, e_invalid, e) _assert_context_output(capsys, "element2element.") @skip_test_if_CompareSAX_assertions_disabled def test_element2element_context_invalid_name__right(self, capsys): Parser = suds.sax.parser.Parser e = Parser().parse(string=suds.byte_str("")).root() e_invalid = object() pytest.raises(AssertionError, CompareSAX.element2element, e, e_invalid) _assert_context_output(capsys, "element2element.") @skip_test_if_CompareSAX_assertions_disabled def test_empty_vs_non_empty_document(self, capsys): document = suds.sax.document.Document() data = "" pytest.raises(AssertionError, CompareSAX.document2data, document, data) _assert_context_output(capsys, "document2data") #TODO: TestSAXModelFeatures tests should be removed once their respective SAX # document model features get tested by SAX document model specific unit tests. #TODO: Additional missing suds SAX document model unit tests: # * SAX parser fails on documents with multiple root elements. # * SAX document may contain at most one element, accessible as root(). # * SAX document append() overwrites the root element silently. class TestSAXModelFeatures: """SAX document model feature testing using the CompareSAX interface.""" @skip_test_if_CompareSAX_assertions_disabled @pytest.mark.parametrize(("data1", "data2"), ( # Differently placed default namespace declaration. ('', ''), # Differently placed namespace prefix declaration. ('', ''), # Element's textual content merged. ("111222", "111222"), ("111222", "111222"), ("111222", "111222"), # Explicit "" namespace == no prefix or default namespace. ('', ""), ('', ""), # Extra leading/trailing textual whitespace trimmed in mixed content # elements with more than one child element. (" \n\n \t\t\n\n", ""), (" \nxxx\n \t\t\n\n", "xxx"))) def test_data2data(self, data1, data2, capsys): CompareSAX.data2data(data1, data2) assert_no_output(capsys) def _assert_context_output(capsys, context): """ Test utility asserting an expected captured stderr context output and no captured stdout output. """ out, err = capsys.readouterr() assert not out assert err == "Failed SAX XML comparison context:\n %s\n" % (context,)