11import typing
22import pytest
33from unittest .mock import Mock , patch , AsyncMock
4+ import asyncio
45
56from whatsapp_api_client_python .API import GreenAPI
67
@@ -12,26 +13,76 @@ class TestAsyncMethods:
1213 @pytest .mark .asyncio
1314 async def test_single_async_method (self ):
1415 """Тестируем только один метод для упрощения отладки"""
15- mock_response = AsyncMock ()
16+ # Создаем реальный мок-объект с нужными атрибутами
17+ mock_response = Mock ()
1618 mock_response .code = 200
1719 mock_response .data = {"example" : {"key" : "value" }}
1820
19- with patch ("whatsapp_api_client_python.API.Session.request" , return_value = mock_response ) as mock_request :
21+ # Создаем асинхронную функцию, которая возвращает наш мок
22+ async def mock_request (* args , ** kwargs ):
23+ return mock_response
24+
25+ with patch ("whatsapp_api_client_python.API.Session.request" , side_effect = mock_request ):
2026 # Тестируем только один метод
2127 response = await api .account .getSettingsAsync ()
2228
2329 assert response .code == 200
2430 assert response .data == {"example" : {"key" : "value" }}
25- assert mock_request .call_count == 1
26-
31+
2732 @pytest .mark .asyncio
28- async def test_async_methods (self ):
29- """Полный тест всех методов"""
30- # Создаем простой асинхронный мок
31- async def mock_request (* args , ** kwargs ):
33+ async def test_async_methods_with_different_status_codes (self ):
34+ """Тестируем все методы с разными кодами ответа"""
35+ # Создаем список мок-ответов с разными статусами
36+ mock_responses = []
37+ for i in range (50 ): # Создаем достаточно ответов
3238 mock_response = Mock ()
33- mock_response .code = 200
34- mock_response .data = {"example" : {"key" : "value" }}
39+ # Чередуем коды статусов: 200, 401, 403
40+ status_code = [200 , 401 , 403 ][i % 3 ]
41+ mock_response .code = status_code
42+ if status_code == 200 :
43+ mock_response .data = {"example" : {"key" : "value" }}
44+ else :
45+ mock_response .data = {"error" : "Unauthorized" if status_code == 401 else "Forbidden" }
46+ mock_responses .append (mock_response )
47+
48+ async def mock_request (* args , ** kwargs ):
49+ return mock_responses .pop (0 )
50+
51+ with patch ("whatsapp_api_client_python.API.Session.request" , side_effect = mock_request ):
52+ methods_coroutines = []
53+ methods_coroutines .extend (self .account_methods ())
54+ methods_coroutines .extend (self .group_methods ())
55+ methods_coroutines .extend (self .status_methods ())
56+ methods_coroutines .extend (self .log_methods ())
57+ methods_coroutines .extend (self .queue_methods ())
58+ methods_coroutines .extend (self .read_mark_methods ())
59+ methods_coroutines .extend (self .receiving_methods ())
60+ methods_coroutines .extend (self .sending_methods ())
61+ methods_coroutines .extend (self .service_methods ())
62+
63+ responses = []
64+ for coro in methods_coroutines :
65+ response = await coro
66+ responses .append (response )
67+
68+ # Проверяем что все ответы имеют допустимые коды статуса
69+ valid_codes = [200 , 401 , 403 ]
70+ for response in responses :
71+ assert response .code in valid_codes
72+ if response .code == 200 :
73+ assert response .data == {"example" : {"key" : "value" }}
74+ else :
75+ assert "error" in response .data
76+
77+ @pytest .mark .asyncio
78+ async def test_async_methods_all_success (self ):
79+ """Тестируем все методы с успешными ответами"""
80+ # Создаем мок для успешных ответов
81+ mock_response = Mock ()
82+ mock_response .code = 200
83+ mock_response .data = {"example" : {"key" : "value" }}
84+
85+ async def mock_request (* args , ** kwargs ):
3586 return mock_response
3687
3788 with patch ("whatsapp_api_client_python.API.Session.request" , side_effect = mock_request ):
0 commit comments