Skip to content

Commit 2a7bca4

Browse files
committed
Add 'really_needs_auth' fixture that skips the test if no API key env var is found
1 parent 4a6d00d commit 2a7bca4

9 files changed

+27
-25
lines changed

test/fixtures.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@
4444
def needs_auth() -> None:
4545
utils.load_dotenv()
4646

47+
@pytest.fixture(scope="session")
48+
def really_needs_auth() -> None:
49+
utils.load_dotenv()
50+
# Check if any of the supported API keys is set
51+
if not (os.getenv("OPENAI_API_KEY") or os.getenv("AZURE_OPENAI_API_KEY")):
52+
pytest.skip("No API key found")
53+
4754

4855
@pytest.fixture(scope="session")
4956
def embedding_model() -> AsyncEmbeddingModel:

test/test_demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from typeagent.knowpro.interfaces import ScoredSemanticRefOrdinal
1313
from typeagent.podcasts import podcast
1414

15-
from fixtures import needs_auth
15+
from fixtures import really_needs_auth
1616

1717
tests_dir = os.path.dirname(__file__)
1818
root_dir = os.path.dirname(tests_dir)
@@ -27,7 +27,7 @@
2727
)
2828

2929

30-
def test_main(needs_auth: None):
30+
def test_main(really_needs_auth: None):
3131
# auth is needed because we use embeddings.
3232
# TODO: Only use the embeddings loaded from the file and cached.
3333
asyncio.run(main(DEFAULT_FILE))

test/test_knowledge.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
)
1515
from typeagent.knowpro import convknowledge, kplib
1616

17-
from fixtures import needs_auth # type: ignore # Used!
17+
from fixtures import really_needs_auth
1818

1919

2020
class MockKnowledgeExtractor:
@@ -34,7 +34,7 @@ def mock_knowledge_extractor() -> convknowledge.KnowledgeExtractor:
3434
return MockKnowledgeExtractor() # type: ignore
3535

3636

37-
def test_create_knowledge_extractor(needs_auth: None):
37+
def test_create_knowledge_extractor(really_needs_auth: None):
3838
"""Test creating a knowledge extractor."""
3939
extractor = create_knowledge_extractor()
4040
assert isinstance(extractor, convknowledge.KnowledgeExtractor)

test/test_property_index_population.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
from typeagent.podcasts.podcast import PodcastMessage
2222
from typeagent.storage import SqliteStorageProvider
2323

24+
from fixtures import really_needs_auth
25+
2426

2527
class MockEmbeddingModel(AsyncEmbeddingModel):
2628
def __init__(self):
@@ -33,7 +35,7 @@ async def get_embeddings(self, keys: list[str]) -> np.ndarray:
3335

3436

3537
@pytest.mark.asyncio
36-
async def test_property_index_population_from_database():
38+
async def test_property_index_population_from_database(really_needs_auth):
3739
"""Test that property index is correctly populated when reopening a database."""
3840
load_dotenv()
3941
temp_db_path = tempfile.mktemp(suffix=".sqlite")

test/test_query_method.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,11 @@
1111
from typeagent.knowpro.convsettings import ConversationSettings
1212
from typeagent.transcripts.transcript import TranscriptMessage, TranscriptMessageMeta
1313

14-
15-
@pytest.fixture(scope="session")
16-
def needs_auth() -> None:
17-
"""Load environment variables for authentication."""
18-
load_dotenv()
14+
from fixtures import really_needs_auth
1915

2016

2117
@pytest.mark.asyncio
22-
async def test_query_method_basic(needs_auth: None):
18+
async def test_query_method_basic(really_needs_auth: None):
2319
"""Test the basic query method workflow."""
2420
# Create a conversation with some test data
2521
test_model = AsyncEmbeddingModel(model_name=TEST_MODEL_NAME)
@@ -65,7 +61,7 @@ async def test_query_method_basic(needs_auth: None):
6561

6662

6763
@pytest.mark.asyncio
68-
async def test_query_method_empty_conversation(needs_auth: None):
64+
async def test_query_method_empty_conversation(really_needs_auth: None):
6965
"""Test query method on an empty conversation."""
7066
test_model = AsyncEmbeddingModel(model_name=TEST_MODEL_NAME)
7167
settings = ConversationSettings(model=test_model)

