File: test_data.py

package info (click to toggle)
universal-pathlib 0.3.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,656 kB
  • sloc: python: 20,552; makefile: 5
file content (260 lines) | stat: -rw-r--r-- 8,028 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import stat

import pytest

from upath import UnsupportedOperation
from upath import UPath
from upath.implementations.data import DataPath

from ..cases import JoinablePathTests
from ..cases import NonWritablePathTests
from ..cases import ReadablePathTests
from ..utils import OverrideMeta
from ..utils import overrides_base


class TestUPathDataPath(
    JoinablePathTests,
    ReadablePathTests,
    NonWritablePathTests,
    metaclass=OverrideMeta,
):
    """
    Unit-tests for the DataPath implementation of UPath.
    """

    @pytest.fixture(autouse=True)
    def path(self):
        path = "data:text/plain;base64,aGVsbG8gd29ybGQ="
        self.path = UPath(path)

    @pytest.fixture(autouse=True)
    def path_file(self, path):
        self.path_file = self.path

    @overrides_base
    def test_is_correct_class(self):
        assert isinstance(self.path, DataPath)

    @overrides_base
    def test_with_segments(self):
        # DataPath does not support joins, so in all usual cases it'll raise
        with pytest.raises(UnsupportedOperation):
            self.path.with_segments("data:text/plain;base64,", "aGVsbG8K")
        # but you can instantiate with a single full url
        self.path.with_segments("data:text/plain;base64,aGVsbG8K")

    @overrides_base
    def test_parents(self):
        # DataPath is always a absolute path with no parents
        assert self.path.parents == []

    @overrides_base
    def test_with_name(self):
        # DataPath does not support name changes
        with pytest.raises(UnsupportedOperation):
            self.path.with_name("newname")

    @overrides_base
    def test_with_suffix(self):
        # DataPath does not support suffix changes
        with pytest.raises(UnsupportedOperation):
            self.path.with_suffix(".new")

    @overrides_base
    def test_with_stem(self):
        # DataPath does not support stem changes
        with pytest.raises(UnsupportedOperation):
            self.path.with_stem("newname")

    @overrides_base
    def test_suffix(self):
        # DataPath does not have suffixes
        assert self.path.suffix == ""

    @overrides_base
    def test_suffixes(self):
        # DataPath does not have suffixes
        assert self.path.suffixes == []

    @overrides_base
    def test_repr_after_with_name(self):
        with pytest.raises(UnsupportedOperation):
            repr(self.path.with_name("data:,ABC"))

    @overrides_base
    def test_repr_after_with_suffix(self):
        with pytest.raises(UnsupportedOperation):
            repr(self.path.with_suffix(""))

    @overrides_base
    def test_child_path(self):
        # DataPath does not support joins, so child paths are unsupported
        with pytest.raises(UnsupportedOperation):
            super().test_child_path()

    @overrides_base
    def test_pickling_child_path(self):
        # DataPath does not support joins, so child paths are unsupported
        with pytest.raises(UnsupportedOperation):
            super().test_pickling_child_path()

    @overrides_base
    def test_relative_to(self):
        # DataPath only relative_to with itself
        with pytest.raises(ValueError):
            self.path.relative_to("data:,ABC")
        self.path.relative_to(self.path)

    @overrides_base
    def test_is_relative_to(self):
        # DataPath only relative_to with itself
        assert not self.path.is_relative_to("data:,ABC")
        assert self.path.is_relative_to(self.path)

    @overrides_base
    def test_full_match(self):
        assert self.path.full_match("*")
        assert not self.path.full_match("xxx")

    @overrides_base
    def test_trailing_slash_joinpath_is_identical(self):
        # DataPath has no slashes, and is not joinable
        with pytest.raises(UnsupportedOperation):
            super().test_trailing_slash_joinpath_is_identical()

    @overrides_base
    def test_trailing_slash_is_stripped(self):
        # DataPath has no slashes, and is not joinable
        with pytest.raises(UnsupportedOperation):
            super().test_trailing_slash_is_stripped()

    @overrides_base
    def test_parents_end_at_anchor(self):
        # DataPath does not support joins
        with pytest.raises(UnsupportedOperation):
            super().test_parents_end_at_anchor()

    @overrides_base
    def test_anchor_is_its_own_parent(self):
        # DataPath does not support joins
        assert self.path.path == self.path.parent.path

    @overrides_base
    def test_private_url_attr_in_sync(self):
        # DataPath does not support joins, so we check on self.path
        assert self.path._url

    @overrides_base
    def test_stat_dir_st_mode(self):
        # DataPath does not have directories
        assert not stat.S_ISDIR(self.path.stat().st_mode)

    @overrides_base
    def test_exists(self):
        # A valid DataPath always exists
        assert self.path.exists()

    @overrides_base
    def test_glob(self):
        # DataPath does not have dirs, joins or globs
        assert list(self.path.glob("*")) == []

    @overrides_base
    def test_rglob(self):
        # DataPath does not have dirs, joins or globs
        assert list(self.path.rglob("*")) == []

    @overrides_base
    def test_is_dir(self):
        # DataPath does not have directories
        assert not self.path.is_dir()

    @overrides_base
    def test_is_file(self):
        # DataPath is always a file
        assert self.path.is_file()

    @overrides_base
    def test_iterdir(self):
        # DataPath does not have directories
        with pytest.raises(NotADirectoryError):
            self.path.iterdir()

    @overrides_base
    def test_iterdir_parent_iteration(self):
        with pytest.raises(NotADirectoryError):
            super().test_iterdir_parent_iteration()

    @overrides_base
    def test_iterdir2(self):
        # DataPath does not have directories, or joins
        with pytest.raises(NotADirectoryError):
            self.path_file.iterdir()

    @overrides_base
    def test_iterdir_trailing_slash(self):
        # DataPath does not have directories, or joins
        with pytest.raises(UnsupportedOperation):
            super().test_iterdir_trailing_slash()

    @overrides_base
    def test_read_bytes(self):
        assert self.path.read_bytes() == b"hello world"

    @overrides_base
    def test_read_text(self):
        assert self.path.read_text() == "hello world"

    @overrides_base
    def test_walk(self):
        # DataPath does not have directories
        assert list(self.path.walk()) == []

    @overrides_base
    def test_walk_top_down_false(self):
        # DataPath does not have directories
        assert list(self.path.walk(top_down=False)) == []

    @overrides_base
    def test_samefile(self):
        # DataPath doesn't have joins, so only identical paths are samefile
        f1 = UPath("data:text/plain;base64,aGVsbG8gd29ybGQ=")
        f2 = UPath("data:text/plain;base64,SGVsbG8gd29ybGQ=")

        assert f1.samefile(f2) is False
        assert f1.samefile(f2.path) is False
        assert f1.samefile(f1) is True
        assert f1.samefile(f1.path) is True

    @overrides_base
    def test_info(self):
        # DataPath info checks
        p0 = self.path

        assert p0.info.exists() is True
        assert p0.info.is_file() is True
        assert p0.info.is_dir() is False
        assert p0.info.is_symlink() is False

    @overrides_base
    def test_mkdir_raises(self):
        # DataPaths always exist and are files
        with pytest.raises(FileExistsError):
            self.path_file.mkdir()

    @overrides_base
    def test_touch_raises(self):
        # DataPaths always exist, so touch is a noop
        self.path_file.touch()

    @overrides_base
    def test_unlink(self):
        # DataPaths can't be deleted
        with pytest.raises(UnsupportedOperation):
            self.path_file.unlink()

    @overrides_base
    def test_copy_into__dir_to_str_tempdir(self):
        # There are no directories in DataPath
        assert not self.path.is_dir()