From f079f9740096e708cef363729989297655a7c218 Mon Sep 17 00:00:00 2001 From: Richard Hitier Date: Thu, 29 Apr 2021 12:44:00 +0200 Subject: [PATCH] Backend tests gets resources db file --- tests/backend_tests.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/tests/backend_tests.py b/tests/backend_tests.py index 78f73ad..a74a134 100644 --- a/tests/backend_tests.py +++ b/tests/backend_tests.py @@ -1,4 +1,7 @@ +import os +import sys import unittest +from shutil import copyfile from pdc_config import TestConfig from app import create_app, db_mgr, db @@ -7,18 +10,29 @@ from app.auth.models import User class BaseTestCase(unittest.TestCase): def setUp(self): - # configure data base + # initialise app self.app = create_app(TestConfig) + + # copy resource demo db to test file + appdir = os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir) + sqlite_file_name = os.path.abspath(os.path.join(appdir, 'resources', 'lesia-btp.sqlite')) + if not os.path.isdir(self.app.instance_path): + os.mkdir(self.app.instance_path) + self.db_path = os.path.join(self.app.instance_path, 'test.db') + copyfile(sqlite_file_name, self.db_path) + + # force db path to newly create file + self.app.config.update( + SQLALCHEMY_DATABASE_URI='sqlite:///' + self.db_path + ) + + # update flask context self.app_context = self.app.app_context() self.app_context.push() - # TODO: shall we always copy db from resources sqlite file ? - # that would allow us to run all tests in sqlite:memory: - # db.create_all() - # admin = User(email='admin@nowhere.org', name='admin', login='admin', password='admin', role='admin') - # db.session.add(admin) - # db.session.commit() def tearDown(self): + if os.path.isfile(self.db_path): + os.remove(self.db_path) self.app_context.pop() def test_always_true(self): -- libgit2 0.21.2