Files
calminer/tests/e2e/test_consumption.py

49 lines
1.7 KiB
Python

from uuid import uuid4
from playwright.sync_api import Page, expect
def test_consumption_form_loads(page: Page):
"""Verify the consumption form page loads correctly."""
page.goto("/ui/consumption")
expect(page).to_have_title("Consumption · CalMiner")
expect(page.locator("h2:has-text('Add Consumption Record')")).to_be_visible()
def test_create_consumption_item(page: Page):
"""Test creating a new consumption item through the UI."""
# First, create a scenario to associate the consumption with.
page.goto("/ui/scenarios")
scenario_name = f"Consumption Test Scenario {uuid4()}"
page.fill("input[name='name']", scenario_name)
page.click("button[type='submit']")
with page.expect_response("**/api/scenarios/"):
pass # Wait for the scenario to be created
# Now, navigate to the consumption page and add an item.
page.goto("/ui/consumption")
# Create a consumption item.
consumption_desc = "Diesel for generators"
page.select_option("#consumption-form-scenario", label=scenario_name)
page.fill("textarea[name='description']", consumption_desc)
page.fill("input[name='amount']", "5000")
page.click("button[type='submit']")
with page.expect_response("**/api/consumption/") as response_info:
pass
assert response_info.value.status == 201
# Verify the new item appears in the table.
page.select_option("#consumption-scenario-filter", label=scenario_name)
expect(
page.locator("#consumption-table-body tr").filter(
has_text=consumption_desc
)
).to_be_visible()
# Verify the feedback message.
expect(page.locator("#consumption-feedback")).to_have_text(
"Consumption record saved."
)