diff --git a/tests/integration/encoders/test_openai_integration.py b/tests/integration/encoders/test_openai_integration.py index 3298071b46152df0431ccce140873281e3ef9597..7c6cfe06343ff7734fc1415d010e50a872771884 100644 --- a/tests/integration/encoders/test_openai_integration.py +++ b/tests/integration/encoders/test_openai_integration.py @@ -1,3 +1,4 @@ +import os import pytest from semantic_router.encoders.openai import OpenAIEncoder @@ -11,22 +12,37 @@ def openai_encoder(): class TestOpenAIEncoder: + @pytest.mark.skipif( + os.environ.get("OPENAI_API_KEY") is None, reason="OpenAI API key required" + ) def test_openai_encoder_init_success(self, openai_encoder): assert openai_encoder.client is not None + @pytest.mark.skipif( + os.environ.get("OPENAI_API_KEY") is None, reason="OpenAI API key required" + ) def test_openai_encoder_dims(self, openai_encoder): embeddings = openai_encoder(["test document"]) assert len(embeddings) == 1 assert len(embeddings[0]) == 1536 + @pytest.mark.skipif( + os.environ.get("OPENAI_API_KEY") is None, reason="OpenAI API key required" + ) def test_openai_encoder_call_truncation(self, openai_encoder): openai_encoder([long_doc]) + @pytest.mark.skipif( + os.environ.get("OPENAI_API_KEY") is None, reason="OpenAI API key required" + ) def test_openai_encoder_call_no_truncation(self, openai_encoder): with pytest.raises(ValueError) as _: # default truncation is True openai_encoder([long_doc], truncate=False) + @pytest.mark.skipif( + os.environ.get("OPENAI_API_KEY") is None, reason="OpenAI API key required" + ) def test_openai_encoder_call_uninitialized_client(self, openai_encoder): # Set the client to None to simulate an uninitialized client openai_encoder.client = None diff --git a/tests/unit/test_layer.py b/tests/unit/test_layer.py index bc7be980b21c9dcc9308d3264fbe4584cd2429ff..c6c36b7ee21d6769170721404e2e3ea619f16991 100644 --- a/tests/unit/test_layer.py +++ b/tests/unit/test_layer.py @@ -280,39 +280,41 @@ class TestRouteLayer: os.environ.get("PINECONE_API_KEY") is None, reason="Pinecone API key required" ) def test_query_filter_pinecone(self, openai_encoder, routes, index_cls): - pinecone_api_key = os.environ["PINECONE_API_KEY"] - pineconeindex = PineconeIndex(api_key=pinecone_api_key) - route_layer = RouteLayer( - encoder=openai_encoder, routes=routes, index=pineconeindex - ) - time.sleep(10) # allow for index to be populated - query_result = route_layer(text="Hello", route_filter=["Route 1"]).name + if type(index_cls) == PineconeIndex: + pinecone_api_key = os.environ["PINECONE_API_KEY"] + pineconeindex = PineconeIndex(api_key=pinecone_api_key) + route_layer = RouteLayer( + encoder=openai_encoder, routes=routes, index=pineconeindex + ) + time.sleep(10) # allow for index to be populated + query_result = route_layer(text="Hello", route_filter=["Route 1"]).name - try: - route_layer(text="Hello", route_filter=["Route 8"]).name - except ValueError: - assert True + try: + route_layer(text="Hello", route_filter=["Route 8"]).name + except ValueError: + assert True - assert query_result in ["Route 1"] + assert query_result in ["Route 1"] @pytest.mark.skipif( os.environ.get("PINECONE_API_KEY") is None, reason="Pinecone API key required" ) def test_namespace_pinecone_index(self, openai_encoder, routes, index_cls): - pinecone_api_key = os.environ["PINECONE_API_KEY"] - pineconeindex = PineconeIndex(api_key=pinecone_api_key, namespace="test") - route_layer = RouteLayer( - encoder=openai_encoder, routes=routes, index=pineconeindex - ) - time.sleep(10) # allow for index to be populated - query_result = route_layer(text="Hello", route_filter=["Route 1"]).name + if type(index_cls) == PineconeIndex: + pinecone_api_key = os.environ["PINECONE_API_KEY"] + pineconeindex = PineconeIndex(api_key=pinecone_api_key, namespace="test") + route_layer = RouteLayer( + encoder=openai_encoder, routes=routes, index=pineconeindex + ) + time.sleep(10) # allow for index to be populated + query_result = route_layer(text="Hello", route_filter=["Route 1"]).name - try: - route_layer(text="Hello", route_filter=["Route 8"]).name - except ValueError: - assert True + try: + route_layer(text="Hello", route_filter=["Route 8"]).name + except ValueError: + assert True - assert query_result in ["Route 1"] + assert query_result in ["Route 1"] def test_query_with_no_index(self, openai_encoder, index_cls): route_layer = RouteLayer(encoder=openai_encoder, index=index_cls())