|
1 | | -import sys |
2 | 1 | import pathlib |
| 2 | +import pickle |
| 3 | +import sys |
3 | 4 | import warnings |
4 | 5 |
|
5 | 6 | import pytest |
6 | | - |
7 | 7 | from upath import UPath |
8 | 8 | from upath.implementations.s3 import S3Path |
9 | 9 | from upath.tests.cases import BaseTests |
@@ -38,7 +38,12 @@ class TestUpath(BaseTests): |
38 | 38 | def path(self, local_testdir): |
39 | 39 | with warnings.catch_warnings(): |
40 | 40 | warnings.simplefilter("ignore") |
41 | | - self.path = UPath(f"mock:{local_testdir}") |
| 41 | + |
| 42 | + # On Windows the path needs to be prefixed with `/`, becaue |
| 43 | + # `UPath` implements `_posix_flavour`, which requires a `/` root |
| 44 | + # in order to correctly deserialize pickled objects |
| 45 | + root = "/" if sys.platform.startswith("win") else "" |
| 46 | + self.path = UPath(f"mock:{root}{local_testdir}") |
42 | 47 |
|
43 | 48 | def test_fsspec_compat(self): |
44 | 49 | pass |
@@ -137,3 +142,37 @@ def test_create_from_type(path, storage_options, module, object_type): |
137 | 142 | except (ImportError, ModuleNotFoundError): |
138 | 143 | # fs failed to import |
139 | 144 | pass |
| 145 | + |
| 146 | + |
| 147 | +def test_child_path(): |
| 148 | + path_a = UPath("gcs://bucket/folder") |
| 149 | + path_b = UPath("gcs://bucket") / "folder" |
| 150 | + |
| 151 | + assert str(path_a) == str(path_b) |
| 152 | + assert path_a._root == path_b._root |
| 153 | + assert path_a._drv == path_b._drv |
| 154 | + assert path_a._parts == path_b._parts |
| 155 | + assert path_a._url == path_b._url |
| 156 | + |
| 157 | + |
| 158 | +def test_pickling(): |
| 159 | + path = UPath("gcs://bucket/folder", storage_options={"anon": True}) |
| 160 | + pickled_path = pickle.dumps(path) |
| 161 | + recovered_path = pickle.loads(pickled_path) |
| 162 | + |
| 163 | + assert type(path) == type(recovered_path) |
| 164 | + assert str(path) == str(recovered_path) |
| 165 | + assert path.fs.storage_options == recovered_path.fs.storage_options |
| 166 | + |
| 167 | + |
| 168 | +def test_pickling_child_path(): |
| 169 | + path = UPath("gcs://bucket", anon=True) / "subfolder" / "subsubfolder" |
| 170 | + pickled_path = pickle.dumps(path) |
| 171 | + recovered_path = pickle.loads(pickled_path) |
| 172 | + |
| 173 | + assert type(path) == type(recovered_path) |
| 174 | + assert str(path) == str(recovered_path) |
| 175 | + assert path._drv == recovered_path._drv |
| 176 | + assert path._root == recovered_path._root |
| 177 | + assert path._parts == recovered_path._parts |
| 178 | + assert path.fs.storage_options == recovered_path.fs.storage_options |
0 commit comments