File: test_tsd_client.py

package info (click to toggle)
python-stone 3.3.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,956 kB
  • sloc: python: 21,786; objc: 498; sh: 29; makefile: 11
file content (170 lines) | stat: -rw-r--r-- 5,775 bytes parent folder | download
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import textwrap
import unittest

from stone.backends.tsd_client import TSDClientBackend
from stone.ir import Api, ApiNamespace, ApiRoute, Void, Int32
from stone.ir.data_types import Struct

MYPY = False
if MYPY:
    import typing  # noqa: F401 # pylint: disable=import-error,unused-import,useless-suppression


class TestGeneratedTSDClient(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def _get_api(self):
        # type () -> Api
        api = Api(version='0.1b1')
        api.route_schema = Struct('Route', 'stone_cfg', None)
        route1 = ApiRoute('get_metadata', 1, None)
        route1.set_attributes(None, ':route:`get_metadata`', Void(), Void(), Void(), {
            'scope': 'events.read'
        })
        route2 = ApiRoute('get_metadata', 2, None)
        route2.set_attributes(None, ':route:`get_metadata:2`', Void(), Int32(), Void(), {
            'scope': 'events.read'
        })
        route3 = ApiRoute('get_metadata', 3, None)
        route3.set_attributes(None, ':route:`get_metadata:3`', Int32(), Int32(), Void(), {
            'scope': None
        })
        ns = ApiNamespace('files')
        ns.add_route(route1)
        ns.add_route(route2)
        ns.add_route(route3)
        api.namespaces[ns.name] = ns
        return api, ns

    def test__generate_types_single_ns(self):
        # type: () -> None
        api, _ = self._get_api()
        backend = TSDClientBackend(
            target_folder_path="output",
            args=['files', 'files']
        )
        backend._generate_routes(api, 0, 0)
        result = backend.output_buffer_to_string()
        expected = textwrap.dedent(
            '''\

            /**
             * getMetadata()
             *
             * When an error occurs, the route rejects the promise with type Error<void>.
             */
            public filesGetMetadata(): Promise<void>;

            /**
             * getMetadataV2()
             *
             * When an error occurs, the route rejects the promise with type Error<void>.
             */
            public filesGetMetadataV2(): Promise<number>;

            /**
             * getMetadataV3()
             *
             * When an error occurs, the route rejects the promise with type Error<void>.
             * @param arg The request parameters.
             */
            public filesGetMetadataV3(arg: number): Promise<number>;
            ''')
        self.assertEqual(result, expected)

    def test__generate_types_with_wrap_response_flag(self):
        # type: () -> None
        api, _ = self._get_api()
        backend = TSDClientBackend(
            target_folder_path="output",
            args=['files', 'files', '--wrap-response-in', 'DropboxResponse']
        )
        backend._generate_routes(api, 0, 0)
        result = backend.output_buffer_to_string()
        expected = textwrap.dedent(
            '''\

            /**
             * getMetadata()
             *
             * When an error occurs, the route rejects the promise with type Error<void>.
             */
            public filesGetMetadata(): Promise<DropboxResponse<void>>;

            /**
             * getMetadataV2()
             *
             * When an error occurs, the route rejects the promise with type Error<void>.
             */
            public filesGetMetadataV2(): Promise<DropboxResponse<number>>;

            /**
             * getMetadataV3()
             *
             * When an error occurs, the route rejects the promise with type Error<void>.
             * @param arg The request parameters.
             */
            public filesGetMetadataV3(arg: number): Promise<DropboxResponse<number>>;
            ''')
        self.assertEqual(result, expected)

    def test_route_with_version_number_conflict(self):
        # type: () -> None
        api, ns = self._get_api()

        # Add a conflicting route
        route3 = ApiRoute('get_metadata_v2', 1, None)
        route3.set_attributes(None, None, Void(), Int32(), Void(), {})
        ns.add_route(route3)

        backend = TSDClientBackend(
            target_folder_path="output",
            args=['files', 'files']
        )
        with self.assertRaises(RuntimeError) as cm:
            backend._generate_routes(api, 0, 0)
        self.assertTrue(str(cm.exception).startswith(
            'There is a name conflict between'))

    def test_route_with_attributes_in_docstring(self):
        # type: () -> None
        api, _ = self._get_api()
        backend = TSDClientBackend(
            target_folder_path="output",
            args=['files', 'files', '-a', 'scope']
        )
        backend._generate_routes(api, 0, 0)
        result = backend.output_buffer_to_string()
        expected = textwrap.dedent(
            '''\

            /**
             * getMetadata()
             *
             * Route attributes:
             *   scope: events.read
             *
             * When an error occurs, the route rejects the promise with type Error<void>.
             */
            public filesGetMetadata(): Promise<void>;

            /**
             * getMetadataV2()
             *
             * Route attributes:
             *   scope: events.read
             *
             * When an error occurs, the route rejects the promise with type Error<void>.
             */
            public filesGetMetadataV2(): Promise<number>;

            /**
             * getMetadataV3()
             *
             * When an error occurs, the route rejects the promise with type Error<void>.
             * @param arg The request parameters.
             */
            public filesGetMetadataV3(arg: number): Promise<number>;
            ''')
        self.assertEqual(result, expected)