|
| 1 | +""" |
| 2 | +Unit tests for the function calling utilities. |
| 3 | +
|
| 4 | +This module contains tests for sending airtime, sending messages, and searching news |
| 5 | +using the Africa's Talking API and DuckDuckGo News API. The tests mock external |
| 6 | +dependencies to ensure isolation and reliability. |
| 7 | +""" |
| 8 | + |
| 9 | +import os |
| 10 | +import re |
| 11 | +from unittest.mock import patch |
| 12 | +from utils.function_call import send_airtime, send_message, search_news |
| 13 | + |
| 14 | +# Load environment variables: TEST_PHONE_NUMBER |
| 15 | +PHONE_NUMBER = os.getenv("TEST_PHONE_NUMBER") |
| 16 | + |
| 17 | + |
| 18 | +@patch("utils.function_call.africastalking.Airtime") |
| 19 | +def test_send_airtime_success(mock_airtime): |
| 20 | + """ |
| 21 | + Test the send_airtime function to ensure it successfully sends airtime. |
| 22 | +
|
| 23 | + This test mocks the Africa's Talking Airtime API and verifies that the |
| 24 | + send_airtime function returns a response containing the word 'Sent'. |
| 25 | +
|
| 26 | + Parameters |
| 27 | + ---------- |
| 28 | + mock_airtime : MagicMock |
| 29 | + Mocked Airtime API from Africa's Talking. |
| 30 | + """ |
| 31 | + # Configure the mock Airtime response |
| 32 | + mock_airtime.return_value.send.return_value = { |
| 33 | + "numSent": 1, |
| 34 | + "responses": [{"status": "Sent"}], |
| 35 | + } |
| 36 | + |
| 37 | + # Call the send_airtime function |
| 38 | + result = send_airtime(PHONE_NUMBER, "KES", 5) |
| 39 | + |
| 40 | + # Define patterns to check in the response |
| 41 | + message_patterns = [ |
| 42 | + r"Sent", |
| 43 | + ] |
| 44 | + |
| 45 | + # Assert each pattern is found in the response |
| 46 | + for pattern in message_patterns: |
| 47 | + assert re.search( |
| 48 | + pattern, str(result) |
| 49 | + ), f"Pattern '{pattern}' not found in response" |
| 50 | + |
| 51 | + |
| 52 | +@patch("utils.function_call.africastalking.SMS") |
| 53 | +def test_send_message_success(mock_sms): |
| 54 | + """ |
| 55 | + Test the send_message function to ensure it successfully sends a message. |
| 56 | +
|
| 57 | + This test mocks the Africa's Talking SMS API and verifies that the |
| 58 | + send_message function returns a response containing 'Sent to 1/1'. |
| 59 | +
|
| 60 | + Parameters |
| 61 | + ---------- |
| 62 | + mock_sms : MagicMock |
| 63 | + Mocked SMS API from Africa's Talking. |
| 64 | + """ |
| 65 | + # Configure the mock SMS response |
| 66 | + mock_sms.return_value.send.return_value = { |
| 67 | + "SMSMessageData": {"Message": "Sent to 1/1"} |
| 68 | + } |
| 69 | + |
| 70 | + # Call the send_message function |
| 71 | + result = send_message(PHONE_NUMBER, "In Qwen, we trust", os.getenv("AT_USERNAME")) |
| 72 | + |
| 73 | + # Define patterns to check in the response |
| 74 | + message_patterns = [r"Sent to 1/1"] |
| 75 | + |
| 76 | + # Assert each pattern is found in the response |
| 77 | + for pattern in message_patterns: |
| 78 | + assert re.search( |
| 79 | + pattern, str(result) |
| 80 | + ), f"Pattern '{pattern}' not found in response" |
| 81 | + |
| 82 | + |
| 83 | +@patch("utils.function_call.DDGS") |
| 84 | +def test_search_news_success(mock_ddgs): |
| 85 | + """ |
| 86 | + Test the search_news function to ensure it retrieves news articles correctly. |
| 87 | +
|
| 88 | + This test mocks the DuckDuckGo News API and verifies that the |
| 89 | + search_news function returns results matching the expected patterns. |
| 90 | +
|
| 91 | + Parameters |
| 92 | + ---------- |
| 93 | + mock_ddgs : MagicMock |
| 94 | + Mocked DuckDuckGo DDGS API. |
| 95 | + """ |
| 96 | + # Configure the mock DDGS response with a realistic news article |
| 97 | + mock_ddgs.return_value.news.return_value = [ |
| 98 | + { |
| 99 | + "date": "2024-12-20T02:07:00+00:00", |
| 100 | + "title": "Hedge fund leader loves this AI stock", |
| 101 | + "body": "Sample article body text", |
| 102 | + "url": "https://example.com/article", |
| 103 | + "image": "https://example.com/image.jpg", |
| 104 | + "source": "MSN", |
| 105 | + } |
| 106 | + ] |
| 107 | + |
| 108 | + # Call the search_news function |
| 109 | + result = search_news("AI") |
| 110 | + |
| 111 | + # Define regex patterns to validate response format |
| 112 | + patterns = [ |
| 113 | + r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+\d{2}:\d{2}", # Date format |
| 114 | + r'"title":\s*"[^"]+?"', # Title field |
| 115 | + r'"source":\s*"[^"]+?"', # Source field |
| 116 | + r'https?://[^\s<>"]+?', # URL format |
| 117 | + ] |
| 118 | + |
| 119 | + # Convert result to string for regex matching |
| 120 | + result_str = str(result) |
| 121 | + |
| 122 | + # Assert all patterns match in the result |
| 123 | + for pattern in patterns: |
| 124 | + assert re.search( |
| 125 | + pattern, result_str |
| 126 | + ), f"Pattern '{pattern}' not found in response" |
| 127 | + |
| 128 | + # Verify that the news method was called with expected arguments |
| 129 | + mock_ddgs.return_value.news.assert_called_once_with( |
| 130 | + keywords="AI", region="wt-wt", safesearch="off", timelimit="d", max_results=5 |
| 131 | + ) |
0 commit comments