Skip to content

Commit

Permalink
Use before_update instead of validator
Browse files Browse the repository at this point in the history
  • Loading branch information
nadzyah committed Aug 11, 2023
1 parent fe32585 commit 55c88f1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 27 deletions.
26 changes: 0 additions & 26 deletions backend/test_observer/data_access/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@
Mapped,
mapped_column,
relationship,
validates,
)

from test_observer.data_access.models_enums import (
ArtefactStatus,
TestExecutionStatus,
FamilyName,
)


Expand Down Expand Up @@ -97,30 +95,6 @@ class Artefact(Base):
UniqueConstraint("name", "version", "source", name="unique_artefact"),
)

@validates("source")
def validate_source(
self, key: str, value: dict, family_name: str | None = None # noqa: ARG002
) -> dict:
"""
Validate source field
:key: the key to validate. Not used now but is passed during the call.
I'm not replacing it with _ to avoid confusions in future
:value: the value to validate
:family_name: optional argument for the case when stage is not set yet
but we need it to get family name
"""
if (
self.stage and self.stage.family.name == FamilyName.SNAP.value
) or family_name == FamilyName.SNAP.value:
# Check that store key is specified
if "store" not in value:
raise ValueError("Snap artefacts should have store key in source")
# Check that store key has a correct value
if not isinstance(value["store"], str):
raise ValueError("Store key in source field should be a string")
return value


class ArtefactBuild(Base):
"""A model to represent specific builds of artefact (e.g. arm64 revision 2)"""
Expand Down
11 changes: 10 additions & 1 deletion backend/test_observer/data_access/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
from sqlalchemy import select, event
from sqlalchemy.engine.base import Connection
from sqlalchemy.orm import Mapper

from .models import Stage, Family, Artefact
from .models_enums import FamilyName


@event.listens_for(Artefact, "before_insert")
@event.listens_for(Artefact, "before_update")
def validate_artefact(
mapper: Mapper, connection: Connection, artefact: Artefact # noqa: ARG001
) -> None:
Expand All @@ -41,4 +44,10 @@ def validate_artefact(
family_id = connection.execute(stage).scalar()
stage_family = select(Family.name).where(Family.id == family_id)
family_name = connection.execute(stage_family).scalar()
artefact.source = artefact.validate_source("source", artefact.source, family_name)
if family_name == FamilyName.SNAP.value:
# Check that store key is specified
if "store" not in artefact.source:
raise ValueError("Snap artefacts should have store key in source")
# Check that store key has a correct value
if not isinstance(artefact.source["store"], str):
raise ValueError("Store key in source field should be a string")

0 comments on commit 55c88f1

Please sign in to comment.