Skip to content

Commit 41ec4bb

Browse files
committed
Refactor exception handling and model validation in WhatsApp API wrapper
- Improved exception classes for better clarity and structure. - Enhanced validation logic in Pydantic models for message and contact handling. - Standardized string formatting and validation patterns across utility functions. - Cleaned up code by removing unnecessary comments and ensuring consistent formatting. - Added detailed docstrings for better understanding of function purposes and parameters.
1 parent c625d3a commit 41ec4bb

File tree

10 files changed

+1046
-1280
lines changed

10 files changed

+1046
-1280
lines changed

tests/conftest.py

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
"""
22
Pytest configuration and fixtures for WhatsApp API wrapper tests.
33
"""
4+
5+
from unittest.mock import AsyncMock, Mock
6+
47
import pytest
5-
from unittest.mock import Mock, AsyncMock
8+
69
from whatsapp_api_wrapper import WhatsAppAPI
7-
from whatsapp_api_wrapper.models import *
810
from whatsapp_api_wrapper.exceptions import *
11+
from whatsapp_api_wrapper.models import *
912

1013

1114
@pytest.fixture
@@ -49,11 +52,7 @@ def mock_httpx_client():
4952
@pytest.fixture
5053
def sample_message_data():
5154
"""Sample message data for testing."""
52-
return {
53-
"to": "1234567890@c.us",
54-
"body": "Test message",
55-
"type": "text"
56-
}
55+
return {"to": "1234567890@c.us", "body": "Test message", "type": "text"}
5756

5857

5958
@pytest.fixture
@@ -67,7 +66,7 @@ def sample_contact_data():
6766
"isGroup": False,
6867
"isUser": True,
6968
"isMyContact": True,
70-
"isBlocked": False
69+
"isBlocked": False,
7170
}
7271

7372

@@ -83,7 +82,7 @@ def sample_chat_data():
8382
"timestamp": 1623456789,
8483
"archived": False,
8584
"pinned": False,
86-
"isMuted": False
85+
"isMuted": False,
8786
}
8887

8988

@@ -96,10 +95,10 @@ def sample_group_data():
9695
"description": "Test group description",
9796
"participants": [
9897
{"id": "1111111111@c.us", "isAdmin": True},
99-
{"id": "2222222222@c.us", "isAdmin": False}
98+
{"id": "2222222222@c.us", "isAdmin": False},
10099
],
101100
"admins": ["1111111111@c.us"],
102-
"inviteCode": "test-invite-code"
101+
"inviteCode": "test-invite-code",
103102
}
104103

105104

@@ -111,29 +110,22 @@ def sample_session_data():
111110
"ready": True,
112111
"qr": None,
113112
"status": "authenticated",
114-
"webhook": None
113+
"webhook": None,
115114
}
116115

117116

118117
@pytest.fixture
119118
def sample_error_response():
120119
"""Sample error response data."""
121-
return {
122-
"success": False,
123-
"error": "Test error message",
124-
"code": "TEST_ERROR"
125-
}
120+
return {"success": False, "error": "Test error message", "code": "TEST_ERROR"}
126121

127122

128123
@pytest.fixture
129124
def mock_successful_response():
130125
"""Mock successful HTTP response."""
131126
mock_response = Mock()
132127
mock_response.status_code = 200
133-
mock_response.json.return_value = {
134-
"success": True,
135-
"data": {"message": "Operation successful"}
136-
}
128+
mock_response.json.return_value = {"success": True, "data": {"message": "Operation successful"}}
137129
mock_response.raise_for_status.return_value = None
138130
return mock_response
139131

@@ -146,6 +138,6 @@ def mock_error_response():
146138
mock_response.json.return_value = {
147139
"success": False,
148140
"error": "Bad request",
149-
"code": "BAD_REQUEST"
141+
"code": "BAD_REQUEST",
150142
}
151143
return mock_response

0 commit comments

Comments
 (0)