Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overflow and Type Errors added to concept.py . Required tests for qua… #77

Merged
merged 7 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
## Changes Made
<!-- List the key changes made in this PR -->

-
-
-
-
-
-

## Testing
<!-- Describe how these changes were tested -->
Expand All @@ -27,4 +27,3 @@

## Additional Notes
<!-- Add any other context about the PR here -->

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,4 @@ scrap/
.DS_Store
.vscode/
.ruff_cache/
.python-version
21 changes: 20 additions & 1 deletion healthchain/models/data/concept.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from enum import Enum
from pydantic import BaseModel, Field
from pydantic import BaseModel, Field, field_validator
from typing import Optional, Dict, Union


Expand All @@ -21,6 +21,25 @@ class Quantity(DataType):
value: Optional[Union[str, float]] = None
unit: Optional[str] = None

@field_validator("value")
@classmethod
def validate_value(cls, value: Union[str, float]):
if value is None:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran the testing suite and got some errors from this line. The logic here should be slightly different. If None is passed in then we want to just return None back, however if a type other than str or float is passed then we can raise the warning. I've added a suggestion below (written on my phone so will need some edits).

Suggested change
if value is None:
if value is None:
return None
elif type(value) is not str or float:
raise TypeError

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay got it ! Will make the change.

raise TypeError(
f"Value CANNOT be a {type(value)} object. Must be float or string in float format."
)

try:
return float(value)

except ValueError:
raise ValueError(f"Invalid value '{value}' . Must be a float Number.")

except OverflowError:
raise OverflowError(
"Invalid value . Value is too large resulting in overflow."
)


class Range(DataType):
low: Optional[Quantity] = None
Expand Down
2 changes: 1 addition & 1 deletion poetry.lock

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

48 changes: 48 additions & 0 deletions tests/test_quantity_class.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import pytest
from healthchain.models.data.concept import Quantity
from pydantic import ValidationError


# Valid Cases
def test_valid_float_and_integer():
valid_floats = [1.0, 0.1, 4.0, 5.99999, 12455.321, 33, 1234]
for num in valid_floats:
q = Quantity(value=num, unit="mg")
assert q.value == num


def test_valid_string():
valid_strings = ["100", "100.000001", ".1", "1.", ".123", "1234.", "123989"]
for string in valid_strings:
q = Quantity(value=string, unit="mg")
assert q.value == float(string)


# Invalid Cases
def test_invalid_strings():
invalid_strings = [
"1.0.0",
"1..123",
"..123",
"12..",
"12a.56",
"1e4.6",
"12#.45",
"12.12@3",
"12@3",
]
for string in invalid_strings:
with pytest.raises(ValidationError) as exception_info:
Quantity(value=string, unit="mg")
assert "Invalid value" in str(exception_info.value)


# Edge Cases
def test_edge_cases():
edge_cases = ["", "None", None]
for val in edge_cases:
with pytest.raises((ValidationError, TypeError)) as exception_info:
Quantity(value=val, unit="mg")

exception_info_str = str(exception_info.value)
assert any(msg in exception_info_str for msg in ["CANNOT", "Invalid value"])
Loading