77import pytest
88
99from replicate import Replicate , AsyncReplicate
10- from replicate ._client import _get_api_token_from_environment
10+ from replicate .lib . cog import get_api_token_from_environment
1111from replicate ._exceptions import ReplicateError
1212
1313
1414class TestGetApiTokenFromEnvironment :
15- """Test the _get_api_token_from_environment function."""
15+ """Test the get_api_token_from_environment function."""
1616
1717 def test_cog_no_current_scope_method_falls_back_to_env (self ):
1818 """Test fallback when cog exists but has no current_scope method."""
1919 mock_cog = mock .MagicMock ()
2020 del mock_cog .current_scope # Remove the method
2121 with mock .patch .dict (os .environ , {"REPLICATE_API_TOKEN" : "env-token" }):
2222 with mock .patch .dict (sys .modules , {"cog" : mock_cog }):
23- token = _get_api_token_from_environment ()
23+ token = get_api_token_from_environment ()
2424 assert token == "env-token"
2525
2626 def test_cog_current_scope_returns_none_falls_back_to_env (self ):
@@ -29,7 +29,7 @@ def test_cog_current_scope_returns_none_falls_back_to_env(self):
2929 mock_cog .current_scope .return_value = None
3030 with mock .patch .dict (os .environ , {"REPLICATE_API_TOKEN" : "env-token" }):
3131 with mock .patch .dict (sys .modules , {"cog" : mock_cog }):
32- token = _get_api_token_from_environment ()
32+ token = get_api_token_from_environment ()
3333 assert token == "env-token"
3434
3535 def test_cog_scope_no_context_attr_falls_back_to_env (self ):
@@ -40,7 +40,7 @@ def test_cog_scope_no_context_attr_falls_back_to_env(self):
4040 mock_cog .current_scope .return_value = mock_scope
4141 with mock .patch .dict (os .environ , {"REPLICATE_API_TOKEN" : "env-token" }):
4242 with mock .patch .dict (sys .modules , {"cog" : mock_cog }):
43- token = _get_api_token_from_environment ()
43+ token = get_api_token_from_environment ()
4444 assert token == "env-token"
4545
4646 def test_cog_scope_context_not_dict_falls_back_to_env (self ):
@@ -51,7 +51,7 @@ def test_cog_scope_context_not_dict_falls_back_to_env(self):
5151 mock_cog .current_scope .return_value = mock_scope
5252 with mock .patch .dict (os .environ , {"REPLICATE_API_TOKEN" : "env-token" }):
5353 with mock .patch .dict (sys .modules , {"cog" : mock_cog }):
54- token = _get_api_token_from_environment ()
54+ token = get_api_token_from_environment ()
5555 assert token == "env-token"
5656
5757 def test_cog_scope_no_replicate_api_token_key_falls_back_to_env (self ):
@@ -62,7 +62,7 @@ def test_cog_scope_no_replicate_api_token_key_falls_back_to_env(self):
6262 mock_cog .current_scope .return_value = mock_scope
6363 with mock .patch .dict (os .environ , {"REPLICATE_API_TOKEN" : "env-token" }):
6464 with mock .patch .dict (sys .modules , {"cog" : mock_cog }):
65- token = _get_api_token_from_environment ()
65+ token = get_api_token_from_environment ()
6666 assert token == "env-token"
6767
6868 def test_cog_scope_replicate_api_token_valid_string (self ):
@@ -73,7 +73,7 @@ def test_cog_scope_replicate_api_token_valid_string(self):
7373 mock_cog .current_scope .return_value = mock_scope
7474 with mock .patch .dict (os .environ , {"REPLICATE_API_TOKEN" : "env-token" }):
7575 with mock .patch .dict (sys .modules , {"cog" : mock_cog }):
76- token = _get_api_token_from_environment ()
76+ token = get_api_token_from_environment ()
7777 assert token == "cog-token"
7878
7979 def test_cog_scope_replicate_api_token_case_insensitive (self ):
@@ -84,7 +84,7 @@ def test_cog_scope_replicate_api_token_case_insensitive(self):
8484 mock_cog .current_scope .return_value = mock_scope
8585 with mock .patch .dict (os .environ , {"REPLICATE_API_TOKEN" : "env-token" }):
8686 with mock .patch .dict (sys .modules , {"cog" : mock_cog }):
87- token = _get_api_token_from_environment ()
87+ token = get_api_token_from_environment ()
8888 assert token == "cog-token"
8989
9090 def test_cog_scope_replicate_api_token_empty_string (self ):
@@ -95,7 +95,7 @@ def test_cog_scope_replicate_api_token_empty_string(self):
9595 mock_cog .current_scope .return_value = mock_scope
9696 with mock .patch .dict (os .environ , {"REPLICATE_API_TOKEN" : "env-token" }):
9797 with mock .patch .dict (sys .modules , {"cog" : mock_cog }):
98- token = _get_api_token_from_environment ()
98+ token = get_api_token_from_environment ()
9999 assert token == "" # Should return empty string, not env token
100100
101101 def test_cog_scope_replicate_api_token_none (self ):
@@ -106,7 +106,7 @@ def test_cog_scope_replicate_api_token_none(self):
106106 mock_cog .current_scope .return_value = mock_scope
107107 with mock .patch .dict (os .environ , {"REPLICATE_API_TOKEN" : "env-token" }):
108108 with mock .patch .dict (sys .modules , {"cog" : mock_cog }):
109- token = _get_api_token_from_environment ()
109+ token = get_api_token_from_environment ()
110110 assert token is None # Should return None, not env token
111111
112112 def test_cog_current_scope_raises_exception_falls_back_to_env (self ):
@@ -115,28 +115,28 @@ def test_cog_current_scope_raises_exception_falls_back_to_env(self):
115115 mock_cog .current_scope .side_effect = RuntimeError ("Scope error" )
116116 with mock .patch .dict (os .environ , {"REPLICATE_API_TOKEN" : "env-token" }):
117117 with mock .patch .dict (sys .modules , {"cog" : mock_cog }):
118- token = _get_api_token_from_environment ()
118+ token = get_api_token_from_environment ()
119119 assert token == "env-token"
120120
121121 def test_no_env_token_returns_none (self ):
122122 """Test that None is returned when no environment token is set and cog unavailable."""
123123 with mock .patch .dict (os .environ , {}, clear = True ): # Clear all env vars
124124 with mock .patch .dict (sys .modules , {"cog" : None }):
125- token = _get_api_token_from_environment ()
125+ token = get_api_token_from_environment ()
126126 assert token is None
127127
128128 def test_env_token_empty_string (self ):
129129 """Test that empty string from environment is returned."""
130130 with mock .patch .dict (os .environ , {"REPLICATE_API_TOKEN" : "" }):
131131 with mock .patch .dict (sys .modules , {"cog" : None }):
132- token = _get_api_token_from_environment ()
132+ token = get_api_token_from_environment ()
133133 assert token == ""
134134
135135 def test_env_token_valid_string (self ):
136136 """Test that valid token from environment is returned."""
137137 with mock .patch .dict (os .environ , {"REPLICATE_API_TOKEN" : "env-token" }):
138138 with mock .patch .dict (sys .modules , {"cog" : None }):
139- token = _get_api_token_from_environment ()
139+ token = get_api_token_from_environment ()
140140 assert token == "env-token"
141141
142142
0 commit comments