Skip to content

Commit

Permalink
Merge pull request #213 from notoraptor/cw-482-anonymize-frontend-tests
Browse files Browse the repository at this point in the history
[CW-482] S'assurer que les tests frontend ne dépendent pas des fake_data actuels (sauf pour les noms des fake users)
  • Loading branch information
soline-b authored Jan 17, 2025
2 parents e2d3b0e + 116fdfc commit 39486bb
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 42 deletions.
8 changes: 5 additions & 3 deletions clockwork_frontend_test/test_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
DASHBOARD_TABLE_CONTENT.append(
[
job["slurm"]["cluster_name"],
job["slurm"]["job_id"],
int(
job["slurm"]["job_id"]
), # job ID is currently handled as a numeric value
job["slurm"]["name"],
job["slurm"]["job_state"].lower(),
get_default_display_date(job["slurm"]["submit_time"]),
Expand Down Expand Up @@ -116,7 +118,7 @@ def test_dashboard_table_default_content(page: Page):
cols = rows.nth(index_row).locator("td")
expect(cols).to_have_count(8)
for index_col, content_col in enumerate(content_row):
expect(cols.nth(index_col)).to_contain_text(content_col)
expect(cols.nth(index_col)).to_contain_text(str(content_col))


def test_dashboard_table_sorting(page: Page):
Expand Down Expand Up @@ -195,4 +197,4 @@ def _check_dashboard_table(page: Page, table_content: list):
cols = rows.nth(index_row).locator("td")
expect(cols).to_have_count(8)
for index_col, content_col in enumerate(content_row):
expect(cols.nth(index_col)).to_contain_text(content_col)
expect(cols.nth(index_col)).to_contain_text(str(content_col))
8 changes: 6 additions & 2 deletions clockwork_frontend_test/test_jobs_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ def _load_all_jobs_search_page(page: Page):
page.goto(f"{BASE_URL}/jobs/search?nbr_items_per_page={len(sorted_jobs)}")


def _check_jobs_table(page: Page, table_content: list):
def _check_jobs_table(page: Page, table_content: list, expect_content=True):
"""Check jobs table contains expected table content.
table_content is a list or rows, each row is a list of texts expected in related columns.
"""
if expect_content:
assert table_content
table = page.locator("table#search_table")
expect(table).to_have_count(1)
rows = table.locator("tbody tr")
Expand Down Expand Up @@ -528,7 +530,7 @@ def test_multiple_filters(page: Page):
and get_inferred_job_state(job["slurm"]["job_state"]) != "PENDING"
][:40]

_check_jobs_table(page, expected_results)
_check_jobs_table(page, expected_results, expect_content=False)

# Reset all filters.

Expand Down Expand Up @@ -561,6 +563,8 @@ def test_filter_by_job_array(page: Page):
for searched_job in sorted_jobs:
if searched_job["slurm"]["array_job_id"] != "0":
break
else:
raise AssertionError("No job found with a valid array_job_id")
searched_array_job_id = searched_job["slurm"]["array_job_id"]

expected_results = [
Expand Down
33 changes: 0 additions & 33 deletions scripts/ensure_one_fake_admin_in_db.py

This file was deleted.

34 changes: 34 additions & 0 deletions scripts/insert_hardcoded_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import json
import os
import sys
import random


def get_jobs_hardcoded_values():
Expand Down Expand Up @@ -80,6 +81,33 @@ def get_job_user_props_hardcoded_values(fake_data: dict):
]


def ensure_admin_users(fake_data: dict):
"""Make sure there is at least 1 fake admin."""
users = fake_data["users"]
admin_users = [user for user in users if user.get("admin_access", False)]
if not admin_users and users:
users[0]["admin_access"] = True
assert [user for user in fake_data["users"] if user.get("admin_access", False)]


def ensure_job_arrays(fake_data: dict):
"""Make sure some fake jobs belong to valid job arrays."""
jobs_with_array_id = [
job for job in fake_data["jobs"] if job["slurm"]["array_job_id"] != "0"
]
if not jobs_with_array_id:
# No yet jobs in valid job arrays.
# Add 2 jobs to 2 separate job arrays.
nb_fake_jobs = len(fake_data["jobs"])
assert nb_fake_jobs >= 2
id_job_1 = random.randint(0, nb_fake_jobs)
id_job_2 = (id_job_1 + 1) % nb_fake_jobs
fake_data["jobs"][id_job_1]["slurm"]["array_job_id"] = "1234"
fake_data["jobs"][id_job_2]["slurm"]["array_job_id"] = "5678"

assert [job for job in fake_data["jobs"] if job["slurm"]["array_job_id"] != "0"]


def main(argv):

my_parser = argparse.ArgumentParser()
Expand Down Expand Up @@ -113,6 +141,12 @@ def main(argv):
# Insert fake job user props
fake_data["job_user_props"] = get_job_user_props_hardcoded_values(fake_data)

# Make sure there are some admin users
ensure_admin_users(fake_data)

# Make sure some jobs are in valid job arrays
ensure_job_arrays(fake_data)

# Write the new fake data in the output file
with open(output_file, "w") as f:
json.dump(fake_data, f, indent=2)
Expand Down
3 changes: 0 additions & 3 deletions scripts/launch_frontend_tests_in_clockwork_dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ playwright install chromium
echo Store fake data
python3 scripts/store_fake_data_in_db.py

echo Ensure at least 1 fake admin user
python3 scripts/ensure_one_fake_admin_in_db.py

echo Launch clockwork web server in background
python3 -m flask run --host="0.0.0.0" &

Expand Down
3 changes: 2 additions & 1 deletion test_common/fake_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"nbr_items_per_page": 40,
"dark_mode": false,
"language": "en"
}
},
"admin_access": true
},
{
"mila_email_username": "[email protected]",
Expand Down

0 comments on commit 39486bb

Please sign in to comment.