Blame view

tests/cli_tests.py 1.73 KB
2a06e4b6   hitier   New cli unit tests
1
2
3
import unittest

from app import create_app, db, User
3262868b   hitier   New Cli tests
4
from app.commands.commands import feed_from_irap, show_roles, user_add, user_show_all, create_db
2a06e4b6   hitier   New cli unit tests
5
6
7
8
9
10
11
12
from pdc_config import TestConfig

from app.commands import bp as cli_bp


class CliBaseTestCase(unittest.TestCase):

    def setUp(self):
3262868b   hitier   New Cli tests
13
14
        # Get rid of a strange 'ValueError: I/O operation' when pytest catches app.logger outputs
        # Error can also be bypassed with 'pytest -s' option
2a06e4b6   hitier   New cli unit tests
15
16
17
18
19
20
21
22
23
24
25
26
27
        TestConfig.PDC_LOGS_LEVEL = 'ERROR'
        # Force sqlite in memory db
        TestConfig.SQLALCHEMY_DATABASE_URI = 'sqlite:///:memory:'
        # Create and register app
        self.app = create_app(TestConfig)
        self.app.register_blueprint(cli_bp)
        self.app_context = self.app.app_context()
        self.app_context.push()
        db.create_all()

    def tearDown(self):
        pass

3262868b   hitier   New Cli tests
28
29
30
31
32
    def test_create_db(self):
        runner = self.app.test_cli_runner()
        result = runner.invoke(create_db)
        self.assertTrue('Created sqlite' in result.output)

61bd8b00   hitier   Rename cli tests
33
    def test_show_all(self):
2a06e4b6   hitier   New cli unit tests
34
35
        runner = self.app.test_cli_runner()
        result = runner.invoke(user_show_all)
61bd8b00   hitier   Rename cli tests
36
        self.assertTrue('id' in result.output)
2a06e4b6   hitier   New cli unit tests
37

61bd8b00   hitier   Rename cli tests
38
    def test_show_roles(self):
2a06e4b6   hitier   New cli unit tests
39
40
        runner = self.app.test_cli_runner()
        result = runner.invoke(show_roles)
61bd8b00   hitier   Rename cli tests
41
        self.assertTrue('PUBLIC' in result.output)
2a06e4b6   hitier   New cli unit tests
42
43
44
45
46
47
48

    def test_add_user(self):
        runner = self.app.test_cli_runner()

        all_users = User.query.all()
        self.assertEqual(0, len(all_users))
        # invoke the command directly
61bd8b00   hitier   Rename cli tests
49
50
        arguments = ['geo@ici.fr', 'Géo Trouvetou', 'gt', 'passwd', 'PUBLIC']
        runner.invoke(user_add, arguments)
2a06e4b6   hitier   New cli unit tests
51
        all_users = User.query.all()
61bd8b00   hitier   Rename cli tests
52
        self.assertEqual(1, len(all_users))