Skip to content

Commit

Permalink
Fixing merge conflict bugs with new scheduler dataclass
Browse files Browse the repository at this point in the history
* Added tests for rawacf_format field in schedule lines
  • Loading branch information
RemingtonRohel committed Oct 17, 2024
1 parent 4e2e8c2 commit f2cee8f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
25 changes: 13 additions & 12 deletions scheduler/scd_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import datetime as dt
import shutil
import sys
from typing_extensions import Annotated, Union, Literal, Self
from typing_extensions import Annotated, Union, Literal, Self, Optional

from pydantic.dataclasses import dataclass
from pydantic import field_validator, Field, model_validator
Expand Down Expand Up @@ -44,7 +44,7 @@ class ScheduleLine:
scheduling_mode: Literal["common", "discretionary", "special"]
kwargs: list[str] = Field(default_factory=list)
embargo: bool = False
rawacf_format: str = ""
rawacf_format: Optional[Literal["dmap", "hdf5"]] = None

def __str__(self):
dur = self.duration
Expand All @@ -57,7 +57,7 @@ def __str__(self):
f" {self.experiment}"
f" {self.scheduling_mode}"
f"{' --embargo' if self.embargo else ''}"
f"{self.rawacf_format}"
f"{' --rawacf_format=' + self.rawacf_format if self.rawacf_format is not None else ''}"
f"{' ' + ' '.join(self.kwargs) if len(self.kwargs) > 0 else ''}"
)
return line
Expand Down Expand Up @@ -129,18 +129,18 @@ def from_str(cls, line: str) -> Self:
embargo = "--embargo" in kwargs
if embargo:
kwargs.remove("--embargo")
raw_format = ""

raw_format = None
raw_format_flag = ["rawacf_format" in x for x in kwargs]
if any(raw_format_flag):
idx = raw_format_flag.index(True)
if len(kwargs[i].split("=")) == 1:
if len(kwargs[idx].split("=")) == 1:
# supplied as --rawacf_format <format>
raw_format = kwargs.pop(i + 1)
kwargs.pop(i)
raw_format = kwargs.pop(idx + 1)
kwargs.pop(idx)
else:
# supplied as --rawacf_format=<format>
raw_format = kwargs.pop(i).split("=")[1]
raw_format = kwargs.pop(idx).split("=")[1]

scd_line = ScheduleLine(
timestamp=dt.datetime.strptime(f"{fields[0]} {fields[1]}", "%Y%m%d %H:%M"),
Expand Down Expand Up @@ -209,7 +209,7 @@ def create_line(
duration,
kwargs,
embargo=False,
rawacf_format="",
rawacf_format=None,
) -> ScheduleLine:
"""
Creates a line dictionary from inputs, turning the date and time into a timestamp since epoch.
Expand Down Expand Up @@ -247,6 +247,7 @@ def create_line(
scheduling_mode=scheduling_mode,
kwargs=kwargs,
embargo=embargo,
rawacf_format=rawacf_format,
)

def read_scd(self):
Expand Down Expand Up @@ -302,7 +303,7 @@ def add_line(
duration="-",
kwargs=None,
embargo=False,
rawacf_format="",
rawacf_format=None,
):
"""
Adds a new line to the schedule.
Expand Down Expand Up @@ -379,7 +380,7 @@ def remove_line(
duration="-",
kwargs=None,
embargo=False,
rawacf_format="",
rawacf_format=None,
):
"""
Removes a line from the schedule
Expand Down
26 changes: 26 additions & 0 deletions tests/scheduler/test_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
:author: Kevin Krieger
"""

import copy
import shutil
import unittest
import os
Expand Down Expand Up @@ -517,6 +518,31 @@ def test_invalid_kwargs_string(self):
)
line.test(self.site_id)

def test_rawacf_format(self):
"""
Test getting the next month from a given date with wrong type
"""
line_dict = copy.deepcopy(self.linedict)
formats = ["hdf5", "dmap", None]
lines = [
"20000101 06:30 60 0 normalscan common --rawacf_format=hdf5\n",
"20000101 06:30 60 0 normalscan common --rawacf_format=dmap\n",
"20000101 06:30 60 0 normalscan common\n",
]
for fmt, ln in zip(formats, lines):
line_dict["rawacf_format"] = fmt
line = scd_utils.ScheduleLine.from_str(ln)
self.assertEqual(line.rawacf_format, fmt)
self.assertEqual(str(line), ln.strip())

with self.assertRaises(ValidationError):
line_dict["rawacf_format"] = "json"
scd_utils.ScheduleLine(**line_dict)

with self.assertRaises(ValidationError):
line_dict["rawacf_format"] = 0
scd_utils.ScheduleLine(**line_dict)

# read_scd tests
def test_invalid_num_args(self):
"""
Expand Down

0 comments on commit f2cee8f

Please sign in to comment.