Skip to content

Commit d68a913

Browse files
shawn-yang-googlecopybara-github
authored andcommitted
chore: Add a fixture to mock _get_gca_resource in test_reasoning_engines unit test.
PiperOrigin-RevId: 698169229
1 parent deb6c9c commit d68a913

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

tests/unit/vertex_langchain/test_reasoning_engines.py

+22-15
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,16 @@ def types_reasoning_engine_mock():
431431
yield types_reasoning_engine_mock
432432

433433

434+
@pytest.fixture(scope="function")
435+
def get_gca_resource_mock():
436+
with mock.patch.object(
437+
base.VertexAiResourceNoun,
438+
"_get_gca_resource",
439+
) as get_gca_resource_mock:
440+
get_gca_resource_mock.return_value = _TEST_REASONING_ENGINE_OBJ
441+
yield get_gca_resource_mock
442+
443+
434444
class InvalidCapitalizeEngineWithoutQuerySelf:
435445
"""A sample Reasoning Engine with an invalid query method."""
436446

@@ -533,15 +543,14 @@ def test_create_reasoning_engine(
533543
tarfile_open_mock,
534544
cloudpickle_dump_mock,
535545
get_reasoning_engine_mock,
546+
get_gca_resource_mock,
536547
):
537-
test_reasoning_engine = reasoning_engines.ReasoningEngine.create(
548+
reasoning_engines.ReasoningEngine.create(
538549
self.test_app,
539550
display_name=_TEST_REASONING_ENGINE_DISPLAY_NAME,
540551
requirements=_TEST_REASONING_ENGINE_REQUIREMENTS,
541552
extra_packages=[_TEST_REASONING_ENGINE_EXTRA_PACKAGE_PATH],
542553
)
543-
# Manually set _gca_resource here to prevent the mocks from propagating.
544-
test_reasoning_engine._gca_resource = _TEST_REASONING_ENGINE_OBJ
545554
create_reasoning_engine_mock.assert_called_with(
546555
parent=_TEST_PARENT,
547556
reasoning_engine=_TEST_INPUT_REASONING_ENGINE_OBJ,
@@ -595,20 +604,19 @@ def test_create_reasoning_engine_requirements_from_file(
595604
tarfile_open_mock,
596605
cloudpickle_dump_mock,
597606
get_reasoning_engine_mock,
607+
get_gca_resource_mock,
598608
):
599609
with mock.patch(
600610
"builtins.open",
601611
mock.mock_open(read_data="google-cloud-aiplatform==1.29.0"),
602612
) as mock_file:
603-
test_reasoning_engine = reasoning_engines.ReasoningEngine.create(
613+
reasoning_engines.ReasoningEngine.create(
604614
self.test_app,
605615
display_name=_TEST_REASONING_ENGINE_DISPLAY_NAME,
606616
requirements="requirements.txt",
607617
extra_packages=[_TEST_REASONING_ENGINE_EXTRA_PACKAGE_PATH],
608618
)
609619
mock_file.assert_called_with("requirements.txt")
610-
# Manually set _gca_resource here to prevent the mocks from propagating.
611-
test_reasoning_engine._gca_resource = _TEST_REASONING_ENGINE_OBJ
612620
create_reasoning_engine_mock.assert_called_with(
613621
parent=_TEST_PARENT,
614622
reasoning_engine=_TEST_INPUT_REASONING_ENGINE_OBJ,
@@ -808,15 +816,14 @@ def test_delete_after_create_reasoning_engine(
808816
cloudpickle_dump_mock,
809817
get_reasoning_engine_mock,
810818
delete_reasoning_engine_mock,
819+
get_gca_resource_mock,
811820
):
812821
test_reasoning_engine = reasoning_engines.ReasoningEngine.create(
813822
self.test_app,
814823
display_name=_TEST_REASONING_ENGINE_DISPLAY_NAME,
815824
requirements=_TEST_REASONING_ENGINE_REQUIREMENTS,
816825
extra_packages=[_TEST_REASONING_ENGINE_EXTRA_PACKAGE_PATH],
817826
)
818-
# Manually set _gca_resource here to prevent the mocks from propagating.
819-
test_reasoning_engine._gca_resource = _TEST_REASONING_ENGINE_OBJ
820827
create_reasoning_engine_mock.assert_called_with(
821828
parent=_TEST_PARENT,
822829
reasoning_engine=_TEST_INPUT_REASONING_ENGINE_OBJ,
@@ -834,10 +841,9 @@ def test_delete_after_get_reasoning_engine(
834841
self,
835842
get_reasoning_engine_mock,
836843
delete_reasoning_engine_mock,
844+
get_gca_resource_mock,
837845
):
838846
test_reasoning_engine = reasoning_engines.ReasoningEngine(_TEST_RESOURCE_ID)
839-
# Manually set _gca_resource here to prevent the mocks from propagating.
840-
test_reasoning_engine._gca_resource = _TEST_REASONING_ENGINE_OBJ
841847
get_reasoning_engine_mock.assert_called_with(
842848
name=_TEST_REASONING_ENGINE_RESOURCE_NAME,
843849
retry=_TEST_RETRY,
@@ -852,10 +858,9 @@ def test_query_reasoning_engine(
852858
get_reasoning_engine_mock,
853859
query_reasoning_engine_mock,
854860
to_dict_mock,
861+
get_gca_resource_mock,
855862
):
856863
test_reasoning_engine = reasoning_engines.ReasoningEngine(_TEST_RESOURCE_ID)
857-
# Manually set _gca_resource here to prevent the mocks from propagating.
858-
test_reasoning_engine._gca_resource = _TEST_REASONING_ENGINE_OBJ
859864
get_reasoning_engine_mock.assert_called_with(
860865
name=_TEST_REASONING_ENGINE_RESOURCE_NAME,
861866
retry=_TEST_RETRY,
@@ -866,10 +871,12 @@ def test_query_reasoning_engine(
866871
)
867872
to_dict_mock.assert_called_once()
868873

869-
def test_operation_schemas(self, get_reasoning_engine_mock):
874+
def test_operation_schemas(
875+
self,
876+
get_reasoning_engine_mock,
877+
get_gca_resource_mock,
878+
):
870879
test_reasoning_engine = reasoning_engines.ReasoningEngine(_TEST_RESOURCE_ID)
871-
# Manually set _gca_resource here to prevent the mocks from propagating.
872-
test_reasoning_engine._gca_resource = _TEST_REASONING_ENGINE_OBJ
873880
test_reasoning_engine._operation_schemas = (
874881
_TEST_REASONING_ENGINE_OPERATION_SCHEMAS
875882
)

0 commit comments

Comments
 (0)