test_gmail.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import pytest
  2. from llama_index.readers.schema.base import Document
  3. from embedchain.loaders.gmail import GmailLoader
  4. @pytest.fixture
  5. def mock_download_loader(mocker):
  6. return mocker.patch("embedchain.loaders.gmail.download_loader")
  7. @pytest.fixture
  8. def mock_quopri(mocker):
  9. return mocker.patch("embedchain.loaders.gmail.quopri.decodestring", return_value=b"your_test_decoded_string")
  10. @pytest.fixture
  11. def mock_beautifulsoup(mocker):
  12. return mocker.patch("embedchain.loaders.gmail.BeautifulSoup", return_value=mocker.MagicMock())
  13. @pytest.fixture
  14. def gmail_loader(mock_download_loader, mock_quopri, mock_beautifulsoup):
  15. return GmailLoader()
  16. def test_load_data_file_not_found(gmail_loader, mocker):
  17. with pytest.raises(FileNotFoundError):
  18. with mocker.patch("os.path.isfile", return_value=False):
  19. gmail_loader.load_data("your_query")
  20. def test_load_data(gmail_loader, mock_download_loader, mocker):
  21. mock_gmail_reader_instance = mocker.MagicMock()
  22. text = "your_test_email_text"
  23. metadata = {
  24. "id": "your_test_id",
  25. "snippet": "your_test_snippet",
  26. }
  27. mock_gmail_reader_instance.load_data.return_value = [Document(text=text, extra_info=metadata)]
  28. mock_download_loader.return_value = mock_gmail_reader_instance
  29. with mocker.patch("os.path.isfile", return_value=True):
  30. response_data = gmail_loader.load_data("your_query")
  31. assert "doc_id" in response_data
  32. assert "data" in response_data
  33. assert isinstance(response_data["doc_id"], str)
  34. assert isinstance(response_data["data"], list)