1# -*- coding: utf-8 -*-
2
3# This program is free software; you can redistribute it and/or modify it under
4# the terms of the (LGPL) GNU Lesser General Public License as published by the
5# Free Software Foundation; either version 3 of the License, or (at your
6# option) any later version.
7#
8# This program is distributed in the hope that it will be useful, but WITHOUT
9# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10# FOR A PARTICULAR PURPOSE. See the GNU Library Lesser General Public License
11# for more details at ( http://www.gnu.org/licenses/lgpl.html ).
12#
13# You should have received a copy of the GNU Lesser General Public License
14# along with this program; if not, write to the Free Software Foundation, Inc.,
15# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16# written by: Jurko Gospodnetić ( jurko.gospodnetic@pke.hr )
17
18"""
19Suds MX system unit tests.
20
21Implemented using the 'pytest' testing framework.
22
23"""
24
25if __name__ == "__main__":
26    import testutils
27    testutils.run_using_pytest(globals())
28
29from suds.mx.typer import Typer
30
31import pytest
32
33
34def _prefix(i):
35    """Prefixes expected to be constructed by Typer.getprefix()."""
36    return "ns%d" % (i,)
37
38
39class TestTyper:
40
41    class Test_genprefix:
42
43        class MockNode:
44
45            def __init__(self, namespace_mapping):
46                self.mock_call_params = []
47                self.__namespace_mapping = namespace_mapping
48
49            def resolvePrefix(self, prefix, default):
50                self.mock_call_params.append((prefix, default))
51                return self.__namespace_mapping.get(prefix, default)
52
53        def test_no_mapped_prefixes(self):
54            node = self.__class__.MockNode({})
55            test_namespace = "test namespace"
56            result = Typer.genprefix(node, ("unused-prefix", test_namespace))
57            assert result == (_prefix(1), test_namespace)
58            assert node.mock_call_params == [(_prefix(1), None)]
59
60        def test_several_already_mapped_prefixes(self):
61            test_namespace = "test namespace"
62            node = self.__class__.MockNode({
63                _prefix(1): "another namespace",
64                _prefix(2): "another namespace"})
65            result = Typer.genprefix(node, ("unused-prefix", test_namespace))
66            assert result == (_prefix(3), test_namespace)
67            assert node.mock_call_params == [
68                (_prefix(i), None) for i in [1, 2, 3]]
69
70        def test_last_free_namespace(self):
71            test_namespace = "test namespace"
72            node = self.__class__.MockNode(dict(
73                (_prefix(i), "another namespace")
74                for i in range(1, 1023)))
75            result = Typer.genprefix(node, ("unused-prefix", test_namespace))
76            assert result == (_prefix(1023), test_namespace)
77            expected_calls = [(_prefix(i), None) for i in range(1, 1024)]
78            assert node.mock_call_params == expected_calls
79
80        def test_no_free_namespace(self):
81            test_namespace = "test namespace"
82            node = self.__class__.MockNode(dict(
83                (_prefix(i), "another namespace") for i in range(1, 1024)))
84            e = pytest.raises(Exception,
85                Typer.genprefix, node, ("unused-prefix", test_namespace)).value
86            try:
87                assert str(e) == "auto prefix, exhausted"
88            finally:
89                del e  # explicitly break circular reference chain in Python 3
90            expected_calls = [(_prefix(i), None) for i in range(1, 1024)]
91            assert node.mock_call_params == expected_calls
92
93        def test_already_mapped_namespace_with_no_unused_prefix_before(self):
94            test_prefix_index = 2
95            test_namespace = "test namespace"
96            node = self.__class__.MockNode({
97                _prefix(1): "another namespace",
98                _prefix(test_prefix_index): test_namespace,
99                _prefix(3): "another namespace"})
100            result = Typer.genprefix(node, ("unused-prefix", test_namespace))
101            assert result == (_prefix(test_prefix_index), test_namespace)
102            expected_calls = [(_prefix(i), None)
103                for i in range(1, test_prefix_index + 1)]
104            assert node.mock_call_params == expected_calls
105
106        def test_already_mapped_namespace_with_unused_prefix_before(self):
107            unused_prefix_index = 2
108            test_namespace = "test namespace"
109            node = self.__class__.MockNode({
110                _prefix(1): "another namespace",
111                _prefix(3): test_namespace,
112                _prefix(4): "another namespace"})
113            result = Typer.genprefix(node, ("unused-prefix", test_namespace))
114            assert result == (_prefix(unused_prefix_index), test_namespace)
115            expected_calls = [(_prefix(i), None)
116                for i in range(1, unused_prefix_index + 1)]
117            assert node.mock_call_params == expected_calls
118