Blame view

tests/db_tests.py 3.55 KB
7bca61b9   hitier   New models tests
1
import os
a2074730   hitier   Feed projects labels
2
import unittest
8e2f6111   hitier   Update tests with...
3
from pprint import pprint
a2074730   hitier   Feed projects labels
4

f1bb8c76   hitier   Move auth tests t...
5
from app import create_app, db, User
a2074730   hitier   Feed projects labels
6
7
from app.models import Project
from pdc_config import TestConfig
8e2f6111   hitier   Update tests with...
8
from tests.common_db_feed import resources_to_instancedb
a2074730   hitier   Feed projects labels
9
10
11


class DbBaseTestCase(unittest.TestCase):
7bca61b9   hitier   New models tests
12
13
14
15
16
17
    def setUp(self, mode='memory'):
        """

        :param mode:  memory or btp
        :return:
        """
a2074730   hitier   Feed projects labels
18
        self.app = create_app(TestConfig)
7bca61b9   hitier   New models tests
19
20
21
22
23
24
25
26
27
28
        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
a2074730   hitier   Feed projects labels
29
        self.app.config.update(
7bca61b9   hitier   New models tests
30
            SQLALCHEMY_DATABASE_URI=uri
a2074730   hitier   Feed projects labels
31
        )
7bca61b9   hitier   New models tests
32
33
        # Push context
        #
a2074730   hitier   Feed projects labels
34
35
        self.app_context = self.app.app_context()
        self.app_context.push()
7bca61b9   hitier   New models tests
36
37
38
39
        # Create and feed db if empty
        #
        if self.mode == 'memory':
            db.create_all()
8e2f6111   hitier   Update tests with...
40
41
            # TODO: to be rewriten for new category/label model
            # feed_projects()
a2074730   hitier   Feed projects labels
42
43

    def tearDown(self):
7bca61b9   hitier   New models tests
44
45
46
47
48
49
        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)
a2074730   hitier   Feed projects labels
50
51
        self.app_context.pop()

7bca61b9   hitier   New models tests
52

8e2f6111   hitier   Update tests with...
53
54
55
56
57
58
59
60
61
62
class AnyModelTestCase(DbBaseTestCase):
    def setUp(self):
        DbBaseTestCase.setUp(self, 'btp')

    def tearDown(self):
        DbBaseTestCase.tearDown(self)

    def test_projects_struct(self):
        for project in Project.query.all():
            with self.subTest(project):
9b43874a   hitier   Change project_st...
63
                categories = list(project.to_struct()['category_labels'].keys())
8e2f6111   hitier   Update tests with...
64
65
66
67
                self.assertEqual(['Domaine', 'Pôle'], categories,
                                 f"Failed on {project.name} categories: {categories}")


7bca61b9   hitier   New models tests
68
69
70
71
72
73
74
75
class ChargeModelTestCase(DbBaseTestCase):
    def setUp(self):
        DbBaseTestCase.setUp(self, 'btp')

    def tearDown(self):
        DbBaseTestCase.tearDown(self)

    def test_btp(self):
a2074730   hitier   Feed projects labels
76
        projects = Project.query.all()
7bca61b9   hitier   New models tests
77
        self.assertEqual(101, len(projects))
f1bb8c76   hitier   Move auth tests t...
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120


class AuthModelTestCase(DbBaseTestCase):

    def skip_if_no_sqlitememory(self):
        if 'memory' not in self.app.config['SQLALCHEMY_DATABASE_URI']:
            self.skipTest("Needs in memory sqlite")

    def get_admin(self):
        return User.query.filter(User.name == 'admin').one()

    def setUp(self):
        DbBaseTestCase.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)

    def test_setrole(self):
        admin = self.get_admin()
        admin.set_role("ADMIN")
        db.session.commit()
        admin = self.get_admin()
        self.assertTrue(admin is not None)
        self.assertTrue(admin.has_role("ADMIN"))
        self.assertFalse(admin.has_role("SERVICE"))

    def test_setrole_valueerror(self):
        admin = self.get_admin()
        with self.assertRaises(ValueError) as ve:
            admin.set_role("NOSUCHROLE")

    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"))