|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from upath import UPath |
| 6 | + |
| 7 | + |
| 8 | +class BaseTests: |
| 9 | + def test_cwd(self): |
| 10 | + with pytest.raises(NotImplementedError): |
| 11 | + self.path.cwd() |
| 12 | + |
| 13 | + def test_home(self): |
| 14 | + with pytest.raises(NotImplementedError): |
| 15 | + self.path.home() |
| 16 | + |
| 17 | + def test_stat(self): |
| 18 | + stat = self.path.stat() |
| 19 | + assert stat |
| 20 | + |
| 21 | + def test_chmod(self): |
| 22 | + with pytest.raises(NotImplementedError): |
| 23 | + self.path.joinpath("file1.txt").chmod(777) |
| 24 | + |
| 25 | + @pytest.mark.parametrize( |
| 26 | + "url, expected", [("file1.txt", True), ("fakefile.txt", False)] |
| 27 | + ) |
| 28 | + def test_exists(self, url, expected): |
| 29 | + print(self.path) |
| 30 | + path = self.path.joinpath(url) |
| 31 | + assert path.exists() == expected |
| 32 | + |
| 33 | + def test_expanduser(self): |
| 34 | + with pytest.raises(NotImplementedError): |
| 35 | + self.path.expanduser() |
| 36 | + |
| 37 | + def test_glob(self, pathlib_base): |
| 38 | + mock_glob = list(self.path.glob("**.txt")) |
| 39 | + path_glob = list(pathlib_base.glob("**/*.txt")) |
| 40 | + |
| 41 | + assert len(mock_glob) == len(path_glob) |
| 42 | + assert all( |
| 43 | + map( |
| 44 | + lambda m: m.path |
| 45 | + in [str(p).replace("\\", "/") for p in path_glob], |
| 46 | + mock_glob, |
| 47 | + ) |
| 48 | + ) |
| 49 | + |
| 50 | + def test_group(self): |
| 51 | + with pytest.raises(NotImplementedError): |
| 52 | + self.path.group() |
| 53 | + |
| 54 | + def test_is_dir(self): |
| 55 | + assert self.path.is_dir() |
| 56 | + |
| 57 | + path = self.path.joinpath("file1.txt") |
| 58 | + assert not path.is_dir() |
| 59 | + |
| 60 | + def test_is_file(self): |
| 61 | + path = self.path.joinpath("file1.txt") |
| 62 | + assert path.is_file() |
| 63 | + assert not self.path.is_file() |
| 64 | + |
| 65 | + def test_is_mount(self): |
| 66 | + with pytest.raises(NotImplementedError): |
| 67 | + self.path.is_mount() |
| 68 | + |
| 69 | + def test_is_symlink(self): |
| 70 | + with pytest.raises(NotImplementedError): |
| 71 | + self.path.is_symlink() |
| 72 | + |
| 73 | + def test_is_socket(self): |
| 74 | + with pytest.raises(NotImplementedError): |
| 75 | + self.path.is_socket() |
| 76 | + |
| 77 | + def test_is_fifo(self): |
| 78 | + with pytest.raises(NotImplementedError): |
| 79 | + self.path.is_fifo() |
| 80 | + |
| 81 | + def test_is_block_device(self): |
| 82 | + with pytest.raises(NotImplementedError): |
| 83 | + self.path.is_block_device() |
| 84 | + |
| 85 | + def test_is_char_device(self): |
| 86 | + with pytest.raises(NotImplementedError): |
| 87 | + self.path.is_char_device() |
| 88 | + |
| 89 | + def test_iterdir(self, local_testdir): |
| 90 | + pl_path = Path(local_testdir) |
| 91 | + |
| 92 | + up_iter = list(self.path.iterdir()) |
| 93 | + pl_iter = list(pl_path.iterdir()) |
| 94 | + |
| 95 | + assert len(up_iter) == len(pl_iter) |
| 96 | + pnames = [p.name for p in pl_iter] |
| 97 | + assert all(map(lambda x: x.name in pnames, up_iter)) |
| 98 | + |
| 99 | + def test_lchmod(self): |
| 100 | + with pytest.raises(NotImplementedError): |
| 101 | + self.path.lchmod(mode=77) |
| 102 | + |
| 103 | + def test_lstat(self): |
| 104 | + with pytest.raises(NotImplementedError): |
| 105 | + self.path.lstat() |
| 106 | + |
| 107 | + def test_mkdir(self): |
| 108 | + new_dir = self.path.joinpath("new_dir") |
| 109 | + new_dir.mkdir() |
| 110 | + assert new_dir.exists() |
| 111 | + |
| 112 | + def test_open(self): |
| 113 | + pass |
| 114 | + |
| 115 | + def test_owner(self): |
| 116 | + with pytest.raises(NotImplementedError): |
| 117 | + self.path.owner() |
| 118 | + |
| 119 | + def test_read_bytes(self, pathlib_base): |
| 120 | + mock = self.path.joinpath("file2.txt") |
| 121 | + pl = pathlib_base.joinpath("file2.txt") |
| 122 | + assert mock.read_bytes() == pl.read_bytes() |
| 123 | + |
| 124 | + def test_read_text(self, local_testdir): |
| 125 | + upath = self.path.joinpath("file1.txt") |
| 126 | + assert ( |
| 127 | + upath.read_text() |
| 128 | + == Path(local_testdir).joinpath("file1.txt").read_text() |
| 129 | + ) |
| 130 | + |
| 131 | + def test_readlink(self): |
| 132 | + with pytest.raises(NotImplementedError): |
| 133 | + self.path.readlink() |
| 134 | + |
| 135 | + @pytest.mark.xfail |
| 136 | + def test_rename(self): |
| 137 | + # need to impliment |
| 138 | + raise False |
| 139 | + |
| 140 | + def test_replace(self): |
| 141 | + pass |
| 142 | + |
| 143 | + def test_resolve(self): |
| 144 | + pass |
| 145 | + |
| 146 | + def test_rglob(self): |
| 147 | + pass |
| 148 | + |
| 149 | + def test_samefile(self): |
| 150 | + pass |
| 151 | + |
| 152 | + def test_symlink_to(self): |
| 153 | + pass |
| 154 | + |
| 155 | + def test_touch_unlink(self): |
| 156 | + path = self.path.joinpath("test_touch.txt") |
| 157 | + path.touch() |
| 158 | + assert path.exists() |
| 159 | + path.unlink() |
| 160 | + assert not path.exists() |
| 161 | + |
| 162 | + # should raise FileNotFoundError since file is missing |
| 163 | + with pytest.raises(FileNotFoundError): |
| 164 | + path.unlink() |
| 165 | + |
| 166 | + # file doesn't exists, but missing_ok is True |
| 167 | + path.unlink(missing_ok=True) |
| 168 | + |
| 169 | + def test_link_to(self): |
| 170 | + pass |
| 171 | + |
| 172 | + def test_write_bytes(self, pathlib_base): |
| 173 | + fn = "test_write_bytes.txt" |
| 174 | + s = b"hello_world" |
| 175 | + path = self.path.joinpath(fn) |
| 176 | + path.write_bytes(s) |
| 177 | + assert path.read_bytes() == s |
| 178 | + |
| 179 | + def test_write_text(self, pathlib_base): |
| 180 | + fn = "test_write_text.txt" |
| 181 | + s = "hello_world" |
| 182 | + path = self.path.joinpath(fn) |
| 183 | + path.write_text(s) |
| 184 | + assert path.read_text() == s |
| 185 | + |
| 186 | + def prepare_file_system(self): |
| 187 | + self.make_top_folder() |
| 188 | + self.make_test_files() |
| 189 | + |
| 190 | + def make_top_folder(self): |
| 191 | + self.path.mkdir(parents=True, exist_ok=True) |
| 192 | + |
| 193 | + def make_test_files(self): |
| 194 | + folder1 = self.path.joinpath("folder1") |
| 195 | + folder1.mkdir(exist_ok=True) |
| 196 | + folder1_files = ["file1.txt", "file2.txt"] |
| 197 | + for f in folder1_files: |
| 198 | + p = folder1.joinpath(f) |
| 199 | + p.touch() |
| 200 | + p.write_text(f) |
| 201 | + |
| 202 | + file1 = self.path.joinpath("file1.txt") |
| 203 | + file1.touch() |
| 204 | + file1.write_text("hello world") |
| 205 | + file2 = self.path.joinpath("file2.txt") |
| 206 | + file2.touch() |
| 207 | + file2.write_bytes(b"hello world") |
| 208 | + |
| 209 | + def test_fsspec_compat(self): |
| 210 | + fs = self.path.fs |
| 211 | + scheme = self.path._url.scheme |
| 212 | + content = b"a,b,c\n1,2,3\n4,5,6" |
| 213 | + |
| 214 | + p1 = f"{scheme}:///tmp/output1.csv" |
| 215 | + upath1 = UPath(p1) |
| 216 | + upath1.write_bytes(content) |
| 217 | + with fs.open(p1) as f: |
| 218 | + assert f.read() == content |
| 219 | + upath1.unlink() |
| 220 | + |
| 221 | + # write with fsspec, read with upath |
| 222 | + p2 = f"{scheme}:///tmp/output2.csv" |
| 223 | + with fs.open(p2, "wb") as f: |
| 224 | + f.write(content) |
| 225 | + upath2 = UPath(p2) |
| 226 | + assert upath2.read_bytes() == content |
| 227 | + upath2.unlink() |
0 commit comments