def test_collection_name_check(self): illegals = [None, '', '$aaa', 'aaa$b', 'system.aaa', '#aaa', '@aaa'] for i in illegals: with self.subTest(i=i): actual = Utils.collection_name_check(i) self.assertFalse(actual) # 文字列以外の方は文字列に変換される actual = Utils.collection_name_check(345) self.assertTrue(actual)
def recursive(data: dict) -> dict: """ 再帰で辞書を走査して、日付データの変換などを行う 要リファクタリング :param dict data: :return: :rtype: dict """ output = {} for key, value in data.items(): if isinstance(value, dict): if not Utils.collection_name_check(key): sys.exit(f'この名前は使用できません {key}') converted_value = self._convert_datetime(value) output.update({key: recursive(converted_value)}) elif isinstance(value, list): # 日付データが含まれていたらdatetimeオブジェクトに変換 value = self._date_replace(value) # 通常のリストデータの場合 if Utils.item_literal_check(value): if not self._field_name_check(key): sys.exit(f'フィールド名に不備があります {key}') list_tmp_data = value # 子要素としてのリストデータの場合 else: if not Utils.collection_name_check(key): sys.exit(f'この名前は使用できません {key}') list_tmp_data = [ recursive(self._convert_datetime(i)) for i in value ] output.update({key: list_tmp_data}) else: if not self._field_name_check(key): sys.exit(f'フィールド名に不備があります {key}') output.update({key: value}) return output
def recursive(reading_dict_data: dict) -> dict: """ edman用に変換を行う 再帰 要リファクタリング :param dict reading_dict_data: :return: output :rtype: dict """ output = {} parent = -2 # 説明変数 my = -1 # 説明変数 for key, value in reading_dict_data.items(): if isinstance(value, dict): if not Utils.collection_name_check(key): sys.exit(f'この名前はコレクション名にできません {key}') # リファレンス作成 ref_list.append(DBRef(key, ObjectId())) # tmpから子データが返ってくる tmp = recursive(self._convert_datetime(value)) # 親のリファレンス(コレクション)を追加 # rootの場合は追加されない if len(ref_list) > 1: tmp.update({self.parent: ref_list[parent]}) # 子データのリファレンスを取得して親のデータに入れる child_ref = self._get_child_reference(tmp) if list(child_ref.values())[0]: # 子データがない場合もある tmp.update(child_ref) # self.parentがない場合(=root)の場合にoidを追加する if self.parent not in tmp: tmp.update({'_id': ref_list[0].id}) del ref_list[my] # バルクインサート用のリストを作成 list_output.append(self._list_intercept_hook(key, tmp)) output.update({key: tmp}) elif isinstance(value, list) and Utils.item_literal_check(value): if not self._field_name_check(key): sys.exit(f'フィールド名に不備があります {key}') # 日付データが含まれていたらdatetimeオブジェクトに変換 output.update({key: self._date_replace(value)}) elif isinstance(value, list): if not Utils.collection_name_check(key): sys.exit(f'この名前はコレクション名にできません {key}') tmp_list = [] for i in value: # リファレンス作成 ref_list.append(DBRef(key, ObjectId())) # tmpから子データが返ってくる tmp = recursive(self._convert_datetime(i)) # 親のリファレンス(コレクション)を追加 # rootの場合は追加されない if len(ref_list) > 1: tmp.update({self.parent: ref_list[parent]}) # 子要素だけのデータの場合else区へ飛ばないのでここでoidを作成する if '_id' not in tmp: tmp.update({'_id': ref_list[my].id}) # 子データのリファレンスを取得して親のデータに入れる child_ref = self._get_child_reference(tmp) if list(child_ref.values())[0]: # 子データがない場合もある tmp.update(child_ref) del ref_list[my] tmp_list.append(tmp) # バルクインサート用のリストを作成 list_output.append(self._list_intercept_hook( key, tmp_list)) output.update({key: tmp_list}) else: if not self._field_name_check(key): sys.exit(f'フィールド名に不備があります {key}') # oidを取得して追加(rootの場合、すでに作成されている場合がある?) if '_id' not in output: output.update({'_id': ref_list[my].id}) output.update({key: value}) return output