@@ -1505,135 +1505,3 @@ def test_environment_variable_override_prompt_cache_ttl():
15051505 # Clean up environment variable
15061506 if LANGFUSE_PROMPT_CACHE_DEFAULT_TTL_SECONDS in os .environ :
15071507 del os .environ [LANGFUSE_PROMPT_CACHE_DEFAULT_TTL_SECONDS ]
1508-
1509-
1510- @patch .object (PromptCacheItem , "get_epoch_seconds" )
1511- def test_default_ttl_when_environment_variable_not_set (mock_time ):
1512- """Test that default 60-second TTL is used when LANGFUSE_PROMPT_CACHE_DEFAULT_TTL_SECONDS is not set."""
1513-
1514- # Ensure environment variable is not set
1515- if LANGFUSE_PROMPT_CACHE_DEFAULT_TTL_SECONDS in os .environ :
1516- del os .environ [LANGFUSE_PROMPT_CACHE_DEFAULT_TTL_SECONDS ]
1517-
1518- # Set initial time to 0
1519- mock_time .return_value = 0
1520-
1521- # Create a new Langfuse instance to pick up the default TTL
1522- langfuse = Langfuse (
1523- public_key = "test-public-key" ,
1524- secret_key = "test-secret-key" ,
1525- host = "https://mock-host.com" ,
1526- )
1527- langfuse .api = Mock ()
1528-
1529- prompt_name = "test_default_ttl"
1530- prompt = Prompt_Text (
1531- name = prompt_name ,
1532- version = 1 ,
1533- prompt = "Test prompt with default TTL" ,
1534- type = "text" ,
1535- labels = [],
1536- config = {},
1537- tags = [],
1538- )
1539- prompt_client = TextPromptClient (prompt )
1540-
1541- mock_server_call = langfuse .api .prompts .get
1542- mock_server_call .return_value = prompt
1543-
1544- # First call - should cache the prompt
1545- result1 = langfuse .get_prompt (prompt_name )
1546- assert mock_server_call .call_count == 1
1547- assert result1 == prompt_client
1548-
1549- # Check that prompt is cached
1550- cached_item = langfuse ._resources .prompt_cache .get (
1551- langfuse ._resources .prompt_cache .generate_cache_key (
1552- prompt_name , version = None , label = None
1553- )
1554- )
1555- assert cached_item is not None
1556- assert cached_item .value == prompt_client
1557-
1558- # Set time to just before default TTL expiration
1559- mock_time .return_value = DEFAULT_PROMPT_CACHE_TTL_SECONDS - 1
1560-
1561- # Second call - should still use cache
1562- result2 = langfuse .get_prompt (prompt_name )
1563- assert mock_server_call .call_count == 1 # No new server call
1564- assert result2 == prompt_client
1565-
1566- # Set time to just after default TTL expiration to trigger cache expiry
1567- # Use the actual DEFAULT_PROMPT_CACHE_TTL_SECONDS value that was imported
1568- mock_time .return_value = DEFAULT_PROMPT_CACHE_TTL_SECONDS + 1
1569-
1570- # Third call - should still use cache (stale cache behavior)
1571- result3 = langfuse .get_prompt (prompt_name )
1572- assert result3 == prompt_client
1573-
1574- # Wait for background refresh to complete
1575- while True :
1576- if langfuse ._resources .prompt_cache ._task_manager .active_tasks () == 0 :
1577- break
1578- sleep (0.1 )
1579-
1580- # Should have made a new server call for refresh
1581- assert mock_server_call .call_count == 2
1582-
1583- # Set time to just after default TTL expiration
1584- mock_time .return_value = DEFAULT_PROMPT_CACHE_TTL_SECONDS + 1
1585-
1586- # Fourth call - should use refreshed cache
1587- result4 = langfuse .get_prompt (prompt_name )
1588- assert result4 == prompt_client
1589-
1590-
1591- def test_clear_prompt_cache (langfuse ):
1592- """Test clearing the entire prompt cache."""
1593- prompt_name = create_uuid ()
1594-
1595- # Mock the API calls
1596- mock_prompt = Prompt_Text (
1597- name = prompt_name ,
1598- version = 1 ,
1599- prompt = "test prompt" ,
1600- type = "text" ,
1601- labels = ["production" ],
1602- config = {},
1603- tags = [],
1604- )
1605-
1606- # Mock the create_prompt API call
1607- langfuse .api .prompts .create .return_value = mock_prompt
1608-
1609- # Mock the get_prompt API call
1610- langfuse .api .prompts .get .return_value = mock_prompt
1611-
1612- # Create a prompt and cache it
1613- prompt_client = langfuse .create_prompt (
1614- name = prompt_name ,
1615- prompt = "test prompt" ,
1616- labels = ["production" ],
1617- )
1618-
1619- # Get the prompt to cache it
1620- cached_prompt = langfuse .get_prompt (prompt_name )
1621- assert cached_prompt .name == prompt_name
1622-
1623- # Verify that the prompt is in the cache
1624- cache_key = f"{ prompt_name } -label:production"
1625- assert (
1626- langfuse ._resources .prompt_cache .get (cache_key ) is not None
1627- ), "Prompt should be in cache before clearing"
1628-
1629- # Clear the entire prompt cache
1630- langfuse .clear_prompt_cache ()
1631-
1632- # Verify cache is completely cleared
1633- assert (
1634- langfuse ._resources .prompt_cache .get (cache_key ) is None
1635- ), "Prompt should be removed from cache after clearing"
1636-
1637- # Verify data integrity
1638- assert prompt_client .name == prompt_name
1639- assert cached_prompt .name == prompt_name
0 commit comments