Changeset - 6f08138ba931
[Not reviewed]
0 1 2
Lance Edgar (lance) - 4 months ago 2024-07-04 11:39:29
lance@edbob.org
fix: avoid command line errors if sqlalchemy not installed

some commands will not work without sqlalchemy of course, but some
still need to (e.g. backup)
3 files changed with 83 insertions and 8 deletions:
0 comments (0 inline, 0 general)
rattail/commands/typer.py
Show inline comments
 
@@ -36,7 +36,6 @@ from typing_extensions import Annotated
 
from wuttjamaican.util import load_entry_points
 

	
 
from rattail.config import make_config
 
from rattail.db.config import configure_versioning
 
from rattail.progress import ConsoleProgress, SocketProgress
 

	
 

	
 
@@ -199,19 +198,25 @@ def make_cli_config(ctx):
 
    kwargs = ctx.params
 

	
 
    # if args say not to "init" then we make a sort of empty config
 
    if kwargs['no_init']:
 
    if kwargs.get('no_init'):
 
        config = make_config([], extend=False, versioning=False)
 

	
 
    else: # otherwise we make a proper config, and maybe turn on versioning
 
        logging.basicConfig()
 
        config = make_config(files=kwargs['config_paths'] or None,
 
                             plus_files=kwargs['plus_config_paths'] or None,
 
                             extend=not kwargs['no_extend_config'],
 
        config = make_config(files=kwargs.get('config_paths') or None,
 
                             plus_files=kwargs.get('plus_config_paths') or None,
 
                             extend=not kwargs.get('no_extend_config'),
 
                             versioning=False)
 
        if kwargs['versioning']:
 
        if kwargs.get('versioning'):
 
            from rattail.db.config import configure_versioning
 
            configure_versioning(config, force=True)
 
        elif not kwargs['no_versioning']:
 
            configure_versioning(config)
 
        elif not kwargs.get('no_versioning'):
 
            try:
 
                from rattail.db.config import configure_versioning
 
            except ImportError:
 
                pass
 
            else:
 
                configure_versioning(config)
 

	
 
    # import our primary data model now, just in case it hasn't fully been
 
    # imported yet.  this it to be sure association proxies and the like
tests/commands/__init__.py
Show inline comments
 
new file 100644
tests/commands/test_typer.py
Show inline comments
 
new file 100644
 
# -*- coding: utf-8; -*-
 

	
 
from unittest import TestCase
 
from unittest.mock import MagicMock, patch
 

	
 
from wuttjamaican.testing import FileConfigTestCase
 

	
 
from rattail.commands import typer as mod
 

	
 

	
 
class TestMakeCliConfig(FileConfigTestCase):
 

	
 
    def test_basic(self):
 
        # nb. must specify config file to avoid any files in dev environment
 
        myconf = self.write_file('my.conf', '')
 
        ctx = MagicMock(params={
 
            'config_paths': [myconf],
 
        })
 
        config = mod.make_cli_config(ctx)
 
        self.assertEqual(config.files_read, [myconf])
 

	
 
    def test_no_init(self):
 
        ctx = MagicMock(params={
 
            'no_init': True,
 
        })
 
        config = mod.make_cli_config(ctx)
 
        self.assertEqual(config.files_read, [])
 

	
 
    def test_bad_model(self):
 
        myconf = self.write_file('my.conf', """
 
[rattail]
 
model = invalid_model_spec
 
""")
 
        ctx = MagicMock(params={
 
            'config_paths': [myconf],
 
        })
 
        config = mod.make_cli_config(ctx)
 
        self.assertRaises(ImportError, config.get_model)
 

	
 
    def test_versioning(self):
 
        # nb. must specify config file to avoid any files in dev environment
 
        myconf = self.write_file('my.conf', '')
 
        ctx = MagicMock(params={
 
            'config_paths': [myconf],
 
            'versioning': True,
 
        })
 

	
 
        configure_versioning = MagicMock()
 
        mock_config = MagicMock(configure_versioning=configure_versioning)
 
        with patch.dict('sys.modules', **{'rattail.db.config': mock_config}):
 
            config = mod.make_cli_config(ctx)
 
            configure_versioning.assert_called_once()
 

	
 
    def test_missing_sqlalchemy(self):
 
        # nb. must specify config file to avoid any files in dev environment
 
        myconf = self.write_file('my.conf', '')
 
        ctx = MagicMock(params={
 
            'config_paths': [myconf],
 
        })
 

	
 
        orig_import = __import__
 

	
 
        def mock_import(name, globals=None, locals=None, fromlist={}, level=0):
 
            if name == 'rattail.db.config' and fromlist == ('configure_versioning',):
 
                return ImportError
 
            return orig_import(name, globals=globals, locals=locals, fromlist=fromlist, level=level)
 

	
 
        with patch('builtins.__import__', side_effect=mock_import):
 
            # nb. just make sure there are no errors
 
            config = mod.make_cli_config(ctx)
0 comments (0 inline, 0 general)