Skip to content

Commit

Permalink
#203 Finish translating existing commit dialog tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexhad6 committed Jan 2, 2024
1 parent ec0eb77 commit dc44d51
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 37 deletions.
2 changes: 1 addition & 1 deletion cypress/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def start() -> None:
from paramview import start_server # pylint: disable=import-outside-toplevel

reset()
start_server(DB_PATH, auto_open=False)
start_server(DB_PATH, open_window=True)


def reset(single: bool = False, long: bool = False) -> None:
Expand Down
18 changes: 9 additions & 9 deletions src/components/ParamSection/CommitDialog/ComparisonList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,15 @@ export default function ComparisonList({ shouldUpdate }: ComparisonListProps) {

return (
<Box sx={comparisonListContainerSx}>
{dataDiff === null ? (
<Typography>{`No changes from ${latestCommitDescription}`}</Typography>
) : (
<>
<Typography>{`Changes from ${latestCommitDescription}`}</Typography>
<List disablePadding sx={rootListSx}>
<DataDiffListItem dataDiff={dataDiff} />
</List>
</>
<Typography data-testid="commit-changes-message">
{dataDiff === null
? `No changes from ${latestCommitDescription}`
: `Changes from ${latestCommitDescription}`}
</Typography>
{dataDiff !== null && (
<List disablePadding sx={rootListSx}>
<DataDiffListItem dataDiff={dataDiff} />
</List>
)}
</Box>
);
Expand Down
17 changes: 11 additions & 6 deletions tests/e2e/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ def reset_db(num_commits: int = 1) -> None:
"""Clear the database and make some initial commits."""
clear_db()
with freeze_time(get_datetime(1)):
param = CustomParam(int=123, str="test")
# Convert last updated from freezegun.api.FakeDatetime to a true datetime
setattr(
param,
"_Param__last_updated",
datetime.fromtimestamp(param.last_updated.timestamp()),
)
initial_data = ParamDict(
{
"commit_id": 1,
Expand All @@ -101,10 +108,8 @@ def reset_db(num_commits: int = 1) -> None:
"dict": {"int": 123, "str": "test"},
"paramList": ParamList([123, "test"]),
"paramDict": ParamDict(int=123, str="test"),
"struct": CustomStruct(
int=123, str="test", param=CustomParam(int=123, str="test")
),
"param": CustomParam(int=123, str="test"),
"struct": CustomStruct(int=123, str="test", param=param),
"param": param,
}
)
commit_to_db(initial_data)
Expand Down Expand Up @@ -136,7 +141,7 @@ class CaptureDialogs:
class CommitInfo:
"""Information for a given commit."""

def __init__(self, commit_id: int) -> None:
def __init__(self, commit_id: int, message: str | None = None) -> None:
self.id = commit_id
self.date = get_datetime_display(commit_id)
self.message = get_commit_message_display(commit_id)
self.message = get_commit_message_display(commit_id, message)
125 changes: 106 additions & 19 deletions tests/e2e/test_commit_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,39 @@

import pytest
from playwright.sync_api import Page, expect
from tests.e2e.helpers import reset_db
from tests.e2e.helpers import (
CommitInfo,
reset_db,
load_classes_from_db,
get_datetime_input,
)

NUM_COMMITS = 3
NEW_COMMIT_MESSAGE = "New commit"
FIRST_COMMIT = CommitInfo(1)
NEW_COMMIT = CommitInfo(2, NEW_COMMIT_MESSAGE)


@pytest.fixture(autouse=True)
def setup(page: Page) -> None:
"""Automatically run before each test in this module."""
reset_db(NUM_COMMITS)
reset_db()
page.goto("/")
page.get_by_test_id("edit-button").click()


def test_refuses_to_commit_with_no_message(page: Page) -> None:
"""Refuses to make a commit if there is no message."""
commit_message = page.get_by_test_id("commit-message-text-field")
commit_message_input = commit_message.get_by_role("textbox")

# Attempt to make a commit
page.get_by_test_id("open-commit-dialog-button").click()
page.get_by_test_id("make-commit-button").click()

# Message field exists and is enabled, meaning a commit was not made
expect(commit_message_input).to_be_enabled()


def test_reopen_resets_commit_message(page: Page) -> None:
"""Commit message resets when the commit dialog is reopened."""
open_commit_dialog_button = page.get_by_test_id("open-commit-dialog-button")
Expand All @@ -23,8 +43,8 @@ def test_reopen_resets_commit_message(page: Page) -> None:

# Open commit dialog and type a new commit
open_commit_dialog_button.click()
commit_message_input.fill("New commit")
expect(commit_message_input).to_have_value("New commit")
commit_message_input.fill(NEW_COMMIT_MESSAGE)
expect(commit_message_input).to_have_value(NEW_COMMIT_MESSAGE)

# Close commit dialog
page.get_by_test_id("close-commit-dialog-button").click()
Expand All @@ -34,25 +54,92 @@ def test_reopen_resets_commit_message(page: Page) -> None:
expect(commit_message_input).to_have_value("")


def test_refuses_to_commit_with_no_message(page: Page) -> None:
"""Refuses to make a commit if there is no message."""
# pylint: disable-next=too-many-locals
def test_make_commit(page: Page) -> None:
"""Can edit data and make a commit."""
commit_select_combobox = page.get_by_test_id("commit-select-combobox")
commit_select_combobox_input = commit_select_combobox.get_by_role("combobox")
commit_message = page.get_by_test_id("commit-message-text-field")
commit_message_input = commit_message.get_by_role("textbox")

# Attempt to make a commit
page.get_by_test_id("open-commit-dialog-button").click()
page.get_by_test_id("make-commit-button").click()
int_item = page.get_by_test_id("parameter-list-item-int")
int_item_input = int_item.get_by_test_id("leaf-input").get_by_role("textbox")
int_old_item = page.get_by_test_id("comparison-list-item-old-int")
int_new_item = page.get_by_test_id("comparison-list-item-new-int")

# Message field exists and is enabled, meaning a commit was not made
expect(commit_message_input).to_be_enabled()
float_item = page.get_by_test_id("parameter-list-item-float")
float_item_input = float_item.get_by_test_id("leaf-input").get_by_role("textbox")
float_old_item = page.get_by_test_id("comparison-list-item-old-float")
float_new_item = page.get_by_test_id("comparison-list-item-new-float")

str_item = page.get_by_test_id("parameter-list-item-str")
str_item_input = str_item.get_by_test_id("leaf-input").get_by_role("textbox")
str_old_item = page.get_by_test_id("comparison-list-item-old-str")
str_new_item = page.get_by_test_id("comparison-list-item-new-str")

# Initial values
expect(int_item_input).to_have_value("123")
expect(float_item_input).to_have_value("1.2345")
expect(str_item_input).to_have_value("test")

# def test_make_commit(page: Page) -> None:
# """Can edit data and make a commit."""
# Edit parameter values
int_item_input.fill("456")
float_item_input.fill("5.6789")

# Opem commit dialog
page.get_by_test_id("open-commit-dialog-button").click()

# Commit list reflects what was changed
expect(page.get_by_test_id("commit-changes-message")).to_have_text(
f"Changes from latest commit ({FIRST_COMMIT.message})"
)
expect(int_old_item).to_have_text("int123")
expect(int_new_item).to_have_text("int456")
expect(float_old_item).to_have_text("float1.2345")
expect(float_new_item).to_have_text("float5.6789")
expect(str_old_item).not_to_be_attached()
expect(str_new_item).not_to_be_attached()

# Enter message and make commit
commit_message_input.fill(NEW_COMMIT_MESSAGE)
commit_message_input.press("Enter")
expect(commit_select_combobox_input).to_have_value(NEW_COMMIT.message)

# Values were updated
expect(int_item).to_have_text("int456")
expect(float_item).to_have_text("float5.679")
expect(str_item).to_have_text("strtest")


def test_commit_backend_format(page: Page) -> None:
"""
Commits to the backend in the correct format for datetime and Quantity and (i.e. the
format that allows them to be read by Python).
"""
commit_message = page.get_by_test_id("commit-message-text-field")
commit_message_input = commit_message.get_by_role("textbox")

datetime_item = page.get_by_test_id("parameter-list-item-datetime")
datetime_item_input = datetime_item.get_by_test_id("leaf-input").locator(
"input[type=datetime-local]"
)
quantity_item = page.get_by_test_id("parameter-list-item-Quantity")
quantity_item_input = quantity_item.get_by_test_id("leaf-input").get_by_role(
"textbox"
)
quantity_item_unit_input = quantity_item.get_by_test_id(
"leaf-unit-input"
).get_by_role("textbox")

# Update datetime and quantity
datetime_item_input.fill(get_datetime_input(2))
quantity_item_input.fill("5.6789")
quantity_item_unit_input.fill("GHz")

# Make commit
page.get_by_test_id("open-commit-dialog-button").click()
commit_message_input.fill(NEW_COMMIT_MESSAGE)
commit_message_input.press("Enter")

# def test_commit_format(page: Page) -> None:
# """
# Commits in the correct format for datetime and Quantity and (i.e. the format that
# allows them to be read by Python).
# """
# Load classes in the backend (test fails if this fails)
load_classes_from_db()
2 changes: 1 addition & 1 deletion tests/e2e/test_commit_selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest
from playwright.sync_api import Page, expect
from tests.e2e.helpers import reset_db, commit_to_db, CommitInfo
from tests.e2e.helpers import CommitInfo, reset_db, commit_to_db

NUM_COMMITS = 3
FIRST_COMMIT = CommitInfo(1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Tests for parameter navigation."""
"""Tests for parameter viewing."""

import pytest
from playwright.sync_api import Page, expect
Expand Down

0 comments on commit dc44d51

Please sign in to comment.