Debian Codegraph @

tests/functional/docs/__init__.py

raw
1 # Copyright 2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"). You
4 # may not use this file except in compliance with the License. A copy of
5 # the License is located at
6 #
7 # https://aws.amazon.com/apache2.0/
8 #
9 # or in the "license" file accompanying this file. This file is
10 # distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11 # ANY KIND, either express or implied. See the License for the specific
12 # language governing permissions and limitations under the License.
13 import os
14 import shutil
15 import tempfile
16
17 from tests import unittest
18
19
20 class BaseDocsFunctionalTests(unittest.TestCase):
21 def setUp(self):
22 self.docs_root_dir = tempfile.mkdtemp()
23 self.root_services_path = os.path.join(
24 self.docs_root_dir, 'reference', 'services'
25 )
26
27 def tearDown(self):
28 shutil.rmtree(self.docs_root_dir)
29
30 def assert_contains_lines_in_order(self, lines, contents):
31 for line in lines:
32 assert line in contents
33 beginning = contents.find(line)
34 contents = contents[(beginning + len(line)) :]
35
36 def get_class_document_block(self, class_name, contents):
37 start_class_document = f'.. py:class:: {class_name}'
38 start_index = contents.find(start_class_document)
39 assert start_index != -1, 'Class is not found in contents'
40 contents = contents[start_index:]
41 end_index = contents.find(' .. py:class::', len(start_class_document))
42 return contents[:end_index]
43
44 def get_method_document_block(self, method_name, contents):
45 start_method_document = f' .. py:method:: {method_name}('
46 start_index = contents.find(start_method_document)
47 assert start_index != -1, 'Method is not found in contents'
48 contents = contents[start_index:]
49 end_index = contents.find(
50 ' .. py:method::', len(start_method_document)
51 )
52 return contents[:end_index]
53
54 def get_request_syntax_document_block(self, contents):
55 start_marker = '**Request Syntax**'
56 start_index = contents.find(start_marker)
57 assert start_index != -1, 'There is no request syntax section'
58 contents = contents[start_index:]
59 end_index = contents.find(':type', len(start_marker))
60 return contents[:end_index]
61
62 def get_response_syntax_document_block(self, contents):
63 start_marker = '**Response Syntax**'
64 start_index = contents.find(start_marker)
65 assert start_index != -1, 'There is no response syntax section'
66 contents = contents[start_index:]
67 end_index = contents.find('**Response Structure**', len(start_marker))
68 return contents[:end_index]
69
70 def get_request_parameter_document_block(self, param_name, contents):
71 start_param_document = f':type {param_name}:'
72 start_index = contents.find(start_param_document)
73 assert start_index != -1, 'Param is not found in contents'
74 contents = contents[start_index:]
75 end_index = contents.find(':type', len(start_param_document))
76 return contents[:end_index]
77
78 def get_response_parameter_document_block(self, param_name, contents):
79 start_param_document = '**Response Structure**'
80 start_index = contents.find(start_param_document)
81 assert start_index != -1, 'There is no response structure'
82
83 start_param_document = f'- **{param_name}**'
84 start_index = contents.find(start_param_document)
85 assert start_index != -1, 'Param is not found in contents'
86 contents = contents[start_index:]
87 end_index = contents.find('- **', len(start_param_document))
88 return contents[:end_index]
89
90 def get_nested_file_contents(self, service_name, sub_folder, file_name):
91 service_file_path = os.path.join(
92 self.root_services_path,
93 service_name,
94 sub_folder,
95 f'{file_name}.rst',
96 )
97 with open(service_file_path, 'rb') as f:
98 return f.read().decode('utf-8')