Commit f079f9740096e708cef363729989297655a7c218
1 parent
a2074730
Exists in
master
and in
4 other branches
Backend tests gets resources db file
Showing
1 changed file
with
21 additions
and
7 deletions
Show diff stats
tests/backend_tests.py
1 | +import os | |
2 | +import sys | |
1 | 3 | import unittest |
4 | +from shutil import copyfile | |
2 | 5 | |
3 | 6 | from pdc_config import TestConfig |
4 | 7 | from app import create_app, db_mgr, db |
... | ... | @@ -7,18 +10,29 @@ from app.auth.models import User |
7 | 10 | |
8 | 11 | class BaseTestCase(unittest.TestCase): |
9 | 12 | def setUp(self): |
10 | - # configure data base | |
13 | + # initialise app | |
11 | 14 | self.app = create_app(TestConfig) |
15 | + | |
16 | + # copy resource demo db to test file | |
17 | + appdir = os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir) | |
18 | + sqlite_file_name = os.path.abspath(os.path.join(appdir, 'resources', 'lesia-btp.sqlite')) | |
19 | + if not os.path.isdir(self.app.instance_path): | |
20 | + os.mkdir(self.app.instance_path) | |
21 | + self.db_path = os.path.join(self.app.instance_path, 'test.db') | |
22 | + copyfile(sqlite_file_name, self.db_path) | |
23 | + | |
24 | + # force db path to newly create file | |
25 | + self.app.config.update( | |
26 | + SQLALCHEMY_DATABASE_URI='sqlite:///' + self.db_path | |
27 | + ) | |
28 | + | |
29 | + # update flask context | |
12 | 30 | self.app_context = self.app.app_context() |
13 | 31 | self.app_context.push() |
14 | - # TODO: shall we always copy db from resources sqlite file ? | |
15 | - # that would allow us to run all tests in sqlite:memory: | |
16 | - # db.create_all() | |
17 | - # admin = User(email='admin@nowhere.org', name='admin', login='admin', password='admin', role='admin') | |
18 | - # db.session.add(admin) | |
19 | - # db.session.commit() | |
20 | 32 | |
21 | 33 | def tearDown(self): |
34 | + if os.path.isfile(self.db_path): | |
35 | + os.remove(self.db_path) | |
22 | 36 | self.app_context.pop() |
23 | 37 | |
24 | 38 | def test_always_true(self): | ... | ... |