Skip to content

Commit f919b2c

Browse files
authored
Merge pull request #30 from FAIRDataPipeline/feature/error-handling
Feature/error handling
2 parents 5060a11 + 126960d commit f919b2c

File tree

8 files changed

+161
-18
lines changed

8 files changed

+161
-18
lines changed

codecov.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ignore:
2+
- "*/tests/*"

fairdatapipeline/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"raise_issue_by_data_product",
77
"raise_issue_by_index",
88
"raise_issue_with_config",
9+
"raise_issue_by_type",
910
"raise_issue_by_existing_data_product",
1011
"raise_issue_with_submission_script",
1112
"raise_issue_with_github_repo",
@@ -19,6 +20,7 @@
1920
raise_issue_by_data_product,
2021
raise_issue_by_existing_data_product,
2122
raise_issue_by_index,
23+
raise_issue_by_type,
2224
raise_issue_with_config,
2325
raise_issue_with_github_repo,
2426
raise_issue_with_submission_script,

fairdatapipeline/fdp_utils.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ def get_entry(
6060
+ " Query = "
6161
+ url
6262
)
63-
6463
return response.json()["results"]
6564

6665

@@ -105,7 +104,11 @@ def extract_id(url: str) -> str:
105104
Returns:
106105
| str: id derrived from the url
107106
"""
108-
return list(filter(None, urlsplit(url).path.split("/")))[-1]
107+
108+
split_url_path = urlsplit(url).path.split("/")
109+
if not split_url_path:
110+
raise IndexError(f"Unable to extract ID from registry URL: {url}")
111+
return [s for s in split_url_path if s != ""][-1]
109112

110113

111114
def post_entry(
@@ -132,8 +135,6 @@ def post_entry(
132135

133136
response = requests.post(_url, _data, headers=headers)
134137

135-
# print(response.request.headers)
136-
137138
if response.status_code == 409:
138139
logging.info("Entry Exists: Attempting to return Existing Entry")
139140
existing_entry = get_entry(url, endpoint, data)

fairdatapipeline/pipeline.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,18 @@ def initialise(token: str, config: str, script: str) -> dict:
9292
config_filetype_url = config_filetype_response["url"]
9393

9494
# Get user for registry admin account
95-
user = fdp_utils.get_entry(
95+
results = fdp_utils.get_entry(
9696
url=registry_url,
9797
endpoint="users",
9898
query={"username": "admin"},
9999
token=token,
100100
api_version=api_version,
101-
)[0]
101+
)
102102

103+
if not results:
104+
raise IndexError(f"list {results} empty")
105+
else:
106+
user = results[0]
103107
# Check users exists
104108
if not user:
105109
raise ValueError(
@@ -109,15 +113,17 @@ def initialise(token: str, config: str, script: str) -> dict:
109113

110114
user_url = user["url"]
111115
user_id = fdp_utils.extract_id(user_url)
112-
113116
# Get author(s)
114-
author = fdp_utils.get_entry(
117+
results = fdp_utils.get_entry(
115118
url=registry_url,
116119
endpoint="user_author",
117120
query={"user": user_id},
118121
api_version=api_version,
119-
)[0]
120-
122+
)
123+
if not results:
124+
raise IndexError(f"list {results} empty")
125+
else:
126+
author = results[0]
121127
# Check user author exists
122128
if not author:
123129
raise ValueError(
@@ -334,7 +340,6 @@ def finalise(token: str, handle: dict) -> None:
334340
api_version = handle["yaml"]["run_metadata"]["api_version"]
335341

336342
datastore = fdp_utils.remove_local_from_root(datastore)
337-
338343
datastore_root = fdp_utils.get_entry(
339344
url=registry_url,
340345
endpoint="storage_root",

fairdatapipeline/raise_issue.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def raise_issue_by_type(
148148
# flake8: noqa C901
149149
def raise_issue(
150150
handle: dict,
151-
type: str,
151+
issue_type: str,
152152
issue: str,
153153
severity: int,
154154
index: bool = None,
@@ -159,7 +159,7 @@ def raise_issue(
159159
group: bool = True,
160160
) -> None:
161161
current_group = issue + ":" + str(severity)
162-
if type in [
162+
if issue_type in [
163163
"config",
164164
"submission_script",
165165
"github_repo",
@@ -233,7 +233,7 @@ def raise_issue(
233233
# Write to handle and return path
234234
issues_dict = {
235235
"index": index,
236-
"type": type,
236+
"type": issue_type,
237237
"use_data_product": data_product,
238238
"use_component": component,
239239
"version": version,

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ line_length = 79
7777
line-length = 79
7878

7979
[tool.pytest.ini_options]
80-
addopts = '-s -v'
80+
addopts = '-s -v --cov=fairdatapipeline --cov-report=html --cov-report=term'
8181
markers = [
8282
"pipeline: tests for 'pipeline' module ",
8383
"issue: tests for raising issues ",

tests/test_fdp_utils.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ def write_csv_path(test_dir: str) -> str:
2626

2727

2828
# Test is_file()
29+
@pytest.mark.utilities
2930
def test_is_file_exists(test_dir: str) -> None:
3031
test_file = os.path.join(test_dir, "test.csv")
3132
assert fdp_utils.is_file(test_file)
3233

3334

35+
@pytest.mark.utilities
3436
@pytest.mark.parametrize(
3537
"file_path",
3638
[
@@ -43,12 +45,14 @@ def test_is_file_not_exists(file_path: str) -> None:
4345
assert not fdp_utils.is_file(file_path)
4446

4547

48+
@pytest.mark.utilities
4649
@pytest.mark.parametrize("file_path", ["read_csv_path", "write_csv_path"])
4750
def test_is_yaml(file_path: str, request: FixtureRequest) -> None:
4851
file_path = request.getfixturevalue(file_path)
4952
assert fdp_utils.is_yaml(file_path)
5053

5154

55+
@pytest.mark.utilities
5256
@pytest.mark.parametrize(
5357
"file_path",
5458
[
@@ -62,12 +66,14 @@ def test_is_yaml_not(file_path: str) -> None:
6266
assert not fdp_utils.is_yaml(file_path)
6367

6468

69+
@pytest.mark.utilities
6570
@pytest.mark.parametrize("file_path", ["read_csv_path", "write_csv_path"])
6671
def test_is_valid_yaml(file_path: str, request: FixtureRequest) -> None:
6772
file_path = request.getfixturevalue(file_path)
6873
assert fdp_utils.is_yaml(file_path)
6974

7075

76+
@pytest.mark.utilities
7177
@pytest.mark.parametrize(
7278
"file_path",
7379
[
@@ -81,6 +87,7 @@ def test_is_valid_yaml_not(file_path: str) -> None:
8187
assert not fdp_utils.is_valid_yaml(file_path)
8288

8389

90+
@pytest.mark.utilities
8491
def test_read_token(test_dir: str) -> None:
8592
token = os.path.join(test_dir, "test_token")
8693
assert (
@@ -89,6 +96,7 @@ def test_read_token(test_dir: str) -> None:
8996
)
9097

9198

99+
@pytest.mark.utilities
92100
def test_get_token(test_dir: str) -> None:
93101
token = os.path.join(test_dir, "test_token")
94102
assert (
@@ -97,6 +105,7 @@ def test_get_token(test_dir: str) -> None:
97105
)
98106

99107

108+
@pytest.mark.utilities
100109
def test_read_token_get_token(test_dir: str) -> None:
101110
token = os.path.join(test_dir, "test_token")
102111
assert fdp_utils.read_token(token) == fdp_utils.get_token(token)
@@ -109,6 +118,7 @@ def token() -> str:
109118
)
110119

111120

121+
@pytest.mark.utilities
112122
def test_get_file_hash(test_dir: str) -> None:
113123
file_path = os.path.join(test_dir, "test.csv")
114124
if platform.system() == "Windows":
@@ -123,32 +133,47 @@ def test_get_file_hash(test_dir: str) -> None:
123133
)
124134

125135

136+
@pytest.mark.utilities
126137
def test_random_hash_is_string() -> None:
127138
assert type(fdp_utils.random_hash()) == str
128139

129140

141+
@pytest.mark.utilities
130142
def test_random_hash_length() -> None:
131143
assert len(fdp_utils.random_hash()) == 40
132144

133145

146+
@pytest.mark.utilities
134147
def test_extract_id() -> None:
135148
assert fdp_utils.extract_id("http://localhost:8000/api/object/85") == "85"
136149

137150

151+
@pytest.mark.utilities
152+
def test_extract_id_should_fail() -> None:
153+
with pytest.raises(IndexError):
154+
fdp_utils.extract_id("")
155+
156+
157+
@pytest.mark.utilities
138158
def test_get_headers() -> None:
139159
assert type(fdp_utils.get_headers()) == dict
160+
headers = {"Accept": "application/json; version=" + "1.0.0"}
161+
assert headers == fdp_utils.get_headers()
140162

141163

164+
@pytest.mark.utilities
142165
def test_get_headers_with_token(token: str) -> None:
143166
headers = fdp_utils.get_headers(token=token)
144167
assert headers["Authorization"] == "token " + token
145168

146169

170+
@pytest.mark.utilities
147171
def test_get_headers_post() -> None:
148172
headers = fdp_utils.get_headers(request_type="post")
149173
assert headers["Content-Type"] == "application/json"
150174

151175

176+
@pytest.mark.utilities
152177
def test_get_headers_api_version() -> None:
153178
headers = fdp_utils.get_headers(api_version="0.0.1")
154179
assert headers["Accept"] == "application/json; version=0.0.1"
@@ -232,6 +257,32 @@ def test_get_entry(url: str, token: str, storage_root_test: dict) -> None:
232257
assert entry[0] == storage_root_test
233258

234259

260+
@pytest.mark.utilities
261+
def test_get_entry_author(url: str, token: str) -> None:
262+
263+
results = fdp_utils.get_entry(
264+
url=url,
265+
query={"user": 2},
266+
token=token,
267+
endpoint="user_author",
268+
)
269+
with pytest.raises(IndexError):
270+
_ = results[0]
271+
272+
273+
@pytest.mark.utilities
274+
def test_get_entry_users(url: str, token: str) -> None:
275+
276+
results = fdp_utils.get_entry(
277+
url=url,
278+
query={"username": "admin1"},
279+
token=token,
280+
endpoint="users",
281+
)
282+
with pytest.raises(IndexError):
283+
_ = results[0]
284+
285+
235286
@pytest.mark.utilities
236287
def test_get_entity(url: str, storage_root_test: dict) -> None:
237288
entity = fdp_utils.get_entity(

0 commit comments

Comments
 (0)