From ddeeec955abb93a1d8e22fc8cf234df624db8180 Mon Sep 17 00:00:00 2001 From: rha104 <rha104@uib.no> Date: Thu, 4 Feb 2021 12:54:46 +0100 Subject: [PATCH] Removed Multi object, use Batch instead Also updated and fixed tests, fixtures and readme --- README.md | 26 ++++++++++--------- setra_client/client.py | 16 ++++++------ setra_client/models.py | 17 +++++------- tests/conftest.py | 8 +++--- ...l_fixture.json => batch_fail_fixture.json} | 0 tests/fixtures/batch_fixture.json | 3 ++- ...e.json => batch_with_voucher_fixture.json} | 14 +++++----- tests/test_client.py | 22 ++++++++-------- tests/test_models.py | 21 +++++++++------ 9 files changed, 65 insertions(+), 62 deletions(-) rename tests/fixtures/{multi_fail_fixture.json => batch_fail_fixture.json} (100%) rename tests/fixtures/{multi_fixture.json => batch_with_voucher_fixture.json} (87%) diff --git a/README.md b/README.md index 2960f83..a7f1720 100644 --- a/README.md +++ b/README.md @@ -4,19 +4,12 @@ Client for doing HTTP requests to the SETRA api ```python 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'}) -batch = Batch.from_dict({ - "client": 1, - "batchid": 2, - "period": 3, - "interface": 4, - "vouchertype": 5, - "batchid_interface": 6 -}) + transactions = [Transaction.from_dict({ "voucherid": 1, "account": 1, @@ -45,8 +38,17 @@ vouchers = [Voucher.from_dict({ "transactions": transactions })] -multi = Multi(batch=batch, vouchers=vouchers) -response = c.post_multi(multi) +batch = Batch.from_dict({ + "client": 1, + "batchid": 2, + "period": 3, + "interface": 4, + "vouchertype": 5, + "batchid_interface": 6, + "vouchers": vouchers +}) + +response = client.post_new_batch(batch) ``` Running tests diff --git a/setra_client/client.py b/setra_client/client.py index 1a50bf1..2d4c915 100644 --- a/setra_client/client.py +++ b/setra_client/client.py @@ -6,7 +6,7 @@ from typing import Tuple, Union, List import requests -from setra_client.models import Multi +from setra_client.models import Batch logger = logging.getLogger(__name__) @@ -37,13 +37,13 @@ class SetraEndpoints: batch_url='api/batch/', transaction_url='api/transaction/', voucher_url='api/voucher/', - multi_url='api/addtrans/' + new_batch_url='api/addtrans/' ): self.baseurl = url self.batch_url = batch_url self.transaction_url = transaction_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. """ @@ -83,8 +83,8 @@ class SetraEndpoints: return urllib.parse.urljoin(self.baseurl, '/'.join((self.voucher_url, vouch_id))) - def post_multi(self): - return urllib.parse.urljoin(self.baseurl, self.multi_url) + def post_new_batch(self): + return urllib.parse.urljoin(self.baseurl, self.new_batch_url) class SetraClient(object): @@ -197,14 +197,14 @@ class SetraClient(object): response = self.get(url) 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 """ - url = self.urls.post_multi() + url = self.urls.post_new_batch() headers = {'Content-Type': 'application/json'} response = self.post(url, - data=multidata.json(), + data=batchdata.json(), headers=headers, return_response=return_response) return response diff --git a/setra_client/models.py b/setra_client/models.py index 6c1cba8..a121b2c 100644 --- a/setra_client/models.py +++ b/setra_client/models.py @@ -31,14 +31,6 @@ class BaseModel(pydantic.BaseModel): 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): account: str @@ -67,7 +59,12 @@ class Voucher(BaseModel): transactions: typing.List[Transaction] -class Multi(BaseModel): - batch: Batch +class Batch(BaseModel): + """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] diff --git a/tests/conftest.py b/tests/conftest.py index 7263375..8a225c0 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -73,10 +73,10 @@ def trans_fail_fixture(): @pytest.fixture -def multi_fixture(): - return load_json_file('multi_fixture.json') +def batch_with_voucher_fixture(): + return load_json_file('batch_with_voucher_fixture.json') @pytest.fixture -def multi_fail_fixture(): - return load_json_file('multi_fail_fixture.json') +def batch_fail_fixture(): + return load_json_file('batch_fail_fixture.json') diff --git a/tests/fixtures/multi_fail_fixture.json b/tests/fixtures/batch_fail_fixture.json similarity index 100% rename from tests/fixtures/multi_fail_fixture.json rename to tests/fixtures/batch_fail_fixture.json diff --git a/tests/fixtures/batch_fixture.json b/tests/fixtures/batch_fixture.json index c5fefce..298ce71 100644 --- a/tests/fixtures/batch_fixture.json +++ b/tests/fixtures/batch_fixture.json @@ -4,5 +4,6 @@ "period": 3, "interface": 4, "vouchertype": 5, - "batchid_interface": 6 + "batchid_interface": 6, + "vouchers": [] } diff --git a/tests/fixtures/multi_fixture.json b/tests/fixtures/batch_with_voucher_fixture.json similarity index 87% rename from tests/fixtures/multi_fixture.json rename to tests/fixtures/batch_with_voucher_fixture.json index cadf9f0..a58a3d1 100644 --- a/tests/fixtures/multi_fixture.json +++ b/tests/fixtures/batch_with_voucher_fixture.json @@ -1,12 +1,10 @@ { - "batch": { - "client": 1, - "batchid": 55, - "period": 1, - "interface": 1, - "vouchertype": 1, - "batchid_interface": 1 - }, + "client": 1, + "batchid": 55, + "period": 1, + "interface": 1, + "vouchertype": 1, + "batchid_interface": 1, "vouchers": [ { "exref": 1, diff --git a/tests/test_client.py b/tests/test_client.py index 5acc7f6..826fc57 100644 --- a/tests/test_client.py +++ b/tests/test_client.py @@ -4,7 +4,7 @@ from requests import HTTPError from setra_client.client import SetraEndpoints from setra_client.client import SetraClient from json.decoder import JSONDecodeError -from setra_client.models import Multi +from setra_client.models import Batch @pytest.fixture @@ -278,26 +278,26 @@ def test_failing_to_get_all_transactions(client, requests_mock, baseurl): 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""" - url = SetraEndpoints(baseurl).post_multi() - multi = Multi.from_dict(multi_fixture) + url = SetraEndpoints(baseurl).post_new_batch() + batch = Batch.from_dict(batch_with_voucher_fixture) 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'} -def test_successfully_post_multi_with_response(client, multi_fixture, requests_mock, baseurl): - """A working POST multi call with return_response=True, +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""" - 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 - multi = Multi.from_dict(multi_fixture) - response = client.post_multi(multi, return_response=True) # we get a response back + batch = Batch.from_dict(batch_with_voucher_fixture) + response = client.post_new_batch(batch, return_response=True) # we get a response back assert response.json() == {'somestatus': 'ok'} assert response.status_code == 200 diff --git a/tests/test_models.py b/tests/test_models.py index fb1049b..aa0e46e 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,21 +1,25 @@ import pytest 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): - # Check correct example work assert Batch(**batch_fixture) def test_voucher(voucher_fixture): - # Check correct example work 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): - # Check correct example work assert Transaction(**trans_fixture) @@ -25,10 +29,11 @@ def test_transaction_fail(trans_fail_fixture): Transaction(**trans_fail_fixture) -def test_multi(multi_fixture): - assert Multi(**multi_fixture) +def test_batch_with_voucher(batch_with_voucher_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): - Multi(**multi_fail_fixture) + Batch(**batch_fail_fixture) -- GitLab