Skip to content

Commit

Permalink
Add frontend and backend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Yanglin-Tao committed May 12, 2024
1 parent 406a32d commit 46ef9fa
Show file tree
Hide file tree
Showing 5 changed files with 366 additions and 2 deletions.
201 changes: 199 additions & 2 deletions pdm.lock

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

6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies = [
"google-auth>=2.29.0",
"google-auth-oauthlib>=1.2.0",
"google-api-python-client>=2.127.0",
"selenium>=4.20.0",
]

requires-python = ">=3.11"
Expand All @@ -34,3 +35,8 @@ dev = [
"black>=24.1.1",
"types-psycopg2>=2.9.21.20240417",
]

[tool.pytest.ini_options]
pythonpath = [
"src"
]
Empty file added tests/conftest.py
Empty file.
97 changes: 97 additions & 0 deletions tests/test_crawl_tasks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import pytest
import os
from src import *
from src.open_source_python_template.crawlTasks import get_tasks
from unittest.mock import patch, mock_open, MagicMock, PropertyMock
from google.oauth2.credentials import Credentials
from google.auth.transport.requests import Request
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError

# Sample data for mocking responses
MOCK_TASKS = {
"items": [
{"title": "Task 1", "id": "1", "tasklist_id": "tl1", "tasklist_title": "TaskList 1"}
]
}

MOCK_TASKLISTS = {
"items": [
{"title": "TaskList 1", "id": "tl1"}
]
}

def test_get_tasks_existing_token_valid():
with patch('os.path.exists', return_value=True), \
patch('google.oauth2.credentials.Credentials.from_authorized_user_file') as mock_cred, \
patch('googleapiclient.discovery.build') as mock_build:

mock_cred.return_value.valid = True
mock_cred.return_value.universe_domain = 'googleapis.com'

# Mock Google API client setup
mock_service = MagicMock()
mock_build.return_value = mock_service
mock_cred.return_value.valid = True

# Setup Google API client mock
mock_service = MagicMock()
mock_build.return_value = mock_service
mock_service.tasklists().list().execute.return_value = MOCK_TASKLISTS
mock_service.tasks().list().execute.return_value = MOCK_TASKS

# Execute function
results = get_tasks()

# Assert results
assert len(results) == 1
assert results[0]['title'] == "Task 1"

def test_get_tasks_existing_token_expired():
with patch('os.path.exists', return_value=True), \
patch('google.oauth2.credentials.Credentials.from_authorized_user_file') as mock_cred, \
patch('google.auth.transport.requests.Request'), \
patch('googleapiclient.discovery.build') as mock_build, \
patch('builtins.open', mock_open()):

# Setup mock credentials
mock_cred.return_value.valid = False
mock_cred.return_value.expired = True
mock_cred.return_value.refresh_token = True

# Refresh method does nothing but update valid state
def refresh(request):
mock_cred.return_value.valid = True

mock_cred.return_value.refresh = refresh

# Setup Google API client mock
mock_service = mock_build.return_value
mock_tasklists = mock_service.tasklists().list().execute.return_value = MOCK_TASKLISTS
mock_tasks = mock_service.tasks().list().execute.return_value = MOCK_TASKS

# Execute function
results = get_tasks()

# Assert results
assert len(results) == 1
assert results[0]['title'] == "Task 1"

def test_get_tasks_no_tasklists():
with patch('os.path.exists', return_value=True), \
patch('google.oauth2.credentials.Credentials.from_authorized_user_file') as mock_cred, \
patch('googleapiclient.discovery.build') as mock_build:

# Setup mock credentials
mock_cred.return_value.valid = True

# Setup Google API client mock
mock_service = mock_build.return_value
mock_service.tasklists().list().execute.return_value = {"items": []}

# Execute function
results = get_tasks()

# Assert results
assert results == []
Loading

0 comments on commit 46ef9fa

Please sign in to comment.