Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/backend_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:

- name: Run tests and build coverage file
run: |
export PYTHONPATH=. pytest
export PYTHONPATH=backend pytest
pytest --cache-clear --cov=. --cov-report term-missing backend/tests/

- name: Format with black
Expand Down
2 changes: 1 addition & 1 deletion backend/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def add_run(request):
converted_run_name, additional_message = sanitize_name(run_name)

try:
Run(converted_run_name, workflow_name, df_mode_name,)
Run(converted_run_name, workflow_name, df_mode_name)
message = f"Created run {converted_run_name}. \n{additional_message}" if len(additional_message) > 0 else f"Created run {converted_run_name}."
return JsonResponse({"success": True, "message": message, "data": {"run_name": converted_run_name}})
except Exception as e:
Expand Down
20 changes: 13 additions & 7 deletions backend/protzilla/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from backend.protzilla.form import Form
from backend.protzilla.steps import Messages, Output, Plots, Step, StepManager
from backend.protzilla.utilities import format_trace
from backend.protzilla.workflow import get_available_workflow_names


def get_available_run_names() -> list[str]:
Expand Down Expand Up @@ -64,7 +65,7 @@ def get_available_run_info() -> str | tuple[
metadata = {}
tags = metadata.get("tags", set())

run_name = {
run = {
"run_name": run_name,
"creation_date": metadata.get("creation_date", "date not available"),
"modification_date": metadata.get("modification_date", "date not available"),
Expand All @@ -74,10 +75,10 @@ def get_available_run_info() -> str | tuple[
"run_tags": list(tags)
}

if run_name["favourite_status"]:
runs_favourited.append(run_name)
if run["favourite_status"]:
runs_favourited.append(run)
else:
runs.append(run_name)
runs.append(run)

for tag in tags:
all_tags.add(tag)
Expand Down Expand Up @@ -176,12 +177,13 @@ def __init__(

if run_name in get_available_run_names():
self._run_read()
elif workflow_name:
elif workflow_name and workflow_name in get_available_workflow_names():
self.df_mode = df_mode
self._workflow_read()
else:
raise ValueError(
f"No run named {run_name} has been found and no workflow has been provided. Please reference an existing run or provide a workflow to create a new one."
self.__class__._instances.pop(run_name)
raise FileNotFoundError(
f"No run named {run_name} or workflow named {workflow_name} has been found. Please reference an existing run or workflow to create a new one."
)

self._initialized = True
Expand Down Expand Up @@ -212,12 +214,16 @@ def run_path(self) -> str:
@auto_save
def update_run_name(self, new_run_name: str) -> None:
if self.run_name != new_run_name:
old_name = self.run_name
self.disk_operator.update_run_name(new_run_name)
self.update_modification_date()
self._instances.pop(self.run_name, None)
self._instances[new_run_name] = self
self.run_name = new_run_name

self.__class__._instances[new_run_name] = self
self.__class__._instances.pop(old_name, None)

@error_handling
def metadata_read(self) -> dict:
return self.disk_operator.read_metadata()
Expand Down
2 changes: 1 addition & 1 deletion backend/protzilla/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def __init__(
if run_name is not None and run_name.strip() is not None
else f"runner_{random_string()}"
)

print("PATH: ", RUNS_PATH, self.run_name)
if os.path.exists(Path(f"{RUNS_PATH}/{self.run_name}")):
self._overwrite_run_prompt()
print("\n\n")
Expand Down
61 changes: 44 additions & 17 deletions backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import time
import uuid
import tempfile
from pathlib import Path
from shutil import rmtree

Expand All @@ -11,11 +12,14 @@
import pytest
from PIL import Image

from django.conf import settings
from unittest.mock import patch

from backend.protzilla.methods.importing import MaxQuantImport
from backend.protzilla.run import Run

from backend.protzilla.constants.paths import RUNS_PATH
from backend.tests.paths import TEST_METADATA_PATH, TEST_MSDATA_PATH
from backend.tests.paths import TEST_METADATA_PATH, TEST_MSDATA_PATH, TEST_TEMPLATE_PATH, TEST_RUNS_PATH, TEST_WORKFLOWS_PATH
from backend.protzilla.utilities import random_string


Expand All @@ -27,21 +31,39 @@ def pytest_addoption(parser):
help="If 'True', tests will open figures using the default renderer",
)

@pytest.fixture(scope="session",autouse=True)
def setup_paths_and_cleanup():

TEST_RUNS_PATH.mkdir(parents=True, exist_ok=True)

with (
patch("backend.protzilla.constants.paths.RUNS_PATH", TEST_RUNS_PATH),
patch("backend.protzilla.runner.RUNS_PATH", TEST_RUNS_PATH),
patch("backend.protzilla.data_analysis.protein_graphs.RUNS_PATH", TEST_RUNS_PATH),
patch("backend.protzilla.constants.paths.WORKFLOWS_PATH", TEST_WORKFLOWS_PATH)
):
yield

# After the test or fixture that uses this fixture is done, remove the directory
while TEST_RUNS_PATH.exists():
time.sleep(1)
rmtree(TEST_RUNS_PATH)



@pytest.fixture(scope="function")
def run_name_and_cleanup():
def run_name():
# Generate a unique run name
run_name = f"test_run_{uuid.uuid4()}"
run_path = Path(RUNS_PATH) / run_name

# Yield the run name to the test or fixture that uses this fixture
yield run_name

# After the test or fixture that uses this fixture is done, remove the directory
while run_path.exists():
time.sleep(1)
rmtree(run_path)

@pytest.fixture(scope="session")
def static_run_name():
#provide a static run_name to use in multiple tests
static_run_name = "static_run_name"
yield static_run_name

@pytest.fixture
def maxquant_data_file():
Expand All @@ -53,20 +75,20 @@ def metadata_file():


@pytest.fixture(scope="function")
def run_standard(run_name_and_cleanup):
run_name = run_name_and_cleanup
def run_standard(run_name):
run_name = run_name
yield Run(run_name=run_name, workflow_name="standard", df_mode="memory")


@pytest.fixture(scope="function")
def run_empty(run_name_and_cleanup):
run_name = run_name_and_cleanup
def run_empty(run_name):
run_name = run_name
yield Run(run_name=run_name, workflow_name="test-run-empty", df_mode="memory")


@pytest.fixture(scope="function")
def run_imported(run_name_and_cleanup, maxquant_data_file):
run_name = run_name_and_cleanup
def run_imported(run_name, maxquant_data_file):
run_name = run_name
run = Run(run_name=run_name, workflow_name="test-run-empty", df_mode="memory")
run.step_add(MaxQuantImport())
run.current_form(
Expand All @@ -90,9 +112,6 @@ def show_figures(request):
def tests_folder_name():
name = f"tests_{random_string()}"
yield name
while Path(f"{RUNS_PATH}/{name}").exists():
time.sleep(1)
rmtree(Path(f"{RUNS_PATH}/{name}"))


@pytest.fixture
Expand Down Expand Up @@ -265,3 +284,11 @@ def open_graph_from_base64(encoded_string):
@pytest.fixture
def helpers():
return Helpers

@pytest.fixture(scope="session", autouse=True)
def test_template_dir():
"""
Adds the backend/tests/templates directory to Django's template DIRS during tests,
so that test-only templates like dummy index.html can be used.
"""
settings.TEMPLATES[0]['DIRS'].insert(0, TEST_TEMPLATE_PATH)
12 changes: 12 additions & 0 deletions backend/tests/main/test_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.test import TestCase
from django.urls import reverse

class FrontendRoutingTest(TestCase):

def test_get_csrf_token_url(self):
response = self.client.get(reverse('get_csrf_token'))
self.assertEqual(response.status_code, 200)

def test_random_route(self):
response = self.client.get('/some-random-route/')
self.assertTemplateUsed(response, 'index.html')
Loading
Loading