cli_tests.py
1.73 KB
1
2
3
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import unittest
from app import create_app, db, User
from app.commands.commands import feed_from_irap, show_roles, user_add, user_show_all, create_db
from pdc_config import TestConfig
from app.commands import bp as cli_bp
class CliBaseTestCase(unittest.TestCase):
def setUp(self):
# Get rid of a strange 'ValueError: I/O operation' when pytest catches app.logger outputs
# Error can also be bypassed with 'pytest -s' option
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
def test_create_db(self):
runner = self.app.test_cli_runner()
result = runner.invoke(create_db)
self.assertTrue('Created sqlite' in result.output)
def test_show_all(self):
runner = self.app.test_cli_runner()
result = runner.invoke(user_show_all)
self.assertTrue('id' in result.output)
def test_show_roles(self):
runner = self.app.test_cli_runner()
result = runner.invoke(show_roles)
self.assertTrue('PUBLIC' in result.output)
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
arguments = ['geo@ici.fr', 'Géo Trouvetou', 'gt', 'passwd', 'PUBLIC']
runner.invoke(user_add, arguments)
all_users = User.query.all()
self.assertEqual(1, len(all_users))