Commit f079f9740096e708cef363729989297655a7c218

Authored by hitier
1 parent a2074730

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 import unittest 3 import unittest
  4 +from shutil import copyfile
2 5
3 from pdc_config import TestConfig 6 from pdc_config import TestConfig
4 from app import create_app, db_mgr, db 7 from app import create_app, db_mgr, db
@@ -7,18 +10,29 @@ from app.auth.models import User @@ -7,18 +10,29 @@ from app.auth.models import User
7 10
8 class BaseTestCase(unittest.TestCase): 11 class BaseTestCase(unittest.TestCase):
9 def setUp(self): 12 def setUp(self):
10 - # configure data base 13 + # initialise app
11 self.app = create_app(TestConfig) 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 self.app_context = self.app.app_context() 30 self.app_context = self.app.app_context()
13 self.app_context.push() 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 def tearDown(self): 33 def tearDown(self):
  34 + if os.path.isfile(self.db_path):
  35 + os.remove(self.db_path)
22 self.app_context.pop() 36 self.app_context.pop()
23 37
24 def test_always_true(self): 38 def test_always_true(self):