backend_tests.py 2.44 KB
import os
import unittest
from pprint import pprint

from pdc_config import TestConfig
from app import create_app, db_mgr
from tests.common_db_feed import resources_to_instancedb


class BaseTestCase(unittest.TestCase):
    def setUp(self):
        # initialise app
        self.app = create_app(TestConfig)
        self.db_path = resources_to_instancedb(self.app)

        # force db path to newly create file
        self.app.config.update(
            SQLALCHEMY_DATABASE_URI='sqlite:///' + self.db_path
        )

        # update flask context
        self.app_context = self.app.app_context()
        self.app_context.push()

    def tearDown(self):
        if os.path.isfile(self.db_path):
            os.remove(self.db_path)
        self.app_context.pop()

    def test_always_true(self):
        self.assertTrue(True)


class DbMgrTestCase(BaseTestCase):

    def test_projects(self):
        all_projects = db_mgr.projects()
        self.assertEqual(102, len(all_projects))

    def test_projects_columns(self):
        all_projects = db_mgr.projects()
        self.assertEqual(5, len(all_projects[0]))

    def test_projects_all_columns(self):
        all_projects = db_mgr.projects()
        for p in all_projects:
            with self.subTest(p):
                self.assertEqual(5, len(p), f"Failed on {p[1]}")

    def test_projects_labels(self):
        self.assertEqual(0, 0)
        all_projects = db_mgr.projects()
        # test all projects but skip headers first row
        for p in all_projects[1:]:
            with self.subTest(p):
                # the number of labels for each category and any project should be less than 2
                self.assertTrue(len(p[2]) <= 2, f"Failed on {p[1]}: " + ", ".join(p[2]))
                self.assertTrue(len(p[3]) <= 2, f"Failed on {p[1]}: " + ", ".join(p[3]))

    def test_agents(self):
        all_agents = db_mgr.agents()
        self.assertEqual(548, len(all_agents))

    def test_charges_by_agent(self):
        all_charges = db_mgr.charges_by_agent(355)
        self.assertEqual(17, len(all_charges))

    def test_charges_by_agent_stacked(self):
        stacked_charges = db_mgr.charges_by_agent_stacked(60)
        # Waiting for 17 periods + headers line
        self.assertEqual(18, len(stacked_charges))

    def test_charges_by_project_stacked(self):
        stacked_charges = db_mgr.charges_by_project_stacked(60)
        # Waiting for 17 periods + headers line
        self.assertEqual(18, len(stacked_charges))