itplanning/tests/testBase.py

46 lines
1.5 KiB
Python
Raw Normal View History

2021-06-01 12:16:05 +00:00
import tempfile
import unittest
import os
2021-06-21 20:33:01 +00:00
from json import loads
2021-06-01 12:16:05 +00:00
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
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)
2021-06-21 20:33:01 +00:00
if method == "get":
rv = self.client.get(uri, query_string=args, follow_redirects=True)
if method == "post":
rv = self.client.post(uri, data=args, follow_redirects=True)
if return_code == 200:
data = loads(rv.data)
else:
data = None
2021-06-01 12:16:05 +00:00
self.assertEqual(rv.status_code, return_code)
2021-06-21 20:33:01 +00:00
return rv, data