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

Merge branch 'rework-post_new_batch-interface' into 'master'

Redefine the interface for post_new_batch()

See merge request !20
parents bc7611aa d97431cc
No related branches found
No related tags found
1 merge request!20Redefine the interface for post_new_batch()
Pipeline #90043 passed
...@@ -288,13 +288,17 @@ class SetraClient(object): ...@@ -288,13 +288,17 @@ class SetraClient(object):
data=batchdata.json(), data=batchdata.json(),
headers=headers, headers=headers,
return_response=True) return_response=True)
try:
content = response.json()
except requests.exceptions.JSONDecodeError:
content = response.content
if response.status_code == 202: if response.status_code == 202:
return response.content, 'Accepted' return 'Accepted', {}
elif response.status_code == 409: elif response.status_code == 409:
return response.content, "Conflict" return 'Conflict', content
else: else:
response.raise_for_status() return 'Unknown', {'code': response.status_code, 'content': content}
return response
def put_update_batch(self, batchdata: InputBatch): def put_update_batch(self, batchdata: InputBatch):
""" """
......
...@@ -289,25 +289,44 @@ def test_failing_to_get_all_transactions(client, requests_mock, baseurl): ...@@ -289,25 +289,44 @@ def test_failing_to_get_all_transactions(client, requests_mock, baseurl):
# Test post_new_batch method # Test post_new_batch method
def test_successfully_post_batch_with_voucher(client, batch_with_voucher_fixture, requests_mock, baseurl): 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() url = SetraEndpoints(baseurl).post_new_batch()
batch = InputBatch.from_dict(batch_with_voucher_fixture) 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 state, data = client.post_new_batch(batch) # we get a response object back
assert response.json() == {'somestatus': 'ok'} assert state == 'Accepted'
assert data == {}
def test_successfully_post_batch_with_voucher_and_response(client, batch_with_voucher_fixture, requests_mock, baseurl): 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, """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() 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) batch = InputBatch.from_dict(batch_with_voucher_fixture)
response = client.post_new_batch(batch) # we get a response object back state, data = client.post_new_batch(batch) # we get a response object back
assert response.json() == {'somestatus': 'ok'} assert state == 'Accepted'
assert response.status_code == 200 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( 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