def get_output_text_for_nested_list(self) -> ParamType: # 入力 input = LINE_BREAK.join([ 'address_list = [["Hiroshima-1-1", "Hiroshima-2-2"], ["Tokyo-0-0", "Tokyo-10-0"]]', 'def get_type_def():', ' return [address_list]' ]) # 期待結果組み立て expected = LINE_BREAK.join(['TypeAddressList = List[List[str]]']) return self._get_params(f'{TEST_DATA_DIR}/sequence/nested_list.py', input, expected)
def get_output_text_for_simple_dict(self) -> ParamType: # 入力 input = LINE_BREAK.join([ 'user = {"username": "******"}', 'def get_type_def():', ' return [user]', ]) # 期待結果組み立て expected: str = LINE_BREAK.join([ 'TypeUser = TypedDict("TypeUser", { "username": str })', ]) return self._get_params(f'{TEST_DATA_DIR}/simple_dict.py', input, expected)
def get_output_text_for_simple_tuple(self) -> ParamType: # 入力 input = LINE_BREAK.join([ 'fruits = ("apple", "banana")', 'def get_type_def():', ' return [fruits]', ]) # 期待結果組み立て expected: str = LINE_BREAK.join([ 'TypeFruits = Tuple[str, str]', ]) return self._get_params(f'{TEST_DATA_DIR}/sequence/simple_tuple.py', input, expected)
def get_nested_dict(self) -> DataDictType: source_dict = { 'dog': { 'leg': 4, 'cuteness': 9999, 'has_tail': True, 'owner': { 'name': 'pompom', } }, 'cat': { 'leg': 4, 'dog': None, 'cuteness': 'awesome', }, } expected = LINE_BREAK.join([ 'TypeCat = TypedDict("TypeCat", { "leg": int, "dog": None, "cuteness": str })', 'TypeOwner = TypedDict("TypeOwner", { "name": str })', 'TypeDog = TypedDict("TypeDog", { "leg": int, "cuteness": int, "has_tail": bool, "owner": TypeOwner })', 'TypeAnimalDic = TypedDict("TypeAnimalDic", { "dog": TypeDog, "cat": TypeCat })', '' ]) return self._get_params(source_dict, 'animal_dic', expected)
def get_output_text_for_dictionary_list(self) -> ParamType: # 入力 input = LINE_BREAK.join([ 'users = [{"username": "******", "user_age": 100}, {"username": "******", "user_age": 0}]', 'def get_type_def():', ' return [users]', ]) # 期待結果組み立て expected: str = LINE_BREAK.join([ 'TypeUsernameUserAge = TypedDict("TypeUsernameUserAge", { "username": str, "user_age": int })', 'TypeUsers = List[TypeUsernameUserAge]', ]) return self._get_params(f'{TEST_DATA_DIR}/sequence/dictionary_list.py', input, expected)
def get_only_writing_text(self) -> ParamType: # 期待結果組み立て expected: str = LINE_BREAK.join([ f'{TypeDefCreateLogic.TYPE_DEF_LINE}', ]) expected = f'{TYPE_MODULE_IMPORT_LINE}{LINE_BREAK}{expected}' return self._get_params(expected)
def get_output_text_child(self) -> CommandParamType: # 入力 input = LINE_BREAK.join([ 'fruits = ["apple", "banana"]', 'user = {"name": "pompom", "age": 100}', 'def get_type_def():', ' return [fruits, user]', ]) # 期待結果組み立て expected: str = LINE_BREAK.join([ 'TypeFruits = List[str]', 'TypeUser = TypedDict("TypeUser", { "name": str, "age": int })', ]) return self._get_params(f'test_types/child/child_custom_type.py', input, expected)
def get_output_text_for_tuple_dict(self) -> ParamType: # 入力 input: str = LINE_BREAK.join([ 'user = {"username": "******", "tel": ("000-0000", "123-4567")}', 'characters_dict = {"characters": ({"name": "purin", "has_twitter_acount": False,}, {"name": "kitty", "has_twitter_acount": True,})}', 'def get_type_def():', ' return [user, characters_dict]', ]) # 期待結果組み立て expected: str = LINE_BREAK.join([ 'TypeUser = TypedDict("TypeUser", { "username": str, "tel": Tuple[str, str] })', 'TypeCharacters = TypedDict("TypeCharacters", { "name": str, "has_twitter_acount": bool })', 'TypeCharactersDict = TypedDict("TypeCharactersDict", { "characters": Tuple[TypeCharacters, TypeCharacters] })', ]) return self._get_params(f'{TEST_DATA_DIR}/tuple_dict.py', input, expected)
def get_output_text_excluded(self) -> CommandParamType: # 入力 input = LINE_BREAK.join([ 'fruits = ["apple", "banana"]', 'user = {"name": "pompom", "age": 100}', 'def get_type_def():', ' return [fruits, user]', ]) return self._get_excluded_params(f'test_types/exclude.py', input)
def get_output_text_for_multiple_nested_dict(self) -> ParamType: # 入力 input: str = LINE_BREAK.join([ 'response = { "body": { "message": "success", "status_code": 200}, "has_error": False }', 'user = {"username": "******", "age": 100,} ', 'def get_type_def():', ' return [response, user]', ]) # 期待結果組み立て expected: str = LINE_BREAK.join([ 'TypeBody = TypedDict("TypeBody", { "message": str, "status_code": int })', 'TypeResponse = TypedDict("TypeResponse", { "body": TypeBody, "has_error": bool })', 'TypeUser = TypedDict("TypeUser", { "username": str, "age": int })', ]) return self._get_params(f'{TEST_DATA_DIR}/nested_dict.py', input, expected)
def get_output_text_for_external_module(self) -> ParamType: # 外部モジュールの作成 external_module_text = LINE_BREAK.join( ['user = {"username": "******", "is_authenticated": False}']) self._setup_for_write(external_module_text, f'{TEST_DATA_DIR}/test_module.py') # 入力 input = LINE_BREAK.join([ 'def get_type_def():', ' from .test_module import user', ' return [user]', ]) # 期待結果 expected = LINE_BREAK.join([ 'TypeUser = TypedDict("TypeUser", { "username": str, "is_authenticated": bool })', ]) return self._get_params(f'{TEST_DATA_DIR}/external_module.py', input, expected)
def get_primitive(self) -> ParamType: mock_dict = {'VALUE': 'value'} expected = LINE_BREAK.join([ 'TypeSetting = TypedDict("TypeSetting", { "VALUE": str })', '', 'setting_dict: TypeSetting = {', ' "VALUE": \'value\',', '}', ]) return self._get_param(f'{TEST_DATA_DIR}/primitive.py', mock_dict, expected)
def get_sequence(self) -> ParamType: mock_dict = { 'NAME_LIST': ['pom', 'purin', 'john doe'] } expected = LINE_BREAK.join([ 'TypeSetting = TypedDict("TypeSetting", { "NAME_LIST": List[str] })', '', 'setting_dict: TypeSetting = {', ' "NAME_LIST": [\'pom\', \'purin\', \'john doe\'],', '}', ]) return self._get_param(f'{TEST_DATA_DIR}/sequence.py', mock_dict, expected)
def get_snake_case_dict(self) -> DataDictType: source_dict = { 'user_info': { 'user_id': 1, 'is_authenticated': False, 'username': '******', } } expected = LINE_BREAK.join([ 'TypeUserInfo = TypedDict("TypeUserInfo", { "user_id": int, "is_authenticated": bool, "username": str })', 'TypeUser = TypedDict("TypeUser", { "user_info": TypeUserInfo })', '', ]) return self._get_params(source_dict, 'user', expected)
def _get_param(self, out_file_name: str, setting_mock_dict: dict, expected: str) -> ParamType: self._setup_for_write('', out_file_name) expected_text = LINE_BREAK.join([ TYPE_MODULE_IMPORT_LINE, expected, '', 'def get_setting_dict() -> TypeSetting:', ' return setting_dict' ]) # 期待結果 self._setup_for_write(expected_text, f'{out_file_name[:-3]}_expected.py') return ( self._get_out_file_path(out_file_name), setting_mock_dict, expected_text, )
def get_various_object(self) -> ParamType: mock_dict = { 'IS_DEBUG': True, 'ALLOWED_PORTS': (80, 443, 8080), 'ADMIN_USERS': [{'name': 'pompom-purin', 'age': 100}, {'name': 'django', 'age': 3}], } expected = LINE_BREAK.join([ 'TypeADMINUSERS = TypedDict("TypeADMINUSERS", { "name": str, "age": int })', 'TypeSetting = TypedDict("TypeSetting", { "ADMIN_USERS": List[TypeADMINUSERS], "ALLOWED_PORTS": Tuple[int, int, int], "IS_DEBUG": bool })', '', 'setting_dict: TypeSetting = {', ' "ADMIN_USERS": [{\'name\': \'pompom-purin\', \'age\': 100}, {\'name\': \'django\', \'age\': 3}],', ' "ALLOWED_PORTS": (80, 443, 8080),', ' "IS_DEBUG": True,', '}', ]) return self._get_param(f'{TEST_DATA_DIR}/various_object.py', mock_dict, expected)