Skip to content

Commit

Permalink
add unit test for web
Browse files Browse the repository at this point in the history
  • Loading branch information
superstar54 committed Dec 13, 2023
1 parent 7aa929f commit 922b3aa
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 8 deletions.
10 changes: 3 additions & 7 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,10 @@ jobs:
run: sudo apt update && sudo apt install --no-install-recommends libfftw3-dev quantum-espresso

- name: Install Python dependencies
run: pip install -e .[pre-commit,tests]

- name: Copy custom node file, add custom node path to sys path
run: |
mkdir ~/.scinode
mkdir ~/.scinode/custom_node
export PYTHONPATH=~/.scinode/custom_node/:$PYTHONPATH
echo "PYTHONPATH=$PYTHONPATH" >> $GITHUB_ENV
pip install -e .[pre-commit,tests]
playwright install
- name: Create AiiDA profile
run: verdi setup -n --config .github/config/profile.yaml

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,4 @@ dmypy.json
#
*.pdf
tests/work
/tests/**/*.png
2 changes: 1 addition & 1 deletion aiida_worktree/web/frontend/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
<title>AiiDA-WorkTree App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pre-commit = [
tests = [
"pytest~=7.0",
"pytest-cov~=2.7,<2.11",
"playwright",
]

[project.scripts]
Expand Down
Empty file added pytest.ini
Empty file.
8 changes: 8 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

## Requirements

In order to run the test for the web frontend, one need to install

```console
playwright install
```
27 changes: 27 additions & 0 deletions tests/web/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import pytest
from fastapi.testclient import TestClient
from aiida_worktree.web.backend.app.api import app
from playwright.sync_api import sync_playwright


# Define a fixture for the FastAPI app client
@pytest.fixture(scope="module")
def client():
return TestClient(app)


# Define a fixture for the browser
@pytest.fixture(scope="module")
def browser():
with sync_playwright() as p:
browser = p.chromium.launch()
yield browser
browser.close()


# Define a fixture for the page
@pytest.fixture(scope="module")
def page(browser):
with browser.new_page() as page:
yield page
page.close()
9 changes: 9 additions & 0 deletions tests/web/test_backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Sample test case for the root route
def test_root_route(client):
response = client.get("/api")
assert response.status_code == 200
assert response.json() == {"message": "Welcome to AiiDA-WorkTree."}


# Add more test cases for your other routes and features
# For example, testing authentication, database interactions, etc.
63 changes: 63 additions & 0 deletions tests/web/test_frontend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import pytest


def test_homepage(page):
page.goto("http://localhost:3000")

assert page.title() == "AiiDA-WorkTree App"

# Check for the existence of a specific element on the page
# Attempt to locate the element
element = page.locator("a[href='/worktree']")

# Check if the element is found
if not element.is_visible():
pytest.fail("Element 'a[href='/wortre']' not found on the page")


def test_worktree(page):
page.goto("http://localhost:3000/worktree")

# Check for the existence of a specific element on the page

# Verify the presence of the WorkTreeTable heading
assert page.locator("h2").inner_text() == "WorkTree"

# Verify the presence of the search input
assert page.locator(".search-input").is_visible()

# Verify the presence of the table header columns
# Verify the presence of the table header columns
assert page.locator("th:has-text('PK')").is_visible()
assert page.locator("th:has-text('Created')").is_visible()
assert page.locator("th:has-text('Process Label')").is_visible()
assert page.locator("th:has-text('State')").is_visible()
assert page.locator("th:has-text('Actions')").is_visible()

# Verify the presence of pagination controls
assert page.locator(".pagination").is_visible()

# Verify the presence of at least one row in the table
assert page.locator("tr").count() >= 2 # Including header row


def test_worktree_item(page, wt_calcfunction):

wt = wt_calcfunction
wt.submit(wait=True)
page.goto("http://localhost:3000/worktree/{}".format(wt.pk))
page.wait_for_timeout(3000)

page.get_by_text("sumdiff3").is_visible()

# Simulate user interaction (e.g., clicking a button)
# Replace the selector with the actual selector of the button you want to click
# You should identify the button that triggers an action in your component
page.get_by_role("button", name="Arrange").click()
page.wait_for_timeout(3000)
# Capture a screenshot
screenshot = page.screenshot()

# Save the screenshot to a file
with open("screenshot.png", "wb") as f:
f.write(screenshot)

0 comments on commit 922b3aa

Please sign in to comment.