Skip to content

Commit 4f878ef

Browse files
committed
refactor: standardize test error handling by injecting err_400 and err_404 fixtures
1 parent 36401d7 commit 4f878ef

24 files changed

Lines changed: 124 additions & 107 deletions

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,6 @@ testpaths = [
8585
addopts = [
8686
"--import-mode=importlib",
8787
]
88+
markers = [
89+
"slow: marks tests as slow (deselect with '-m \"not slow\"')",
90+
]

tests/archive_file/test_archivefile_device.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ def params_multiple_pages(params):
2020
return params | {"rowLimit": 2}
2121

2222

23-
def test_invalid_param_value(requester, params):
23+
def test_invalid_param_value(requester, params, err_400):
2424
params_invalid_param_value = params | {"deviceCode": "XYZ123"}
25-
with pytest.raises(requests.HTTPError, match=r"API Error 127"):
25+
with pytest.raises(requests.HTTPError, match=err_400):
2626
requester.getArchivefile(params_invalid_param_value)
2727

2828

@@ -32,9 +32,9 @@ def test_invalid_params_missing_required(requester, params):
3232
requester.getArchivefile(params)
3333

3434

35-
def test_invalid_param_name(requester, params):
35+
def test_invalid_param_name(requester, params, err_400):
3636
params_invalid_param_name = params | {"deviceCodes": "BPR-Folger-59"}
37-
with pytest.raises(requests.HTTPError, match=r"API Error 129"):
37+
with pytest.raises(requests.HTTPError, match=err_400):
3838
requester.getArchivefile(params_invalid_param_name)
3939

4040

tests/archive_file/test_archivefile_download.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
import requests
55

66

7-
def test_invalid_param_value(requester):
8-
with pytest.raises(requests.HTTPError, match=r"API Error 96"):
7+
def test_invalid_param_value(requester, err_400):
8+
with pytest.raises(requests.HTTPError, match=err_400):
99
requester.downloadArchivefile("FAKEFILE.XYZ")
1010

1111

12-
def test_invalid_params_missing_required(requester):
13-
with pytest.raises(requests.HTTPError, match=r"API Error 128"):
12+
def test_invalid_params_missing_required(requester, err_400):
13+
with pytest.raises(requests.HTTPError, match=err_400):
1414
requester.downloadArchivefile()
1515

1616

tests/archive_file/test_archivefile_location.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import requests
33

44

5-
def test_invalid_param_value(requester, params_location):
5+
def test_invalid_param_value(requester, params_location, err_400):
66
params_invalid_param_value = params_location | {"locationCode": "XYZ123"}
7-
with pytest.raises(requests.HTTPError, match=r"API Error 127"):
7+
with pytest.raises(requests.HTTPError, match=err_400):
88
requester.getArchivefile(params_invalid_param_value)
99

1010

@@ -14,18 +14,18 @@ def test_invalid_params_missing_required(requester, params_location):
1414
requester.getArchivefile(params_location)
1515

1616

17-
def test_invalid_param_name(requester, params_location):
17+
def test_invalid_param_name(requester, params_location, err_400):
1818
params_invalid_param_name = params_location | {"locationCodes": "NCBC"}
19-
with pytest.raises(requests.HTTPError, match=r"API Error 129"):
19+
with pytest.raises(requests.HTTPError, match=err_400):
2020
requester.getArchivefile(params_invalid_param_name)
2121

2222

23-
def test_no_data(requester, params_location):
23+
def test_no_data(requester, params_location, err_400):
2424
params_no_data = params_location | {
2525
"dateFrom": "2000-01-01",
2626
"dateTo": "2000-01-02",
2727
}
28-
with pytest.raises(requests.HTTPError, match=r"API Error 127"):
28+
with pytest.raises(requests.HTTPError, match=err_400):
2929
requester.getArchivefile(params_no_data)
3030

3131

tests/conftest.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
from pathlib import Path
3-
3+
import requests
44
import pytest
55
from dotenv import load_dotenv
66
from onc import ONC
@@ -18,6 +18,15 @@ def pytest_configure():
1818
def requester(tmp_path) -> ONC:
1919
return ONC(production=is_prod, outPath=tmp_path)
2020

21+
@pytest.fixture
22+
def err_400():
23+
return f"{requests.codes.bad} Client Error"
24+
25+
26+
@pytest.fixture
27+
def err_404():
28+
return f"{requests.codes.not_found} Client Error"
29+
2130

2231
@pytest.fixture(scope="session")
2332
def util():

tests/data_product_delivery/test_data_product_delivery_cancel.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
import requests
33

44

5-
def test_invalid_request_id(requester):
6-
with pytest.raises(requests.HTTPError, match=r"API Error 127"):
5+
def test_invalid_request_id(requester, err_400):
6+
with pytest.raises(requests.HTTPError, match=err_400):
77
requester.cancelDataProduct(1234567890)

tests/data_product_delivery/test_data_product_delivery_download.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
import requests
33

44

5-
def test_invalid_run_id(requester):
6-
with pytest.raises(requests.HTTPError, match=r"API Error 127"):
5+
def test_invalid_run_id(requester, err_400):
6+
with pytest.raises(requests.HTTPError, match=err_400):
77
requester.downloadDataProduct(1234567890)

tests/data_product_delivery/test_data_product_delivery_order.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
import requests
33

44

5-
def test_invalid_param_value(requester, params):
5+
def test_invalid_param_value(requester, params, err_400):
66
params_invalid_param_value = params | {"dataProductCode": "XYZ123"}
7-
with pytest.raises(requests.HTTPError, match=r"API Error 127"):
7+
with pytest.raises(requests.HTTPError, match=err_400):
88
requester.orderDataProduct(params_invalid_param_value)
99

1010

11+
@pytest.mark.slow
1112
def test_valid_default(requester, params, expected_keys_download_results, util):
1213
data = requester.orderDataProduct(params)
1314

@@ -30,6 +31,7 @@ def test_valid_default(requester, params, expected_keys_download_results, util):
3031
)
3132

3233

34+
@pytest.mark.slow
3335
def test_valid_no_metadata(requester, params, expected_keys_download_results, util):
3436
data = requester.orderDataProduct(params, includeMetadataFile=False)
3537

@@ -49,6 +51,7 @@ def test_valid_no_metadata(requester, params, expected_keys_download_results, ut
4951
)
5052

5153

54+
@pytest.mark.slow
5255
def test_valid_results_only(requester, params, expected_keys_download_results, util):
5356
data = requester.orderDataProduct(params, downloadResultsOnly=True)
5457

tests/data_product_delivery/test_data_product_delivery_restart.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
import requests
33

44

5-
def test_invalid_request_id(requester):
6-
with pytest.raises(requests.HTTPError, match=r"API Error 127"):
5+
def test_invalid_request_id(requester, err_400):
6+
with pytest.raises(requests.HTTPError, match=err_400):
77
requester.restartDataProduct(1234567890)

tests/data_product_delivery/test_data_product_delivery_run.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
import requests
33

44

5-
def test_invalid_request_id(requester):
6-
with pytest.raises(requests.HTTPError, match=r"API Error 127"):
5+
def test_invalid_request_id(requester, err_400):
6+
with pytest.raises(requests.HTTPError, match=err_400):
77
requester.runDataProduct(1234567890)

0 commit comments

Comments
 (0)