Skip to content

Commit

Permalink
feat: add TES models with unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Karanjot786 committed Oct 12, 2024
1 parent 7adf65b commit a0be90a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 10 deletions.
32 changes: 32 additions & 0 deletions crategen/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"""Models package for TES, WES, and WRROC.
This package contains Pydantic models that conform to the GA4GH schemas for Task Execution Services (TES),
Workflow Execution Services (WES), and WRROC. These models are used for data validation and type safety
throughout the CrateGen project.
"""

from .tes_models import (
TESData,
TESExecutor,
TESExecutorLog,
TESFileType,
TESInput,
TESOutput,
TESOutputFileLog,
TESResources,
TESState,
TESTaskLog,
)

__all__ = [
"TESData",
"TESInput",
"TESOutput",
"TESExecutor",
"TESTaskLog",
"TESResources",
"TESExecutorLog",
"TESOutputFileLog",
"TESFileType",
"TESState",
]
26 changes: 25 additions & 1 deletion crategen/models/tes_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,31 @@


class TESFileType(str, Enum):
"""Enumeration of TES file types.
**Attributes:**
- **FILE** (`str`): Represents a file.
- **DIRECTORY** (`str`): Represents a directory.
"""
FILE = "FILE"
DIRECTORY = "DIRECTORY"


class TESState(str, Enum):
"""Enumeration of TES task states.
**Attributes:**
- **UNKNOWN** (`str`): The task state is unknown.
- **QUEUED** (`str`): The task is queued.
- **INITIALIZING** (`str`): The task is initializing.
- **RUNNING** (`str`): The task is running.
- **PAUSED** (`str`): The task is paused.
- **COMPLETE** (`str`): The task is complete.
- **EXECUTOR_ERROR** (`str`): The task encountered an executor error.
- **SYSTEM_ERROR** (`str`): The task encountered a system error.
- **CANCELLED** (`str`): The task was cancelled.
"""
UNKNOWN = "UNKNOWN"
QUEUED = "QUEUED"
INITIALIZING = "INITIALIZING"
Expand Down Expand Up @@ -66,11 +86,12 @@ class TESExecutorLog(BaseModel):

@validator("start_time", "end_time", pre=True, always=True)
def validate_datetime(cls, value):
"""Convert start and end times to RFC 3339 format."""
return convert_to_iso8601(value)


class TESExecutor(BaseModel):
"""An array of executors to be run
"""An array of executors to be run.
**Attributes:**
- **image** (`str`): Name of the container image.
Expand Down Expand Up @@ -164,6 +185,7 @@ def validate_content_and_url(cls, values):

@validator("path")
def validate_path(cls, value):
"""Validate that the path is an absolute path."""
if not os.path.isabs(value):
raise ValueError("The 'path' attribute must contain an absolute path.")
return value
Expand Down Expand Up @@ -191,6 +213,7 @@ class TESOutput(BaseModel):

@validator("path")
def validate_path(cls, value):
"""Validate that the path is an absolute path."""
if not os.path.isabs(value):
raise ValueError("The 'path' attribute must contain an absolute path.")
return value
Expand Down Expand Up @@ -221,6 +244,7 @@ class TESTaskLog(BaseModel):

@validator("start_time", "end_time", pre=True, always=True)
def validate_datetime(cls, value):
"""Convert start and end times to RFC 3339 format."""
return convert_to_iso8601(value)


Expand Down
22 changes: 13 additions & 9 deletions tests/unit/test_tes_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@
from pydantic import ValidationError

from crategen.models.tes_models import (
TESInput,
TESOutput,
TESExecutor,
TESData,
TESExecutor,
TESFileType,
TESInput,
TESOutput,
TESResources,
TESState,
TESResources
)

EXPECTED_CPU_CORES = 2
EXPECTED_RAM_GB = 4.0
EXPECTED_DISK_GB = 10.0
EXPECTED_PREEMPTIBLE = False

def test_tes_input_with_url():
"""Test TESInput model with a valid URL and absolute path."""
Expand Down Expand Up @@ -174,10 +178,10 @@ def test_tes_data_valid():
)
],
resources=TESResources(
cpu_cores=2,
ram_gb=4.0,
disk_gb=10.0,
preemptible=False,
cpu_cores=EXPECTED_CPU_CORES,
ram_gb=EXPECTED_RAM_GB,
disk_gb=EXPECTED_DISK_GB,
preemptible=EXPECTED_PREEMPTIBLE,
),
volumes=["/data"],
tags={"project": "CrateGen"},
Expand All @@ -186,7 +190,7 @@ def test_tes_data_valid():
assert tes_data.inputs[0].url == "https://raw.githubusercontent.com/elixir-cloud-aai/CrateGen/refs/heads/main/README.md"
assert tes_data.outputs[0].path == "/data/output/LICENSE"
assert tes_data.executors[0].image == "python:3.8-slim"
assert tes_data.resources.cpu_cores == 2
assert tes_data.resources.cpu_cores == EXPECTED_CPU_CORES
assert tes_data.volumes == ["/data"]
assert tes_data.tags == {"project": "CrateGen"}

Expand Down

0 comments on commit a0be90a

Please sign in to comment.