|
2 | 2 | # Permissions are hereby granted under the terms of the MIT License:
|
3 | 3 | # https://opensource.org/licenses/MIT.
|
4 | 4 | import unittest
|
| 5 | +from unittest.mock import patch |
| 6 | +from unittest.mock import MagicMock |
5 | 7 |
|
6 | 8 | from fsspec.registry import register_implementation
|
7 | 9 |
|
8 | 10 | from xcube.core.store import DataStoreError
|
9 | 11 | from xcube.core.store import list_data_store_ids
|
10 | 12 | from xcube.core.store import new_data_store
|
| 13 | + |
11 | 14 | import pytest
|
12 | 15 |
|
13 | 16 |
|
@@ -57,6 +60,35 @@ def test_get_data_opener_ids(self):
|
57 | 60 | store.get_data_opener_ids(data_id="test.geotiff", data_type="mldataset"),
|
58 | 61 | )
|
59 | 62 |
|
| 63 | + @patch("fsspec.filesystem") |
| 64 | + def test_has_data(self, mock_filesystem): |
| 65 | + # Mock the HTTPFileSystem instance and its `exists` method |
| 66 | + mock_http_fs = MagicMock() |
| 67 | + mock_filesystem.return_value = mock_http_fs |
| 68 | + mock_http_fs.exists.return_value = True |
| 69 | + mock_http_fs.sep = "/" |
| 70 | + |
| 71 | + store = new_data_store("https", root="test.org") |
| 72 | + |
| 73 | + res = store.has_data(data_id="test.tif") |
| 74 | + self.assertEqual(mock_filesystem.call_count, 1) |
| 75 | + mock_http_fs.exists.assert_called_once_with("https://test.org/test.tif") |
| 76 | + self.assertTrue(res) |
| 77 | + |
| 78 | + res = store.has_data(data_id="test.tif", data_type="dataset") |
| 79 | + mock_http_fs.exists.assert_called_with("https://test.org/test.tif") |
| 80 | + self.assertEqual(mock_http_fs.exists.call_count, 2) |
| 81 | + self.assertTrue(res) |
| 82 | + |
| 83 | + res = store.has_data(data_id="test.tif", data_type="mldataset") |
| 84 | + mock_http_fs.exists.assert_called_with("https://test.org/test.tif") |
| 85 | + self.assertEqual(mock_http_fs.exists.call_count, 3) |
| 86 | + self.assertTrue(res) |
| 87 | + |
| 88 | + res = store.has_data(data_id="test.tif", data_type="geodataframe") |
| 89 | + self.assertEqual(mock_http_fs.exists.call_count, 3) |
| 90 | + self.assertFalse(res) |
| 91 | + |
60 | 92 |
|
61 | 93 | def test_fsspec_instantiation_error():
|
62 | 94 | error_string = "deliberate instantiation error for testing"
|
|
0 commit comments