1# Copyright (C) 2022 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15import unittest 16import os 17import sys 18 19ROOT_DIR = os.path.dirname( 20 os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 21sys.path.append(os.path.join(ROOT_DIR)) 22 23from generators.stdlib_docs.stdlib import FunctionDocs, ViewFunctionDocs, TableViewDocs 24 25DESC = """-- 26-- First line. 27-- Second line.""" 28 29COLS_STR = """-- 30-- @column slice_id Id of slice. 31-- @column slice_name Name of slice.""" 32 33COLS_SQL_STR = "slice_id INT, slice_name STRING" 34 35ARGS_STR = """-- 36-- @arg utid INT Utid of thread. 37-- @arg name STRING String name. 38""" 39 40ARGS_SQL_STR = "utid INT, name STRING" 41 42RET_STR = """-- 43-- @ret BOOL Exists. 44""" 45 46RET_SQL_STR = "BOOL" 47 48SQL_STR = "SELECT * FROM slice" 49 50 51class TestStdlib(unittest.TestCase): 52 53 # Valid schemas 54 55 def test_valid_table(self): 56 valid_table_comment = f"{DESC}\n{COLS_STR}".split('\n') 57 58 docs, create_errors = TableViewDocs.create_from_comment( 59 "", valid_table_comment, 'common', ('table', 'tab_name', 'to_ignore')) 60 self.assertFalse(create_errors) 61 62 validation_errors = docs.check_comment() 63 self.assertFalse(validation_errors) 64 65 def test_valid_function(self): 66 valid_function = f"{DESC}\n{ARGS_STR}\n{RET_STR}".split('\n') 67 valid_regex_matches = ('fun_name', ARGS_SQL_STR, RET_SQL_STR, SQL_STR) 68 docs, create_errors = FunctionDocs.create_from_comment( 69 "", valid_function, 'common', valid_regex_matches) 70 self.assertFalse(create_errors) 71 72 validation_errors = docs.check_comment() 73 self.assertFalse(validation_errors) 74 75 def test_valid_view_function(self): 76 valid_view_function = f"{DESC}\n{ARGS_STR}\n{COLS_STR}".split('\n') 77 valid_regex_matches = ('fun_name', ARGS_SQL_STR, COLS_SQL_STR, SQL_STR) 78 docs, create_errors = ViewFunctionDocs.create_from_comment( 79 "", valid_view_function, 'common', valid_regex_matches) 80 self.assertFalse(create_errors) 81 82 validation_errors = docs.check_comment() 83 self.assertFalse(validation_errors) 84 85 # Missing modules in names 86 87 def test_missing_module_in_table_name(self): 88 valid_table_comment = f"{DESC}\n{COLS_STR}".split('\n') 89 90 _, create_errors = TableViewDocs.create_from_comment( 91 "", valid_table_comment, 'android', ('table', 'tab_name', 'to_ignore')) 92 self.assertTrue(create_errors) 93 94 def test_missing_module_in_function_name(self): 95 valid_function = f"{DESC}\n{ARGS_STR}\n{RET_STR}".split('\n') 96 valid_regex_matches = ('fun_name', ARGS_SQL_STR, RET_SQL_STR, SQL_STR) 97 _, create_errors = FunctionDocs.create_from_comment("", valid_function, 98 'android', 99 valid_regex_matches) 100 self.assertTrue(create_errors) 101 102 def test_missing_module_in_view_function_name(self): 103 valid_view_function = f"{DESC}\n{ARGS_STR}\n{COLS_STR}".split('\n') 104 valid_regex_matches = ('fun_name', ARGS_SQL_STR, COLS_SQL_STR, SQL_STR) 105 _, create_errors = ViewFunctionDocs.create_from_comment( 106 "", valid_view_function, 'android', valid_regex_matches) 107 self.assertTrue(create_errors) 108 109 # Missing part of schemas 110 111 def test_missing_desc_in_table_name(self): 112 comment = f"{COLS_STR}".split('\n') 113 114 _, create_errors = TableViewDocs.create_from_comment( 115 "", comment, 'common', ('table', 'tab_name', 'to_ignore')) 116 self.assertTrue(create_errors) 117 118 def test_missing_cols_in_table(self): 119 comment = f"{DESC}".split('\n') 120 121 _, create_errors = TableViewDocs.create_from_comment( 122 "", comment, 'common', ('table', 'tab_name', 'to_ignore')) 123 self.assertTrue(create_errors) 124 125 def test_missing_desc_in_function(self): 126 comment = f"{ARGS_STR}\n{RET_STR}".split('\n') 127 valid_regex_matches = ('fun_name', ARGS_SQL_STR, RET_SQL_STR, SQL_STR) 128 _, create_errors = FunctionDocs.create_from_comment("", comment, 'common', 129 valid_regex_matches) 130 self.assertTrue(create_errors) 131 132 def test_missing_args_in_function(self): 133 comment = f"{DESC}\n{RET_STR}".split('\n') 134 valid_regex_matches = ('fun_name', ARGS_SQL_STR, RET_SQL_STR, SQL_STR) 135 _, create_errors = FunctionDocs.create_from_comment("", comment, 'common', 136 valid_regex_matches) 137 self.assertTrue(create_errors) 138 139 def test_missing_ret_in_function(self): 140 comment = f"{DESC}\n{ARGS_STR}".split('\n') 141 valid_regex_matches = ('fun_name', ARGS_SQL_STR, RET_SQL_STR, SQL_STR) 142 _, create_errors = FunctionDocs.create_from_comment("", comment, 'common', 143 valid_regex_matches) 144 self.assertTrue(create_errors) 145 146 def test_missing_desc_in_view_function(self): 147 comment = f"{ARGS_STR}\n{COLS_STR}".split('\n') 148 valid_regex_matches = ('fun_name', ARGS_SQL_STR, COLS_SQL_STR, SQL_STR) 149 _, create_errors = ViewFunctionDocs.create_from_comment( 150 "", comment, 'common', valid_regex_matches) 151 self.assertTrue(create_errors) 152 153 def test_missing_args_in_view_function(self): 154 comment = f"{DESC}\n{COLS_STR}".split('\n') 155 valid_regex_matches = ('fun_name', ARGS_SQL_STR, COLS_SQL_STR, SQL_STR) 156 _, create_errors = ViewFunctionDocs.create_from_comment( 157 "", comment, 'common', valid_regex_matches) 158 self.assertTrue(create_errors) 159 160 def test_missing_cols_in_view_function(self): 161 comment = f"{DESC}\n{ARGS_STR}".split('\n') 162 valid_regex_matches = ('fun_name', ARGS_SQL_STR, COLS_SQL_STR, SQL_STR) 163 _, create_errors = ViewFunctionDocs.create_from_comment( 164 "", comment, 'common', valid_regex_matches) 165 self.assertTrue(create_errors) 166 167 # Validate elements 168 169 def test_invalid_table_columns(self): 170 invalid_cols = "-- @column slice_id" 171 comment = f"{DESC}\n{invalid_cols}".split('\n') 172 173 docs, create_errors = TableViewDocs.create_from_comment( 174 "", comment, 'common', ('table', 'tab_name', 'to_ignore')) 175 self.assertFalse(create_errors) 176 177 validation_errors = docs.check_comment() 178 self.assertTrue(validation_errors) 179 180 def test_invalid_view_function_columns(self): 181 comment = f"{DESC}\n{ARGS_STR}\n{COLS_STR}".split('\n') 182 cols_sql_str = "slice_id INT" 183 valid_regex_matches = ('fun_name', ARGS_SQL_STR, cols_sql_str, SQL_STR) 184 docs, create_errors = ViewFunctionDocs.create_from_comment( 185 "", comment, 'common', valid_regex_matches) 186 self.assertFalse(create_errors) 187 188 validation_errors = docs.check_comment() 189 self.assertTrue(validation_errors) 190 191 def test_invalid_arguments(self): 192 valid_function = f"{DESC}\n{ARGS_STR}\n{RET_STR}".split('\n') 193 args_sql_str = "utid BOOL" 194 valid_regex_matches = ('fun_name', args_sql_str, RET_SQL_STR, SQL_STR) 195 docs, create_errors = FunctionDocs.create_from_comment( 196 "", valid_function, 'common', valid_regex_matches) 197 self.assertFalse(create_errors) 198 199 validation_errors = docs.check_comment() 200 self.assertTrue(validation_errors) 201 202 def test_invalid_ret(self): 203 valid_function = f"{DESC}\n{ARGS_STR}\n{RET_STR}".split('\n') 204 ret_sql_str = "utid BOOL" 205 valid_regex_matches = ('fun_name', ARGS_SQL_STR, ret_sql_str, SQL_STR) 206 docs, create_errors = FunctionDocs.create_from_comment( 207 "", valid_function, 'common', valid_regex_matches) 208 self.assertFalse(create_errors) 209 210 validation_errors = docs.check_comment() 211 self.assertTrue(validation_errors) 212