Skip to content
Snippets Groups Projects
Commit ddeeec95 authored by rha104's avatar rha104
Browse files

Removed Multi object, use Batch instead

Also updated and fixed tests, fixtures and readme
parent 2104ee0a
No related branches found
No related tags found
1 merge request!5Fixed dataformat. Moved transactions list onto voucher.
Pipeline #53219 passed
...@@ -4,19 +4,12 @@ Client for doing HTTP requests to the SETRA api ...@@ -4,19 +4,12 @@ Client for doing HTTP requests to the SETRA api
```python ```python
from setra_client import SetraClient from setra_client import SetraClient
from setra_client.models import Batch, Multi, Transaction, Voucher from setra_client.models import Batch, Transaction, Voucher
c = SetraClient(url='https://example.com', client = SetraClient(url='https://example.com',
headers={'X-Gravitee-API-Key': 'c-d-a-b'}) headers={'X-Gravitee-API-Key': 'c-d-a-b'})
batch = Batch.from_dict({
"client": 1,
"batchid": 2,
"period": 3,
"interface": 4,
"vouchertype": 5,
"batchid_interface": 6
})
transactions = [Transaction.from_dict({ transactions = [Transaction.from_dict({
"voucherid": 1, "voucherid": 1,
"account": 1, "account": 1,
...@@ -45,8 +38,17 @@ vouchers = [Voucher.from_dict({ ...@@ -45,8 +38,17 @@ vouchers = [Voucher.from_dict({
"transactions": transactions "transactions": transactions
})] })]
multi = Multi(batch=batch, vouchers=vouchers) batch = Batch.from_dict({
response = c.post_multi(multi) "client": 1,
"batchid": 2,
"period": 3,
"interface": 4,
"vouchertype": 5,
"batchid_interface": 6,
"vouchers": vouchers
})
response = client.post_new_batch(batch)
``` ```
Running tests Running tests
......
...@@ -6,7 +6,7 @@ from typing import Tuple, Union, List ...@@ -6,7 +6,7 @@ from typing import Tuple, Union, List
import requests import requests
from setra_client.models import Multi from setra_client.models import Batch
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -37,13 +37,13 @@ class SetraEndpoints: ...@@ -37,13 +37,13 @@ class SetraEndpoints:
batch_url='api/batch/', batch_url='api/batch/',
transaction_url='api/transaction/', transaction_url='api/transaction/',
voucher_url='api/voucher/', voucher_url='api/voucher/',
multi_url='api/addtrans/' new_batch_url='api/addtrans/'
): ):
self.baseurl = url self.baseurl = url
self.batch_url = batch_url self.batch_url = batch_url
self.transaction_url = transaction_url self.transaction_url = transaction_url
self.voucher_url = voucher_url self.voucher_url = voucher_url
self.multi_url = multi_url self.new_batch_url = new_batch_url
""" Get endpoints relative to the SETRA API URL. """ """ Get endpoints relative to the SETRA API URL. """
...@@ -83,8 +83,8 @@ class SetraEndpoints: ...@@ -83,8 +83,8 @@ class SetraEndpoints:
return urllib.parse.urljoin(self.baseurl, return urllib.parse.urljoin(self.baseurl,
'/'.join((self.voucher_url, vouch_id))) '/'.join((self.voucher_url, vouch_id)))
def post_multi(self): def post_new_batch(self):
return urllib.parse.urljoin(self.baseurl, self.multi_url) return urllib.parse.urljoin(self.baseurl, self.new_batch_url)
class SetraClient(object): class SetraClient(object):
...@@ -197,14 +197,14 @@ class SetraClient(object): ...@@ -197,14 +197,14 @@ class SetraClient(object):
response = self.get(url) response = self.get(url)
return response.json() return response.json()
def post_multi(self, multidata: Multi, return_response: bool = False): def post_new_batch(self, batchdata: Batch, return_response: bool = False):
""" """
POST combination of batch, vouchers and transactions POST combination of batch, vouchers and transactions
""" """
url = self.urls.post_multi() url = self.urls.post_new_batch()
headers = {'Content-Type': 'application/json'} headers = {'Content-Type': 'application/json'}
response = self.post(url, response = self.post(url,
data=multidata.json(), data=batchdata.json(),
headers=headers, headers=headers,
return_response=return_response) return_response=return_response)
return response return response
......
...@@ -31,14 +31,6 @@ class BaseModel(pydantic.BaseModel): ...@@ -31,14 +31,6 @@ class BaseModel(pydantic.BaseModel):
return cls.from_dict(data) return cls.from_dict(data)
class Batch(BaseModel):
"""Model for making json formatted list of a Batch"""
client: str
batchid: str
period: str
interface: str
vouchertype: str
class Transaction(BaseModel): class Transaction(BaseModel):
account: str account: str
...@@ -67,7 +59,12 @@ class Voucher(BaseModel): ...@@ -67,7 +59,12 @@ class Voucher(BaseModel):
transactions: typing.List[Transaction] transactions: typing.List[Transaction]
class Multi(BaseModel): class Batch(BaseModel):
batch: Batch """Model representing a batch, with a list of vouchers (and each voucher has a list of transactions)"""
client: str
batchid: str
period: str
interface: str
vouchertype: str
vouchers: typing.List[Voucher] vouchers: typing.List[Voucher]
...@@ -73,10 +73,10 @@ def trans_fail_fixture(): ...@@ -73,10 +73,10 @@ def trans_fail_fixture():
@pytest.fixture @pytest.fixture
def multi_fixture(): def batch_with_voucher_fixture():
return load_json_file('multi_fixture.json') return load_json_file('batch_with_voucher_fixture.json')
@pytest.fixture @pytest.fixture
def multi_fail_fixture(): def batch_fail_fixture():
return load_json_file('multi_fail_fixture.json') return load_json_file('batch_fail_fixture.json')
...@@ -4,5 +4,6 @@ ...@@ -4,5 +4,6 @@
"period": 3, "period": 3,
"interface": 4, "interface": 4,
"vouchertype": 5, "vouchertype": 5,
"batchid_interface": 6 "batchid_interface": 6,
"vouchers": []
} }
{ {
"batch": { "client": 1,
"client": 1, "batchid": 55,
"batchid": 55, "period": 1,
"period": 1, "interface": 1,
"interface": 1, "vouchertype": 1,
"vouchertype": 1, "batchid_interface": 1,
"batchid_interface": 1
},
"vouchers": [ "vouchers": [
{ {
"exref": 1, "exref": 1,
......
...@@ -4,7 +4,7 @@ from requests import HTTPError ...@@ -4,7 +4,7 @@ from requests import HTTPError
from setra_client.client import SetraEndpoints from setra_client.client import SetraEndpoints
from setra_client.client import SetraClient from setra_client.client import SetraClient
from json.decoder import JSONDecodeError from json.decoder import JSONDecodeError
from setra_client.models import Multi from setra_client.models import Batch
@pytest.fixture @pytest.fixture
...@@ -278,26 +278,26 @@ def test_failing_to_get_all_transactions(client, requests_mock, baseurl): ...@@ -278,26 +278,26 @@ def test_failing_to_get_all_transactions(client, requests_mock, baseurl):
assert response == {'error': 'some json error message'} assert response == {'error': 'some json error message'}
# Test post_multi method # Test post_new_batch method
def test_successfully_post_multi(client, multi_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 200, with json content"""
url = SetraEndpoints(baseurl).post_multi() url = SetraEndpoints(baseurl).post_new_batch()
multi = Multi.from_dict(multi_fixture) batch = Batch.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={'somestatus': 'ok'}, status_code=200, request_headers={"Content-Type": "application/json"})
response = client.post_multi(multi) # we only get json back response = client.post_new_batch(batch) # we only get json back
assert response == {'somestatus': 'ok'} assert response == {'somestatus': 'ok'}
def test_successfully_post_multi_with_response(client, multi_fixture, requests_mock, baseurl): def test_successfully_post_batch_with_voucher_and_response(client, batch_with_voucher_fixture, requests_mock, baseurl):
"""A working POST multi 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 200, with json content"""
url = SetraEndpoints(baseurl).post_multi() 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={'somestatus': 'ok'}, status_code=200, request_headers={"Content-Type": "application/json"}) #expect json content
multi = Multi.from_dict(multi_fixture) batch = Batch.from_dict(batch_with_voucher_fixture)
response = client.post_multi(multi, return_response=True) # we get a response back response = client.post_new_batch(batch, return_response=True) # we get a response back
assert response.json() == {'somestatus': 'ok'} assert response.json() == {'somestatus': 'ok'}
assert response.status_code == 200 assert response.status_code == 200
import pytest import pytest
from pydantic import ValidationError from pydantic import ValidationError
from setra_client.models import Batch, Multi, Voucher, Transaction from setra_client.models import Batch, Voucher, Transaction
def test_batch(batch_fixture): def test_batch(batch_fixture):
# Check correct example work
assert Batch(**batch_fixture) assert Batch(**batch_fixture)
def test_voucher(voucher_fixture): def test_voucher(voucher_fixture):
# Check correct example work
assert Voucher(**voucher_fixture) assert Voucher(**voucher_fixture)
def test_voucher_has_transactions(voucher_fixture):
voucher = Voucher(**voucher_fixture)
assert len(voucher.transactions) == 2
assert voucher.transactions.__getitem__(0).amount == 1
assert voucher.transactions.__getitem__(1).amount == 2
def test_transaction(trans_fixture): def test_transaction(trans_fixture):
# Check correct example work
assert Transaction(**trans_fixture) assert Transaction(**trans_fixture)
...@@ -25,10 +29,11 @@ def test_transaction_fail(trans_fail_fixture): ...@@ -25,10 +29,11 @@ def test_transaction_fail(trans_fail_fixture):
Transaction(**trans_fail_fixture) Transaction(**trans_fail_fixture)
def test_multi(multi_fixture): def test_batch_with_voucher(batch_with_voucher_fixture):
assert Multi(**multi_fixture) assert Batch(**batch_with_voucher_fixture)
def test_multi_fail(multi_fail_fixture): def test_batch_fail(batch_fail_fixture):
# Using wrong data format, should fail:
with pytest.raises(ValidationError): with pytest.raises(ValidationError):
Multi(**multi_fail_fixture) Batch(**batch_fail_fixture)
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