From 7bca61b903e6f95b9c754f1d0cf08a29d4455c4f Mon Sep 17 00:00:00 2001 From: Richard Hitier Date: Tue, 4 May 2021 18:17:34 +0200 Subject: [PATCH] New models tests --- tests/db_tests.py | 52 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/tests/db_tests.py b/tests/db_tests.py index 06420b3..a9ad1d1 100644 --- a/tests/db_tests.py +++ b/tests/db_tests.py @@ -1,31 +1,63 @@ +import os import unittest from app import create_app, db, User from app.models import Project from pdc_config import TestConfig -from tests.common_db_feed import feed_projects +from tests.common_db_feed import feed_projects, resources_to_instancedb class DbBaseTestCase(unittest.TestCase): - def setUp(self): + def setUp(self, mode='memory'): + """ + + :param mode: memory or btp + :return: + """ self.app = create_app(TestConfig) - # force db uri to sqlite memory + self.mode = mode + # Set uri based on called mode + # + if self.mode == 'memory': + # force db uri to sqlite memory + uri = 'sqlite:///:memory:' + elif self.mode == 'btp': + self.db_path = resources_to_instancedb(self.app) + # force db path to newly create file + uri = 'sqlite:///' + self.db_path self.app.config.update( - SQLALCHEMY_DATABASE_URI='sqlite:///:memory:' + SQLALCHEMY_DATABASE_URI=uri ) + # Push context + # self.app_context = self.app.app_context() self.app_context.push() - db.create_all() - feed_projects() + # Create and feed db if empty + # + if self.mode == 'memory': + db.create_all() + feed_projects() def tearDown(self): - db.session.remove() - db.drop_all() + if self.mode == 'memory': + db.session.remove() + db.drop_all() + elif self.mode == 'btp': + if os.path.isfile(self.db_path): + os.remove(self.db_path) self.app_context.pop() - def test_first(self): + +class ChargeModelTestCase(DbBaseTestCase): + def setUp(self): + DbBaseTestCase.setUp(self, 'btp') + + def tearDown(self): + DbBaseTestCase.tearDown(self) + + def test_btp(self): projects = Project.query.all() - self.assertEqual(3, len(projects)) + self.assertEqual(101, len(projects)) class AuthModelTestCase(DbBaseTestCase): -- libgit2 0.21.2