Blame view

tests/backend_tests.py 2.83 KB
506d2f02   hitier   Initialize unit t...
1
import unittest
b48d8e69   hitier   Changed charges_b...
2

506d2f02   hitier   Initialize unit t...
3
from pdc_config import TestConfig
7b74daf6   hitier   New User role man...
4
5
from app import create_app, db_mgr, db
from app.auth.models import User
506d2f02   hitier   Initialize unit t...
6
7


506d2f02   hitier   Initialize unit t...
8
9
10
class BaseTestCase(unittest.TestCase):
    def setUp(self):
        # configure data base
506d2f02   hitier   Initialize unit t...
11
12
13
        self.app = create_app(TestConfig)
        self.app_context = self.app.app_context()
        self.app_context.push()
5b7c87fe   hitier   Run auth tests on...
14
15
        # TODO: shall we always copy db from resources sqlite file ?
        #       that would allow us to run all tests in sqlite:memory:
b48d8e69   hitier   Changed charges_b...
16
17
18
19
        # db.create_all()
        # admin = User(email='admin@nowhere.org', name='admin', login='admin', password='admin', role='admin')
        # db.session.add(admin)
        # db.session.commit()
506d2f02   hitier   Initialize unit t...
20
21
22
23
24
25

    def tearDown(self):
        self.app_context.pop()

    def test_always_true(self):
        self.assertTrue(True)
1aeda847   hitier   Two db request wr...
26
27
28
29
30
31
32
33
34
35
36


class DbMgrTestCase(BaseTestCase):

    def test_agents(self):
        all_agents = db_mgr.agents()
        print(len(all_agents))
        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...
37
        self.assertEqual(17, len(all_charges))
7b74daf6   hitier   New User role man...
38

b48d8e69   hitier   Changed charges_b...
39
40
41
42
43
44
45
46
47
48
    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))

7b74daf6   hitier   New User role man...
49
50

class AuthModelTestCase(BaseTestCase):
e8879e9f   hitier   New get_admin tin...
51

5b7c87fe   hitier   Run auth tests on...
52
53
54
55
    def skip_if_no_sqlitememory(self):
        if 'memory' not in self.app.config['SQLALCHEMY_DATABASE_URI']:
            self.skipTest("Needs in memory sqlite")

e8879e9f   hitier   New get_admin tin...
56
57
58
    def get_admin(self):
        return User.query.filter(User.name == 'admin').one()

5b7c87fe   hitier   Run auth tests on...
59
60
61
62
63
64
65
66
67
68
69
    def setUp(self):
        BaseTestCase.setUp(self)
        self.skip_if_no_sqlitememory()
        db.create_all()
        admin = User(email='admin@nowhere.org', name='admin', login='admin', role='admin')
        db.session.add(admin)
        db.session.commit()

    def test_in_memory(self):
        self.app.logger.info("In memory Sqlite DB for tests")
        self.assertTrue(True)
7b74daf6   hitier   New User role man...
70
71

    def test_setrole(self):
e8879e9f   hitier   New get_admin tin...
72
        admin = self.get_admin()
42c07429   hitier   Default role valu...
73
        admin.set_role("ADMIN")
7b74daf6   hitier   New User role man...
74
        db.session.commit()
e8879e9f   hitier   New get_admin tin...
75
        admin = self.get_admin()
7b74daf6   hitier   New User role man...
76
        self.assertTrue(admin is not None)
42c07429   hitier   Default role valu...
77
78
        self.assertTrue(admin.has_role("ADMIN"))
        self.assertFalse(admin.has_role("SERVICE"))
7b74daf6   hitier   New User role man...
79
80

    def test_setrole_valueerror(self):
e8879e9f   hitier   New get_admin tin...
81
        admin = self.get_admin()
7b74daf6   hitier   New User role man...
82
        with self.assertRaises(ValueError) as ve:
42c07429   hitier   Default role valu...
83
            admin.set_role("NOSUCHROLE")
4770ee5d   hitier   Now passwords are...
84
85
86
87
88
89
90

    def test_setcheckpassword(self):
        admin = self.get_admin()
        admin.set_password("hahaha")
        db.session.commit()
        admin2 = self.get_admin()
        self.assertTrue(admin2.check_password("hahaha"))