Skip to content

Commit

Permalink
Update tests and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Yanglin-Tao committed May 12, 2024
1 parent 46ef9fa commit 6ff83d2
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 43 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ Frontend HTML templates can be found in `templates/` folder, such as `index.html

Static elements, scripts, and styles can be found in `static/` folder, such as `style.css`.

### Testing
#### Frontend tests
Before running frontend tests, ensure you have [Chrome Driver](https://chromedriver.chromium.org/downloads) installed and updated to latest version. Then run the following command:

pdm run pytest tests/test_index.py

### Backend tests [Work in Progress]
Run the following command to run backend tests:

pdm run pytest tests/test_crawl_tasks.py

### Making Contributions
### Issues
1. Create an Issue: Before you make significant changes or improvements, please check if an existing issue already addresses your concern. If not, submit a new issue providing as much relevant information as possible.
Expand Down
61 changes: 33 additions & 28 deletions tests/test_crawl_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,24 @@
# Sample data for mocking responses
MOCK_TASKS = {
"items": [
{"title": "Task 1", "id": "1", "tasklist_id": "tl1", "tasklist_title": "TaskList 1"}
{
"title": "Task 1",
"id": "1",
"tasklist_id": "tl1",
"tasklist_title": "TaskList 1",
}
]
}

MOCK_TASKLISTS = {
"items": [
{"title": "TaskList 1", "id": "tl1"}
]
}
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:

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_cred.return_value.universe_domain = "googleapis.com"

# Mock Google API client setup
mock_service = MagicMock()
Expand All @@ -43,18 +44,20 @@ def test_get_tasks_existing_token_valid():

# Execute function
results = get_tasks()

# Assert results
assert len(results) == 1
assert results[0]['title'] == "Task 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()):

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
Expand All @@ -68,30 +71,32 @@ def refresh(request):

# Setup Google API client mock
mock_service = mock_build.return_value
mock_tasklists = mock_service.tasklists().list().execute.return_value = MOCK_TASKLISTS
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"
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:

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": []}
mock_service.tasklists().list().execute.return_value = {"items": []}

# Execute function
results = get_tasks()

# Assert results
assert results == []
assert results == []
30 changes: 15 additions & 15 deletions tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,40 @@
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


@pytest.fixture(scope="class")
def browser():
# Setup Selenium
service = Service(executable_path='/opt/homebrew/bin/chromedriver')
service = Service(executable_path="/opt/homebrew/bin/chromedriver")
options = Options()
options.add_argument('--headless')
options.add_argument("--headless")
driver = webdriver.Chrome(service=service, options=options)
driver.maximize_window()
driver.maximize_window()
driver.implicitly_wait(10)
yield driver
driver.quit()


class TestIndex:
# Setup browser
# Setup browser
@pytest.fixture(autouse=True)
def setup(self, browser):
self.browser = browser
self.browser.get("http://127.0.0.1:5000")
self.browser.get("http://127.0.0.1:5000")

# Check if page title is correctly displayed
def test_page_title(self):
expected_title = "Tasks Dashboard"
assert self.browser.title == expected_title, f"Expected page title to be '{expected_title}' but got '{self.browser.title}'"
assert (
self.browser.title == expected_title
), f"Expected page title to be '{expected_title}' but got '{self.browser.title}'"

# Check if username is correctly displayed
def test_username_displayed(self):
username_span = self.browser.find_element(By.CSS_SELECTOR, "span.username-placeholder")
expected_username = "username"
username_span = self.browser.find_element(
By.CSS_SELECTOR, "span.username-placeholder"
)
expected_username = "username"
assert username_span.text == expected_username

# Check if search bar is correctly displayed
Expand All @@ -44,7 +50,7 @@ def test_search_bar_displayed(self):

# Check if task lists are correctly displayed
def test_task_categories_displayed(self):
task_categories = self.browser.find_element(By.ID, 'task-categories')
task_categories = self.browser.find_element(By.ID, "task-categories")
assert task_categories.is_displayed()

# Check if task itms are correctly displayed
Expand All @@ -56,9 +62,3 @@ def test_task_item_displayed(self):
def test_dropdown_displayed(self):
dropdown_button = self.browser.find_element(By.CSS_SELECTOR, ".dropdown-toggle")
assert dropdown_button.is_displayed(), "Dropdown button is not displayed"

# Check if checkbox correctly displayed and unselected
def test_task_checkbox_displayed(self):
checkbox = self.browser.find_element(By.ID, "task1")
assert checkbox.is_displayed()
assert not checkbox.is_selected()

0 comments on commit 6ff83d2

Please sign in to comment.