Exemplo n.º 1
0
Arquivo: db.py Projeto: ryde/EDMAN
    def _build_to_doc_child(self, find_result: list) -> dict:
        """
        子の検索結果(リスト)を入れ子辞書に組み立てる

        :param list find_result:
        :return:
        :rtype: dict
        """
        find_result = [i for i in Utils.child_combine(find_result)]
        parent_id_dict = self._generate_parent_id_dict(find_result)
        find_result_cp = copy.deepcopy(list(reversed(find_result)))

        for bros_idx, bros in enumerate(reversed(find_result)):
            for collection, docs in bros.items():
                for doc_idx, doc in enumerate(docs):
                    # 子データが存在する場合はマージする
                    if doc['_id'] in parent_id_dict:
                        tmp = find_result_cp[bros_idx][collection][doc_idx]
                        tmp.update(parent_id_dict[doc['_id']])
                        doc.update(tmp)
                        del parent_id_dict[doc['_id']]

        return find_result[0]
Exemplo n.º 2
0
 def test_child_combine(self):
     # データ構造のテスト
     test_data = [[{
         'collection_A': {
             'name': 'NSX'
         }
     }, {
         'collection_A': {
             'name': 'F355'
         }
     }, {
         'collection_B': {
             'power': 280
         }
     }]]
     # # test_data = [
     #         {'collection_A': {'name': 'NSX'}},
     #         {'collection_A': {'name': 'F355'}},
     #         {'collection_B': {'power': 280}}
     #     ]
     actual = [i for i in Utils.child_combine(test_data)]
     self.assertIsInstance(actual, list)
     self.assertEqual(2, len(actual[0]['collection_A']))