Skip to content

Commit

Permalink
Require confirmation date to be set in the data model #2714
Browse files Browse the repository at this point in the history
  • Loading branch information
iamleeg committed Jun 17, 2022
1 parent f2450ed commit c9ef37d
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 37 deletions.
60 changes: 30 additions & 30 deletions data-serving/reusable-data-service/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion data-serving/reusable-data-service/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ authors = ["Global.health <info@global.health>"]
python = "^3.10"

[tool.poetry.dev-dependencies]
pytest = "^5.2"
pytest = "^7.1.2"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
import dataclasses
import datetime
import json

@dataclasses.dataclass
@dataclasses.dataclass()
class DayZeroCase:
"""This class implements the "day-zero" data schema for Global.health.
At the beginning of an outbreak, we want to collect at least this much
information about an individual case for the line list."""
pass
information about an individual case for the line list.
Parameters here are defined to be keyword-only and not set in the
initialiser, so that clients can use Builder to populate them. Use
the validate() method to determine whether an instance is in a
consistent state (this also means we can add custom validation logic
to that function)."""
_: dataclasses.KW_ONLY
confirmation_date: datetime.datetime = dataclasses.field(init=False)

@classmethod
def from_json(cls, obj: str) -> type:
"""Create an instance of this class from a JSON representation."""
case = cls()
source = json.loads(obj)
for key in source:
setattr(case, key, source[key])
case.validate()
return case

def validate(self):
"""Check whether I am consistent. Raise ValueError if not."""
if not hasattr(self, 'confirmation_date'):
raise ValueError("Confirmation Date is mandatory")
elif self.confirmation_date is None:
raise ValueError("Confirmation Date must have a value")

# Actually we want to capture extra fields which can be specified dynamically:
# so Case is the class that you should use.
Expand Down
7 changes: 4 additions & 3 deletions data-serving/reusable-data-service/tests/test_case_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest
from reusable_data_service import Case

def test_instantiating_case():
case = Case()
assert case is not None
def test_instantiating_case_from_empty_json_is_error():
with pytest.raises(ValueError):
case = Case.from_json('{}')

0 comments on commit c9ef37d

Please sign in to comment.