File tree Expand file tree Collapse file tree 2 files changed +42
-14
lines changed
Expand file tree Collapse file tree 2 files changed +42
-14
lines changed Original file line number Diff line number Diff line change 22
33from . import __version__
44
5- try :
6- import uvicorn # noqa
7- from fastapi import FastAPI # noqa
5+ __all__ = ["main" ]
86
9- server_dependencies_exist = True
10- except ImportError :
11- server_dependencies_exist = False
7+ INSUFFICIENT_DEPENDENCIES_MESSAGE = "To do anything other than print the version and be \
8+ available for importing the client, you must install this package with [server] \
9+ optional dependencies"
1210
1311
14- __all__ = ["main" ]
12+ def check_server_dependencies ():
13+ try :
14+ import uvicorn # noqa
15+ from fastapi import FastAPI # noqa
16+
17+ server_dependencies_exist = True
18+
19+ except ImportError :
20+ server_dependencies_exist = False
21+
22+ return server_dependencies_exist
1523
1624
1725def main ():
1826 parser = ArgumentParser ()
1927 parser .add_argument ("-v" , "--version" , action = "version" , version = __version__ )
2028 parser .parse_args ()
2129
22- if not server_dependencies_exist :
23- print (
24- "To do anything other than print the version and be available for "
25- "importing the client, you must install this package with [server] "
26- "optional dependencies"
27- )
30+ if not check_server_dependencies ():
31+ print (INSUFFICIENT_DEPENDENCIES_MESSAGE )
2832 else :
2933 from .app import main
3034
Original file line number Diff line number Diff line change 11import subprocess
22import sys
3+ from unittest .mock import MagicMock , patch
34
4- from daq_config_server import __version__
5+ from daq_config_server .__main__ import (
6+ INSUFFICIENT_DEPENDENCIES_MESSAGE ,
7+ __version__ ,
8+ main ,
9+ )
510
611
712def test_cli_version ():
813 cmd = [sys .executable , "-m" , "daq_config_server" , "--version" ]
914 assert subprocess .check_output (cmd ).decode ().strip () == __version__
15+
16+
17+ @patch ("daq_config_server.app.main" )
18+ @patch ("daq_config_server.__main__.print" )
19+ @patch ("daq_config_server.__main__.ArgumentParser.parse_args" )
20+ @patch .dict ("sys.modules" , {"uvicorn" : None , "fastapi" : None })
21+ def test_print_and_exit_if_incorrect_dependencies (
22+ mock_parse_args , mock_print : MagicMock , mock_main : MagicMock
23+ ):
24+ main ()
25+ mock_print .assert_called_once_with (INSUFFICIENT_DEPENDENCIES_MESSAGE )
26+ mock_main .assert_not_called ()
27+
28+
29+ @patch ("daq_config_server.app.main" )
30+ @patch ("daq_config_server.__main__.ArgumentParser.parse_args" )
31+ def test_main_runs_with_correct_dependencies (mock_parse_args , mock_main : MagicMock ):
32+ main ()
33+ mock_main .assert_called_once ()
You can’t perform that action at this time.
0 commit comments