-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathtest_general.py
More file actions
262 lines (211 loc) · 8.72 KB
/
Copy pathtest_general.py
File metadata and controls
262 lines (211 loc) · 8.72 KB
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
261
262
import textwrap
from importlib import import_module
import pytest
from frictionless import Detector, FrictionlessException, Package, Resource, platform
BASEURL = "https://raw.githubusercontent.com/frictionlessdata/frictionless-py/master/%s"
# General
def test_resource_from_path_error_bad_path():
with pytest.raises(FrictionlessException) as excinfo:
Resource("data/bad.resource.json")
error = excinfo.value.error
assert error.type == "resource-error"
assert error.note.count("bad.resource.json")
@pytest.mark.vcr
def test_resource_from_path_remote_error_bad_path():
with pytest.raises(FrictionlessException) as excinfo:
Resource(BASEURL % "data/bad.resource.json")
error = excinfo.value.error
assert error.type == "resource-error"
assert error.note.count("bad.resource.json")
def test_resource_source_non_tabular():
path = "data/text.txt"
with Resource(path) as resource:
assert resource.path == path
assert resource.data is None
assert resource.type == "text"
assert resource.basepath is None
assert resource.memory is False
assert resource.multipart is False
assert resource.normpath == path
if not platform.type == "windows":
assert resource.read_bytes() == b"text\n"
assert resource.stats.md5 == "e1cbb0c3879af8347246f12c559a86b5"
assert (
resource.stats.sha256
== "b9e68e1bea3e5b19ca6b2f98b73a54b73daafaa250484902e09982e07a12e733"
)
assert resource.stats.bytes == 5
@pytest.mark.vcr
def test_resource_source_non_tabular_remote():
path = BASEURL % "data/text.txt"
with Resource(path) as resource:
assert resource.path == path
assert resource.data is None
assert resource.type == "text"
assert resource.memory is False
assert resource.multipart is False
assert resource.basepath is None
assert resource.normpath == path
if not platform.type == "windows":
assert resource.read_bytes() == b"text\n"
assert resource.stats.md5 == "e1cbb0c3879af8347246f12c559a86b5"
assert (
resource.stats.sha256
== "b9e68e1bea3e5b19ca6b2f98b73a54b73daafaa250484902e09982e07a12e733"
)
assert resource.stats.bytes == 5
def test_resource_source_non_tabular_error_bad_path():
resource = Resource("data/bad.txt")
with pytest.raises(FrictionlessException) as excinfo:
resource.read_bytes()
error = excinfo.value.error
assert error.type == "scheme-error"
assert error.note.count("data/bad.txt")
def test_resource_source_no_path_and_no_data():
with pytest.raises(FrictionlessException) as excinfo:
Resource.from_descriptor({"name": "name"})
error = excinfo.value.error
reasons = excinfo.value.reasons
assert error.type == "resource-error"
assert error.note == "descriptor is not valid"
assert reasons[0].type == "resource-error"
assert reasons[0].note == 'one of the properties "path" or "data" is required'
def test_resource_source_both_path_and_data():
data = [["id", "name"], ["1", "english"], ["2", "中国人"]]
with pytest.raises(FrictionlessException) as excinfo:
Resource({"name": "name", "data": data, "path": "path"})
error = excinfo.value.error
reasons = excinfo.value.reasons
assert error.type == "resource-error"
assert error.note == "descriptor is not valid"
assert reasons[0].type == "resource-error"
assert reasons[0].note == 'properties "path" and "data" is mutually exclusive'
@pytest.mark.parametrize("create_descriptor", [(False,), (True,)])
def test_resource_standard_specs_properties(create_descriptor):
helpers = import_module("frictionless.helpers")
options = dict(
path="path",
name="name",
title="title",
description="description",
profiles=[],
licenses=[],
sources=[],
)
resource = (
Resource(**options) # type: ignore
if not create_descriptor
else Resource(helpers.create_descriptor(**options))
)
assert resource.path == "path"
assert resource.name == "name"
assert resource.title == "title"
assert resource.description == "description"
assert resource.profile is None
assert resource.licenses == []
assert resource.sources == []
def test_resource_official_hash_bytes_rows():
resource = Resource({"name": "name", "path": "path", "hash": "hash", "bytes": 1})
descriptor = resource.to_descriptor()
assert descriptor["hash"] == "hash"
assert descriptor["bytes"] == 1
def test_resource_official_hash_bytes_rows_with_hashing_algorithm():
resource = Resource(
{"name": "name", "path": "path", "hash": "sha256:hash", "bytes": 1}
)
descriptor = resource.to_descriptor()
assert descriptor["hash"] == "sha256:hash"
assert descriptor["bytes"] == 1
def test_resource_description_html():
resource = Resource(description="**test**")
assert resource.description == "**test**"
assert resource.description_html == "<p><strong>test</strong></p>"
def test_resource_description_html_multiline():
resource = Resource(description="**test**\n\nline")
assert resource.description == "**test**\n\nline"
assert resource.description_html == "<p><strong>test</strong></p><p>line</p>"
def test_resource_description_html_not_set():
resource = Resource()
assert resource.description is None
assert resource.description_html == ""
def test_resource_description_text():
resource = Resource(description="**test**\n\nline")
assert resource.description == "**test**\n\nline"
assert resource.description_text == "test line"
def test_resource_description_text_plain():
resource = Resource(description="It's just a plain text. Another sentence")
assert resource.description == "It's just a plain text. Another sentence"
assert resource.description_text == "It's just a plain text. Another sentence"
def test_resource_set_base_path():
resource = Resource(basepath="/data")
assert resource.basepath == "/data"
resource.basepath = "/data/csv"
assert resource.basepath == "/data/csv"
def test_resource_set_detector():
detector_set_init = Detector(field_missing_values=["na"])
resource = Resource("data/table.csv", detector=detector_set_init)
assert resource.detector == detector_set_init
detector_set = Detector(sample_size=3)
resource.detector = detector_set
assert resource.detector == detector_set
def test_resource_set_package():
test_package_1 = Package()
resource = Resource(package=test_package_1)
assert resource.package == test_package_1
test_package_2 = Package()
resource.package = test_package_2
assert resource.package == test_package_2
@pytest.mark.skip
def test_resource_pprint():
resource = Resource(
name="resource",
title="My Resource",
description="My Resource for the Guide",
path="data/table.csv",
)
expected = """
{'name': 'resource',
'title': 'My Resource',
'description': 'My Resource for the Guide',
'path': 'data/table.csv'}
"""
assert repr(resource) == textwrap.dedent(expected).strip()
# Bugs
def test_resource_not_existent_local_file_with_no_format_issue_287():
resource = Resource("bad")
with pytest.raises(FrictionlessException) as excinfo:
resource.open()
error = excinfo.value.error
assert error.type == "scheme-error"
assert error.note.count("[Errno 2]") and error.note.count("bad")
@pytest.mark.vcr
def test_resource_not_existent_remote_file_with_no_format_issue_287():
resource = Resource("http://example.com/bad")
with pytest.raises(FrictionlessException) as excinfo:
resource.open()
error = excinfo.value.error
assert error.type == "scheme-error"
assert error.note == "404 Client Error: Not Found for url: http://example.com/bad"
@pytest.mark.skipif(platform.type == "windows", reason="Fix on Windows")
def test_resource_preserve_format_from_descriptor_on_infer_issue_188():
resource = Resource({"name": "name", "path": "data/table.csvformat", "format": "csv"})
resource.infer(stats=True)
assert resource.to_descriptor() == {
"name": "name",
"path": "data/table.csvformat",
"type": "table",
"format": "csv",
"scheme": "file",
"encoding": "utf-8",
"mediatype": "text/csv",
"hash": "sha256:350e813ea15d84c697a7b03446a8fa9d7fca9883167ad70986a173c29f8253fd",
"bytes": 58,
"fields": 2,
"rows": 3,
"schema": {
"fields": [
{"name": "city", "type": "string"},
{"name": "population", "type": "integer"},
]
},
}