Skip to content

Commit

Permalink
Merge pull request #9 from ZeroGachis/feature/greater-than-validator
Browse files Browse the repository at this point in the history
✨ Validators - Add "greater-than" rule
  • Loading branch information
LeKangouroo authored Mar 21, 2023
2 parents 0e21136 + 2b9206d commit 74520be
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ demo/

# Docker dev env
docker-compose.override.yml

# Jetbrains IDEs
.idea/
17 changes: 16 additions & 1 deletion magicparse/validators.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from decimal import Decimal
from .transform import Transform
import re

Expand Down Expand Up @@ -36,4 +37,18 @@ def key() -> str:
return "regex-matches"


builtins = [RegexMatches]
class GreaterThan(Validator):
def __init__(self, threshold: float) -> None:
self.threshold = Decimal(threshold)

def apply(self, value: Decimal) -> Decimal:
if value > self.threshold:
return value
raise ValueError(f"value must be greater than {self.threshold}")

@staticmethod
def key() -> str:
return "greater-than"


builtins = [GreaterThan, RegexMatches]
48 changes: 47 additions & 1 deletion tests/test_validators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from magicparse.validators import RegexMatches, Validator
from decimal import Decimal
from magicparse.validators import GreaterThan, RegexMatches, Validator
import pytest
import re
from unittest import TestCase
Expand All @@ -24,6 +25,17 @@ def test_no_name_provided(self):
with pytest.raises(ValueError, match="validator must have a 'name' key"):
Validator.build({})

def test_greater_than_params_are_correct(self):
validator = Validator.build(
{
"name": "greater-than",
"parameters": {"threshold": 20.2},
}
)
assert isinstance(validator, GreaterThan)
assert isinstance(validator.threshold, Decimal)
assert validator.threshold == 20.2


class TestRegexMatches(TestCase):
def test_match(self):
Expand Down Expand Up @@ -65,3 +77,37 @@ def test_register(self):

validator = Validator.build({"name": "is-the-answer"})
assert isinstance(validator, self.IsTheAnswerValidator)


class TestGreaterThanValidator(TestCase):
def test_it_successfully_returns_the_value_when_greater_than_threshold(self):
validator = Validator.build(
{"name": "greater-than", "parameters": {"threshold": 11}}
)

assert validator.apply(12) == 12

def test_it_successfully_returns_the_value_when_greater_than_decimal_threshold(
self,
):
validator = Validator.build(
{"name": "greater-than", "parameters": {"threshold": 11.4}}
)

assert validator.apply(11.5) == 11.5

def test_it_raises_an_error_when_the_value_is_lower_than_threshold(self):
validator = Validator.build(
{"name": "greater-than", "parameters": {"threshold": 10}}
)

with pytest.raises(ValueError, match="value must be greater than 10"):
validator.apply(9.9999)

def test_it_raises_an_error_when_the_value_is_equal_to_threshold(self):
validator = Validator.build(
{"name": "greater-than", "parameters": {"threshold": 10}}
)

with pytest.raises(ValueError, match="value must be greater than 10"):
validator.apply(10)

0 comments on commit 74520be

Please sign in to comment.