Skip to content

Commit

Permalink
[Fix] remove TestRunConfig usage from TH Backend (#581) (#6)
Browse files Browse the repository at this point in the history
* Add the repeat endpoint, deprecate the test_run_config endpoints and

force the test run execution creation to use selected tests, and not test_run_config_id.

* Deprecating unit tests related with
test_run_config, creating new endpoint with required selected_tests
parameter and ignoring the test_run_config reference, renaming the
repeat endpoint method and adding comments.

* Updating select_tests logic, moving to a utils
file, and adding unit test for the repeat endpoint

* Updating comments and adding missing attribution to
None for test_run_config_id

* Adding missing TODO comment.

* Fixing selected_tests logic

* Updating the 'repeat' unit test to use more
suites to cover more cases.

* Removing "/new" endpoint, updating logic of the
default test run execution endpoint and updating unit tests, "repeat"
endpoint and utils

* Adding the new DB column "collection_id" to the
TestSuiteExecution table. A DB revision file was created and the code
was refactored to use this new info.

* Removing unnecessary import
  • Loading branch information
antonio-amjr authored Nov 10, 2023
1 parent 077002c commit 96d93d8
Show file tree
Hide file tree
Showing 13 changed files with 1,173 additions and 730 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Adding the new column collection_id
Revision ID: 96ee37627a48
Revises: 9996326cbd1d
Create Date: 2023-08-15 14:42:39.893126
"""
import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision = "96ee37627a48"
down_revision = "9996326cbd1d"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"testsuiteexecution", sa.Column("collection_id", sa.String(), nullable=False)
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column("testsuiteexecution", "collection_id")
# ### end Alembic commands ###
8 changes: 4 additions & 4 deletions app/api/api_v1/endpoints/test_run_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
router = APIRouter()


@router.get("/", response_model=List[schemas.TestRunConfig])
@router.get("/", response_model=List[schemas.TestRunConfig], deprecated=True)
def read_test_run_configs(
db: Session = Depends(get_db),
skip: int = 0,
Expand All @@ -38,7 +38,7 @@ def read_test_run_configs(
return crud.test_run_config.get_multi(db, skip=skip, limit=limit)


@router.post("/", response_model=schemas.TestRunConfig)
@router.post("/", response_model=schemas.TestRunConfig, deprecated=True)
def create_test_run_config(
*,
db: Session = Depends(get_db),
Expand All @@ -56,7 +56,7 @@ def create_test_run_config(
)


@router.put("/{id}", response_model=schemas.TestRunConfig)
@router.put("/{id}", response_model=schemas.TestRunConfig, deprecated=True)
def update_test_run_config(
*,
db: Session = Depends(get_db),
Expand All @@ -78,7 +78,7 @@ def update_test_run_config(
return test_run_config


@router.get("/{id}", response_model=schemas.TestRunConfig)
@router.get("/{id}", response_model=schemas.TestRunConfig, deprecated=True)
def read_test_run_config(
*,
db: Session = Depends(get_db),
Expand Down
66 changes: 53 additions & 13 deletions app/api/api_v1/endpoints/test_run_executions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
from app.test_engine import TEST_ENGINE_ABORTING_TESTING_MESSAGE
from app.test_engine.test_runner import AbortError, LoadingError, TestRunner
from app.test_engine.test_script_manager import TestNotFound
from app.utils import (
formated_datetime_now_str,
remove_title_date,
selected_tests_from_execution,
)
from app.version import version_information

router = APIRouter()
Expand Down Expand Up @@ -72,21 +77,12 @@ def create_test_run_execution(
*,
db: Session = Depends(get_db),
test_run_execution_in: schemas.TestRunExecutionCreate,
selected_tests: Optional[schemas.TestSelection] = None,
selected_tests: schemas.TestSelection,
) -> TestRunExecution:
"""
Create new test run execution.
"""
test_run_config_present = test_run_execution_in.test_run_config_id is not None
selected_tests_present = selected_tests is not None

if test_run_config_present and selected_tests_present:
msg = "Only either test_run_config_id or selected_tests must be present"
raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail=msg)
"""Create a new test run execution."""

if not test_run_config_present and not selected_tests_present:
msg = "Either test_run_config_id or selected_tests must be present"
raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail=msg)
# TODO: Remove test_run_config completely from the project
test_run_execution_in.test_run_config_id = None

test_run_execution = crud.test_run_execution.create(
db=db, obj_in=test_run_execution_in, selected_tests=selected_tests
Expand Down Expand Up @@ -230,6 +226,50 @@ def unarchive(
return crud.test_run_execution.unarchive(db=db, db_obj=test_run_execution)


@router.post("/{id}/repeat", response_model=schemas.TestRunExecutionWithChildren)
def repeat_test_run_execution(
*, db: Session = Depends(get_db), id: int, title: Optional[str] = None
) -> TestRunExecution:
"""Repeat a test run execution by id.
Args:
id (int): test run execution id
title (str): Optional title to the repeated test run execution. If not provided,
the old title will be used with the date and time updated.
Raises:
HTTPException: if no test run execution exists for the provided id
Returns:
TestRunExecution: new test run execution with the same test cases from id
"""
execution_to_repeat = crud.test_run_execution.get(db=db, id=id)
if not execution_to_repeat:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="TestRunExecution not found"
)

if title is None:
# If no title is provided, the old title will be used without data info
title = remove_title_date(execution_to_repeat.title)

date_now = formated_datetime_now_str()
title += date_now

test_run_execution_in = schemas.TestRunExecutionCreate(title=title)
test_run_execution_in.description = execution_to_repeat.description
test_run_execution_in.project_id = execution_to_repeat.project_id
test_run_execution_in.operator_id = execution_to_repeat.operator_id
# TODO: Remove test_run_config completely from the project
test_run_execution_in.test_run_config_id = None

selected_tests = selected_tests_from_execution(execution_to_repeat)

return crud.test_run_execution.create(
db=db, obj_in=test_run_execution_in, selected_tests=selected_tests
)


@router.delete("/{id}", response_model=schemas.TestRunExecutionInDBBase)
def remove_test_run_execution(
*,
Expand Down
1 change: 1 addition & 0 deletions app/models/test_suite_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class TestSuiteExecution(Base):
id: Mapped[int] = mapped_column(primary_key=True, index=True)
public_id: Mapped[str] = mapped_column(nullable=False)
execution_index: Mapped[int] = mapped_column(nullable=False)
collection_id: Mapped[str] = mapped_column(nullable=False)

state: Mapped[TestStateEnum] = mapped_column(
Enum(TestStateEnum), nullable=False, default=TestStateEnum.PENDING
Expand Down
1 change: 1 addition & 0 deletions app/schemas/test_suite_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class TestSuiteExecutionBase(BaseModel):
state: TestStateEnum
public_id: str
execution_index: int
collection_id: str


# Properties shared by models stored in DB
Expand Down
9 changes: 7 additions & 2 deletions app/test_engine/test_script_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ def __pending_test_suites_for_test_collection(
test_suite = test_collection.test_suites[test_suite_id]

# Create pending test suite
test_suite_execution = self.__pending_test_suite_execution(test_suite)
test_suite_execution = self.__pending_test_suite_execution(
test_suite, test_collection
)

# Create pending test cases
test_cases = self.___pending_test_cases_for_test_suite(
Expand All @@ -130,6 +132,7 @@ def __pending_test_suites_for_test_collection(
def __pending_test_suite_execution(
self,
test_suite: TestSuiteDeclaration,
test_collection: TestCollectionDeclaration,
) -> TestSuiteExecution:
"""
This will create a DB entry for test suite.
Expand All @@ -139,7 +142,9 @@ def __pending_test_suite_execution(
metadata = self.__find_or_create_test_suite_metadata(test_suite=test_suite)

test_suite_execution = TestSuiteExecution(
public_id=metadata.public_id, test_suite_metadata=metadata
public_id=metadata.public_id,
test_suite_metadata=metadata,
collection_id=test_collection.name,
)
return test_suite_execution

Expand Down
5 changes: 5 additions & 0 deletions app/tests/api/api_v1/test_test_run_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
#
from http import HTTPStatus

import pytest
from fastapi.testclient import TestClient
from sqlalchemy.orm import Session

from app.core.config import settings
from app.tests.utils.test_run_config import create_random_test_run_config


@pytest.mark.deprecated("The test_run_config is now deprecated along with this test")
def test_create_test_run_config(client: TestClient, db: Session) -> None:
data = {
"name": "Foo",
Expand Down Expand Up @@ -50,6 +52,7 @@ def test_create_test_run_config(client: TestClient, db: Session) -> None:
assert "id" in content


@pytest.mark.deprecated("The test_run_config is now deprecated along with this test")
def test_create_test_run_config_invalid_selection(
client: TestClient, db: Session
) -> None:
Expand Down Expand Up @@ -77,6 +80,7 @@ def test_create_test_run_config_invalid_selection(
assert "detail" in content


@pytest.mark.deprecated("The test_run_config is now deprecated along with this test")
def test_read_test_run_config(client: TestClient, db: Session) -> None:
test_run_config = create_random_test_run_config(db)
response = client.get(
Expand All @@ -90,6 +94,7 @@ def test_read_test_run_config(client: TestClient, db: Session) -> None:
assert content["selected_tests"] == test_run_config.selected_tests


@pytest.mark.deprecated("The test_run_config is now deprecated along with this test")
def test_update_test_run_config(client: TestClient, db: Session) -> None:
test_run_config = create_random_test_run_config(db)
data = {"name": "Updated Name"}
Expand Down
Loading

0 comments on commit 96d93d8

Please sign in to comment.