Commit 7bca61b903e6f95b9c754f1d0cf08a29d4455c4f
1 parent
f3dbebc2
Exists in
master
and in
4 other branches
New models tests
Base test class is now inititated with mode btp or memory
Showing
1 changed file
with
42 additions
and
10 deletions
Show diff stats
tests/db_tests.py
1 | +import os | |
1 | 2 | import unittest |
2 | 3 | |
3 | 4 | from app import create_app, db, User |
4 | 5 | from app.models import Project |
5 | 6 | from pdc_config import TestConfig |
6 | -from tests.common_db_feed import feed_projects | |
7 | +from tests.common_db_feed import feed_projects, resources_to_instancedb | |
7 | 8 | |
8 | 9 | |
9 | 10 | class DbBaseTestCase(unittest.TestCase): |
10 | - def setUp(self): | |
11 | + def setUp(self, mode='memory'): | |
12 | + """ | |
13 | + | |
14 | + :param mode: memory or btp | |
15 | + :return: | |
16 | + """ | |
11 | 17 | self.app = create_app(TestConfig) |
12 | - # force db uri to sqlite memory | |
18 | + self.mode = mode | |
19 | + # Set uri based on called mode | |
20 | + # | |
21 | + if self.mode == 'memory': | |
22 | + # force db uri to sqlite memory | |
23 | + uri = 'sqlite:///:memory:' | |
24 | + elif self.mode == 'btp': | |
25 | + self.db_path = resources_to_instancedb(self.app) | |
26 | + # force db path to newly create file | |
27 | + uri = 'sqlite:///' + self.db_path | |
13 | 28 | self.app.config.update( |
14 | - SQLALCHEMY_DATABASE_URI='sqlite:///:memory:' | |
29 | + SQLALCHEMY_DATABASE_URI=uri | |
15 | 30 | ) |
31 | + # Push context | |
32 | + # | |
16 | 33 | self.app_context = self.app.app_context() |
17 | 34 | self.app_context.push() |
18 | - db.create_all() | |
19 | - feed_projects() | |
35 | + # Create and feed db if empty | |
36 | + # | |
37 | + if self.mode == 'memory': | |
38 | + db.create_all() | |
39 | + feed_projects() | |
20 | 40 | |
21 | 41 | def tearDown(self): |
22 | - db.session.remove() | |
23 | - db.drop_all() | |
42 | + if self.mode == 'memory': | |
43 | + db.session.remove() | |
44 | + db.drop_all() | |
45 | + elif self.mode == 'btp': | |
46 | + if os.path.isfile(self.db_path): | |
47 | + os.remove(self.db_path) | |
24 | 48 | self.app_context.pop() |
25 | 49 | |
26 | - def test_first(self): | |
50 | + | |
51 | +class ChargeModelTestCase(DbBaseTestCase): | |
52 | + def setUp(self): | |
53 | + DbBaseTestCase.setUp(self, 'btp') | |
54 | + | |
55 | + def tearDown(self): | |
56 | + DbBaseTestCase.tearDown(self) | |
57 | + | |
58 | + def test_btp(self): | |
27 | 59 | projects = Project.query.all() |
28 | - self.assertEqual(3, len(projects)) | |
60 | + self.assertEqual(101, len(projects)) | |
29 | 61 | |
30 | 62 | |
31 | 63 | class AuthModelTestCase(DbBaseTestCase): | ... | ... |