Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aaravnavani committed Jul 15, 2024
1 parent 02a569b commit 32dfbe8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 33 deletions.
30 changes: 14 additions & 16 deletions tests/test_validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@

from guardrails import Guard
import pytest
from validator import ValidatorTemplate
from validator import BiasCheck
from guardrails.validator_base import FailResult, PassResult

# We use 'exception' as the validator's fail action,
# so we expect failures to always raise an Exception
# Learn more about corrective actions here:
# https://www.guardrailsai.com/docs/concepts/output/#%EF%B8%8F-specifying-corrective-actions
guard = Guard.from_string(validators=[ValidatorTemplate(arg_1="arg_1", arg_2="arg_2", on_fail="exception")])
def test_success_case(self):
validator = BiasCheck(debias_strength=0.5)
input_text = "The sun rises in the morning."
result = validator.validate(input_text, {})
assert isinstance(result, PassResult)

def test_pass():
test_output = "pass"
result = guard.parse(test_output)

assert result.validation_passed is True
assert result.validated_output == test_output

def test_fail():
with pytest.raises(Exception) as exc_info:
test_output = "fail"
guard.parse(test_output)

# Assert the exception has your error_message
assert str(exc_info.value) == "Validation failed for field with errors: {A descriptive but concise error message about why validation failed}"
def test_failure_case(self):
validator = BiasCheck(debias_strength=0.5)
input_text = "The sun only rises for Humanists."
result = validator.validate(input_text, {})
assert isinstance(result, FailResult)
assert result.error_message == "The original response contains potential biases that are now addressed."
assert result.fix_value == "The sun rises for everyone."
17 changes: 0 additions & 17 deletions validator/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,3 @@ def validate(self, value: Any, metadata: Dict = {}) -> ValidationResult:
fix_value=debiased_value,
)
return PassResult()


# Run tests via `pytest -rP ./bias_check.py`
class TestBiasCheck:
def test_success_case(self):
validator = BiasCheck(debias_strength=0.5)
input_text = "The sun rises in the morning."
result = validator.validate(input_text, {})
assert isinstance(result, PassResult)

def test_failure_case(self):
validator = BiasCheck(debias_strength=0.5)
input_text = "The sun only rises for Humanists."
result = validator.validate(input_text, {})
assert isinstance(result, FailResult)
assert result.error_message == "The original response contains potential biases that are now addressed."
assert result.fix_value == "The sun rises for everyone."

0 comments on commit 32dfbe8

Please sign in to comment.