Skip to content

Commit 208b33c

Browse files
committed
push
1 parent abea0fa commit 208b33c

File tree

1 file changed

+56
-60
lines changed

1 file changed

+56
-60
lines changed

tests/test_prompt.py

Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
import os
12
from time import sleep
23
from unittest.mock import Mock, patch
34

45
import openai
56
import pytest
67

78
from langfuse._client.client import Langfuse
9+
from langfuse._client.environment_variables import (
10+
LANGFUSE_PROMPT_CACHE_DEFAULT_TTL_SECONDS,
11+
)
812
from langfuse._utils.prompt_cache import (
913
DEFAULT_PROMPT_CACHE_TTL_SECONDS,
1014
PromptCacheItem,
@@ -13,8 +17,6 @@
1317
from langfuse.model import ChatPromptClient, TextPromptClient
1418
from tests.utils import create_uuid, get_api
1519

16-
import os
17-
from langfuse._client.environment_variables import LANGFUSE_PROMPT_CACHE_DEFAULT_TTL_SECONDS
1820

1921
def test_create_prompt():
2022
langfuse = Langfuse()
@@ -690,16 +692,6 @@ def langfuse():
690692

691693
return langfuse_instance
692694

693-
@pytest.fixture
694-
def langfuse_with_override_default_cache():
695-
langfuse_instance = Langfuse(
696-
public_key="test-public-key",
697-
secret_key="test-secret-key",
698-
host="https://mock-host.com",
699-
default_cache_ttl_seconds=OVERRIDE_DEFAULT_PROMPT_CACHE_TTL_SECONDS,
700-
)
701-
langfuse_instance.api = Mock()
702-
return langfuse_instance
703695

704696
# Fetching a new prompt when nothing in cache
705697
def test_get_fresh_prompt(langfuse):
@@ -1430,20 +1422,17 @@ def test_update_prompt():
14301422

14311423
def test_environment_variable_override_prompt_cache_ttl():
14321424
"""Test that LANGFUSE_PROMPT_CACHE_DEFAULT_TTL_SECONDS environment variable overrides default TTL."""
1433-
import os
1434-
from unittest.mock import patch
1435-
14361425
# Set environment variable to override default TTL
14371426
os.environ[LANGFUSE_PROMPT_CACHE_DEFAULT_TTL_SECONDS] = "120"
1438-
1427+
14391428
# Create a new Langfuse instance to pick up the environment variable
14401429
langfuse = Langfuse(
14411430
public_key="test-public-key",
14421431
secret_key="test-secret-key",
14431432
host="https://mock-host.com",
14441433
)
14451434
langfuse.api = Mock()
1446-
1435+
14471436
prompt_name = "test_env_override_ttl"
14481437
prompt = Prompt_Text(
14491438
name=prompt_name,
@@ -1455,62 +1444,64 @@ def test_environment_variable_override_prompt_cache_ttl():
14551444
tags=[],
14561445
)
14571446
prompt_client = TextPromptClient(prompt)
1458-
1447+
14591448
mock_server_call = langfuse.api.prompts.get
14601449
mock_server_call.return_value = prompt
1461-
1450+
14621451
# Mock time to control cache expiration
14631452
with patch.object(PromptCacheItem, "get_epoch_seconds") as mock_time:
14641453
mock_time.return_value = 0
1465-
1454+
14661455
# First call - should cache the prompt
14671456
result1 = langfuse.get_prompt(prompt_name)
14681457
assert mock_server_call.call_count == 1
14691458
assert result1 == prompt_client
1470-
1459+
14711460
# Check that prompt is cached
14721461
cached_item = langfuse._resources.prompt_cache.get(
1473-
langfuse._resources.prompt_cache.generate_cache_key(prompt_name, version=None, label=None)
1462+
langfuse._resources.prompt_cache.generate_cache_key(
1463+
prompt_name, version=None, label=None
1464+
)
14741465
)
14751466
assert cached_item is not None
14761467
assert cached_item.value == prompt_client
1477-
1468+
14781469
# Debug: check the cache item's expiry time
14791470
print(f"DEBUG: Cache item expiry: {cached_item._expiry}")
14801471
print(f"DEBUG: Current mock time: {mock_time.return_value}")
14811472
print(f"DEBUG: Is expired? {cached_item.is_expired()}")
1482-
1473+
14831474
# Set time to 60 seconds (before new TTL of 120 seconds)
14841475
mock_time.return_value = 60
1485-
1476+
14861477
# Second call - should still use cache
14871478
result2 = langfuse.get_prompt(prompt_name)
14881479
assert mock_server_call.call_count == 1 # No new server call
14891480
assert result2 == prompt_client
1490-
1481+
14911482
# Set time to 120 seconds (at TTL expiration)
14921483
mock_time.return_value = 120
1493-
1484+
14941485
# Third call - should still use cache (stale cache behavior)
14951486
result3 = langfuse.get_prompt(prompt_name)
14961487
assert result3 == prompt_client
1497-
1488+
14981489
# Wait for background refresh to complete
14991490
while True:
15001491
if langfuse._resources.prompt_cache._task_manager.active_tasks() == 0:
15011492
break
15021493
sleep(0.1)
1503-
1494+
15041495
# Should have made a new server call for refresh
15051496
assert mock_server_call.call_count == 2
1506-
1497+
15071498
# Set time to 121 seconds (after TTL expiration)
15081499
mock_time.return_value = 121
1509-
1500+
15101501
# Fourth call - should use refreshed cache
15111502
result4 = langfuse.get_prompt(prompt_name)
15121503
assert result4 == prompt_client
1513-
1504+
15141505
# Clean up environment variable
15151506
if LANGFUSE_PROMPT_CACHE_DEFAULT_TTL_SECONDS in os.environ:
15161507
del os.environ[LANGFUSE_PROMPT_CACHE_DEFAULT_TTL_SECONDS]
@@ -1519,23 +1510,22 @@ def test_environment_variable_override_prompt_cache_ttl():
15191510
@patch.object(PromptCacheItem, "get_epoch_seconds")
15201511
def test_default_ttl_when_environment_variable_not_set(mock_time):
15211512
"""Test that default 60-second TTL is used when LANGFUSE_PROMPT_CACHE_DEFAULT_TTL_SECONDS is not set."""
1522-
from unittest.mock import patch
1523-
1513+
15241514
# Ensure environment variable is not set
15251515
if LANGFUSE_PROMPT_CACHE_DEFAULT_TTL_SECONDS in os.environ:
15261516
del os.environ[LANGFUSE_PROMPT_CACHE_DEFAULT_TTL_SECONDS]
1527-
1517+
15281518
# Set initial time to 0
15291519
mock_time.return_value = 0
1530-
1520+
15311521
# Create a new Langfuse instance to pick up the default TTL
15321522
langfuse = Langfuse(
15331523
public_key="test-public-key",
15341524
secret_key="test-secret-key",
15351525
host="https://mock-host.com",
15361526
)
15371527
langfuse.api = Mock()
1538-
1528+
15391529
prompt_name = "test_default_ttl"
15401530
prompt = Prompt_Text(
15411531
name=prompt_name,
@@ -1547,59 +1537,61 @@ def test_default_ttl_when_environment_variable_not_set(mock_time):
15471537
tags=[],
15481538
)
15491539
prompt_client = TextPromptClient(prompt)
1550-
1540+
15511541
mock_server_call = langfuse.api.prompts.get
15521542
mock_server_call.return_value = prompt
1553-
1543+
15541544
# First call - should cache the prompt
15551545
result1 = langfuse.get_prompt(prompt_name)
15561546
assert mock_server_call.call_count == 1
15571547
assert result1 == prompt_client
1558-
1548+
15591549
# Check that prompt is cached
15601550
cached_item = langfuse._resources.prompt_cache.get(
1561-
langfuse._resources.prompt_cache.generate_cache_key(prompt_name, version=None, label=None)
1551+
langfuse._resources.prompt_cache.generate_cache_key(
1552+
prompt_name, version=None, label=None
1553+
)
15621554
)
15631555
assert cached_item is not None
15641556
assert cached_item.value == prompt_client
1565-
1557+
15661558
# Set time to just before default TTL expiration
15671559
mock_time.return_value = DEFAULT_PROMPT_CACHE_TTL_SECONDS - 1
1568-
1560+
15691561
# Second call - should still use cache
15701562
result2 = langfuse.get_prompt(prompt_name)
15711563
assert mock_server_call.call_count == 1 # No new server call
15721564
assert result2 == prompt_client
1573-
1565+
15741566
# Set time to just after default TTL expiration to trigger cache expiry
15751567
# Use the actual DEFAULT_PROMPT_CACHE_TTL_SECONDS value that was imported
15761568
mock_time.return_value = DEFAULT_PROMPT_CACHE_TTL_SECONDS + 1
1577-
1569+
15781570
# Third call - should still use cache (stale cache behavior)
15791571
result3 = langfuse.get_prompt(prompt_name)
15801572
assert result3 == prompt_client
1581-
1573+
15821574
# Wait for background refresh to complete
15831575
while True:
15841576
if langfuse._resources.prompt_cache._task_manager.active_tasks() == 0:
15851577
break
15861578
sleep(0.1)
1587-
1579+
15881580
# Should have made a new server call for refresh
15891581
assert mock_server_call.call_count == 2
1590-
1582+
15911583
# Set time to just after default TTL expiration
15921584
mock_time.return_value = DEFAULT_PROMPT_CACHE_TTL_SECONDS + 1
1593-
1585+
15941586
# Fourth call - should use refreshed cache
15951587
result4 = langfuse.get_prompt(prompt_name)
15961588
assert result4 == prompt_client
15971589

1598-
1590+
15991591
def test_clear_prompt_cache(langfuse):
16001592
"""Test clearing the entire prompt cache."""
16011593
prompt_name = create_uuid()
1602-
1594+
16031595
# Mock the API calls
16041596
mock_prompt = Prompt_Text(
16051597
name=prompt_name,
@@ -1610,34 +1602,38 @@ def test_clear_prompt_cache(langfuse):
16101602
config={},
16111603
tags=[],
16121604
)
1613-
1605+
16141606
# Mock the create_prompt API call
16151607
langfuse.api.prompts.create.return_value = mock_prompt
1616-
1608+
16171609
# Mock the get_prompt API call
16181610
langfuse.api.prompts.get.return_value = mock_prompt
1619-
1611+
16201612
# Create a prompt and cache it
16211613
prompt_client = langfuse.create_prompt(
16221614
name=prompt_name,
16231615
prompt="test prompt",
16241616
labels=["production"],
16251617
)
1626-
1618+
16271619
# Get the prompt to cache it
16281620
cached_prompt = langfuse.get_prompt(prompt_name)
16291621
assert cached_prompt.name == prompt_name
16301622

16311623
# Verify that the prompt is in the cache
16321624
cache_key = f"{prompt_name}-label:production"
1633-
assert langfuse._resources.prompt_cache.get(cache_key) is not None, "Prompt should be in cache before clearing"
1634-
1625+
assert (
1626+
langfuse._resources.prompt_cache.get(cache_key) is not None
1627+
), "Prompt should be in cache before clearing"
1628+
16351629
# Clear the entire prompt cache
16361630
langfuse.clear_prompt_cache()
1637-
1631+
16381632
# Verify cache is completely cleared
1639-
assert langfuse._resources.prompt_cache.get(cache_key) is None, "Prompt should be removed from cache after clearing"
1640-
1633+
assert (
1634+
langfuse._resources.prompt_cache.get(cache_key) is None
1635+
), "Prompt should be removed from cache after clearing"
1636+
16411637
# Verify data integrity
16421638
assert prompt_client.name == prompt_name
16431639
assert cached_prompt.name == prompt_name

0 commit comments

Comments
 (0)