cli_tests.py 1.73 KB
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))