Blame view

tests/backend_tests.py 2.91 KB
f079f974   hitier   Backend tests get...
1
import os
506d2f02   hitier   Initialize unit t...
2
import unittest
8e2f6111   hitier   Update tests with...
3
from pprint import pprint
b48d8e69   hitier   Changed charges_b...
4

506d2f02   hitier   Initialize unit t...
5
from pdc_config import TestConfig
b6c0e3c1   hitier   Update resource's...
6
from app import create_app, db_mgr
a84fcae0   hitier   Extract code to c...
7
from tests.common_db_feed import resources_to_instancedb
506d2f02   hitier   Initialize unit t...
8
9


506d2f02   hitier   Initialize unit t...
10
11
class BaseTestCase(unittest.TestCase):
    def setUp(self):
f079f974   hitier   Backend tests get...
12
        # initialise app
506d2f02   hitier   Initialize unit t...
13
        self.app = create_app(TestConfig)
a84fcae0   hitier   Extract code to c...
14
        self.db_path = resources_to_instancedb(self.app)
f079f974   hitier   Backend tests get...
15
16
17
18
19
20
21

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

        # update flask context
506d2f02   hitier   Initialize unit t...
22
23
        self.app_context = self.app.app_context()
        self.app_context.push()
506d2f02   hitier   Initialize unit t...
24
25

    def tearDown(self):
f079f974   hitier   Backend tests get...
26
27
        if os.path.isfile(self.db_path):
            os.remove(self.db_path)
506d2f02   hitier   Initialize unit t...
28
29
30
31
        self.app_context.pop()

    def test_always_true(self):
        self.assertTrue(True)
1aeda847   hitier   Two db request wr...
32
33
34
35


class DbMgrTestCase(BaseTestCase):

b6c0e3c1   hitier   Update resource's...
36
37
38
39
40
41
42
43
    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]))

8e2f6111   hitier   Update tests with...
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    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]))

1aeda847   hitier   Two db request wr...
60
61
    def test_agents(self):
        all_agents = db_mgr.agents()
1aeda847   hitier   Two db request wr...
62
63
64
65
        self.assertEqual(548, len(all_agents))

    def test_charges_by_agent(self):
        all_charges = db_mgr.charges_by_agent(355)
9c7c1dfa   hitier   Fix test with new...
66
        self.assertEqual(17, len(all_charges))
7b74daf6   hitier   New User role man...
67

b48d8e69   hitier   Changed charges_b...
68
69
70
71
72
73
74
75
76
    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))
1b42d082   hitier   New category_labe...
77

95880b0d   hitier   New charges_for_p...
78
79
80
81
82
83
    def test_charges_for_projects_stacked(self):
        stacked_charges = db_mgr.charges_for_projects_stacked()
        # Waiting for 17 periods + headers line
        self.assertEqual(18, len(stacked_charges))
        self.assertEqual(102, len(stacked_charges[0]))

1b42d082   hitier   New category_labe...
84
85
86
87
    def test_category_labels(self):
        category_labels = db_mgr.category_labels()
        categories = [_cl['name'] for _cl in category_labels]
        self.assertEqual(['Domaine', 'Pôle'], categories)