Skip to content

Commit

Permalink
Fix test so it's independent of pydantic version.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 709039268
Change-Id: I1ab916b16e2ea83acdc00fe99d22548243b3b524
  • Loading branch information
sam-bailey authored and copybara-github committed Dec 23, 2024
1 parent 70131b6 commit 64fa1b2
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions py/copycat/copycat_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
# limitations under the License.

import json
import keyword

from absl.testing import absltest
from absl.testing import parameterized
from vertexai import generative_models
import mock
import pandas as pd
import pydantic

from copycat import copycat
from copycat import google_ads
Expand Down Expand Up @@ -1038,17 +1039,27 @@ def test_generate_new_ad_copy_returns_expected_response_for_non_json_chat_model_
vectorstore_exemplar_selection_method="random",
)

response = copycat_instance.generate_new_ad_copy(
keywords=["my keyword 1, my keyword 2"],
style_guide="This is my style guide.",
num_in_context_examples=2,
system_instruction_kwargs=dict(
company_name="My company",
language="english",
),
existing_headlines=[existing_headlines],
existing_descriptions=[existing_descriptions],
)[0]
with mock.patch.object(
google_ads.GoogleAd, "model_validate_json"
) as mock_model_validate_json:

def raise_validation_error(self, *args, **kwargs):
# Mocking the validation error because otherwise the error message
# changes every time the pydantic version is updated.
raise pydantic.ValidationError("Mock validation error.", [])

mock_model_validate_json.side_effect = raise_validation_error
response = copycat_instance.generate_new_ad_copy(
keywords=["my keyword 1, my keyword 2"],
style_guide="This is my style guide.",
num_in_context_examples=2,
system_instruction_kwargs=dict(
company_name="My company",
language="english",
),
existing_headlines=[existing_headlines],
existing_descriptions=[existing_descriptions],
)[0]

# I don't want to test the similarity metrics here, so I'm just setting
# them to None.
Expand All @@ -1064,13 +1075,7 @@ def test_generate_new_ad_copy_returns_expected_response_for_non_json_chat_model_
descriptions=existing_descriptions or [],
),
evaluation_results=copycat.EvaluationResults(
errors=[
"1 validation error for GoogleAd\n Invalid JSON: expected"
" ident at line 1 column 2 [type=json_invalid,"
" input_value='not a json', input_type=str]\n For"
" further information visit"
" https://errors.pydantic.dev/2.7/v/json_invalid"
],
errors=["0 validation errors for Mock validation error.\n"],
warnings=[],
headlines_are_memorised=False if existing_headlines else None,
descriptions_are_memorised=False
Expand Down

0 comments on commit 64fa1b2

Please sign in to comment.