From 38ca64f28b842ef05185728e05ffba49d5041677 Mon Sep 17 00:00:00 2001 From: Alexey Leshchenko Date: Mon, 18 Mar 2024 18:04:32 +0300 Subject: [PATCH] Fix server response parsing --- fast_bitrix24/server_response.py | 17 +++++++++-------- ...crm_dealcategory_stage_list_batch_of_one.py} | 0 tests/test_server_responses.py | 12 +++++------- 3 files changed, 14 insertions(+), 15 deletions(-) rename tests/real_responses/{crm_dealcategory_stage_list_2.py => crm_dealcategory_stage_list_batch_of_one.py} (100%) diff --git a/fast_bitrix24/server_response.py b/fast_bitrix24/server_response.py index 01acf99..1f22bfa 100644 --- a/fast_bitrix24/server_response.py +++ b/fast_bitrix24/server_response.py @@ -45,11 +45,13 @@ def extract_results(self) -> Union[Dict, List[Dict]]: if not self.is_batch(): return self.extract_from_single_response(self.result) - if self.get_by_ID: - return self.extract_from_single_response(self.result["result"]) - else: + if not self.get_by_ID: return self.extract_from_batch_response(self.result["result"]) + extracted = self.extract_from_single_response(self.result["result"]) + + return extracted[0] if isinstance(extracted, list) else extracted + def raise_for_errors(self): errors = self.extract_errors() if errors: @@ -67,8 +69,7 @@ def extract_errors(self): def is_batch(self) -> bool: return isinstance(self.result, dict) and "result" in self.result - @staticmethod - def extract_from_single_response(result: dict): + def extract_from_single_response(self, result: dict): # если результат вызова содержит только словарь {ключ: список}, # то вернуть этот список. # См. https://github.com/leshchenko1979/fast_bitrix24/issues/132 @@ -76,8 +77,8 @@ def extract_from_single_response(result: dict): # метод `crm.stagehistory.list` возвращает dict["items", list] -- # разворачиваем его в список - if ServerResponseParser.is_nested(result): - contents = ServerResponseParser.extract_from_nested(result) + if self.is_nested(result): + contents = self.extract_from_nested(result) if isinstance(contents, list): return contents @@ -88,7 +89,7 @@ def is_nested(result) -> bool: return isinstance(result, dict) and len(result) == 1 @staticmethod - def extract_from_nested(result): + def extract_from_nested(result: dict): return next(iter(result.values())) diff --git a/tests/real_responses/crm_dealcategory_stage_list_2.py b/tests/real_responses/crm_dealcategory_stage_list_batch_of_one.py similarity index 100% rename from tests/real_responses/crm_dealcategory_stage_list_2.py rename to tests/real_responses/crm_dealcategory_stage_list_batch_of_one.py diff --git a/tests/test_server_responses.py b/tests/test_server_responses.py index e3933d0..4fc0f06 100644 --- a/tests/test_server_responses.py +++ b/tests/test_server_responses.py @@ -184,16 +184,14 @@ def test_crm_dealcategory_stage_list(bx_dummy): assert isinstance(results, dict) -@pytest.mark.skip( - reason="Ошибка возникает только при заворачивании в батч более одного элемента, " - "но это должно вылечиться тем, что мы перестанем такое заворачивать в батч" -) -def test_crm_dealcategory_stage_list_2(bx_dummy): - from tests.real_responses.crm_dealcategory_stage_list_2 import response +def test_crm_dealcategory_stage_list_batch_of_one(bx_dummy): + from tests.real_responses.crm_dealcategory_stage_list_batch_of_one import response bx_dummy.srh = MockSRH(response) results = bx_dummy.get_by_ID("crm.dealcategory.stage.list", [0]) - assert isinstance(results, dict) + + # should return a list of dicts + assert isinstance(results, list) and isinstance(results[0], dict) def test_catalog_document_element_list(bx_dummy):