From 841342442ba30c7af4b8d556a63a2bb80e686c68 Mon Sep 17 00:00:00 2001
From: rha104 <rha104@uib.no>
Date: Thu, 28 Jan 2021 13:37:29 +0100
Subject: [PATCH] Fixed dataformat. Moved transactions list onto voucher.

And added two new tests
---
 README.md                           | 16 ++---
 setra_client/models.py              | 14 ++---
 tests/conftest.py                   |  2 +-
 tests/fixtures/multi_fixture.json   | 90 ++++++++++++++---------------
 tests/fixtures/voucher_fixture.json | 46 ++++++++++++++-
 tests/test_client.py                | 22 +++++++
 tests/test_endpoints.py             |  1 +
 7 files changed, 130 insertions(+), 61 deletions(-)

diff --git a/README.md b/README.md
index b89ff94..4e27554 100644
--- a/README.md
+++ b/README.md
@@ -17,12 +17,6 @@ batch = Batch.from_dict({
     "vouchertype": 5,
     "batchid_interface": 6
 })
-vouchers = [Voucher.from_dict({
-    "batchid": 1,
-    "voucherno_interface": 2,
-    "exref": 3,
-    "voucherno": 4
-})]
 transactions = [Transaction.from_dict({
     "voucherid": 1,
     "account": 1,
@@ -44,7 +38,15 @@ transactions = [Transaction.from_dict({
     "extinvref": 1
 })]
 
-multi = Multi(batch=batch, vouchers=vouchers, transactions=transactions)
+vouchers = [Voucher.from_dict({
+    "batchid": 1,
+    "voucherno_interface": 2,
+    "exref": 3,
+    "voucherno": 4,
+    "transactions": transactions
+})]
+
+multi = Multi(batch=batch, vouchers=vouchers)
 response = c.post_multi(multi)
 ```
 
diff --git a/setra_client/models.py b/setra_client/models.py
index c11a0bb..6c1cba8 100644
--- a/setra_client/models.py
+++ b/setra_client/models.py
@@ -40,12 +40,6 @@ class Batch(BaseModel):
     vouchertype: str
 
 
-class Voucher(BaseModel):
-    voucherdate: Optional[datetime.date]
-    exref: Optional[str]
-    voucherno: int
-
-
 class Transaction(BaseModel):
     account: str
     amount: float
@@ -66,8 +60,14 @@ class Transaction(BaseModel):
     extinvref: Optional[str]
 
 
+class Voucher(BaseModel):
+    voucherdate: Optional[datetime.date]
+    exref: Optional[str]
+    voucherno: int
+    transactions: typing.List[Transaction]
+
+
 class Multi(BaseModel):
     batch: Batch
     vouchers: typing.List[Voucher]
-    transactions: typing.List[Transaction]
 
diff --git a/tests/conftest.py b/tests/conftest.py
index 1f66d2b..7263375 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -49,7 +49,7 @@ def client_with_a_header(baseurl):
 
 @pytest.fixture
 def batch_url(baseurl):
-    return SetraEndpoints(baseurl).batch() # https://localhost/api/batch
+    return SetraEndpoints(baseurl).batch() # example: https://localhost/api/batch
 
 
 @pytest.fixture
diff --git a/tests/fixtures/multi_fixture.json b/tests/fixtures/multi_fixture.json
index 8e9d385..f8022fd 100644
--- a/tests/fixtures/multi_fixture.json
+++ b/tests/fixtures/multi_fixture.json
@@ -10,51 +10,51 @@
   "vouchers": [
     {
       "exref": 1,
-      "voucherno": 66
-    }
-  ],
-  "transactions": [
-    {
-      "voucherno_interface": 1,
-      "voucherid": 66,
-      "account": 1,
-      "amount": 1,
-      "transdate": 1,
-      "curamount": 1,
-      "currency": 1,
-      "description": 1,
-      "dim1": 1,
-      "dim2": 1,
-      "dim3": 1,
-      "dim4": 1,
-      "dim5": 1,
-      "dim6": 1,
-      "dim7": 1,
-      "sequenceno": 1,
-      "taxcode": 1,
-      "transtype": 1,
-      "extinvref": 1
-    },
-    {
-      "voucherno_interface": 1,
-      "voucherid": 66,
-      "account": 2,
-      "amount": 2,
-      "transdate": 2,
-      "curamount": 2,
-      "currency": 2,
-      "description": 2,
-      "dim1": 2,
-      "dim2": 2,
-      "dim3": 2,
-      "dim4": 2,
-      "dim5": 2,
-      "dim6": 2,
-      "dim7": 2,
-      "sequenceno": 2,
-      "taxcode": 2,
-      "transtype": 2,
-      "extinvref": 2
+      "voucherno": 66,
+      "transactions": [
+        {
+          "voucherno_interface": 1,
+          "voucherid": 66,
+          "account": 1,
+          "amount": 1,
+          "transdate": 1,
+          "curamount": 1,
+          "currency": 1,
+          "description": 1,
+          "dim1": 1,
+          "dim2": 1,
+          "dim3": 1,
+          "dim4": 1,
+          "dim5": 1,
+          "dim6": 1,
+          "dim7": 1,
+          "sequenceno": 1,
+          "taxcode": 1,
+          "transtype": 1,
+          "extinvref": 1
+        },
+        {
+          "voucherno_interface": 1,
+          "voucherid": 66,
+          "account": 2,
+          "amount": 2,
+          "transdate": 2,
+          "curamount": 2,
+          "currency": 2,
+          "description": 2,
+          "dim1": 2,
+          "dim2": 2,
+          "dim3": 2,
+          "dim4": 2,
+          "dim5": 2,
+          "dim6": 2,
+          "dim7": 2,
+          "sequenceno": 2,
+          "taxcode": 2,
+          "transtype": 2,
+          "extinvref": 2
+        }
+      ]
     }
   ]
 }
diff --git a/tests/fixtures/voucher_fixture.json b/tests/fixtures/voucher_fixture.json
index c63fb58..e4b5743 100644
--- a/tests/fixtures/voucher_fixture.json
+++ b/tests/fixtures/voucher_fixture.json
@@ -2,5 +2,49 @@
   "batchid": 1,
   "voucherno_interface": 2,
   "exref": 3,
-  "voucherno": 4
+  "voucherno": 4,
+  "transactions": [
+    {
+      "voucherno_interface": 1,
+      "voucherid": 66,
+      "account": 1,
+      "amount": 1,
+      "transdate": 1,
+      "curamount": 1,
+      "currency": 1,
+      "description": 1,
+      "dim1": 1,
+      "dim2": 1,
+      "dim3": 1,
+      "dim4": 1,
+      "dim5": 1,
+      "dim6": 1,
+      "dim7": 1,
+      "sequenceno": 1,
+      "taxcode": 1,
+      "transtype": 1,
+      "extinvref": 1
+    },
+    {
+      "voucherno_interface": 1,
+      "voucherid": 66,
+      "account": 2,
+      "amount": 2,
+      "transdate": 2,
+      "curamount": 2,
+      "currency": 2,
+      "description": 2,
+      "dim1": 2,
+      "dim2": 2,
+      "dim3": 2,
+      "dim4": 2,
+      "dim5": 2,
+      "dim6": 2,
+      "dim7": 2,
+      "sequenceno": 2,
+      "taxcode": 2,
+      "transtype": 2,
+      "extinvref": 2
+    }
+  ]
 }
diff --git a/tests/test_client.py b/tests/test_client.py
index 3f6f521..5acc7f6 100644
--- a/tests/test_client.py
+++ b/tests/test_client.py
@@ -227,6 +227,28 @@ def test_successfully_getting_single_voucher(client, requests_mock, baseurl):
     assert response == {'foo': 'bar'}
 
 
+def test_requesting_single_voucher_with_invalid_voucherid(client, requests_mock, baseurl):
+    """Requesting a voucher, with None as voucherid, will request get all vouchers instead """
+    url = SetraEndpoints(baseurl).voucher()
+    requests_mock.get(url, json={'foo': 'bar'}, status_code=200)
+
+    response = client.get_voucher(None) #using None as voucherid
+    assert response == {'foo': 'bar'}
+
+
+def test_successfully_getting_single_voucher_with_alphanumeric_voucherid(client, requests_mock, baseurl):
+    """Requesting a voucher, with alphanumeric voucherid, will work, and not crash
+
+    TODO: investigate if this is intentional behaviour (need spec on voucher id format)
+
+    """
+    url = SetraEndpoints(baseurl).voucher("abcd123efg")
+    requests_mock.get(url, json={'foo': 'bar'}, status_code=200)
+
+    response = client.get_voucher("abcd123efg") #using alphanum string as voucherid
+    assert response == {'foo': 'bar'}
+
+
 def test_failing_to_get_all_vouchers(client, requests_mock, baseurl):
     """A failing GET all vouchers call should still return json"""
     url = SetraEndpoints(baseurl).voucher()
diff --git a/tests/test_endpoints.py b/tests/test_endpoints.py
index 8d30d7f..349cee7 100644
--- a/tests/test_endpoints.py
+++ b/tests/test_endpoints.py
@@ -16,6 +16,7 @@ def test_init_batch_with_value(baseurl):
     endpoints = SetraEndpoints(baseurl)
     assert endpoints.batch(batch_id="5") == baseurl + "/api/batch/5"
 
+
 def test_init_batch_with_empty_value(baseurl):
     endpoints = SetraEndpoints(baseurl)
     assert endpoints.batch(batch_id="") == baseurl + "/api/batch/"
-- 
GitLab