Commit a84fcae0f55280e6856a5fc8994b486948906dbc

Authored by hitier
1 parent 61bd8b00

Extract code to common test lib

Showing 2 changed files with 18 additions and 10 deletions   Show diff stats
tests/backend_tests.py
@@ -6,20 +6,14 @@ from shutil import copyfile @@ -6,20 +6,14 @@ from shutil import copyfile
6 from pdc_config import TestConfig 6 from pdc_config import TestConfig
7 from app import create_app, db_mgr, db 7 from app import create_app, db_mgr, db
8 from app.auth.models import User 8 from app.auth.models import User
  9 +from tests.common_db_feed import resources_to_instancedb
9 10
10 11
11 class BaseTestCase(unittest.TestCase): 12 class BaseTestCase(unittest.TestCase):
12 def setUp(self): 13 def setUp(self):
13 # initialise app 14 # initialise app
14 self.app = create_app(TestConfig) 15 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) 16 + self.db_path = resources_to_instancedb(self.app)
23 17
24 # force db path to newly create file 18 # force db path to newly create file
25 self.app.config.update( 19 self.app.config.update(
@@ -59,4 +53,3 @@ class DbMgrTestCase(BaseTestCase): @@ -59,4 +53,3 @@ class DbMgrTestCase(BaseTestCase):
59 stacked_charges = db_mgr.charges_by_project_stacked(60) 53 stacked_charges = db_mgr.charges_by_project_stacked(60)
60 # Waiting for 17 periods + headers line 54 # Waiting for 17 periods + headers line
61 self.assertEqual(18, len(stacked_charges)) 55 self.assertEqual(18, len(stacked_charges))
62 -  
tests/common_db_feed.py
  1 +import os
  2 +from shutil import copyfile
  3 +
1 from app.models import Category, db, Label, Project, ProjectLabel 4 from app.models import Category, db, Label, Project, ProjectLabel
2 5
  6 +
  7 +def resources_to_instancedb(app):
  8 + # copy resource demo db to test file
  9 + appdir = os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir)
  10 + sqlite_file_name = os.path.abspath(os.path.join(appdir, 'resources', 'lesia-btp.sqlite'))
  11 + if not os.path.isdir(app.instance_path):
  12 + os.mkdir(app.instance_path)
  13 + db_path = os.path.join(app.instance_path, 'test.db')
  14 + copyfile(sqlite_file_name, db_path)
  15 + return db_path
  16 +
  17 +
3 categorized_labels = {'pole': ['Spatial', 'Sol'], 'domaine': ['soleil-terre', 'atmosphere', 'r&t', 'géologie']} 18 categorized_labels = {'pole': ['Spatial', 'Sol'], 'domaine': ['soleil-terre', 'atmosphere', 'r&t', 'géologie']}
4 projects_categories = {'ChemCam': {'pole': 'Spatial', 'domaine': 'géologie'}, 19 projects_categories = {'ChemCam': {'pole': 'Spatial', 'domaine': 'géologie'},
5 'Pilot': {'pole': 'Spatial', 'domaine': 'atmosphere'}, 20 'Pilot': {'pole': 'Spatial', 'domaine': 'atmosphere'},
@@ -30,7 +45,7 @@ def feed_projects(): @@ -30,7 +45,7 @@ def feed_projects():
30 n_l = db.session.query(Label).filter(Label.name == l_name).one() 45 n_l = db.session.query(Label).filter(Label.name == l_name).one()
31 n_pc = ProjectLabel(project=n_p, category=n_c, label=n_l) 46 n_pc = ProjectLabel(project=n_p, category=n_c, label=n_l)
32 db.session.add(n_pc) 47 db.session.add(n_pc)
33 - 48 +
34 db.session.commit() 49 db.session.commit()
35 50
36 51