Skip to content

Commit

Permalink
Updater 0.1 -> 0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterKraus committed Jul 5, 2024
1 parent 316c189 commit 120166c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/dgbowl_schemas/tomato/payload_0_1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
from .tomato import Tomato
from .sample import Sample
from .method import Method
from ..payload_0_2 import Payload as NewPayload

import logging
from pathlib import Path
import yaml
import json

logger = logging.getLogger(__name__)


class Payload(BaseModel, extra=Extra.forbid):
version: Literal["0.1"]
Expand Down Expand Up @@ -46,3 +50,10 @@ def extract_methodfile(cls, values): # pylint: disable=E0213
assert "method" in method
values["method"] = method["method"]
return values

def update(self):
logger.info("Updating from Payload-0.1 to Payload-0.2")
md = self.dict(exclude_defaults=True, exclude_none=True)
md["version"] = "0.2"

return NewPayload(**md)
4 changes: 2 additions & 2 deletions src/dgbowl_schemas/tomato/payload_0_2/tomato.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from pydantic.v1 import BaseModel, Extra, Field
from typing import Literal
from typing import Literal, Optional


class Tomato(BaseModel, extra=Extra.forbid):
Expand Down Expand Up @@ -33,5 +33,5 @@ class Snapshot(BaseModel, extra=Extra.forbid):
output: Output = Field(default_factory=Output)
"""Options for final FAIR data output."""

snapshot: Snapshot = None
snapshot: Optional[Snapshot] = None
"""Options for periodic snapshotting."""
21 changes: 21 additions & 0 deletions tests/test_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,24 @@ def test_payload_yml(inpath, datadir):
with open(f"ref.{inpath.replace('yml','json')}", "r") as ofile:
ref = json.load(ofile)
assert ret == ref


@pytest.mark.parametrize(
"inpath",
[
"ts0.yml", # 0.1
"ts1.yml", # 0.1
"ts2.yml", # 0.1
"ts3.yml", # 0.1
"ts4.yml", # 0.1
],
)
def test_payload_update_chain(inpath, datadir):
os.chdir(datadir)
with open(inpath, "r") as infile:
indict = yaml.safe_load(infile)
ret = to_payload(**indict)
while hasattr(ret, "update"):
ret = ret.update()
print(f"{ret=}")
assert ret.version == "0.2"

0 comments on commit 120166c

Please sign in to comment.