Skip to content

Commit

Permalink
add gmail test
Browse files Browse the repository at this point in the history
  • Loading branch information
Frankbz committed Nov 9, 2024
1 parent 4f95680 commit 6062e11
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 42 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies = [
"asyncio>=3.4.3",
"humanreadable>=0.4.0",
"pytest>=8.3.3",
"pytest-mock>=3.14.0",
]

[tool.uv]
Expand Down
1 change: 1 addition & 0 deletions test/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

134 changes: 134 additions & 0 deletions test/test_gmail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
# pytest test/test_gmail.py -v
import pytest
from datetime import datetime
from unittest.mock import Mock, patch
from src.gmail.client import Gmail


@pytest.fixture
def mock_gmail_service():
"""Fixture to mock the Gmail API service"""
with patch("src.gmail.client.build") as mock_build:
mock_service = Mock()
mock_build.return_value = mock_service
yield mock_service


@pytest.fixture
def gmail_client(mock_gmail_service):
"""Fixture to create a Gmail client with mocked authentication"""
with patch("src.gmail.client.Credentials") as mock_creds:
mock_creds.from_authorized_user_file.return_value = Mock(valid=True)
return Gmail()


def test_query_method(gmail_client, mock_gmail_service):
# Mock response data
mock_messages = {
"messages": [
{"id": "123", "threadId": "thread123"},
{"id": "456", "threadId": "thread456"},
]
}

# Setup the mock chain
mock_service = mock_gmail_service
mock_service.users.return_value.messages.return_value.list.return_value.execute.return_value = mock_messages

# Test the query method
start_date = datetime(2024, 10, 30)
end_date = datetime(2024, 10, 31)
messages = list(
gmail_client.query(start_date=start_date, end_date=end_date, max_results=5)
)

# Assertions
assert len(messages) == 2
assert messages[0]["id"] == "123"
assert messages[1]["id"] == "456"

# Verify the correct query parameters were used
mock_service.users.return_value.messages.return_value.list.assert_called_with(
userId="me", maxResults=5, q="after:2024/10/30 AND before:2024/10/31"
)


def test_get_message(gmail_client, mock_gmail_service):
# Mock response data
mock_message = {
"id": "123",
"payload": {
"headers": [
{"name": "Subject", "value": "Test Subject"},
{"name": "From", "value": "[email protected]"},
]
},
}

# Setup the mock
mock_service = mock_gmail_service
mock_service.users.return_value.messages.return_value.get.return_value.execute.return_value = mock_message

# Test get_message
result = gmail_client.get_message("123")

# Assertions
assert result == mock_message
assert result["payload"]["headers"][0]["value"] == "Test Subject"

# Verify the correct parameters were used
mock_service.users.return_value.messages.return_value.get.assert_called_with(
userId="me", id="123"
)


def test_search_method(gmail_client, mock_gmail_service):
# Mock response data
mock_search_results = {
"messages": [
{"id": "789", "threadId": "thread789"},
{"id": "012", "threadId": "thread012"},
]
}

# Setup the mock
mock_service = mock_gmail_service
mock_service.users.return_value.messages.return_value.list.return_value.execute.return_value = mock_search_results

# Test search method
search_query = "test query"
results = list(gmail_client.search(search_query, max_results=2))

# Assertions
assert len(results) == 2
assert results[0]["id"] == "789"
assert results[1]["id"] == "012"

# Verify the correct parameters were used
mock_service.users.return_value.messages.return_value.list.assert_called_with(
userId="me", maxResults=2, q=search_query
)


def test_get_labels(gmail_client, mock_gmail_service):
# Mock response data
mock_labels = {
"labels": [{"id": "INBOX", "name": "INBOX"}, {"id": "SENT", "name": "SENT"}]
}

# Setup the mock
mock_service = mock_gmail_service
mock_service.users.return_value.labels.return_value.list.return_value.execute.return_value = mock_labels

# Test get_labels
results = gmail_client.get_labels()

# Assertions
assert len(results) == 2
assert results[0]["name"] == "INBOX"
assert results[1]["name"] == "SENT"

# Verify the correct parameters were used
mock_service.users.return_value.labels.return_value.list.assert_called_with(
userId="me"
)
6 changes: 5 additions & 1 deletion tests/test_gmail_api.py → test/test_gmail_api.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# In order to test Google Auth, actual message in your Gmail and API. You can
# replace specific info (like start/end date or message_id) in this file.
# Then compare the output with the actual output in your Gmail.

from datetime import datetime
from src.gmail.API import query, get_message, get_labels, search

Expand All @@ -11,7 +15,7 @@

## Get Single Message Example
# Fetch details of a specific message
message_id = "192e80bb14e565c5" # Replace with actual message ID
message_id = "192e80bb14e565c5"
message = get_message(message_id)
print(f"Subject: {message.get('subject')}")
print(f"Snippet: {message.get('snippet')}")
Expand Down
1 change: 0 additions & 1 deletion tests/__init__.py

This file was deleted.

40 changes: 0 additions & 40 deletions tests/test_gmail.py

This file was deleted.

14 changes: 14 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6062e11

Please sign in to comment.