From d95fa771f5c9a75dece2038ee50619a53f9c43f7 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Sat, 29 Dec 2018 00:07:14 -0800 Subject: check request status codes idiomatically --- python/fatcat_tools/harvest/doi_registrars.py | 4 +-- python/fatcat_tools/workers/elasticsearch.py | 2 +- python/fatcat_web/search.py | 1 + python/tests/routes.py | 40 +++++++++++++-------------- python/tests/subentity_state.py | 38 +++++++++++++------------ 5 files changed, 44 insertions(+), 41 deletions(-) (limited to 'python') diff --git a/python/fatcat_tools/harvest/doi_registrars.py b/python/fatcat_tools/harvest/doi_registrars.py index d27389ba..4a0cb8db 100644 --- a/python/fatcat_tools/harvest/doi_registrars.py +++ b/python/fatcat_tools/harvest/doi_registrars.py @@ -95,11 +95,11 @@ class HarvestCrossrefWorker: while True: http_resp = requests.get(self.api_host_url, params, headers=headers) if http_resp.status_code == 503: - # crud backoff + # crude backoff print("got HTTP {}, pausing for 30 seconds".format(http_resp.status_code)) time.sleep(30.0) continue - assert http_resp.status_code == 200 + http_resp.raise_for_status() resp = http_resp.json() items = self.extract_items(resp) count += len(items) diff --git a/python/fatcat_tools/workers/elasticsearch.py b/python/fatcat_tools/workers/elasticsearch.py index e7abd5ee..3adb088e 100644 --- a/python/fatcat_tools/workers/elasticsearch.py +++ b/python/fatcat_tools/workers/elasticsearch.py @@ -48,5 +48,5 @@ class ElasticsearchReleaseWorker(FatcatWorker): release.ident) print("Updating document: {}".format(elasticsearch_endpoint)) resp = requests.post(elasticsearch_endpoint, json=release_to_elasticsearch(release)) - assert resp.status_code in (200, 201) + resp.raise_for_status() #consumer.commit_offsets() diff --git a/python/fatcat_web/search.py b/python/fatcat_web/search.py index 471e522a..5cebfde4 100644 --- a/python/fatcat_web/search.py +++ b/python/fatcat_web/search.py @@ -36,6 +36,7 @@ def do_search(q, limit=50, fulltext_only=True): (app.config['ELASTICSEARCH_BACKEND'], app.config['ELASTICSEARCH_INDEX']), json=search_request) + #rv.raise_for_status() if resp.status_code != 200: print("elasticsearch non-200 status code: " + str(resp.status_code)) print(resp.content) diff --git a/python/tests/routes.py b/python/tests/routes.py index bca7cc0f..7b4ac442 100644 --- a/python/tests/routes.py +++ b/python/tests/routes.py @@ -9,7 +9,7 @@ from fixtures import * def test_static_routes(app): for route in ('/health', '/robots.txt', '/', '/about'): rv = app.get(route) - assert rv.status_code == 200 + rv.raise_for_status() assert app.get("/static/bogus/route").status_code == 404 @@ -27,16 +27,16 @@ def test_all_views(app): assert rv.status_code == 404 rv = app.get('/container/aaaaaaaaaaaaaeiraaaaaaaaai') - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/container/aaaaaaaaaaaaaeiraaaaaaaaai/history') - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/container/aaaaaaaaaaaaaeiraaaaaaaaai/edit') - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/container/create') - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/container/lookup') assert rv.status_code == 400 @@ -48,34 +48,34 @@ def test_all_views(app): assert rv.status_code == 302 rv = app.get('/creator/aaaaaaaaaaaaaircaaaaaaaaai') - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/creator/aaaaaaaaaaaaaircaaaaaaaaai/history') - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/creator/aaaaaaaaaaaaaircaaaaaaaaai/edit') - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/creator/lookup?orcid=0000-0003-2088-7465') assert rv.status_code == 302 rv = app.get('/file/aaaaaaaaaaaaamztaaaaaaaaai') - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/file/lookup?sha1=7d97e98f8af710c7e7fe703abc8f639e0ee507c4') assert rv.status_code == 302 rv = app.get('/release/aaaaaaaaaaaaarceaaaaaaaaai') - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/release/aaaaaaaaaaaaarceaaaaaaaaai/history') - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/release/aaaaaaaaaaaaarceaaaaaaaaai/edit') - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/release/create') - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/release/lookup?doi=10.123/abc') assert rv.status_code == 302 @@ -84,22 +84,22 @@ def test_all_views(app): assert rv.status_code == 302 rv = app.get('/release/search') - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/work/aaaaaaaaaaaaavkvaaaaaaaaai') - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/work/aaaaaaaaaaaaavkvaaaaaaaaai/history') - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/work/aaaaaaaaaaaaavkvaaaaaaaaai/edit') - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/work/create') assert rv.status_code == 404 rv = app.get('/editgroup/aaaaaaaaaaaabo53aaaaaaaaae') - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/editgroup/ccccccccccccccccccccccccca') print(rv) @@ -110,7 +110,7 @@ def test_all_views(app): #assert rv.status_code == 302 rv = app.get('/editor/aaaaaaaaaaaabkvkaaaaaaaaae') - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/editor/aaaaaaaaaaaabkvkaaaaaaaaae/changelog') - assert rv.status_code == 200 + rv.raise_for_status() diff --git a/python/tests/subentity_state.py b/python/tests/subentity_state.py index 03362686..799f5e03 100644 --- a/python/tests/subentity_state.py +++ b/python/tests/subentity_state.py @@ -36,7 +36,7 @@ def test_relation_states(api, app): eg = quick_eg(api) j2 = api.get_container(api.create_container(j2, editgroup_id=eg.editgroup_id).ident) rv = app.get('/container/{}'.format(j2.ident)) - assert rv.status_code == 200 + rv.raise_for_status() # create inter-related entities eg = quick_eg(api) @@ -57,14 +57,15 @@ def test_relation_states(api, app): assert r1.contribs[0].creator.display_name == "test person" assert r1.contribs[0].creator.state == "active" rv = app.get('/release/{}'.format(r1.ident)) - assert rv.status_code == 200 + rv.raise_for_status() # delete creator eg = quick_eg(api) api.delete_creator(c1.ident, editgroup_id=eg.editgroup_id) api.accept_editgroup(eg.editgroup_id) rv = app.get('/creator/{}'.format(c1.ident)) - assert rv.status_code == 200 # TODO: HTTP status "Gone"? + # TODO: HTTP status "Gone"? + rv.raise_for_status() c1_deleted = api.get_creator(c1.ident) assert c1_deleted.state == "deleted" @@ -77,7 +78,7 @@ def test_relation_states(api, app): assert r1.contribs[0].creator.display_name is None assert r1.contribs[0].creator.state == "deleted" rv = app.get('/release/{}'.format(r1.ident)) - assert rv.status_code == 200 + rv.raise_for_status() # wip container eg = quick_eg(api) @@ -94,7 +95,7 @@ def test_relation_states(api, app): assert r1.contribs[0].creator.display_name is None assert r1.contribs[0].creator.state == "deleted" rv = app.get('/release/{}'.format(r1.ident)) - assert rv.status_code == 200 + rv.raise_for_status() # redirect release r2 = api.get_release(r2.ident, expand="container,creators,files") @@ -116,7 +117,7 @@ def test_relation_states(api, app): rv = app.get('/release/{}'.format(r2.ident)) assert rv.status_code == 302 rv = app.get('/file/{}'.format(f2.ident)) - assert rv.status_code == 200 + rv.raise_for_status() # delete release eg = quick_eg(api) @@ -128,10 +129,11 @@ def test_relation_states(api, app): assert r2.files is None assert r2.contribs is None rv = app.get('/release/{}'.format(r2.ident)) - assert rv.status_code == 200 # TODO: HTTP Gone? + # TODO: HTTP Gone? + rv.raise_for_status() rv = app.get('/file/{}'.format(f2.ident)) print(rv.data) - assert rv.status_code == 200 + rv.raise_for_status() def test_app_entity_states(api, app): @@ -171,23 +173,23 @@ def test_app_entity_states(api, app): # all entities rv = app.get('/container/{}'.format(j1.ident)) - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/container/{}'.format(j2.ident)) assert rv.status_code == 302 rv = app.get('/creator/{}'.format(c1.ident)) - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/creator/{}'.format(c2.ident)) assert rv.status_code == 302 rv = app.get('/file/{}'.format(f1.ident)) - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/file/{}'.format(f2.ident)) assert rv.status_code == 302 rv = app.get('/release/{}'.format(r1.ident)) - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/release/{}'.format(r2.ident)) assert rv.status_code == 302 rv = app.get('/work/{}'.format(r1.work_id)) - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/work/{}'.format(r2.work_id)) assert rv.status_code == 302 @@ -202,23 +204,23 @@ def test_app_entity_states(api, app): # all entities rv = app.get('/container/{}'.format(j1.ident)) - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/container/{}'.format(j2.ident)) assert rv.status_code == 302 rv = app.get('/creator/{}'.format(c1.ident)) - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/creator/{}'.format(c2.ident)) assert rv.status_code == 302 rv = app.get('/file/{}'.format(f1.ident)) - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/file/{}'.format(f2.ident)) assert rv.status_code == 302 rv = app.get('/release/{}'.format(r1.ident)) - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/release/{}'.format(r2.ident)) assert rv.status_code == 302 rv = app.get('/work/{}'.format(r1.work_id)) - assert rv.status_code == 200 + rv.raise_for_status() rv = app.get('/work/{}'.format(r2.work_id)) assert rv.status_code == 302 -- cgit v1.2.3