|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import replicate |
| 4 | +from replicate.lib._files import FileOutput, AsyncFileOutput |
| 5 | +from replicate.lib._predictions_use import URLPath, get_path_url |
| 6 | + |
| 7 | +# Test token for client instantiation |
| 8 | +TEST_TOKEN = "test-bearer-token" |
| 9 | + |
| 10 | + |
| 11 | +def test_get_path_url_with_urlpath(): |
| 12 | + """Test get_path_url returns the URL for URLPath instances.""" |
| 13 | + url = "https://example.com/test.jpg" |
| 14 | + path_proxy = URLPath(url) |
| 15 | + |
| 16 | + result = get_path_url(path_proxy) |
| 17 | + assert result == url |
| 18 | + |
| 19 | + |
| 20 | +def test_get_path_url_with_fileoutput(): |
| 21 | + """Test get_path_url returns the URL for FileOutput instances.""" |
| 22 | + url = "https://example.com/test.jpg" |
| 23 | + file_output = FileOutput(url, replicate.Replicate(bearer_token=TEST_TOKEN)) |
| 24 | + |
| 25 | + result = get_path_url(file_output) |
| 26 | + assert result == url |
| 27 | + |
| 28 | + |
| 29 | +def test_get_path_url_with_async_fileoutput(): |
| 30 | + """Test get_path_url returns the URL for AsyncFileOutput instances.""" |
| 31 | + url = "https://example.com/test.jpg" |
| 32 | + async_file_output = AsyncFileOutput(url, replicate.AsyncReplicate(bearer_token=TEST_TOKEN)) |
| 33 | + |
| 34 | + result = get_path_url(async_file_output) |
| 35 | + assert result == url |
| 36 | + |
| 37 | + |
| 38 | +def test_get_path_url_with_regular_path(): |
| 39 | + """Test get_path_url returns None for regular Path instances.""" |
| 40 | + regular_path = Path("test.txt") |
| 41 | + |
| 42 | + result = get_path_url(regular_path) |
| 43 | + assert result is None |
| 44 | + |
| 45 | + |
| 46 | +def test_get_path_url_with_object_without_target(): |
| 47 | + """Test get_path_url returns None for objects without __url__.""" |
| 48 | + |
| 49 | + # Test with a string |
| 50 | + result = get_path_url("not a path") |
| 51 | + assert result is None |
| 52 | + |
| 53 | + # Test with a dict |
| 54 | + result = get_path_url({"key": "value"}) |
| 55 | + assert result is None |
| 56 | + |
| 57 | + # Test with None |
| 58 | + result = get_path_url(None) |
| 59 | + assert result is None |
| 60 | + |
| 61 | + |
| 62 | +def test_get_path_url_module_level_import(): |
| 63 | + """Test that get_path_url can be imported at module level.""" |
| 64 | + from replicate import get_path_url as module_get_path_url |
| 65 | + |
| 66 | + url = "https://example.com/test.jpg" |
| 67 | + file_output = FileOutput(url, replicate.Replicate(bearer_token=TEST_TOKEN)) |
| 68 | + |
| 69 | + result = module_get_path_url(file_output) |
| 70 | + assert result == url |
| 71 | + |
| 72 | + |
| 73 | +def test_get_path_url_direct_module_access(): |
| 74 | + """Test that get_path_url can be accessed directly from replicate module.""" |
| 75 | + url = "https://example.com/test.jpg" |
| 76 | + file_output = FileOutput(url, replicate.Replicate(bearer_token=TEST_TOKEN)) |
| 77 | + |
| 78 | + result = replicate.get_path_url(file_output) |
| 79 | + assert result == url |
| 80 | + |
| 81 | + |
| 82 | +def test_fileoutput_has_url_attribute(): |
| 83 | + """Test that FileOutput instances have __url__ attribute.""" |
| 84 | + url = "https://example.com/test.jpg" |
| 85 | + file_output = FileOutput(url, replicate.Replicate(bearer_token=TEST_TOKEN)) |
| 86 | + |
| 87 | + assert hasattr(file_output, "__url__") |
| 88 | + assert file_output.__url__ == url |
| 89 | + |
| 90 | + |
| 91 | +def test_async_fileoutput_has_url_attribute(): |
| 92 | + """Test that AsyncFileOutput instances have __url__ attribute.""" |
| 93 | + url = "https://example.com/test.jpg" |
| 94 | + async_file_output = AsyncFileOutput(url, replicate.AsyncReplicate(bearer_token=TEST_TOKEN)) |
| 95 | + |
| 96 | + assert hasattr(async_file_output, "__url__") |
| 97 | + assert async_file_output.__url__ == url |
| 98 | + |
| 99 | + |
| 100 | +def test_urlpath_has_url_attribute(): |
| 101 | + """Test that URLPath instances have __url__ attribute.""" |
| 102 | + url = "https://example.com/test.jpg" |
| 103 | + url_path = URLPath(url) |
| 104 | + |
| 105 | + assert hasattr(url_path, "__url__") |
| 106 | + assert url_path.__url__ == url |
0 commit comments