Skip to content

Commit 117dbdc

Browse files
committed
test: improve lazy client test to follow project conventions
- Remove verbose comments and print statements - Focus on observable behavior rather than internal implementation - Use proper mocking that matches actual cog integration - Test that cog.current_scope() is called on client creation - Address code review feedback from PR discussion
1 parent 0017b48 commit 117dbdc

File tree

1 file changed

+33
-56
lines changed

1 file changed

+33
-56
lines changed

tests/test_simple_lazy.py

Lines changed: 33 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,52 @@
1-
"""Simple test showing the lazy client fix works."""
1+
"""Test lazy client creation in replicate.use()."""
22

33
import os
44
import sys
5-
from typing import Any
65
from unittest.mock import MagicMock, patch
76

87

9-
def test_use_does_not_create_client_immediately():
10-
"""Test that replicate.use() does not create a client until the model is called."""
8+
def test_use_does_not_raise_without_token():
9+
"""Test that replicate.use() works even when no API token is available."""
1110
sys.path.insert(0, "src")
12-
13-
# Clear any existing token to simulate the original error condition
11+
1412
with patch.dict(os.environ, {}, clear=True):
1513
with patch.dict(sys.modules, {"cog": None}):
16-
try:
17-
import replicate
18-
19-
# This should work now - no client is created yet
20-
model: Any = replicate.use("test/model") # type: ignore[misc]
21-
22-
# Verify we got a Function object back
23-
from replicate.lib._predictions_use import Function
24-
25-
assert isinstance(model, Function)
26-
print("✓ replicate.use() works without immediate client creation")
27-
28-
# Verify the client property is a property that will create client on demand
29-
# We can't call it without a token, but we can check it's the right type
30-
assert hasattr(model, "_client_class") # type: ignore[misc]
31-
print("✓ Client class is stored for lazy creation")
32-
33-
except Exception as e:
34-
print(f"✗ Test failed: {e}")
35-
raise
14+
import replicate
15+
16+
# Should not raise an exception
17+
model = replicate.use("test/model")
18+
assert model is not None
3619

3720

38-
def test_client_created_when_model_called():
39-
"""Test that the client is created when the model is called."""
21+
def test_cog_current_scope():
22+
"""Test that cog.current_scope().context is read on each client creation."""
4023
sys.path.insert(0, "src")
41-
42-
# Test that we can create a model function with a token available
43-
# Mock cog to provide a token
24+
25+
mock_context = MagicMock()
26+
mock_context.items.return_value = [("REPLICATE_API_TOKEN", "test-token-1")]
27+
4428
mock_scope = MagicMock()
45-
mock_scope.context = {"REPLICATE_API_TOKEN": "test-token"}
29+
mock_scope.context = mock_context
30+
4631
mock_cog = MagicMock()
4732
mock_cog.current_scope.return_value = mock_scope
4833

4934
with patch.dict(os.environ, {}, clear=True):
5035
with patch.dict(sys.modules, {"cog": mock_cog}):
5136
import replicate
52-
53-
# Create model function - should work without errors
54-
model: Any = replicate.use("test/model") # type: ignore[misc]
55-
print("✓ Model function created successfully")
56-
57-
# Verify the model has the lazy client setup
58-
assert hasattr(model, "_client_class")
59-
assert isinstance(model._client_class, type)
60-
print("✓ Lazy client class is properly configured")
61-
62-
# Test that accessing _client property works (creates client)
63-
try:
64-
client = model._client # This should create the client
65-
assert client is not None
66-
print("✓ Client created successfully when accessed")
67-
except Exception as e:
68-
print(f"ℹ Client creation expected to work but got: {e}")
69-
# This is okay - the important thing is that use() worked
70-
71-
72-
if __name__ == "__main__":
73-
test_use_does_not_create_client_immediately()
74-
test_client_created_when_model_called()
75-
print("\n✓ All tests passed! The lazy client fix works correctly.")
37+
38+
model = replicate.use("test/model")
39+
40+
# Access the client property - this should trigger client creation and cog.current_scope call
41+
_ = model._client
42+
43+
assert mock_cog.current_scope.call_count == 1
44+
45+
# Change the token and access client again - should trigger another call
46+
mock_context.items.return_value = [("REPLICATE_API_TOKEN", "test-token-2")]
47+
48+
# Create a new model to trigger another client creation
49+
model2 = replicate.use("test/model2")
50+
_ = model2._client
51+
52+
assert mock_cog.current_scope.call_count == 2

0 commit comments

Comments
 (0)