From 6fb4a2034857bb72b0c4cc0a65efe1bb81926847 Mon Sep 17 00:00:00 2001 From: vincent Date: Tue, 1 Jun 2021 14:16:05 +0200 Subject: [PATCH] add tests --- tests/__init__.py | 15 +++++++ tests/api/__init__.py | 9 ++++ tests/api/test_appointment.py | 10 +++++ tests/assets/sample.ini | 13 ++++++ tests/base/__init__.py | 10 +++++ tests/base/test_DB.py | 84 +++++++++++++++++++++++++++++++++++ tests/base/test_config.py | 26 +++++++++++ tests/testBase.py | 41 +++++++++++++++++ 8 files changed, 208 insertions(+) create mode 100644 tests/__init__.py create mode 100644 tests/api/__init__.py create mode 100644 tests/api/test_appointment.py create mode 100644 tests/assets/sample.ini create mode 100644 tests/base/__init__.py create mode 100644 tests/base/test_DB.py create mode 100644 tests/base/test_config.py create mode 100644 tests/testBase.py diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..def2768 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1,15 @@ +import unittest +from . import base +from . import api + + +def suite(): + suite = unittest.TestSuite() + suite.addTest(base.suite()) + suite.addTest(api.suite()) + return suite + + +if __name__ == "__main__": + runner = unittest.TextTestRunner() + runner.run(suite()) diff --git a/tests/api/__init__.py b/tests/api/__init__.py new file mode 100644 index 0000000..445331c --- /dev/null +++ b/tests/api/__init__.py @@ -0,0 +1,9 @@ +import unittest +from .test_appointment import AppointmentTestCase + + +def suite(): + suite = unittest.TestSuite() + + suite.addTest(unittest.makeSuite(AppointmentTestCase)) + return suite diff --git a/tests/api/test_appointment.py b/tests/api/test_appointment.py new file mode 100644 index 0000000..73a2146 --- /dev/null +++ b/tests/api/test_appointment.py @@ -0,0 +1,10 @@ +from ..testBase import APITestBase +from pony.orm import db_session + + +class AppointmentTestCase(APITestBase): + def setUp(self): + super().setUp() + + def test_get_appointement(self): + self.make_request("appointment", 200) diff --git a/tests/assets/sample.ini b/tests/assets/sample.ini new file mode 100644 index 0000000..065ee46 --- /dev/null +++ b/tests/assets/sample.ini @@ -0,0 +1,13 @@ +[types] +float = 1.23 +int = 42 +string = Some text here + +[booleans] +bool_false = false +bool_true = true +switch_false = off +switch_true = on +yn_false = no +yn_true = yes + diff --git a/tests/base/__init__.py b/tests/base/__init__.py new file mode 100644 index 0000000..64f15b0 --- /dev/null +++ b/tests/base/__init__.py @@ -0,0 +1,10 @@ +import unittest +from .test_DB import DbTestCase +from .test_config import ConfigTestCase + + +def suite(): + suite = unittest.TestSuite() + suite.addTest(unittest.makeSuite(DbTestCase)) + suite.addTest(unittest.makeSuite(ConfigTestCase)) + return suite diff --git a/tests/base/test_DB.py b/tests/base/test_DB.py new file mode 100644 index 0000000..cac7676 --- /dev/null +++ b/tests/base/test_DB.py @@ -0,0 +1,84 @@ +import unittest +import re +from datetime import datetime +from datetime import timedelta +from pony.orm import db_session +from ITPlanning import db + +DATE_REGEX = r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$" + + +class DbTestCase(unittest.TestCase): + def setUp(self): + db.init_database("sqlite:") + + def tearDown(self): + db.release_database() + + def create_Customer(self): + return db.Customer( + first_name="john", + last_name="DOE", + email="john@doe.fr", + localisation="batiment 1", + mobile_number="0602020020", + phone_number="0321323432", + ) + + def create_service_category(self): + return db.Service_category( + name="install", + duration=45, + free_order=False, + description="test Service_category", + ) + + def create_service(self): + service_category = self.create_service_category() + return db.Service( + name="install", max_appointement=1, service_category=service_category + ) + + def create_planning(self): + return db.Planning( + name="refresh1", + working_plan='{"monday":{"start":"09:00","end":"18:00","breaks":[{"start":"11:k20","end":"11:30"},{"start":"14:30","end":"15:00"}]},"tuesday":{"start":"09:00","end":"18:00","breaks":[{"start":"11:20","end":"11:30"},{"start":"14:30","end":"15:00"}]},"wednesday":{"start":"09:00","end":"18:00","breaks":[{"start":"11:20","end":"11:30"},{"start":"14:30","end":"15:00"}]},"thursday":{"start":"09:00","end":"18:00","breaks":[{"start":"11:20","end":"11:30"},{"start":"14:30","end":"15:00"}]},"friday":{"start":"09:00","end":"18:00","breaks":[{"start":"11:20","end":"11:30"},{"start":"14:30","end":"15:00"}]}', + ) + + def create_ticket(self): + pass + + def create_Planner(self): + pass + + @db_session + def test_appointement(self): + current_date = datetime.now() + customer = self.create_Customer() + service = self.create_service() + planning = self.create_planning() + planning.services.add(service) + end_datetime = current_date + timedelta( + minutes=service.service_category.duration + ) + + appointment = db.Appointment( + book_datetime=current_date, + start_datetime=current_date, + end_datetime=end_datetime, + is_unavaillable=False, + service=service, + planning=planning, + customer=customer, + ) + self.assertIsInstance(appointment, db.Appointment) + self.assertIsInstance(appointment.book_datetime, datetime) + self.assertIsInstance(appointment.start_datetime, datetime) + self.assertIsInstance(appointment.end_datetime, datetime) + self.assertIsInstance(appointment.service, db.Service) + self.assertIsInstance(appointment.planning, db.Planning) + self.assertIsInstance(appointment.customer, db.Customer) + + +if __name__ == "main": + unittest.main() diff --git a/tests/base/test_config.py b/tests/base/test_config.py new file mode 100644 index 0000000..f37070e --- /dev/null +++ b/tests/base/test_config.py @@ -0,0 +1,26 @@ +import unittest +from ITPlanning.config import IniConfig + + +class ConfigTestCase(unittest.TestCase): + def test_sections(self): + conf = IniConfig("tests/assets/sample.ini") + for attr in ("TYPES", "BOOLEANS"): + self.assertTrue(hasattr(conf, attr)) + self.assertIsInstance(getattr(conf, attr), dict) + + def test_types(self): + conf = IniConfig("tests/assets/sample.ini") + self.assertIsInstance(conf.TYPES["float"], float) + self.assertIsInstance(conf.TYPES["int"], int) + self.assertIsInstance(conf.TYPES["string"], str) + + for t in ("bool", "switch", "yn"): + self.assertIsInstance(conf.BOOLEANS[t + "_false"], bool) + self.assertIsInstance(conf.BOOLEANS[t + "_true"], bool) + self.assertFalse(conf.BOOLEANS[t + "_false"]) + self.assertTrue(conf.BOOLEANS[t + "_true"]) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/testBase.py b/tests/testBase.py new file mode 100644 index 0000000..83b2c6f --- /dev/null +++ b/tests/testBase.py @@ -0,0 +1,41 @@ +import tempfile +import unittest +import os +from ITPlanning.config import DefaultConfig +from ITPlanning.db import init_database, release_database +from ITPlanning.app import create_app + + +class TestBase(unittest.TestCase): + def setUp(self): + self.__db = tempfile.mkstemp() + self.config = DefaultConfig() + self.config.BASE["database_uri"] = "sqlite:///" + self.__db[1] + self.config.TESTING = True + init_database(self.config.BASE["database_uri"]) + release_database() + self.__app = create_app(self.config) + self.client = self.__app.test_client() + + def app_context(self, *args, **kwargs): + return self.__app.app_context(*args, **kwargs) + + def request_context(self, *args, **kwargs): + return self.__app.test_request_context(*args, **kwargs) + + def tearDown(self): + release_database() + os.close(self.__db[0]) + os.remove(self.__db[1]) + + +class APITestBase(TestBase): + def make_request(self, endpoint, return_code, args={}, method="get"): + if not isinstance(args, dict): + raise TypeError("'args', expecting a dict, got " + type(args).__name__) + uri = "/api/v1/{}".format(endpoint) + + method = getattr(self.client, method) + rv = method(uri, data=args) + self.assertEqual(rv.status_code, return_code) + return rv