198 lines
6.7 KiB
Python
198 lines
6.7 KiB
Python
"""
|
||
Test file demonstrating AMO CRM response examples
|
||
"""
|
||
import pytest
|
||
from tests.fixtures.amocrm_responses import (
|
||
USERS_RESPONSE,
|
||
PIPELINES_RESPONSE,
|
||
COMPANIES_RESPONSE,
|
||
CONTACTS_RESPONSE,
|
||
DEALS_RESPONSE,
|
||
EVENTS_RESPONSE,
|
||
CUSTOM_FIELDS_RESPONSE
|
||
)
|
||
|
||
|
||
def test_users_response_structure():
|
||
"""Test that users response has expected structure"""
|
||
assert "_embedded" in USERS_RESPONSE
|
||
assert "users" in USERS_RESPONSE["_embedded"]
|
||
|
||
users = USERS_RESPONSE["_embedded"]["users"]
|
||
assert len(users) == 2
|
||
|
||
first_user = users[0]
|
||
assert first_user["id"] == 504141
|
||
assert first_user["name"] == "Иван Иванов"
|
||
assert first_user["email"] == "ivan@example.com"
|
||
assert first_user["is_active"] is True
|
||
|
||
|
||
def test_pipelines_response_structure():
|
||
"""Test that pipelines response has expected structure"""
|
||
assert "_embedded" in PIPELINES_RESPONSE
|
||
assert "pipelines" in PIPELINES_RESPONSE["_embedded"]
|
||
|
||
pipelines = PIPELINES_RESPONSE["_embedded"]["pipelines"]
|
||
assert len(pipelines) == 1
|
||
|
||
pipeline = pipelines[0]
|
||
assert pipeline["id"] == 3130966
|
||
assert pipeline["name"] == "Продажи"
|
||
assert pipeline["is_main"] is True
|
||
|
||
# Check stages
|
||
assert "_embedded" in pipeline
|
||
assert "statuses" in pipeline["_embedded"]
|
||
statuses = pipeline["_embedded"]["statuses"]
|
||
assert len(statuses) == 5
|
||
|
||
|
||
def test_companies_response_structure():
|
||
"""Test that companies response has expected structure"""
|
||
assert "_embedded" in COMPANIES_RESPONSE
|
||
assert "companies" in COMPANIES_RESPONSE["_embedded"]
|
||
|
||
companies = COMPANIES_RESPONSE["_embedded"]["companies"]
|
||
assert len(companies) == 1
|
||
|
||
company = companies[0]
|
||
assert company["id"] == 15960673
|
||
assert company["name"] == "ООО \"Рога и копыта\""
|
||
assert company["responsible_user_id"] == 504141
|
||
|
||
# Check custom fields
|
||
assert "custom_fields_values" in company
|
||
custom_fields = company["custom_fields_values"]
|
||
assert len(custom_fields) == 3
|
||
|
||
# Check phone field
|
||
phone_field = next(f for f in custom_fields if f["field_name"] == "Телефон")
|
||
assert phone_field["field_type"] == "multitext"
|
||
assert phone_field["values"][0]["value"] == "+7 (495) 123-45-67"
|
||
|
||
|
||
def test_contacts_response_structure():
|
||
"""Test that contacts response has expected structure"""
|
||
assert "_embedded" in CONTACTS_RESPONSE
|
||
assert "contacts" in CONTACTS_RESPONSE["_embedded"]
|
||
|
||
contacts = CONTACTS_RESPONSE["_embedded"]["contacts"]
|
||
assert len(contacts) == 1
|
||
|
||
contact = contacts[0]
|
||
assert contact["id"] == 19421421
|
||
assert contact["name"] == "Алексей Смирнов"
|
||
assert contact["first_name"] == "Алексей"
|
||
assert contact["last_name"] == "Смирнов"
|
||
|
||
# Check embedded companies
|
||
assert "_embedded" in contact
|
||
assert "companies" in contact["_embedded"]
|
||
companies = contact["_embedded"]["companies"]
|
||
assert len(companies) == 1
|
||
assert companies[0]["id"] == 15960673
|
||
|
||
|
||
def test_deals_response_structure():
|
||
"""Test that deals response has expected structure"""
|
||
assert "_embedded" in DEALS_RESPONSE
|
||
assert "leads" in DEALS_RESPONSE["_embedded"] # Note: AMO CRM calls deals "leads"
|
||
|
||
deals = DEALS_RESPONSE["_embedded"]["leads"]
|
||
assert len(deals) == 2
|
||
|
||
first_deal = deals[0]
|
||
assert first_deal["id"] == 19620805
|
||
assert first_deal["name"] == "Сделка с ООО \"Рога и копыта\""
|
||
assert first_deal["price"] == 150000
|
||
assert first_deal["status_id"] == 32532073
|
||
assert first_deal["pipeline_id"] == 3130966
|
||
|
||
# Check custom fields
|
||
custom_fields = first_deal["custom_fields_values"]
|
||
priority_field = next(f for f in custom_fields if f["field_name"] == "Приоритет")
|
||
assert priority_field["field_type"] == "select"
|
||
assert priority_field["values"][0]["value"] == "Высокий"
|
||
|
||
# Check embedded relationships
|
||
assert "_embedded" in first_deal
|
||
assert "contacts" in first_deal["_embedded"]
|
||
assert "companies" in first_deal["_embedded"]
|
||
|
||
contacts = first_deal["_embedded"]["contacts"]
|
||
assert contacts[0]["id"] == 19421421
|
||
assert contacts[0]["is_main"] is True
|
||
|
||
|
||
def test_events_response_structure():
|
||
"""Test that events response has expected structure"""
|
||
assert "_embedded" in EVENTS_RESPONSE
|
||
assert "events" in EVENTS_RESPONSE["_embedded"]
|
||
|
||
events = EVENTS_RESPONSE["_embedded"]["events"]
|
||
assert len(events) == 3
|
||
|
||
# Test lead status changed event
|
||
status_event = next(e for e in events if e["type"] == "lead_status_changed")
|
||
assert status_event["entity_type"] == "lead"
|
||
assert status_event["entity_id"] == 19620805
|
||
assert "value_after" in status_event
|
||
assert "value_before" in status_event
|
||
|
||
# Test incoming call event
|
||
call_event = next(e for e in events if e["type"] == "incoming_call")
|
||
assert call_event["entity_type"] == "contact"
|
||
assert call_event["entity_id"] == 19421421
|
||
assert call_event["value_after"][0]["call"]["duration"] == 120
|
||
|
||
|
||
def test_custom_fields_metadata():
|
||
"""Test custom fields metadata structure"""
|
||
assert "_embedded" in CUSTOM_FIELDS_RESPONSE
|
||
assert "custom_fields" in CUSTOM_FIELDS_RESPONSE["_embedded"]
|
||
|
||
fields = CUSTOM_FIELDS_RESPONSE["_embedded"]["custom_fields"]
|
||
assert len(fields) == 4
|
||
|
||
# Test select field with enums
|
||
priority_field = next(f for f in fields if f["name"] == "Приоритет")
|
||
assert priority_field["type"] == "select"
|
||
assert "enums" in priority_field
|
||
assert len(priority_field["enums"]) == 3
|
||
|
||
high_priority = priority_field["enums"][0]
|
||
assert high_priority["value"] == "Высокий"
|
||
assert high_priority["api_code"] == "HIGH"
|
||
|
||
# Test date field
|
||
date_field = next(f for f in fields if f["name"] == "Дата закрытия")
|
||
assert date_field["type"] == "date"
|
||
|
||
# Test numeric field
|
||
numeric_field = next(f for f in fields if f["name"] == "Бюджет клиента")
|
||
assert numeric_field["type"] == "numeric"
|
||
|
||
|
||
def test_date_validation():
|
||
"""Test that dates are in expected range (2017-2026)"""
|
||
# Test deals dates
|
||
deals = DEALS_RESPONSE["_embedded"]["leads"]
|
||
for deal in deals:
|
||
created_at = deal["created_at"]
|
||
updated_at = deal["updated_at"]
|
||
|
||
# Check dates are in range (2017 = 1483228800, 2026 = 1767225600)
|
||
assert 1483228800 <= created_at <= 1767225600, f"created_at {created_at} out of range"
|
||
assert 1483228800 <= updated_at <= 1767225600, f"updated_at {updated_at} out of range"
|
||
|
||
# Test events dates
|
||
events = EVENTS_RESPONSE["_embedded"]["events"]
|
||
for event in events:
|
||
created_at = event["created_at"]
|
||
assert 1483228800 <= created_at <= 1767225600, f"event created_at {created_at} out of range"
|
||
|
||
|
||
if __name__ == "__main__":
|
||
pytest.main([__file__])
|