From 2799d54f1601a82513689edb7d3c606745042a51 Mon Sep 17 00:00:00 2001 From: Christoph Buchner Date: Mon, 30 Nov 2015 20:40:08 +0100 Subject: [PATCH 1/3] Add more tests to raise test coverage. --- .gitignore | 2 ++ gooey/tests/argparse_to_json_unittest.py | 17 +++++++++-------- gooey/tests/config_generator_unittest.py | 20 ++++++++++++++++++++ 3 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 gooey/tests/config_generator_unittest.py diff --git a/.gitignore b/.gitignore index 917675d..908095e 100644 --- a/.gitignore +++ b/.gitignore @@ -23,8 +23,10 @@ pip-log.txt # Unit test / coverage reports .coverage +htmlcov .tox nosetests.xml +.cache # Translations *.mo diff --git a/gooey/tests/argparse_to_json_unittest.py b/gooey/tests/argparse_to_json_unittest.py index 71be3b6..9c6332d 100644 --- a/gooey/tests/argparse_to_json_unittest.py +++ b/gooey/tests/argparse_to_json_unittest.py @@ -158,6 +158,15 @@ def test_mutually(exclusive_group): assert target_arg.dest == data['display_name'] +def test_empty_mutex_group(): + assert not build_radio_group(None) + + +def test_as_json_invalid_widget(): + with pytest.raises(UnknownWidgetType): + as_json(None, 'InvalidWidget', None) + + def get_action(parser, dest): for action in parser._actions: if action.dest == dest: @@ -168,11 +177,3 @@ def find_arg_by_option(group, option_string): for arg in group: if option_string in arg.option_strings: return arg - - - - - - - - diff --git a/gooey/tests/config_generator_unittest.py b/gooey/tests/config_generator_unittest.py new file mode 100644 index 0000000..887de9f --- /dev/null +++ b/gooey/tests/config_generator_unittest.py @@ -0,0 +1,20 @@ +import argparse +import pytest +from gooey.python_bindings.config_generator import * + +# TODO: duplicated from argparse_to_json_unittest, should go into conftest.py +@pytest.fixture +def empty_parser(): + return argparse.ArgumentParser(description='description') + + +def test_create_from_parser(empty_parser): + build_spec = create_from_parser(empty_parser,'.') + assert build_spec['manual_start'] == True + + +def test_create_from_parser_show_config(empty_parser): + build_spec = create_from_parser(empty_parser, + '.', + show_config=True) + assert build_spec['program_description'] == 'description' From cc2c095de859967df59b50cc34b4f9d5f0e31f34 Mon Sep 17 00:00:00 2001 From: Christoph Buchner Date: Mon, 30 Nov 2015 20:40:52 +0100 Subject: [PATCH 2/3] Remove broken function. config_generator.has_argparse could not work as `f` does not exist in scope. Furthermore, I suspect it is probably a duplication accident, as it's very similar to source_parser.has_argparse, which is the only one used in the codebase. --- gooey/python_bindings/config_generator.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/gooey/python_bindings/config_generator.py b/gooey/python_bindings/config_generator.py index 2f9147f..23fae2f 100644 --- a/gooey/python_bindings/config_generator.py +++ b/gooey/python_bindings/config_generator.py @@ -41,8 +41,3 @@ def create_from_parser(parser, source_path, **kwargs): build_spec['manual_start'] = True return build_spec - - - -def has_argparse(module_path): - return any(['.parse_args(' in line.lower() for line in f.readlines()]) From 84fa92a16005417c01a246ff1e56991dbf93fa1a Mon Sep 17 00:00:00 2001 From: Christoph Buchner Date: Tue, 1 Dec 2015 07:38:03 +0100 Subject: [PATCH 3/3] Move pytest fixtures into conftest.py --- gooey/tests/argparse_to_json_unittest.py | 60 --------------------- gooey/tests/config_generator_unittest.py | 7 --- gooey/tests/conftest.py | 67 ++++++++++++++++++++++++ gooey/tests/image_repositoy_unittest.py | 8 --- 4 files changed, 67 insertions(+), 75 deletions(-) create mode 100644 gooey/tests/conftest.py diff --git a/gooey/tests/argparse_to_json_unittest.py b/gooey/tests/argparse_to_json_unittest.py index 9c6332d..387aa72 100644 --- a/gooey/tests/argparse_to_json_unittest.py +++ b/gooey/tests/argparse_to_json_unittest.py @@ -2,66 +2,6 @@ import pytest from gooey.python_bindings.argparse_to_json import * -@pytest.fixture -def empty_parser(): - return argparse.ArgumentParser(description='description') - -@pytest.fixture -def complete_parser(): - parser = argparse.ArgumentParser(description='description') - parser.add_argument("req1", help='filename help msg') # positional - parser.add_argument("req2", help="Name of the file where you'll save the output") # positional - parser.add_argument('-r', dest="req3", default=10, type=int, help='sets the time to count down from', required=True) - parser.add_argument('--req4', dest="req4", default=10, type=int, help='sets the time to count down from', required=True) - - parser.add_argument("-a", "--aa", action="store_true", help="aaa") - parser.add_argument("-b", "--bb", action="store_true", help="bbb") - parser.add_argument('-c', '--cc', action='count') - parser.add_argument("-d", "--dd", action="store_true", help="ddd") - parser.add_argument('-e', '--ee', choices=['yes', 'no'], help='eee') - parser.add_argument("-f", "--ff", default="0000", help="fff") - parser.add_argument("-g", "--gg", action="store_true", help="ggg") - verbosity = parser.add_mutually_exclusive_group() - verbosity.add_argument('-i', '--ii', action="store_true", help="iii") - verbosity.add_argument('-j', '--jj', action="store_true", help="hhh") - return parser - -@pytest.fixture -def subparser(): - parser = argparse.ArgumentParser(description='qidev') - parser.add_argument('--verbose', help='be verbose', dest='verbose', action='store_true', default=False) - subs = parser.add_subparsers(help='commands', dest='command') - - config_parser = subs.add_parser('config', help='configure defaults for qidev') - config_parser.add_argument('field', help='the field to configure', type=str) - config_parser.add_argument('value', help='set field to value', type=str) - - # ######################################################## - connect_parser = subs.add_parser('connect', help='connect to a robot (ip/hostname)') - connect_parser.add_argument('hostname', help='hostname or IP address of the robot', type=str) - - # ######################################################## - install_parser = subs.add_parser('install', help='package and install a project directory on a robot') - install_parser.add_argument('path', help='path to the project directory (containing manifest.xml', type=str) - install_parser.add_argument('--ip', nargs='*', type=str, dest='ip', help='specify hostname(es)/IP address(es)') - return parser - - -@pytest.fixture -def exclusive_group(): - parser = argparse.ArgumentParser(description='description') - verbosity = parser.add_mutually_exclusive_group() - verbosity.add_argument('-i', dest="option1", action="store_true", help="iii") - verbosity.add_argument('-j', dest="option2", action="store_true", help="hhh") - - mutually_exclusive_group = [mutex_action - for group_actions in parser._mutually_exclusive_groups - for mutex_action in group_actions._group_actions] - return mutually_exclusive_group - - - - def test_parser_converts_to_correct_type(empty_parser, complete_parser, subparser): assert convert(subparser)['layout_type'] == 'column' assert convert(empty_parser)['layout_type'] == 'standard' diff --git a/gooey/tests/config_generator_unittest.py b/gooey/tests/config_generator_unittest.py index 887de9f..2003e8f 100644 --- a/gooey/tests/config_generator_unittest.py +++ b/gooey/tests/config_generator_unittest.py @@ -1,12 +1,5 @@ -import argparse -import pytest from gooey.python_bindings.config_generator import * -# TODO: duplicated from argparse_to_json_unittest, should go into conftest.py -@pytest.fixture -def empty_parser(): - return argparse.ArgumentParser(description='description') - def test_create_from_parser(empty_parser): build_spec = create_from_parser(empty_parser,'.') diff --git a/gooey/tests/conftest.py b/gooey/tests/conftest.py new file mode 100644 index 0000000..88782a3 --- /dev/null +++ b/gooey/tests/conftest.py @@ -0,0 +1,67 @@ +import argparse +import pytest + +@pytest.fixture +def empty_parser(): + return argparse.ArgumentParser(description='description') + + +@pytest.fixture +def complete_parser(): + parser = argparse.ArgumentParser(description='description') + parser.add_argument("req1", help='filename help msg') # positional + parser.add_argument("req2", help="Name of the file where you'll save the output") # positional + parser.add_argument('-r', dest="req3", default=10, type=int, help='sets the time to count down from', required=True) + parser.add_argument('--req4', dest="req4", default=10, type=int, help='sets the time to count down from', required=True) + + parser.add_argument("-a", "--aa", action="store_true", help="aaa") + parser.add_argument("-b", "--bb", action="store_true", help="bbb") + parser.add_argument('-c', '--cc', action='count') + parser.add_argument("-d", "--dd", action="store_true", help="ddd") + parser.add_argument('-e', '--ee', choices=['yes', 'no'], help='eee') + parser.add_argument("-f", "--ff", default="0000", help="fff") + parser.add_argument("-g", "--gg", action="store_true", help="ggg") + verbosity = parser.add_mutually_exclusive_group() + verbosity.add_argument('-i', '--ii', action="store_true", help="iii") + verbosity.add_argument('-j', '--jj', action="store_true", help="hhh") + return parser + + +@pytest.fixture +def subparser(): + parser = argparse.ArgumentParser(description='qidev') + parser.add_argument('--verbose', help='be verbose', dest='verbose', action='store_true', default=False) + subs = parser.add_subparsers(help='commands', dest='command') + + config_parser = subs.add_parser('config', help='configure defaults for qidev') + config_parser.add_argument('field', help='the field to configure', type=str) + config_parser.add_argument('value', help='set field to value', type=str) + + # ######################################################## + connect_parser = subs.add_parser('connect', help='connect to a robot (ip/hostname)') + connect_parser.add_argument('hostname', help='hostname or IP address of the robot', type=str) + + # ######################################################## + install_parser = subs.add_parser('install', help='package and install a project directory on a robot') + install_parser.add_argument('path', help='path to the project directory (containing manifest.xml', type=str) + install_parser.add_argument('--ip', nargs='*', type=str, dest='ip', help='specify hostname(es)/IP address(es)') + return parser + + +@pytest.fixture +def exclusive_group(): + parser = argparse.ArgumentParser(description='description') + verbosity = parser.add_mutually_exclusive_group() + verbosity.add_argument('-i', dest="option1", action="store_true", help="iii") + verbosity.add_argument('-j', dest="option2", action="store_true", help="hhh") + + mutually_exclusive_group = [mutex_action + for group_actions in parser._mutually_exclusive_groups + for mutex_action in group_actions._group_actions] + return mutually_exclusive_group + + +@pytest.fixture +def expected_attrs(): + return ('program_icon', 'success_icon', 'running_icon', + 'loading_icon', 'config_icon', 'error_icon') diff --git a/gooey/tests/image_repositoy_unittest.py b/gooey/tests/image_repositoy_unittest.py index 746e8a6..ba764bf 100644 --- a/gooey/tests/image_repositoy_unittest.py +++ b/gooey/tests/image_repositoy_unittest.py @@ -6,12 +6,6 @@ import pytest import tempfile -@pytest.fixture -def expected_attrs(): - return ('program_icon', 'success_icon', 'running_icon', - 'loading_icon', 'config_icon', 'error_icon') - - def test_variable_names_are_pushed_to_module_scope(expected_attrs): ''' The dynamically initialized Globals() should contain the expected images at runtime @@ -62,5 +56,3 @@ def make_user_files(*filenames): def cleanup_temp(*filenames): for filename in filenames: os.remove(os.path.join(tempfile.gettempdir(), filename)) - -