Skip to content
Snippets Groups Projects
Commit d97431cc authored by Jo Sama's avatar Jo Sama :scream:
Browse files

Redefine the interface for post_new_batch()

This interface avoids leaking transport-specific information, while
returning an indication of how the request fared so the user can decide
on how to proceed with processing.
parent bc7611aa
No related branches found
No related tags found
1 merge request!20Redefine the interface for post_new_batch()
Pipeline #90037 passed
......@@ -288,13 +288,17 @@ class SetraClient(object):
data=batchdata.json(),
headers=headers,
return_response=True)
try:
content = response.json()
except requests.exceptions.JSONDecodeError:
content = response.content
if response.status_code == 202:
return response.content, 'Accepted'
return 'Accepted', {}
elif response.status_code == 409:
return response.content, "Conflict"
return 'Conflict', content
else:
response.raise_for_status()
return response
return 'Unknown', {'code': response.status_code, 'content': content}
def put_update_batch(self, batchdata: InputBatch):
"""
......
......@@ -289,25 +289,44 @@ def test_failing_to_get_all_transactions(client, requests_mock, baseurl):
# Test post_new_batch method
def test_successfully_post_batch_with_voucher(client, batch_with_voucher_fixture, requests_mock, baseurl):
"""A working GET call should return HTTP 200, with json content"""
"""A working GET call should return HTTP 202, with json content"""
url = SetraEndpoints(baseurl).post_new_batch()
batch = InputBatch.from_dict(batch_with_voucher_fixture)
requests_mock.post(url, json={'somestatus': 'ok'}, status_code=200, request_headers={"Content-Type": "application/json"})
requests_mock.post(url, json={}, status_code=202, request_headers={"Content-Type": "application/json"})
response = client.post_new_batch(batch) # we get a response object back
assert response.json() == {'somestatus': 'ok'}
state, data = client.post_new_batch(batch) # we get a response object back
assert state == 'Accepted'
assert data == {}
def test_successfully_post_batch_with_voucher_and_response(client, batch_with_voucher_fixture, requests_mock, baseurl):
"""A working POST new batch call with return_response=True,
should return the response with HTTP 200, with json content"""
should return the response with HTTP 202, with json content"""
url = SetraEndpoints(baseurl).post_new_batch()
requests_mock.post(url, json={'somestatus': 'ok'}, status_code=200, request_headers={"Content-Type": "application/json"}) #expect json content
requests_mock.post(url, json={}, status_code=202, request_headers={"Content-Type": "application/json"}) #expect json content
batch = InputBatch.from_dict(batch_with_voucher_fixture)
response = client.post_new_batch(batch) # we get a response object back
assert response.json() == {'somestatus': 'ok'}
assert response.status_code == 200
state, data = client.post_new_batch(batch) # we get a response object back
assert state == 'Accepted'
assert data == {}
def test_conflicting_post_new_batch(client, batch_with_voucher_fixture, requests_mock, baseurl):
url = SetraEndpoints(baseurl).post_new_batch()
requests_mock.post(url, json={'error': 'batch is being processed'}, status_code=409, request_headers={"Content-Type": "application/json"}) #expect json content
batch = InputBatch.from_dict(batch_with_voucher_fixture)
state, data = client.post_new_batch(batch)
assert state == 'Conflict'
assert data == {'error': 'batch is being processed'}
def test_unknown_post_new_batch_state(client, batch_with_voucher_fixture, requests_mock, baseurl):
url = SetraEndpoints(baseurl).post_new_batch()
requests_mock.post(url, json={'error': 'Batch is malformed, no id'}, status_code=500, request_headers={"Content-Type": "application/json"}) #expect json content
batch = InputBatch.from_dict(batch_with_voucher_fixture)
state, data = client.post_new_batch(batch)
assert state == 'Unknown'
assert data == {'code': 500, 'content': {'error': 'Batch is malformed, no id'}}
def test_successfully_getting_batch_complete(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment