def test_create_api_call_args(self): html = '<html><body>' \ '<h3 id="url"><code>/api/v2/hello</code></h3>' \ '<h3 id="method"><code>hello</code></h3>' \ '<h3 id="url-parameters">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '</tbody></h3>' \ '<h3 id="form-parameters">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '</tbody></h3>' \ '<h3 id="query-parameters">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '</tbody></h3></body></html>' expect_api_call_args = "'/hello'.format(hello, bye), method='hello'," \ " query_parameters=query_parameters," \ " form_parameters=form_parameters" api = api_generator.APIGenerator(get_bs(html)) self.assertEqual(api._create_api_call_args(), expect_api_call_args) html = '<html><body>' \ '<h3 id="url"><code>/api/v2/hello</code></h3>' \ '<h3 id="method"><code>hello</code></h3>' \ '</h3></body></html>' expect_api_call_args = "'/hello', method='hello'" api = api_generator.APIGenerator(get_bs(html)) self.assertEqual(api._create_api_call_args(), expect_api_call_args)
def test___lt__(self): html_1 = '<html><body>' \ '<h1 id="test_api_name_abc" >hello</h1>' \ '<html><body><h2><p>hello</p></h2></body></html>' \ '<h3 id="url"><code>/api/v2/hello</code></h3>' \ '<h3 id="method"><code>hello</code></h3>' \ '<h3 id="query-parameters">' \ '<tbody><tr><td>hello</td><td>String</td></tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '<tr><td>num</td><td>Number</td><td>description</td></tr>' \ '<tr><td>boolean</td><td>Boolean</td></tr>' \ '</tbody></h3></body></html>' html_2 = '<html><body>' \ '<h1 id="test_api_name_bcd" >hello</h1>' \ '<html><body><h2><p>hello</p></h2></body></html>' \ '<h3 id="url"><code>/api/v2/hello</code></h3>' \ '<h3 id="method"><code>hello</code></h3>' \ '<h3 id="query-parameters">' \ '<tbody><tr><td>hello</td><td>String</td></tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '<tr><td>num</td><td>Number</td><td>description</td></tr>' \ '<tr><td>boolean</td><td>Boolean</td></tr>' \ '</tbody></h3></body></html>' api_1 = api_generator.APIGenerator(get_bs(html_1)) api_2 = api_generator.APIGenerator(get_bs(html_2)) self.assertTrue(api_1 < api_2)
def test_has_strict_method(self): html = '<html><body></body></html>' api = api_generator.APIGenerator(get_bs(html)) self.assertFalse(api.has_strict_method) html = '<html><body><h3 id="query-parameters">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '</tbody></h3></body></html>' api = api_generator.APIGenerator(get_bs(html)) self.assertTrue(api.has_strict_method) html = '<html><body><h3 id="form-parameters">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '</tbody></h3></body></html>' api = api_generator.APIGenerator(get_bs(html)) self.assertTrue(api.has_strict_method) html = '<html><body><h3 id="form-parameters">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '</tbody></h3>' \ '<h3 id="query-parameters">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '</tbody></h3></body></html>' api = api_generator.APIGenerator(get_bs(html)) self.assertTrue(api.has_strict_method)
def test_api_path(self): html = '<html><body><h3 id="url"><code>/api/v2/hello</code></h3></body></html>' api = api_generator.APIGenerator(get_bs(html)) self.assertEqual(api.api_path, '/api/v2/hello') self.assertEqual(api.api_path, '/api/v2/hello') html = '<html><body><h3><p>hello</p></h3></body></html>' api = api_generator.APIGenerator(get_bs(html)) with self.assertRaises(Exception): _ = api.api_path
def test_method_type(self): html = '<html><body><h3 id="method"><code>hello</code></h3></body></html>' api = api_generator.APIGenerator(get_bs(html)) self.assertEqual(api.method_type, 'hello') self.assertEqual(api.method_type, 'hello') html = '<html><body><h3><p>hello</p></h3></body></html>' api = api_generator.APIGenerator(get_bs(html)) with self.assertRaises(Exception): _ = api.method_type
def test_get_parameters(self): html = '<html><body><h3 id="param">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>bye</td><td>String</td></tr></tbody></h3></body></html>' api = api_generator.APIGenerator(get_bs(html)) parameters = api._get_parameters('param') self.assertEqual(parameters[0].name, 'hello') self.assertEqual(parameters[0].type.doc, 'str') self.assertEqual(parameters[1].name, 'bye') self.assertEqual(parameters[1].type.doc, 'str') html = '<html><body></body></html>' api = api_generator.APIGenerator(get_bs(html)) self.assertEqual(api.url_parameters, [])
def test_create_parameter_assignment(self): html = '<html><body>' \ '<h2><p>hello</p></h2></body>' \ '<h3 id="url"><code>/api/v2/hello</code></h3>' \ '<h3 id="method"><code>hello</code></h3>' \ '<h3 id="form-parameters">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '</tbody></h3>' \ '<h3 id="query-parameters">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '</tbody></h3></body></html>' expect_parameter_assignment = '''query_parameters = { 'hello': hello, 'bye': bye } form_parameters = { 'hello': hello, 'bye': bye }''' api = api_generator.APIGenerator(get_bs(html)) self.assertEqual(api._create_parameter_assignment(), expect_parameter_assignment)
def test_api_description(self): html = '<html><body><h2><p>hello</p></h2></body></html>' api = api_generator.APIGenerator(get_bs(html)) self.assertEqual(api.api_description, 'hello') self.assertEqual(api.api_description, 'hello') self.assertFalse(api.deprecated) self.assertEqual(api.modules, set()) html = '<html><body><h2><span>hello<span/></h2></body></html>' api = api_generator.APIGenerator(get_bs(html)) with self.assertRaises(Exception): _ = api.api_description html = '<html><body><h2><p>hello ※ Deprecated API. xxx <strong>Deprecated API<strong><span><a href="https://backlogpy.org"></a></span></p></h2></body></html>' api = api_generator.APIGenerator(get_bs(html)) self.assertEqual(api.api_description, 'hello ※ Deprecated API. xxx Deprecated API ※ Deprecated API. https://backlogpy.org') self.assertTrue(api.deprecated) self.assertEqual(api.modules, {'from deprecated import deprecated'})
def test_query_parameters(self): html = '<html><body><h3 id="query-parameters">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '</tbody></h3></body></html>' api = api_generator.APIGenerator(get_bs(html)) self.assertEqual(api.query_parameters[0].name, 'hello') self.assertEqual(api.query_parameters[0].type.doc, 'str') self.assertEqual(api.query_parameters[1].name, 'bye') self.assertEqual(api.query_parameters[1].type.doc, 'str')
def test_create_method_doc(self): html = '<html><body>' \ '<h2><p>hello</p></h2></body>' \ '<h3 id="url"><code>/api/v2/hello</code></h3>' \ '<h3 id="method"><code>hello</code></h3>' \ '<h3 id="url-parameters">' \ '<tbody><tr><td>hello</td><td>String</td></tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '<tr><td>num</td><td>Number</td><td>description</td></tr>' \ '</tbody></h3></body></html>' expect_method_doc = ''' hello ''' api = api_generator.APIGenerator(get_bs(html)) self.assertEqual(api._create_method_doc(), expect_method_doc)
def test_create_api_class(self): html = '<html><body><h3 id="url"><code>/api/v2/hello</code></h3></body></html>' expect_api_class = '''# coding: utf-8 """ This file was created by Backlog APIGenerator """ from __future__ import unicode_literals, absolute_import {module_import} from BacklogPy.base import BacklogBase, SUFFIX_JP class Hello(BacklogBase): def __init__(self, space_id, api_key, suffix=SUFFIX_JP): super(Hello, self).__init__(space_id, api_key, suffix=suffix) ''' api = api_generator.APIGenerator(get_bs(html)) print(f'"{api.create_api_class()}"') self.assertEqual(api.create_api_class(), expect_api_class)
def test_create_api_method_with_broken_header(self): html = '<html><body>' \ '<h1 id="test_api_name" >hello</h1>' \ '<html><body><h2><p>hello</p></h2></body></html>' \ '<h3 id="url"><code>/api/v2/hello</code></h3>' \ '<h3 id="method"><code>hello</code></h3>' \ '<h3 id="query-parameters">' \ '<thead><tr><th>header_hello</th><th>String</th><th>header description</th><tr></thead>' \ '<tbody><tr><td>hello</td><td>String</td></tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '<tr><td>num</td><td>Number</td><td>description</td></tr>' \ '<tr><td>boolean</td><td>Boolean</td></tr>' \ '</tbody></h3></body></html>' expect_api_method = ''' def test_api_name_raw(self, query_parameters): """ hello :param dict query_parameters: query_parameters :return: requests Response object :rtype: requests.Response """ return self._request('/hello', method='hello', query_parameters=query_parameters) ''' api = api_generator.APIGenerator(get_bs(html)) self.assertEqual(api.create_api_method(), expect_api_method) expect_api_method = ''' def test_api_name(self, header_hello=None, hello=None, bye=None, num=None, boolean=None): """ hello :param str or None header_hello: header description :param str or None hello: hello :param str or None bye: bye :param int or None num: description :param bool or None boolean: boolean :return: requests Response object :rtype: requests.Response """ query_parameters = { 'header_hello': header_hello, 'hello': hello, 'bye': bye, 'num': num, 'boolean': self._bool_to_str(boolean) } return self._request('/hello', method='hello', query_parameters=query_parameters) ''' api = api_generator.APIGenerator(get_bs(html)) self.assertEqual(api.create_api_method(strict=True), expect_api_method)
def test_create_api_method(self): html = '<html><body>' \ '<h1 id="test_api_name" >hello</h1>' \ '<html><body><h2><p>hello</p></h2></body></html>' \ '<h3 id="url"><code>/api/v2/hello</code></h3>' \ '<h3 id="method"><code>hello</code></h3>' \ '<h3 id="query-parameters">' \ '<tbody><tr><td>hello</td><td>String</td></tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '<tr><td>num</td><td>Number</td><td>description</td></tr>' \ '<tr><td>boolean</td><td>Boolean</td></tr>' \ '</tbody></h3></body></html>' expect_api_method = ''' def test_api_name_raw(self, query_parameters): """ hello :param dict query_parameters: query_parameters :return: requests Response object :rtype: requests.Response """ return self._request('/hello', method='hello', query_parameters=query_parameters) ''' api = api_generator.APIGenerator(get_bs(html)) self.assertEqual(api.create_api_method(), expect_api_method) expect_api_method = ''' def test_api_name(self, hello=None, bye=None, num=None, boolean=None): """ hello :param str or None hello: hello :param str or None bye: bye :param int or None num: description :param bool or None boolean: boolean :return: requests Response object :rtype: requests.Response """ query_parameters = { 'hello': hello, 'bye': bye, 'num': num, 'boolean': self._bool_to_str(boolean) } return self._request('/hello', method='hello', query_parameters=query_parameters) ''' api = api_generator.APIGenerator(get_bs(html)) self.assertEqual(api.create_api_method(strict=True), expect_api_method) html = '<html><body>' \ '<h1 id="test_api_name" >hello</h1>' \ '<strong>Deprecated API</strong><span>xxx <a href="https://backlogpy.org"></a></span>'\ '<html><body><h2><p>hello xxx. <a href="https://backlogpy.org"></a></p></h2></html>' \ '<h3 id="url"><code>/api/v2/hello</code></h3>' \ '<h3 id="method"><code>hello</code></h3>' \ '<h3 id="query-parameters">' \ '<tbody><tr><td>hello</td><td>String</td></tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '<tr><td>num</td><td>Number</td><td>description</td></tr>' \ '<tr><td>boolean</td><td>Boolean</td></tr>' \ '</tbody></h3></body></html>' expect_api_method = ''' @deprecated(reason="xxx https://backlogpy.org") def test_api_name_raw(self, query_parameters): """ hello xxx. ※ Deprecated API. https://backlogpy.org :param dict query_parameters: query_parameters :return: requests Response object :rtype: requests.Response """ return self._request('/hello', method='hello', query_parameters=query_parameters) ''' api = api_generator.APIGenerator(get_bs(html)) self.assertEqual(api.create_api_method(), expect_api_method)
def test_api_name(self): html = '<html><body><h1 id="test_api_name" >hello</h1></body></html>' api = api_generator.APIGenerator(get_bs(html)) self.assertEqual(api.api_name, 'test_api_name') self.assertEqual(api.api_name, 'test_api_name')
def test_create_args_doc(self): html = '<html><body>' \ '<h3 id="url"><code>/api/v2/hello</code></h3>' \ '<h3 id="method"><code>hello</code></h3>' \ '<h3 id="url-parameters">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '</tbody></h3>' \ '<h3 id="form-parameters">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '</tbody></h3>' \ '<h3 id="query-parameters">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '</tbody></h3></body></html>' expect_args_doc = '''\n :param str hello: hello :param str bye: bye :param dict query_parameters: query_parameters :param dict form_parameters: form_parameters\n ''' api = api_generator.APIGenerator(get_bs(html)) self.assertEqual(api._create_args_doc(), expect_args_doc) html = '<html><body>' \ '<h3 id="url"><code>/api/v2/hello</code></h3>' \ '<h3 id="method"><code>hello</code></h3>' \ '<h3 id="url-parameters">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '</tbody></h3>' \ '<h3 id="form-parameters">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '</tbody></h3>' \ '<h3 id="query-parameters">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '</tbody></h3></body></html>' expect_args_doc = '''\n :param str hello: hello :param str bye: bye :param str or None hello: hello :param str or None bye: bye :param str or None hello: hello :param str or None bye: bye\n ''' api = api_generator.APIGenerator(get_bs(html)) self.assertEqual(api._create_args_doc(strict=True), expect_args_doc) html = '<html><body>' \ '<h3 id="url"><code>/api/v2/hello</code></h3>' \ '<h3 id="method"><code>hello</code></h3>' \ '</tbody></h3></body></html>' expect_args_doc = "" api = api_generator.APIGenerator(get_bs(html)) self.assertEqual(api._create_args_doc(), expect_args_doc) html = '<html><body>' \ '<h3 id="url"><code>/api/v2/hello</code></h3>' \ '<h3 id="method"><code>hello</code></h3>' \ '<h3 id="url-parameters">' \ '<tbody><tr><td>hello</td><td>String</td></tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '<tr><td>num</td><td>Number</td><td>description</td></tr>' \ '</tbody></h3></body></html>' expect_args_doc = '''\n :param str hello: hello :param str bye: bye :param int num: description\n ''' api = api_generator.APIGenerator(get_bs(html)) self.assertEqual(api._create_args_doc(), expect_args_doc)
def test_create_method_args(self): html = '<html><body>' \ '<h3 id="url"><code>/api/v2/hello</code></h3>' \ '<h3 id="method"><code>hello</code></h3>' \ '<h3 id="url-parameters">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '</tbody></h3>' \ '<h3 id="form-parameters">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '</tbody></h3>' \ '<h3 id="query-parameters">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '</tbody></h3></body></html>' expect_create_method_args = ", hello, bye, query_parameters, form_parameters" api = api_generator.APIGenerator(get_bs(html)) self.assertEqual(api._create_method_args(), expect_create_method_args) html = '<html><body>' \ '<h3 id="url"><code>/api/v2/hello</code></h3>' \ '<h3 id="method"><code>hello</code></h3>' \ '<h3 id="url-parameters">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '</tbody></h3>' \ '<h3 id="form-parameters">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '</tbody></h3>' \ '<h3 id="query-parameters">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '</tbody></h3></body></html>' expect_create_method_args = ", hello, bye, hello=None, bye=None," \ " hello=None, bye=None" api = api_generator.APIGenerator(get_bs(html)) self.assertEqual(api._create_method_args(strict=True), expect_create_method_args) html = '<html><body>' \ '<h3 id="url"><code>/api/v2/hello</code></h3>' \ '<h3 id="method"><code>hello</code></h3>' \ '<h3 id="url-parameters">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '</tbody></h3>' \ '<h3 id="form-parameters">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>bye</td><td>String</td></tr>' \ '</tbody></h3>' \ '<h3 id="query-parameters">' \ '<tbody><tr><td>hello</td><td>String</td><</tr>' \ '<tr><td>goodbye (Required)</td><td>String</td></tr>' \ '</tbody></h3></body></html>' expect_create_method_args = ", hello, bye, goodbye, hello=None," \ " hello=None, bye=None" api = api_generator.APIGenerator(get_bs(html)) self.assertEqual(api._create_method_args(strict=True), expect_create_method_args) html = '<html><body>' \ '<h3 id="url"><code>/api/v2/hello</code></h3>' \ '<h3 id="method"><code>hello</code></h3>' \ '</tbody></h3></body></html>' expect_create_method_args = "" api = api_generator.APIGenerator(get_bs(html)) self.assertEqual(api._create_method_args(), expect_create_method_args)
def test_space_name(self): html = '<html><body><h3 id="url"><code>/api/v2/hello</code></h3></body></html>' api = api_generator.APIGenerator(get_bs(html)) self.assertEqual(api.space_name, 'hello') self.assertEqual(api.space_name, 'hello')