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
|
# -*- 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 )
"""
Suds Python library plugin unit tests.
Implemented using the 'pytest' testing framework.
"""
import testutils
if __name__ == "__main__":
testutils.run_using_pytest(globals())
import pytest
from suds.plugin import (DocumentContext, DocumentPlugin, InitContext,
InitPlugin, MessageContext, MessagePlugin, Plugin, PluginContainer,
PluginDomain)
class MyDocumentPlugin(DocumentPlugin):
"""Local 'document' plugin class used in the tests in this module."""
pass
class MyInitPlugin(InitPlugin):
"""Local 'init' plugin class used in the tests in this module."""
pass
class MyMessagePlugin(MessagePlugin):
"""Local 'message' plugin class used in the tests in this module."""
pass
@pytest.mark.parametrize("domain, expected_context", (
("document", DocumentContext),
("init", InitContext),
("message", MessageContext)))
def test_collecting_plugins_and_context_per_empty_domain(domain,
expected_context):
container = PluginContainer([])
result = getattr(container, domain)
assert result.__class__ is PluginDomain
assert result.ctx is expected_context
assert result.plugins == []
@pytest.mark.parametrize("domain, plugin_class", (
("document", DocumentPlugin),
("init", InitPlugin),
("message", MessagePlugin)))
def test_collecting_plugins_per_domain(domain, plugin_class):
plugins = [
MyDocumentPlugin(),
MyDocumentPlugin(),
MyMessagePlugin(),
MyDocumentPlugin(),
MyInitPlugin(),
MyInitPlugin(),
MyMessagePlugin(),
InitPlugin(),
MyMessagePlugin(),
MyMessagePlugin(),
None,
MessagePlugin(),
DocumentPlugin(),
MyMessagePlugin(),
MyDocumentPlugin(),
InitPlugin(),
InitPlugin(),
MyInitPlugin(),
MyInitPlugin(),
None,
MyDocumentPlugin(),
DocumentPlugin(),
MessagePlugin(),
DocumentPlugin(),
MessagePlugin(),
DocumentPlugin(),
InitPlugin(),
MessagePlugin(),
object(),
DocumentPlugin(),
MessagePlugin(),
object(),
InitPlugin(),
Plugin(),
Plugin(),
MyInitPlugin()]
container = PluginContainer(plugins)
expected_plugins = [p for p in plugins if isinstance(p, plugin_class)]
result = getattr(container, domain).plugins
assert result == expected_plugins
def test_exception_passing():
class FailingPluginException(Exception):
pass
class FailingPlugin(MessagePlugin):
def marshalled(self, context):
raise FailingPluginException
container = PluginContainer([FailingPlugin()])
pytest.raises(FailingPluginException, container.message.marshalled)
def test_invalid_plugin_domain():
container = PluginContainer([])
domain = "invalid_domain_name"
e = pytest.raises(Exception, getattr, container, domain)
try:
e = e.value
assert e.__class__ is Exception
assert str(e) == "plugin domain (%s), invalid" % (domain,)
finally:
del e # explicitly break circular reference chain in Python 3
|