File: test_dispatcher.py

package info (click to toggle)
python-jsonrpc 1.13.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 484 kB
  • sloc: python: 3,173; makefile: 155
file content (142 lines) | stat: -rw-r--r-- 3,088 bytes parent folder | download | duplicates (4)
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
134
135
136
137
138
139
140
141
142
from ..dispatcher import Dispatcher
import sys
if sys.version_info < (2, 7):
    import unittest2 as unittest
else:
    import unittest


class Math:

    def sum(self, a, b):
        return a + b

    def diff(self, a, b):
        return a - b


class TestDispatcher(unittest.TestCase):

    """ Test Dispatcher functionality."""

    def test_getter(self):
        d = Dispatcher()

        with self.assertRaises(KeyError):
            d["method"]

        d["add"] = lambda *args: sum(args)
        self.assertEqual(d["add"](1, 1), 2)

    def test_in(self):
        d = Dispatcher()
        d["method"] = lambda: ""
        self.assertIn("method", d)

    def test_add_method(self):
        d = Dispatcher()

        @d.add_method
        def add(x, y):
            return x + y

        self.assertIn("add", d)
        self.assertEqual(d["add"](1, 1), 2)

    def test_add_method_with_name(self):
        d = Dispatcher()

        @d.add_method(name="this.add")
        def add(x, y):
            return x + y

        self.assertNotIn("add", d)
        self.assertIn("this.add", d)
        self.assertEqual(d["this.add"](1, 1), 2)

    def test_add_class(self):
        d = Dispatcher()
        d.add_class(Math)

        self.assertIn("math.sum", d)
        self.assertIn("math.diff", d)
        self.assertEqual(d["math.sum"](3, 8), 11)
        self.assertEqual(d["math.diff"](6, 9), -3)

    def test_add_object(self):
        d = Dispatcher()
        d.add_object(Math())

        self.assertIn("math.sum", d)
        self.assertIn("math.diff", d)
        self.assertEqual(d["math.sum"](5, 2), 7)
        self.assertEqual(d["math.diff"](15, 9), 6)

    def test_add_dict(self):
        d = Dispatcher()
        d.add_dict({"sum": lambda *args: sum(args)}, "util")

        self.assertIn("util.sum", d)
        self.assertEqual(d["util.sum"](13, -2), 11)

    def test_add_method_keep_function_definitions(self):

        d = Dispatcher()

        @d.add_method
        def one(x):
            return x

        self.assertIsNotNone(one)

    def test_del_method(self):
        d = Dispatcher()
        d["method"] = lambda: ""
        self.assertIn("method", d)

        del d["method"]
        self.assertNotIn("method", d)

    def test_to_dict(self):
        d = Dispatcher()

        def func():
            return ""

        d["method"] = func
        self.assertEqual(dict(d), {"method": func})

    def test_init_from_object_instance(self):

        class Dummy():

            def one(self):
                pass

            def two(self):
                pass

        dummy = Dummy()

        d = Dispatcher(dummy)

        self.assertIn("one", d)
        self.assertIn("two", d)
        self.assertNotIn("__class__", d)

    def test_init_from_dictionary(self):

        dummy = {
            'one': lambda x: x,
            'two': lambda x: x,
        }

        d = Dispatcher(dummy)

        self.assertIn("one", d)
        self.assertIn("two", d)

    def test_dispatcher_representation(self):

        d = Dispatcher()
        self.assertEqual('{}', repr(d))