Skip to content

Commit

Permalink
Add template ids (#158)
Browse files Browse the repository at this point in the history
* Remove unnecessary ids in end test request

* Add template_id to TestCase model

* Store template_id received from end test request

* Expose template_id in test results report

* Update template id even for existing test cases

* Only set template id if it was provided

* Use more correct template_id in test case
  • Loading branch information
omar-selo authored Apr 17, 2024
1 parent a7b2718 commit 6eb786f
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Add template id to test case
Revision ID: 624a270a03dc
Revises: ae281506fe32
Create Date: 2024-04-17 11:18:27.430695+00:00
"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "624a270a03dc"
down_revision = "ae281506fe32"
branch_labels = None
depends_on = None


def upgrade() -> None:
op.add_column("test_case", sa.Column("template_id", sa.String(), nullable=False))


def downgrade() -> None:
op.drop_column("test_case", "template_id")
1 change: 1 addition & 0 deletions backend/test_observer/controllers/reports/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
TestExecution.review_comment,
Environment.name,
Environment.architecture,
TestCase.template_id,
TestCase.name,
TestCase.category,
TestResult.status,
Expand Down
3 changes: 3 additions & 0 deletions backend/test_observer/controllers/test_executions/end_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ def _store_test_results(
creation_kwargs={"category": r.category},
)

if r.template_id:
test_case.template_id = r.template_id

test_result = TestResult(
test_case=test_case,
test_execution=test_execution,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class C3TestResultStatus(str, Enum):

class C3TestResult(BaseModel):
name: str
template_id: str | None = None
status: C3TestResultStatus
category: str
comment: str
Expand Down
5 changes: 3 additions & 2 deletions backend/test_observer/data_access/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@
UniqueConstraint,
column,
)
from sqlalchemy.engine.default import DefaultExecutionContext
from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy.engine.default import DefaultExecutionContext
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import (
DeclarativeBase,
Mapped,
mapped_column,
relationship,
)
from sqlalchemy.sql import func
from sqlalchemy.ext.hybrid import hybrid_property

from test_observer.data_access.models_enums import (
ArtefactStatus,
Expand Down Expand Up @@ -332,6 +332,7 @@ class TestCase(Base):

name: Mapped[str] = mapped_column(unique=True)
category: Mapped[str]
template_id: Mapped[str] = mapped_column(default="")

def __repr__(self) -> str:
return data_model_repr(self, "name", "category")
Expand Down
1 change: 1 addition & 0 deletions backend/tests/controllers/reports/test_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def _expected_report_row(test_result: TestResult) -> list:
test_execution.review_comment,
environment.name,
environment.architecture,
test_case.template_id,
test_case.name,
test_case.category,
test_result.status.name,
Expand Down
45 changes: 38 additions & 7 deletions backend/tests/controllers/test_executions/test_end_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,19 @@ def test_report_test_execution_data(test_client: TestClient, generator: DataGene
response = test_client.put(
"/v1/test-executions/end-test",
json={
"id": 1,
"ci_link": test_execution.ci_link,
"c3_link": c3_link,
"test_results": [
{
"id": 1,
"name": test_case.name,
"status": "pass",
"category": test_case.category,
"comment": "",
"io_log": "",
},
{
"id": 2,
"name": "test-name-2",
"name": "disk/stats_nvme0n1",
"template_id": "disk/stats_name",
"status": "skip",
"category": "",
"comment": "",
Expand All @@ -66,8 +64,10 @@ def test_report_test_execution_data(test_client: TestClient, generator: DataGene
assert test_execution.c3_link == c3_link
assert test_execution.test_results[0].test_case.name == test_case.name
assert test_execution.test_results[0].status == TestResultStatus.PASSED
assert test_execution.test_results[1].test_case.name == "test-name-2"
assert test_execution.test_results[0].test_case.template_id == test_case.template_id
assert test_execution.test_results[1].test_case.name == "disk/stats_nvme0n1"
assert test_execution.test_results[1].status == TestResultStatus.SKIPPED
assert test_execution.test_results[1].test_case.template_id == "disk/stats_name"


def test_end_test_is_idempotent(test_client: TestClient, generator: DataGenerator):
Expand All @@ -82,11 +82,9 @@ def test_end_test_is_idempotent(test_client: TestClient, generator: DataGenerato
test_client.put(
"/v1/test-executions/end-test",
json={
"id": 1,
"ci_link": test_execution.ci_link,
"test_results": [
{
"id": 1,
"name": "test name",
"status": "pass",
"category": "test category",
Expand Down Expand Up @@ -260,3 +258,36 @@ def test_end_test_uses_test_execution_of_latest_build(
assert curr_test_execution.review_decision == [
TestExecutionReviewDecision.APPROVED_ALL_TESTS_PASS
]


def test_end_test_updates_template_id(
test_client: TestClient, generator: DataGenerator
):
artefact = generator.gen_artefact("beta")
artefact_build = generator.gen_artefact_build(artefact)
environment = generator.gen_environment()
test_execution = generator.gen_test_execution(
artefact_build, environment, ci_link="http://localhost"
)
test_case = generator.gen_test_case(template_id="")

response = test_client.put(
"/v1/test-executions/end-test",
json={
"ci_link": test_execution.ci_link,
"c3_link": "",
"test_results": [
{
"name": test_case.name,
"status": "pass",
"category": test_case.category,
"template_id": "some template id",
"comment": "",
"io_log": "",
}
],
},
)

assert response.status_code == 200
assert test_execution.test_results[0].test_case.template_id == "some template id"
7 changes: 6 additions & 1 deletion backend/tests/data_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,13 @@ def gen_test_case(
self,
name: str = "camera/detect",
category: str = "camera",
template_id: str = "",
) -> TestCase:
test_case = TestCase(name=name, category=category)
test_case = TestCase(
name=name,
category=category,
template_id=template_id,
)
self.db_session.add(test_case)
self.db_session.commit()
return test_case
Expand Down

0 comments on commit 6eb786f

Please sign in to comment.