From cc11fd1b1577b9b92b5bfce65bba14ec81482f70 Mon Sep 17 00:00:00 2001 From: Hugo Perrier Date: Mon, 9 Dec 2024 14:10:15 +0100 Subject: [PATCH] :white_check_mark: Refactor connectors testing --- pyproject.toml | 3 +- tests/conftest.py | 1 - tests/fixtures/connectors.py | 55 --------------------- tests/{connectors => gmail}/__init__.py | 0 tests/{connectors => gmail}/test_gmail.py | 60 ++++++++++++++++++++--- tox.ini | 12 +++-- 6 files changed, 64 insertions(+), 67 deletions(-) delete mode 100644 tests/fixtures/connectors.py rename tests/{connectors => gmail}/__init__.py (100%) rename tests/{connectors => gmail}/test_gmail.py (83%) diff --git a/pyproject.toml b/pyproject.toml index 085cb935..7a04fa8c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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] diff --git a/tests/conftest.py b/tests/conftest.py index 678606f2..bfab74cf 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -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", diff --git a/tests/fixtures/connectors.py b/tests/fixtures/connectors.py deleted file mode 100644 index c556d912..00000000 --- a/tests/fixtures/connectors.py +++ /dev/null @@ -1,55 +0,0 @@ -from unittest.mock import MagicMock, patch - -import pytest -from google.oauth2.credentials import Credentials -from googleapiclient.http import HttpRequestMock - -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": "test@example.com"}, 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 diff --git a/tests/connectors/__init__.py b/tests/gmail/__init__.py similarity index 100% rename from tests/connectors/__init__.py rename to tests/gmail/__init__.py diff --git a/tests/connectors/test_gmail.py b/tests/gmail/test_gmail.py similarity index 83% rename from tests/connectors/test_gmail.py rename to tests/gmail/test_gmail.py index 390bfa2c..05d23e5f 100644 --- a/tests/connectors/test_gmail.py +++ b/tests/gmail/test_gmail.py @@ -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": "test@example.com"}, 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 @@ -266,4 +314,4 @@ def test_gc_send_email(mocked_gc, fake_image, caplog): {"attachment.jpg": fake_image}, ) - assert "Email sent to melusine_testing@yopmail.com, Message Id: 12456" + assert "Email sent to melusine_testing@yopmail.com, Message Id: 12456" in caplog.text diff --git a/tox.ini b/tox.ini index a94c3413..ca640670 100644 --- a/tox.ini +++ b/tox.ini @@ -1,11 +1,11 @@ [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] @@ -13,8 +13,6 @@ 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 @@ -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