|
1 | | -"""Simple test showing the lazy client fix works.""" |
| 1 | +"""Test lazy client creation in replicate.use().""" |
2 | 2 |
|
3 | 3 | import os |
4 | 4 | import sys |
5 | | -from typing import Any |
6 | 5 | from unittest.mock import MagicMock, patch |
7 | 6 |
|
8 | 7 |
|
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.""" |
11 | 10 | sys.path.insert(0, "src") |
12 | | - |
13 | | - # Clear any existing token to simulate the original error condition |
| 11 | + |
14 | 12 | with patch.dict(os.environ, {}, clear=True): |
15 | 13 | 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 |
36 | 19 |
|
37 | 20 |
|
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.""" |
40 | 23 | 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 | + |
44 | 28 | mock_scope = MagicMock() |
45 | | - mock_scope.context = {"REPLICATE_API_TOKEN": "test-token"} |
| 29 | + mock_scope.context = mock_context |
| 30 | + |
46 | 31 | mock_cog = MagicMock() |
47 | 32 | mock_cog.current_scope.return_value = mock_scope |
48 | 33 |
|
49 | 34 | with patch.dict(os.environ, {}, clear=True): |
50 | 35 | with patch.dict(sys.modules, {"cog": mock_cog}): |
51 | 36 | 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