test/test_related_terms_index_population.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
"""Test to verify related terms index population in storage providers."""
66

7-
import asyncio
87
import tempfile
98
import os
109
import pytest
@@ -18,13 +17,13 @@
1817
MessageTextIndexSettings,
1918
RelatedTermIndexSettings,
2019
)
21-
from typeagent.storage.memory.reltermsindex import RelatedTermsIndex
2220
from typeagent.podcasts.podcast import PodcastMessage, PodcastMessageMeta
2321
from typeagent.storage import SqliteStorageProvider
2422

23+
from fixtures import really_needs_auth
2524

2625
@pytest.mark.asyncio
27-
async def test_related_terms_index_population_from_database():
26+
async def test_related_terms_index_population_from_database(really_needs_auth):
2827
"""Test that related terms index is correctly populated when reopening a database."""
2928
load_dotenv()
3029
temp_db_path = tempfile.mktemp(suffix=".sqlite")

test/test_reltermsindex.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from typeagent.storage import SqliteStorageProvider
2727

2828
# Test fixtures
29-
from fixtures import needs_auth, embedding_model, temp_db_path
29+
from fixtures import needs_auth, really_needs_auth, embedding_model, temp_db_path
3030

3131

3232
@pytest_asyncio.fixture(params=["memory", "sqlite"])
@@ -122,7 +122,7 @@ async def test_serialize_and_deserialize(
122122

123123

124124
@pytest.mark.asyncio
125-
async def test_related_terms_index_basic(needs_auth: None) -> None:
125+
async def test_related_terms_index_basic(really_needs_auth: None) -> None:
126126
settings = RelatedTermIndexSettings(TextEmbeddingIndexSettings())
127127
index = RelatedTermsIndex(settings)
128128
assert isinstance(index.aliases, TermToRelatedTermsMap)

test/test_transcripts.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33

44
import pytest
55
import os
6-
import tempfile
76
from datetime import timedelta
8-
from typing import AsyncGenerator
97

108
from typeagent.transcripts.transcript_ingest import (
11-
ingest_vtt_transcript,
129
get_transcript_speakers,
1310
get_transcript_duration,
1411
extract_speaker_from_text,
@@ -24,10 +21,9 @@
2421
format_timestamp_utc,
2522
)
2623
from typeagent.knowpro.convsettings import ConversationSettings
27-
from typeagent.knowpro.interfaces import Datetime
2824
from typeagent.aitools.embeddings import AsyncEmbeddingModel
2925

30-
from fixtures import needs_auth, temp_dir, embedding_model # type: ignore
26+
from fixtures import needs_auth, really_needs_auth, temp_dir, embedding_model # type: ignore
3127

3228

3329
def test_extract_speaker_from_text():
@@ -245,7 +241,7 @@ async def test_transcript_creation():
245241

246242
@pytest.mark.asyncio
247243
async def test_transcript_knowledge_extraction_slow(
248-
needs_auth: None, embedding_model: AsyncEmbeddingModel
244+
really_needs_auth: None, embedding_model: AsyncEmbeddingModel
249245
):
250246
"""
251247
Test that knowledge extraction works during transcript ingestion.

test/test_utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22
# Licensed under the MIT License.
33

44
import os
5-
import pytest
65
from contextlib import redirect_stdout
76
from io import StringIO
87

98
import typeagent.aitools.utils as utils
109

1110

11+
from fixtures import really_needs_auth
12+
13+
1214
def test_timelog():
1315
buf = StringIO()
1416
with redirect_stdout(buf):
@@ -29,7 +31,7 @@ def test_pretty_print():
2931
assert out == '{"a": 1}\n', out
3032

3133

32-
def test_load_dotenv():
34+
def test_load_dotenv(really_needs_auth):
3335
# Call load_dotenv and check for at least one expected key
3436
utils.load_dotenv()
3537
assert "OPENAI_API_KEY" in os.environ or "AZURE_OPENAI_API_KEY" in os.environ

0 commit comments

Comments
 (0)