Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crm.company.contact.items.get возвращает только один элемент, а нужно несколько #257

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions fast_bitrix24/server_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ def extract_results(self) -> Union[Dict, List[Dict]]:

extracted = self.extract_from_single_response(self.result["result"])

return extracted[0] if isinstance(extracted, list) else extracted
return (
extracted[0]
if isinstance(extracted, list) and len(extracted) == 1
else extracted
)

def raise_for_errors(self):
errors = self.extract_errors()
Expand Down Expand Up @@ -86,7 +90,7 @@ def extract_from_single_response(self, result):

@staticmethod
def is_nested(result) -> bool:
return isinstance(result, (dict, list)) and len(result) == 1
return isinstance(result, dict) and len(result.values()) == 1

def extract_from_batch_response(self, result) -> list:
if not result:
Expand Down
35 changes: 35 additions & 0 deletions tests/real_responses/crm_company_contact_items_get.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
response = {
"result": {
"result": {
"205364": [
{"CONTACT_ID": 350354, "SORT": 10, "ROLE_ID": 0, "IS_PRIMARY": "Y"},
{"CONTACT_ID": 369658, "SORT": 10, "ROLE_ID": 0, "IS_PRIMARY": "Y"},
]
},
"result_error": [],
"result_total": [],
"result_next": [],
"result_time": {
"205364": {
"start": 1739205151.785116,
"finish": 1739205151.915156,
"duration": 0.13003993034362793,
"processing": 0.12938594818115234,
"date_start": "2025-02-10T19:32:31+03:00",
"date_finish": "2025-02-10T19:32:31+03:00",
"operating_reset_at": 1739205751,
"operating": 0.7021360397338867,
}
},
},
"time": {
"start": 1739205151.753569,
"finish": 1739205151.916085,
"duration": 0.16251611709594727,
"processing": 0.13108110427856445,
"date_start": "2025-02-10T19:32:31+03:00",
"date_finish": "2025-02-10T19:32:31+03:00",
"operating_reset_at": 1739205751,
"operating": 0,
},
}
62 changes: 34 additions & 28 deletions tests/test_server_responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,10 @@ def test_crm_contact_add_batch(bx_dummy):
"NAME": "TESTR",
"PHONE": [{"VALUE": "78966666647", "VALUE_TYPE": "WORK"}],
"ASSIGNED_BY_ID": -1,
"OPENED": "Y"
"OPENED": "Y",
},
"params": {
"REGISTER_SONET_EVENT": "Y"
}
}
"params": {"REGISTER_SONET_EVENT": "Y"},
},
)

# Проверяем что результат - это ID созданного контакта
Expand All @@ -325,39 +323,47 @@ def test_crm_contact_add_batch(bx_dummy):

def test_crm_contact_list(bx_dummy):
response = {
'result': [
"result": [
{
'ID': '10',
'NAME': 'Абдуалимова Татьяна Александровна',
'SECOND_NAME': None,
'LAST_NAME': None
"ID": "10",
"NAME": "Абдуалимова Татьяна Александровна",
"SECOND_NAME": None,
"LAST_NAME": None,
}
],
'total': 1,
'time': {
'start': 1731743829.0188,
'finish': 1731743829.06444,
'duration': 0.045639991760253906,
'processing': 0.019975900650024414,
'date_start': '2024-11-16T10:57:09+03:00',
'date_finish': '2024-11-16T10:57:09+03:00',
'operating_reset_at': 1731744429,
'operating': 0
}
"total": 1,
"time": {
"start": 1731743829.0188,
"finish": 1731743829.06444,
"duration": 0.045639991760253906,
"processing": 0.019975900650024414,
"date_start": "2024-11-16T10:57:09+03:00",
"date_finish": "2024-11-16T10:57:09+03:00",
"operating_reset_at": 1731744429,
"operating": 0,
},
}

bx_dummy.srh = MockSRH(response)
results = bx_dummy.get_all(
'crm.contact.list',
"crm.contact.list",
{
'params': {
'select': ['ID', 'NAME', 'SECOND_NAME', 'LAST_NAME'],
'filter': {'=ID': ['10']}
"params": {
"select": ["ID", "NAME", "SECOND_NAME", "LAST_NAME"],
"filter": {"=ID": ["10"]},
}
}
},
)

assert isinstance(results, list)
assert len(results) == 1
assert results[0]['ID'] == '10'
assert results[0]['NAME'] == 'Абдуалимова Татьяна Александровна'
assert results[0]["ID"] == "10"
assert results[0]["NAME"] == "Абдуалимова Татьяна Александровна"


def test_crm_company_contact_items_get(bx_dummy):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Suggest adding more assertions to cover different aspects of the response.

It would be beneficial to assert the content of the returned list, such as checking the 'CONTACT_ID', 'SORT', 'ROLE_ID', and 'IS_PRIMARY' fields for each item. This ensures that the function not only returns the correct number of elements but also that the elements contain the expected data.

Suggested implementation:

def test_crm_company_contact_items_get(bx_dummy):
    from tests.real_responses.crm_company_contact_items_get import response

    assert isinstance(response, list)
    # Change the expected count if needed
    assert len(response) == 1

    contact_item = response[0]

    # These expected values should be updated to reflect the actual expected data
    assert contact_item["CONTACT_ID"] == "expected_contact_id"
    assert contact_item["SORT"] == "expected_sort"
    assert contact_item["ROLE_ID"] == "expected_role_id"
    assert contact_item["IS_PRIMARY"] == True

Ensure that the expected values (e.g., "expected_contact_id", "expected_sort", "expected_role_id") match the actual response data for the test. If the true expected values are known, replace these placeholder strings accordingly.

from tests.real_responses.crm_company_contact_items_get import response
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (code-quality): Don't import test modules. (dont-import-test-modules)

ExplanationDon't import test modules.

Tests should be self-contained and don't depend on each other.

If a helper function is used by multiple tests,
define it in a helper module,
instead of importing one test from the other.


bx_dummy.srh = MockSRH(response)
results = bx_dummy.get_by_ID("crm.company.contact.items.get", ["205364"])
assert len(results) == 2