Blame view

tests/db_tests.py 2.08 KB
a2074730   hitier   Feed projects labels
1
2
import unittest

f1bb8c76   hitier   Move auth tests t...
3
from app import create_app, db, User
a2074730   hitier   Feed projects labels
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from app.models import Project
from pdc_config import TestConfig
from tests.common_db_feed import feed_projects


class DbBaseTestCase(unittest.TestCase):
    def setUp(self):
        self.app = create_app(TestConfig)
        # force db uri to sqlite memory
        self.app.config.update(
            SQLALCHEMY_DATABASE_URI='sqlite:///:memory:'
        )
        self.app_context = self.app.app_context()
        self.app_context.push()
        db.create_all()
        feed_projects()

    def tearDown(self):
        db.session.remove()
        db.drop_all()
        self.app_context.pop()

    def test_first(self):
        projects = Project.query.all()
        self.assertEqual(3, len(projects))
f1bb8c76   hitier   Move auth tests t...
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71


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