Skip to content

Commit

Permalink
✅ Refactor connectors testing
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoPerrier committed Dec 9, 2024
1 parent 673c071 commit cc11fd1
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 67 deletions.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ dynamic = ["version"]
dev = ["tox", "pre-commit", "black", "flake8", "isort", "mypy", "pytest", "coverage", "build", "ruff"]
test = ["pytest", "coverage", "pytest-cov", "google-auth-oauthlib", "google-api-python-client"]
transformers = ["transformers>4"]
connectors = ["exchangelib", "google-auth-oauthlib", "google-api-python-client"]
connectors = ["exchangelib"]
gmail = ["google-auth-oauthlib", "google-api-python-client"]
docs = ["mkdocs", "markdown", "mkdocs-material", "mdx-include"]

[tool.setuptools.packages.find]
Expand Down
1 change: 0 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
pytest_plugins = [
"tests.fixtures.backend",
"tests.fixtures.basic_emails",
"tests.fixtures.connectors",
"tests.fixtures.docs",
"tests.fixtures.pipelines",
"tests.fixtures.processors",
Expand Down
55 changes: 0 additions & 55 deletions tests/fixtures/connectors.py

This file was deleted.

File renamed without changes.
60 changes: 54 additions & 6 deletions tests/connectors/test_gmail.py → tests/gmail/test_gmail.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,64 @@
import base64
import logging
import os
from unittest.mock import MagicMock, patch
import pytest

import pandas as pd
import pytest
from google.oauth2.credentials import Credentials
from googleapiclient.http import HttpRequestMock
from unittest.mock import MagicMock, patch


HttpRequestMock = pytest.importorskip('googleapiclient.http.HttpRequestMock')
from google.oauth2.credentials import Credentials
from melusine.connectors.gmail import GmailConnector


def return_value(resp, content):
return content


@pytest.fixture
def mocked_gc():
with patch("melusine.connectors.gmail.build") as mock_build:
with patch("melusine.connectors.gmail.Credentials.from_authorized_user_file") as mock_creds_from_file:
with patch("melusine.connectors.gmail.os.path.exists") as mock_exists:
mock_exists.return_value = True
mock_service = MagicMock()
mock_service.users().getProfile.return_value = HttpRequestMock(
None, {"emailAddress": "[email protected]"}, return_value
)
mock_service.users().labels().list.return_value = HttpRequestMock(
None,
{
"labels": [
{"id": "INBOX", "name": "INBOX", "type": "system"},
{
"id": "TRASH",
"name": "TRASH",
"messageListVisibility": "hide",
"labelListVisibility": "labelHide",
"type": "system",
},
{"id": "UNREAD", "name": "UNREAD", "type": "system"},
]
},
return_value,
)
mock_build.return_value = mock_service
mock_creds_from_file.return_value = Credentials("dummy")

return GmailConnector(token_json_path="token.json", done_label="TRASH", target_column="target")


@pytest.fixture
def fake_image():
image_data = b""
width = height = 100
for _ in range(height):
row_data = b"\xff" * (width * 3)
image_data += row_data

return image_data


def return_value(resp, content):
return content

Expand Down Expand Up @@ -266,4 +314,4 @@ def test_gc_send_email(mocked_gc, fake_image, caplog):
{"attachment.jpg": fake_image},
)

assert "Email sent to [email protected], Message Id: 12456"
assert "Email sent to [email protected], Message Id: 12456" in caplog.text
12 changes: 8 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
[tox]
requires =
tox>=4
env_list = clean, core38, core310, transformers, report
env_list = clean, core38, core310, transformers, gmail, report

[gh-actions]
python =
3.8: clean, core38, transformers
3.8: clean, core38, transformers, gmail
3.10: core310

[testenv]
commands = pytest --cov --cov-append --cov-report xml
deps =
pytest
pytest-cov
google-auth-oauthlib
google-api-python-client
depends =
{core38,transformers}: clean
report: core38,transformers
Expand All @@ -38,6 +36,12 @@ deps={[testenv]deps}
commands = pytest tests/huggingface --cov --cov-append --cov-report xml
extras = transformers

[testenv:gmail]
description = run unit tests with the gmail dependencies
deps={[testenv]deps}
commands = pytest tests/gmail --cov --cov-append --cov-report xml
extras = gmail

[testenv:report]
deps = coverage[toml]
skip_install = true
Expand Down

0 comments on commit cc11fd1

Please sign in to comment.