Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
CalebCourier committed Nov 21, 2023
1 parent fe154fa commit d82cbcc
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions tests/integration_tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,13 @@ def test_pii_filter(mocker):
llm_output=text,
)
# Validated output should be different from input
assert output != text
assert output.validated_output != text

# Validated output should contain masked pii entities
assert all(entity in output for entity in ["<EMAIL_ADDRESS>", "<PHONE_NUMBER>"])
assert all(
entity in output.validated_output
for entity in ["<EMAIL_ADDRESS>", "<PHONE_NUMBER>"]
)

# ------------------
# 2. Initialise Guard from string with setting pii_entities as a list
Expand All @@ -236,18 +239,21 @@ def test_pii_filter(mocker):
llm_output=text,
)
# Validated output should be different from input
assert output != text
assert output.validated_output != text

# Validated output should contain masked pii entities
assert all(entity in output for entity in ["<EMAIL_ADDRESS>", "<PHONE_NUMBER>"])
assert all(
entity in output.validated_output
for entity in ["<EMAIL_ADDRESS>", "<PHONE_NUMBER>"]
)

# Check with text without any pii entities
text = "My email address is xyz and my phone number is unavailable."
output = guard.parse(
llm_output=text,
)
# Validated output should be same as input
assert output == text
assert output.validated_output == text

# ------------------
# 3. Initialise Guard from string without setting pii_entities
Expand All @@ -259,10 +265,10 @@ def test_pii_filter(mocker):
)

text = "My email address is [email protected], and my phone number is 1234567890"
with pytest.raises(ValueError):
output = guard.parse(
llm_output=text,
)
output = guard.parse(
llm_output=text,
)
assert output.error is not None

# ------------------
# 4. Initialise Guard from string without setting pii_entities
Expand All @@ -278,21 +284,27 @@ def test_pii_filter(mocker):
metadata={"pii_entities": "pii"},
)
# Validated output should be different from input
assert output != text
assert output.validated_output != text

# Validated output should contain masked pii entities
assert all(entity in output for entity in ["<EMAIL_ADDRESS>", "<PHONE_NUMBER>"])
assert all(
entity in output.validated_output
for entity in ["<EMAIL_ADDRESS>", "<PHONE_NUMBER>"]
)

# Now try with list of pii entities passed through metadata
output = guard.parse(
llm_output=text,
metadata={"pii_entities": ["EMAIL_ADDRESS", "PHONE_NUMBER"]},
)
# Validated output should be different from input
assert output != text
assert output.validated_output != text

# Validated output should contain masked pii entities
assert all(entity in output for entity in ["<EMAIL_ADDRESS>", "<PHONE_NUMBER>"])
assert all(
entity in output.validated_output
for entity in ["<EMAIL_ADDRESS>", "<PHONE_NUMBER>"]
)

# ------------------
# 5. Initialise Guard from string setting
Expand All @@ -313,12 +325,12 @@ def test_pii_filter(mocker):
metadata={"pii_entities": ["EMAIL_ADDRESS"]},
)
# Validated output should be different from input
assert output != text
assert output.validated_output != text

# Validated output should contain masked EMAIL_ADDRESS
# and not PHONE_NUMBER
assert "<EMAIL_ADDRESS>" in output
assert "<PHONE_NUMBER>" not in output
assert "<EMAIL_ADDRESS>" in output.validated_output
assert "<PHONE_NUMBER>" not in output.validated_output

# ------------------
# 6. Initialise Guard from string setting an incorrect string of pii_entities
Expand All @@ -330,10 +342,10 @@ def test_pii_filter(mocker):
)
text = "My email address is [email protected], and my phone number is 1234567890"

with pytest.raises(ValueError):
output = guard.parse(
llm_output=text,
)
output = guard.parse(
llm_output=text,
)
assert output.error is not None


@register_validator("mycustominstancecheckvalidator", data_type="string")
Expand Down

0 comments on commit d82cbcc

Please sign in to comment.