capsul-flask/capsulflask/tests_base.py

43 lines
1.4 KiB
Python

from io import StringIO
import logging
import unittest
import os
from nanoid import generate
from flask_testing import TestCase
from flask import current_app
from capsulflask import create_app
from capsulflask.db import get_model
class BaseTestCase(TestCase):
def create_app(self):
# Use default connection paramaters
os.environ['POSTGRES_CONNECTION_PARAMETERS'] = "host=localhost port=5432 user=postgres password=dev dbname=capsulflask_test"
os.environ['TESTING'] = '1'
os.environ['LOG_LEVEL'] = 'DEBUG'
os.environ['SPOKE_MODEL'] = 'mock'
os.environ['HUB_MODEL'] = 'capsul-flask'
self.app = create_app()
return self.app
def setUp(self):
self.logs_from_test = StringIO()
self.logs_handler = logging.StreamHandler(self.logs_from_test)
current_app.logger.addHandler(self.logs_handler)
def tearDown(self):
#self.app.logger.removeHandler(self.logs_handler)
#logging.getLogger("asdasd1").warning(f"{self.id()} captured output:\n{self.logs_from_test.getvalue()}\n")
file_object = open('unittest-output.log', 'a')
file_object.write(f"{self.id()} captured output:\n{self.logs_from_test.getvalue()}\n")
file_object.close()
def _login(self, user_email):
get_model().login(user_email)
with self.client.session_transaction() as session:
session['account'] = user_email
session['csrf-token'] = generate()