Skip to content

Commit

Permalink
Validator: added recording of bool expression source code as string f…
Browse files Browse the repository at this point in the history
…or tracking purposes
  • Loading branch information
sebastian-echeverria committed Dec 14, 2024
1 parent 6a8f541 commit 0f9bfb6
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,25 @@
}
],
"title": "Info"
},
"bool_exp_str": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"title": "Bool Exp Str"
}
},
"required": [
"bool_exp",
"success",
"failure",
"info"
"info",
"bool_exp_str"
],
"title": "ValidatorModel",
"type": "object"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,25 @@
}
],
"title": "Info"
},
"bool_exp_str": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"title": "Bool Exp Str"
}
},
"required": [
"bool_exp",
"success",
"failure",
"info"
"info",
"bool_exp_str"
],
"title": "ValidatorModel",
"type": "object"
Expand Down
14 changes: 13 additions & 1 deletion mlte/schema/artifact/spec/v0.0.1/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,25 @@
}
],
"title": "Info"
},
"bool_exp_str": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"title": "Bool Exp Str"
}
},
"required": [
"bool_exp",
"success",
"failure",
"info"
"info",
"bool_exp_str"
],
"title": "ValidatorModel",
"type": "object"
Expand Down
14 changes: 13 additions & 1 deletion mlte/schema/artifact/validated/v0.0.1/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,25 @@
}
],
"title": "Info"
},
"bool_exp_str": {
"anyOf": [
{
"type": "string"
},
{
"type": "null"
}
],
"title": "Bool Exp Str"
}
},
"required": [
"bool_exp",
"success",
"failure",
"info"
"info",
"bool_exp_str"
],
"title": "ValidatorModel",
"type": "object"
Expand Down
3 changes: 3 additions & 0 deletions mlte/validation/model_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class ValidatorModel(BaseModel):
info: Optional[str]
"""A string to be used when recording that the validation was not checked against a condition, just recorded information."""

bool_exp_str: Optional[str]
"""A string representation of the code for the bool expression to check for."""


class ConditionModel(BaseModel):
"""A description of a condition for a property."""
Expand Down
7 changes: 7 additions & 0 deletions mlte/validation/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from __future__ import annotations

import inspect
import typing
from typing import Any, Callable, Optional

Expand Down Expand Up @@ -48,6 +49,11 @@ def __init__(
self.success = success
self.failure = failure
self.info = info
self.bool_exp_str = (
inspect.getsource(bool_exp).strip()
if bool_exp is not None
else None
)

def validate(self, *args, **kwargs) -> Result:
"""
Expand Down Expand Up @@ -93,6 +99,7 @@ def to_model(self) -> ValidatorModel:
success=self.success,
failure=self.failure,
info=self.info,
bool_exp_str=self.bool_exp_str,
)

@classmethod
Expand Down
11 changes: 11 additions & 0 deletions test/spec/test_condition.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,17 @@ def test_round_trip() -> None:
assert condition == loaded


def test_bool_exp_str() -> None:
"""Condition can be converted to model and back."""

condition = TestValue.in_between(1, 10)

assert (
condition.validator.bool_exp_str
== "bool_exp=lambda real: real.value > arg1 and real.value < arg2,"
)


def test_non_serializable_argument():
condition = TestValue.in_between_complex(1.0, TestValue())

Expand Down
2 changes: 2 additions & 0 deletions test/validation/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def get_sample_validator() -> Validator:
success="Test was succesful!",
failure="Test failed :(",
info="Only data was attached",
bool_exp_str="test()",
)
return validator

Expand All @@ -26,6 +27,7 @@ def test_validator_model() -> None:
success="Test was succesful!",
failure="Test failed :(",
info="Only data was attached",
bool_exp_str="test()",
),
]

Expand Down

0 comments on commit 0f9bfb6

Please sign in to comment.