itplanning/tests/testBase.py
2021-06-01 14:16:05 +02:00

42 lines
1.3 KiB
Python

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