Skip to content

Commit

Permalink
For non-kernel artefacts, set due date to be today + 10 days (#154)
Browse files Browse the repository at this point in the history
* For non-kernel artefacts, set due date to be today + 10 days

* Move tests to the approprite file

* Use context sensitive default instead of event listener
  • Loading branch information
nadzyah committed Apr 9, 2024
1 parent af981d1 commit 0c3ed72
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 4 deletions.
21 changes: 19 additions & 2 deletions backend/test_observer/data_access/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
# Written by:
# Nadzeya Hutsko <[email protected]>
# Omar Selo <[email protected]>
from datetime import date, datetime

from datetime import date, datetime, timedelta
from typing import TypeVar

from sqlalchemy import (
Expand All @@ -29,6 +30,7 @@
UniqueConstraint,
column,
)
from sqlalchemy.engine.default import DefaultExecutionContext
from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy.orm import (
DeclarativeBase,
Expand All @@ -37,6 +39,7 @@
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 @@ -77,6 +80,15 @@ def data_model_repr(obj: DataModel, *keys: str) -> str:
return f"{type(obj).__name__}({', '.join(kwargs)})"


def determine_due_date(context: DefaultExecutionContext):
name = context.get_current_parameters()["name"]
is_kernel = name.startswith("linux-") or name.endswith("-kernel")
if not is_kernel:
# If not a kernel, return a date 10 days from now
return date.today() + timedelta(days=10)
return None


class User(Base):
"""
ORM representing users that can be assigned to review artefacts
Expand Down Expand Up @@ -148,7 +160,7 @@ class Artefact(Base):
assignee_id: Mapped[int | None] = mapped_column(ForeignKey("app_user.id"))
assignee: Mapped[User | None] = relationship(back_populates="assignments")
# Default fields
due_date: Mapped[date | None]
due_date: Mapped[date | None] = mapped_column(default=determine_due_date)
status: Mapped[ArtefactStatus] = mapped_column(default=ArtefactStatus.UNDECIDED)
bug_link: Mapped[str] = mapped_column(default="")

Expand Down Expand Up @@ -186,6 +198,11 @@ def __repr__(self) -> str:
"status",
)

@hybrid_property
def is_kernel(self) -> bool:
"""Kernel artefacts start with 'linix-' or end with '-kernel'"""
return self.name.startswith("linux-") or self.name.endswith("-kernel")


class ArtefactBuild(Base):
"""A model to represent specific builds of artefact (e.g. arm64 revision 2)"""
Expand Down
10 changes: 8 additions & 2 deletions backend/tests/controllers/artefacts/test_artefacts.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ def test_get_latest_artefacts_by_family(
"stage": relevant_artefact.stage.name,
"status": relevant_artefact.status,
"assignee": None,
"due_date": None,
"due_date": (
relevant_artefact.due_date.strftime("%Y-%m-%d")
if relevant_artefact.due_date
else None
),
"bug_link": "",
}
]
Expand Down Expand Up @@ -219,7 +223,9 @@ def test_artefact_signoff_approve(test_client: TestClient, generator: DataGenera
"stage": artefact.stage.name,
"status": artefact.status,
"assignee": None,
"due_date": None,
"due_date": (
artefact.due_date.strftime("%Y-%m-%d") if artefact.due_date else None
),
"bug_link": "",
}

Expand Down
74 changes: 74 additions & 0 deletions backend/tests/controllers/test_executions/test_start_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

from datetime import date, timedelta

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

Expand Down Expand Up @@ -194,3 +196,75 @@ def test_new_artefacts_get_assigned_a_reviewer(
artefact = db_session.query(Artefact).filter(Artefact.name == "core22").one()
assert artefact.assignee is not None
assert artefact.assignee.launchpad_handle == user.launchpad_handle


def test_non_kernel_artefact_due_date(db_session: Session, test_client: TestClient):
"""
For non-kernel snaps, the default due date should be set to now + 10 days
"""
test_client.put(
"/v1/test-executions/start-test",
json={
"family": FamilyName.SNAP,
"name": "core22",
"version": "abec123",
"revision": 123,
"track": "22",
"store": "ubuntu",
"arch": "arm64",
"execution_stage": "beta",
"environment": "cm3",
"ci_link": "http://localhost",
},
)

artefact = (
db_session.query(Artefact)
.filter(
Artefact.name == "core22",
Artefact.version == "abec123",
Artefact.store == "ubuntu",
Artefact.track == "22",
Artefact.stage.has(name="beta"),
)
.one_or_none()
)

assert artefact is not None
assert artefact.due_date == date.today() + timedelta(10)


def test_kernel_artefact_due_date(db_session: Session, test_client: TestClient):
"""
For kernel artefacts, due date shouldn't be set to default
"""
test_client.put(
"/v1/test-executions/start-test",
json={
"family": FamilyName.SNAP,
"name": "pi-kernel",
"version": "abec123",
"revision": 123,
"track": "22",
"store": "ubuntu",
"arch": "arm64",
"execution_stage": "beta",
"environment": "cm3",
"ci_link": "http://localhost",
},
)

artefact = (
db_session.query(Artefact)
.filter(
Artefact.name == "pi-kernel",
Artefact.version == "abec123",
Artefact.store == "ubuntu",
Artefact.track == "22",
Artefact.stage.has(name="beta"),
)
.one_or_none()
)

assert artefact is not None
assert artefact.due_date is None

0 comments on commit 0c3ed72

Please sign in to comment.