From 6442766081361026b22bc7949efff295ce7cada7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20tom=20W=C3=B6rden?= <h.tomwoerden@indiscale.com>
Date: Fri, 30 Aug 2024 13:15:25 +0200
Subject: [PATCH] TST: clean up unittests

---
 loanpy/unittests/test_box_loan.py       | 37 +++++++++++++++++--------
 loanpy/unittests/test_request_loan.py   |  9 ++----
 loanpy/unittests/test_request_return.py | 12 ++------
 loanpy/unittests/utils.py               | 26 +++++++++++++++++
 4 files changed, 55 insertions(+), 29 deletions(-)
 create mode 100644 loanpy/unittests/utils.py

diff --git a/loanpy/unittests/test_box_loan.py b/loanpy/unittests/test_box_loan.py
index 9e7631d..77906a0 100644
--- a/loanpy/unittests/test_box_loan.py
+++ b/loanpy/unittests/test_box_loan.py
@@ -1,3 +1,21 @@
+#  This file is a part of the LinkAhead Project.
+#
+#  Copyright (C) 2024 Henrik tom Wörden (h.tomwoerden@indiscale.com)
+#  Copyright (C) 2020-2024 IndiScale GmbH (info@indiscale.com)
+#
+#  This program is free software: you can redistribute it and/or modify
+#  it under the terms of the GNU Affero General Public License as
+#  published by the Free Software Foundation, either version 3 of the
+#  License, or (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU Affero General Public License for more details.
+#
+#  You should have received a copy of the GNU Affero General Public License
+#  along with this program. If not, see <https://www.gnu.org/licenses/>.
+
 from os.path import abspath, dirname, join
 from pytest import raises
 from linkahead import get_connection, configure_connection
@@ -7,20 +25,14 @@ from loan.box_loan import (_caller, create_person,
                          assert_date_in_future, DataError, EmailPatternError,
                          assert_email_pattern)
 
+from utils import get_form_data_example
 
 
-def get_data_example():
-    return abspath(join(dirname(__file__), "request_loan_form.json"))
-
-
-def test_caller():
-    args = [get_data_example()]
-
-    def test_func(data):
-        assert data["box"] == 2345, "should contain the data from json"
-        return 1337
 
-    assert _caller(test_func, args) == 1337
+def test_return_value_of_caller():
+    tval = 1337
+    assert _caller(lambda d: tval, [get_form_data_example()]
+) == tval
 
 def test_create_person():
     p = create_person("anna", "lytik", "a@b.com")
@@ -29,7 +41,7 @@ def test_create_person():
     assert p.get_property(LAST_NAME.name).value == "lytik"
     assert p.get_property(EMAIL.name).value == "a@b.com"
 
-def test_create_person_with_wrong_email_pattern():
+def test_email_validation():
     with raises(EmailPatternError):
         assert_email_pattern("@asdf")
     with raises(EmailPatternError):
@@ -44,6 +56,7 @@ def test_create_person_with_wrong_email_pattern():
     assert_email_pattern("\"#\"@ö.de")
 
 def test_assert_date_in_future():
+    # I can't wait for 2050 :-)
     assert assert_date_in_future("2050-01-01") is None
     with raises(DataError):
         assert_date_in_future("1971-01-01")
diff --git a/loanpy/unittests/test_request_loan.py b/loanpy/unittests/test_request_loan.py
index 2379f7b..63c9375 100644
--- a/loanpy/unittests/test_request_loan.py
+++ b/loanpy/unittests/test_request_loan.py
@@ -8,16 +8,11 @@ from loan.box_loan import (PERSON, FIRST_NAME, EMAIL, BOX, BORROWER,
                          DESTINATION, F_FIRST_NAME, F_EMAIL,
                          F_EXPECTED_RETURN_DATE, DataError, BOX_NUMBER)
 from loan.request_loan import (create_loan, _issue_loan_request)
-
-
-
-
-def get_data_example():
-    return abspath(join(dirname(__file__), "request_loan_form.json"))
+from utils import get_form_data_example
 
 
 def test_issue_loan_request_with_wrong_return_date():
-    data = get_data(get_data_example())
+    data = get_data(get_form_data_example())
     data[F_EXPECTED_RETURN_DATE] = "1983-02-03"
     with raises(DataError):
         _issue_loan_request(data)
diff --git a/loanpy/unittests/test_request_return.py b/loanpy/unittests/test_request_return.py
index 7dd55be..8f6bd3b 100644
--- a/loanpy/unittests/test_request_return.py
+++ b/loanpy/unittests/test_request_return.py
@@ -9,19 +9,11 @@ from loan.box_loan import (FIRST_NAME, EMAIL, LAST_NAME, F_FIRST_NAME,
                          LOAN_REQUESTED, StateError, F_EXPECTED_RETURN_DATE,
                          DataError, LOAN)
 from loan.request_return import get_loan, _issue_return_request
-
-
-
-def get_data_example():
-    return abspath(join(dirname(__file__), "request_loan_form.json"))
-
-
-
-
+from utils import get_form_data_example
 
 
 def test_return_request_with_wrong_return_date():
-    data = get_data(get_data_example())
+    data = get_data(get_form_data_example())
     data[F_EXPECTED_RETURN_DATE] = "1983-02-03"
     with raises(DataError):
         _issue_return_request(data)
diff --git a/loanpy/unittests/utils.py b/loanpy/unittests/utils.py
new file mode 100644
index 0000000..c569757
--- /dev/null
+++ b/loanpy/unittests/utils.py
@@ -0,0 +1,26 @@
+#  This file is a part of the LinkAhead Project.
+#
+#  Copyright (C) 2024 Henrik tom Wörden (h.tomwoerden@indiscale.com)
+#  Copyright (C) 2020-2024 IndiScale GmbH (info@indiscale.com)
+#
+#  This program is free software: you can redistribute it and/or modify
+#  it under the terms of the GNU Affero General Public License as
+#  published by the Free Software Foundation, either version 3 of the
+#  License, or (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU Affero General Public License for more details.
+#
+#  You should have received a copy of the GNU Affero General Public License
+#  along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+"""
+some utility functions
+"""
+
+from os.path import abspath, dirname, join
+
+def get_form_data_example():
+    return abspath(join(dirname(__file__), "request_loan_form.json"))
-- 
GitLab