From 44e71f72b6f778c7001d5072359be0b2489562cb Mon Sep 17 00:00:00 2001 From: benedikt brunner <122370755+Benedikt-Brunner@users.noreply.github.com> Date: Wed, 27 Nov 2024 11:00:54 +0100 Subject: [PATCH] Enforce line length in linter and format code (#8) --- README.md | 2 +- napytau/cli/cli_arguments.py | 1 + napytau/cli/parser.py | 13 ++- napytau/gui/ui_mockup.py | 161 ++++++++++++++++++---------- napytau/headless/headless_mockup.py | 5 +- napytau/ingest/ingest_mockup.py | 2 +- napytau/main.py | 3 +- napytau/util/coalesce.py | 2 +- pyproject.toml | 5 +- ruff.toml | 78 ++++++++++++++ tests/cli/cli_arguments_test.py | 90 ++++++++-------- tests/cli/parser_test.py | 111 +++++++++++-------- tests/main_test.py | 45 +++++--- tests/util/coalesce_test.py | 25 +++-- 14 files changed, 363 insertions(+), 180 deletions(-) create mode 100644 ruff.toml diff --git a/README.md b/README.md index e949bf2..c5eb6b4 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Make sure you have the following installed: 1. Clone the repository 2. Create a virtual environment with `uv venv --python=$PythonVersion` where `$PythonVersion` is greater than 3.10. Note that there is a bug in Python 3.13 that leads to the core dependency TCL not being installed correctly on Windows. -3. Run `uv sync` +3. Run `uv sync --dev` To run the project use `uvx tomlscript run`, optionally you can use nodemon for hot reloading with the command `nodemon --exec uvx tomlscript run`. diff --git a/napytau/cli/cli_arguments.py b/napytau/cli/cli_arguments.py index 88e1dcf..bb0d6e4 100644 --- a/napytau/cli/cli_arguments.py +++ b/napytau/cli/cli_arguments.py @@ -1,6 +1,7 @@ from napytau.util.coalesce import coalesce from argparse import Namespace + class CLIArguments: def __init__(self, raw_args: Namespace): self.headless = coalesce(raw_args.headless, False) diff --git a/napytau/cli/parser.py b/napytau/cli/parser.py index 3200677..0176484 100644 --- a/napytau/cli/parser.py +++ b/napytau/cli/parser.py @@ -1,9 +1,16 @@ import argparse from napytau.cli.cli_arguments import CLIArguments + def parse_cli_arguments() -> CLIArguments: parser = argparse.ArgumentParser(description="Mockup for NaPyTau") - parser.add_argument("--headless", action="store_true", help="Run the application without GUI") - parser.add_argument("--filename", type=str, help="File to ingest (only for mockup, not used in real application)") + parser.add_argument( + "--headless", action="store_true", help="Run the application without GUI" + ) + parser.add_argument( + "--filename", + type=str, + help="File to ingest (only for mockup, not used in real application)", + ) - return CLIArguments(parser.parse_args()) \ No newline at end of file + return CLIArguments(parser.parse_args()) diff --git a/napytau/gui/ui_mockup.py b/napytau/gui/ui_mockup.py index 7651bc4..0509433 100644 --- a/napytau/gui/ui_mockup.py +++ b/napytau/gui/ui_mockup.py @@ -8,91 +8,125 @@ from napytau.cli.cli_arguments import CLIArguments from napytau.core.logic_mockup import logic -customtkinter.set_appearance_mode("System") # Modes: "System" (standard), "Dark", "Light" -customtkinter.set_default_color_theme("blue") # Themes: "blue" (standard), "green", "dark-blue" +customtkinter.set_appearance_mode( + "System" +) # Modes: "System" (standard), "Dark", "Light" +customtkinter.set_default_color_theme( + "blue" +) # Themes: "blue" (standard), "green", "dark-blue" def plot(value, window) -> Canvas: # the figure that will contain the plot - fig = Figure( - figsize=(3, 2), - dpi=100, - facecolor="white", - edgecolor="black" - ) - + fig = Figure(figsize=(3, 2), dpi=100, facecolor="white", edgecolor="black") + # list of squares y = [(i - 50) ** value for i in range(101)] - + # adding the subplot plot1 = fig.add_subplot(111) - + # plotting the graph plot1.plot(y) - + # creating the Tkinter canvas # containing the Matplotlib figure canvas = FigureCanvasTkAgg(fig, master=window) canvas.draw() - + return canvas.get_tk_widget() + class App(customtkinter.CTk): def __init__(self): super().__init__() - + # values - + self.tau = tk.IntVar() self.tau.set(2) - + # configure window self.title("NaPyTau") self.geometry(f"{1100}x{580}") - + # configure grid layout (4x4) self.grid_columnconfigure(3, weight=1) - self.grid_columnconfigure((1,2), weight=0) + self.grid_columnconfigure((1, 2), weight=0) self.grid_rowconfigure((0, 1, 2), weight=1) - + # create sidebar frame with widgets self.sidebar_frame = customtkinter.CTkFrame(self, width=140, corner_radius=0) self.sidebar_frame.grid(row=0, column=0, rowspan=4, sticky="nsew") self.sidebar_frame.grid_rowconfigure(4, weight=1) - self.logo_label = customtkinter.CTkLabel(self.sidebar_frame, text="NaPyTau", - font=customtkinter.CTkFont(size=20, weight="bold")) + self.logo_label = customtkinter.CTkLabel( + self.sidebar_frame, + text="NaPyTau", + font=customtkinter.CTkFont(size=20, weight="bold"), + ) self.logo_label.grid(row=0, column=0, padx=20, pady=(20, 10)) - self.sidebar_button_1 = customtkinter.CTkButton(self.sidebar_frame, command=self.sidebar_button_event) - self.appearance_mode_label = customtkinter.CTkLabel(self.sidebar_frame, text="Appearance Mode:", anchor="w") + self.sidebar_button_1 = customtkinter.CTkButton( + self.sidebar_frame, command=self.sidebar_button_event + ) + self.appearance_mode_label = customtkinter.CTkLabel( + self.sidebar_frame, text="Appearance Mode:", anchor="w" + ) self.appearance_mode_label.grid(row=5, column=0, padx=20, pady=(10, 0)) - self.appearance_mode_optionemenu = customtkinter.CTkOptionMenu(self.sidebar_frame, - values=["Light", "Dark", "System"], - command=self.change_appearance_mode_event) + self.appearance_mode_optionemenu = customtkinter.CTkOptionMenu( + self.sidebar_frame, + values=["Light", "Dark", "System"], + command=self.change_appearance_mode_event, + ) self.appearance_mode_optionemenu.grid(row=6, column=0, padx=20, pady=(10, 10)) - self.scaling_label = customtkinter.CTkLabel(self.sidebar_frame, text="UI Scaling:", anchor="w") + self.scaling_label = customtkinter.CTkLabel( + self.sidebar_frame, text="UI Scaling:", anchor="w" + ) self.scaling_label.grid(row=7, column=0, padx=20, pady=(10, 0)) - self.scaling_optionemenu = customtkinter.CTkOptionMenu(self.sidebar_frame, - values=["80%", "90%", "100%", "110%", "120%"], - command=self.change_scaling_event) + self.scaling_optionemenu = customtkinter.CTkOptionMenu( + self.sidebar_frame, + values=["80%", "90%", "100%", "110%", "120%"], + command=self.change_scaling_event, + ) self.scaling_optionemenu.grid(row=8, column=0, padx=20, pady=(10, 20)) - + # create main entry and button self.entry = customtkinter.CTkEntry(self, textvariable=self.tau) - self.entry.grid(row=3, column=1, columnspan=2, padx=(20, 0), pady=(20, 20), sticky="nsew") - - self.main_button_1 = customtkinter.CTkButton(master=self, fg_color="transparent", border_width=2, - text="calc", - text_color=("gray10", "#DCE4EE"), command=self.calc) - self.main_button_1.grid(row=3, column=3, padx=(20, 20), pady=(20, 20), sticky="nsew") - + self.entry.grid( + row=3, column=1, columnspan=2, padx=(20, 0), pady=(20, 20), sticky="nsew" + ) + + self.main_button_1 = customtkinter.CTkButton( + master=self, + fg_color="transparent", + border_width=2, + text="calc", + text_color=("gray10", "#DCE4EE"), + command=self.calc, + ) + self.main_button_1.grid( + row=3, column=3, padx=(20, 20), pady=(20, 20), sticky="nsew" + ) + # create line graph - + self.line_graph = plot(self.tau.get(), self) - self.line_graph.grid(row=2, column=3, columnspan=1, rowspan=1, padx=(0, 20), pady=(20, 0), sticky="nsew") - + self.line_graph.grid( + row=2, + column=3, + columnspan=1, + rowspan=1, + padx=(0, 20), + pady=(20, 0), + sticky="nsew", + ) + # create slider and progressbar frame - self.slider_progressbar_frame = customtkinter.CTkFrame(self, fg_color="transparent") - self.slider_progressbar_frame.grid(row=2, column=2, padx=(20, 0), pady=(20, 0), sticky="nsew") + self.slider_progressbar_frame = customtkinter.CTkFrame( + self, fg_color="transparent" + ) + self.slider_progressbar_frame.grid( + row=2, column=2, padx=(20, 0), pady=(20, 0), sticky="nsew" + ) self.slider_progressbar_frame.grid_columnconfigure(0, weight=1) self.slider_progressbar_frame.grid_rowconfigure(4, weight=1) self.slider_2 = customtkinter.CTkSlider( @@ -101,40 +135,51 @@ def __init__(self): from_=2, to=5, number_of_steps=3, - orientation="vertical") - self.slider_2.grid(row=0, column=1, rowspan=5, padx=(10, 10), pady=(10, 10), sticky="ns") - + orientation="vertical", + ) + self.slider_2.grid( + row=0, column=1, rowspan=5, padx=(10, 10), pady=(10, 10), sticky="ns" + ) + # create textbox self.label = customtkinter.CTkLabel(self, width=200) - self.label.grid(row=2, column=1, columnspan=1, padx=(20, 0), pady=(20, 0), sticky="nsew") - + self.label.grid( + row=2, column=1, columnspan=1, padx=(20, 0), pady=(20, 0), sticky="nsew" + ) + # set default values self.appearance_mode_optionemenu.set("Dark") self.scaling_optionemenu.set("100%") self.slider_2.configure(command=self.use_slider_value) - self.label.configure(text= "Result: ") - + self.label.configure(text="Result: ") + def change_appearance_mode_event(self, new_appearance_mode: str): customtkinter.set_appearance_mode(new_appearance_mode) - + def change_scaling_event(self, new_scaling: str): new_scaling_float = int(new_scaling.replace("%", "")) / 100 customtkinter.set_widget_scaling(new_scaling_float) - + def sidebar_button_event(self): print("sidebar_button click") - + def use_slider_value(self, _value): self.calc() - - - + def calc(self): entry_value = self.tau.get() - + self.label.configure(text=f"Result: {logic(entry_value)}") self.line_graph = plot(int(entry_value), self) - self.line_graph.grid(row=2, column=3, columnspan=1, rowspan=1, padx=(0, 20), pady=(20, 0), sticky="nsew") + self.line_graph.grid( + row=2, + column=3, + columnspan=1, + rowspan=1, + padx=(0, 20), + pady=(20, 0), + sticky="nsew", + ) def init(cli_arguments: CLIArguments): diff --git a/napytau/headless/headless_mockup.py b/napytau/headless/headless_mockup.py index fedcdcc..847d275 100644 --- a/napytau/headless/headless_mockup.py +++ b/napytau/headless/headless_mockup.py @@ -1,7 +1,10 @@ from napytau.cli.cli_arguments import CLIArguments from napytau.ingest.ingest_mockup import ingest_file + def init(cli_arguments: CLIArguments): print("running headless mockup") if cli_arguments.has_filename(): - print(f"{cli_arguments.get_filename()}:\n{ingest_file(cli_arguments.get_filename())}") + print( + f"{cli_arguments.get_filename()}:\n{ingest_file(cli_arguments.get_filename())}" + ) diff --git a/napytau/ingest/ingest_mockup.py b/napytau/ingest/ingest_mockup.py index 2a43bbd..92c49fd 100644 --- a/napytau/ingest/ingest_mockup.py +++ b/napytau/ingest/ingest_mockup.py @@ -2,4 +2,4 @@ def ingest_file(filename): # Ingest data from a file with open(filename) as f: data = f.read() - return data \ No newline at end of file + return data diff --git a/napytau/main.py b/napytau/main.py index 507301e..5648120 100644 --- a/napytau/main.py +++ b/napytau/main.py @@ -2,9 +2,10 @@ from headless.headless_mockup import init as init_headless from cli.parser import parse_cli_arguments + def main(): args = parse_cli_arguments() - + if args.headless: init_headless(args) else: diff --git a/napytau/util/coalesce.py b/napytau/util/coalesce.py index 0253e53..3e969d2 100644 --- a/napytau/util/coalesce.py +++ b/napytau/util/coalesce.py @@ -2,4 +2,4 @@ def coalesce(*args): for arg in args: if arg is not None: return arg - return None \ No newline at end of file + return None diff --git a/pyproject.toml b/pyproject.toml index cb0d8c0..8d27522 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,8 +16,9 @@ build-backend = "setuptools.build_meta" [tool.tomlscript] run= "uv run napytau/main.py" test = "uv run pytest -rA" -lint = "uv run ruff check" -lint-fix = "uv run ruff check --fix" +lint = "uv run ruff check --config ruff.toml" +lint-fix = "uv run ruff check --config ruff.toml --fix" +format = "uv run ruff format" [dependency-groups] dev = [ diff --git a/ruff.toml b/ruff.toml new file mode 100644 index 0000000..01c1c68 --- /dev/null +++ b/ruff.toml @@ -0,0 +1,78 @@ +# Exclude a variety of commonly ignored directories. +exclude = [ + ".bzr", + ".direnv", + ".eggs", + ".git", + ".git-rewrite", + ".hg", + ".ipynb_checkpoints", + ".mypy_cache", + ".nox", + ".pants.d", + ".pyenv", + ".pytest_cache", + ".pytype", + ".ruff_cache", + ".svn", + ".tox", + ".venv", + ".vscode", + "__pypackages__", + "_build", + "buck-out", + "build", + "dist", + "node_modules", + "site-packages", + "venv", +] + +# Same as Black. +line-length = 88 +indent-width = 4 + +# Assume Python 3.9 +target-version = "py39" + +[lint] +# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default. +# Unlike Flake8, Ruff doesn't enable pycodestyle warnings (`W`) or +# McCabe complexity (`C901`) by default. +select = ["E4", "E7", "E9", "F"] +extend-select = ["E501"] +ignore = [] + +# Allow fix for all enabled rules (when `--fix`) is provided. +fixable = ["ALL"] +unfixable = [] + +# Allow unused variables when underscore-prefixed. +dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$" + +[format] +# Like Black, use double quotes for strings. +quote-style = "double" + +# Like Black, indent with spaces, rather than tabs. +indent-style = "space" + +# Like Black, respect magic trailing commas. +skip-magic-trailing-comma = false + +# Like Black, automatically detect the appropriate line ending. +line-ending = "auto" + +# Enable auto-formatting of code examples in docstrings. Markdown, +# reStructuredText code/literal blocks and doctests are all supported. +# +# This is currently disabled by default, but it is planned for this +# to be opt-out in the future. +docstring-code-format = false + +# Set the line length limit used when formatting code snippets in +# docstrings. +# +# This only has an effect when the `docstring-code-format` setting is +# enabled. +docstring-code-line-length = "dynamic" \ No newline at end of file diff --git a/tests/cli/cli_arguments_test.py b/tests/cli/cli_arguments_test.py index f30b519..0ce5cd9 100644 --- a/tests/cli/cli_arguments_test.py +++ b/tests/cli/cli_arguments_test.py @@ -1,46 +1,52 @@ import unittest from argparse import Namespace + class CLIArgumentsUnitTest(unittest.TestCase): - def test_canBeConstructedFromANamespaceObject(self): - """Can be constructed from a Namespace object""" - from napytau.cli.cli_arguments import CLIArguments - raw_args = Namespace(headless=True, filename="test_filename") - cli_args = CLIArguments(raw_args) - self.assertEqual(cli_args.is_headless(), True) - self.assertEqual(cli_args.get_filename(), "test_filename") - - def test_canBeConstructedFromANamespaceObjectWithNoFilename(self): - """Can be constructed from a Namespace object with no filename""" - from napytau.cli.cli_arguments import CLIArguments - raw_args = Namespace(headless=True, filename=None) - cli_args = CLIArguments(raw_args) - self.assertEqual(cli_args.is_headless(), True) - self.assertEqual(cli_args.get_filename(), None) - - def test_canBeConstructedFromANamespaceObjectWithNoHeadlessFlag(self): - """Can be constructed from a Namespace object with no headless flag""" - from napytau.cli.cli_arguments import CLIArguments - raw_args = Namespace(headless=None, filename="test_filename") - cli_args = CLIArguments(raw_args) - self.assertEqual(cli_args.is_headless(), False) - self.assertEqual(cli_args.get_filename(), "test_filename") - - def test_canBeConstructedFromANamespaceObjectWithNoHeadlessFlagAndNoFilename(self): - """Can be constructed from a Namespace object with no headless flag and no filename""" - from napytau.cli.cli_arguments import CLIArguments - raw_args = Namespace(headless=None, filename=None) - cli_args = CLIArguments(raw_args) - self.assertEqual(cli_args.is_headless(), False) - self.assertEqual(cli_args.get_filename(), None) - - def test_canDetermineIfTheExecutionHeadless(self): - """Can determine if the execution is headless""" - from napytau.cli.cli_arguments import CLIArguments - raw_args = Namespace(headless=True, filename=None) - cli_args = CLIArguments(raw_args) - self.assertEqual(cli_args.is_headless(), True) - - -if __name__ == '__main__': - unittest.main() + def test_canBeConstructedFromANamespaceObject(self): + """Can be constructed from a Namespace object""" + from napytau.cli.cli_arguments import CLIArguments + + raw_args = Namespace(headless=True, filename="test_filename") + cli_args = CLIArguments(raw_args) + self.assertEqual(cli_args.is_headless(), True) + self.assertEqual(cli_args.get_filename(), "test_filename") + + def test_canBeConstructedFromANamespaceObjectWithNoFilename(self): + """Can be constructed from a Namespace object with no filename""" + from napytau.cli.cli_arguments import CLIArguments + + raw_args = Namespace(headless=True, filename=None) + cli_args = CLIArguments(raw_args) + self.assertEqual(cli_args.is_headless(), True) + self.assertEqual(cli_args.get_filename(), None) + + def test_canBeConstructedFromANamespaceObjectWithNoHeadlessFlag(self): + """Can be constructed from a Namespace object with no headless flag""" + from napytau.cli.cli_arguments import CLIArguments + + raw_args = Namespace(headless=None, filename="test_filename") + cli_args = CLIArguments(raw_args) + self.assertEqual(cli_args.is_headless(), False) + self.assertEqual(cli_args.get_filename(), "test_filename") + + def test_canBeConstructedFromANamespaceObjectWithNoHeadlessFlagAndNoFilename(self): + """Can be constructed from a Namespace object with no headless flag and no filename""" # noqa: E501 + from napytau.cli.cli_arguments import CLIArguments + + raw_args = Namespace(headless=None, filename=None) + cli_args = CLIArguments(raw_args) + self.assertEqual(cli_args.is_headless(), False) + self.assertEqual(cli_args.get_filename(), None) + + def test_canDetermineIfTheExecutionHeadless(self): + """Can determine if the execution is headless""" + from napytau.cli.cli_arguments import CLIArguments + + raw_args = Namespace(headless=True, filename=None) + cli_args = CLIArguments(raw_args) + self.assertEqual(cli_args.is_headless(), True) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/cli/parser_test.py b/tests/cli/parser_test.py index 7e97b11..eb2517a 100644 --- a/tests/cli/parser_test.py +++ b/tests/cli/parser_test.py @@ -2,49 +2,74 @@ from argparse import Namespace from unittest.mock import MagicMock, patch + def set_up_mocks() -> (MagicMock, MagicMock, MagicMock): - argparse_module_mock = MagicMock() - argument_parser_mock = MagicMock() - argument_parser_mock.add_argument = MagicMock() - argparse_module_mock.ArgumentParser.return_value = argument_parser_mock - cli_arguments_module_mock = MagicMock() - cli_arguments_module_mock.CLIArguments = MagicMock() - return argparse_module_mock, argument_parser_mock, cli_arguments_module_mock + argparse_module_mock = MagicMock() + argument_parser_mock = MagicMock() + argument_parser_mock.add_argument = MagicMock() + argparse_module_mock.ArgumentParser.return_value = argument_parser_mock + cli_arguments_module_mock = MagicMock() + cli_arguments_module_mock.CLIArguments = MagicMock() + return argparse_module_mock, argument_parser_mock, cli_arguments_module_mock + class ParserUnitTest(unittest.TestCase): - def test_createsAnArgumentParserInstanceAndConfiguresIt(self): - """Creates an ArgumentParser instance and configures it""" - argparse_module_mock, argument_parser_mock, cli_arguments_module_mock = set_up_mocks() - with patch.dict('sys.modules', { - 'argparse': argparse_module_mock, - 'napytau.cli.cli_arguments': cli_arguments_module_mock, - }): - from napytau.cli.parser import parse_cli_arguments - parse_cli_arguments() - self.assertEqual(len(argument_parser_mock.add_argument.mock_calls), 2) - self.assertEqual( - argument_parser_mock.add_argument.mock_calls[0], - (('--headless',), {'action': 'store_true', 'help': 'Run the application without GUI'}) - ) - self.assertEqual( - argument_parser_mock.add_argument.mock_calls[1], - (('--filename',), {'type': str, 'help': 'File to ingest (only for mockup, not used in real application)'}) - ) - - def test_returnsACLIArgumentsInstanceFromTheParsedArguments(self): - """Returns a CLIArguments instance from the parsed arguments""" - argparse_module_mock, argument_parser_mock, cli_arguments_module_mock = set_up_mocks() - with patch.dict('sys.modules', { - 'argparse': argparse_module_mock, - 'napytau.cli.cli_arguments': cli_arguments_module_mock, - }): - from napytau.cli.parser import parse_cli_arguments - test_args = Namespace(headless=True, filename="test_filename") - argument_parser_mock.parse_args.return_value = test_args - cli_arguments_module_mock.CLIArguments.return_value = MagicMock() - parse_cli_arguments() - self.assertEqual(cli_arguments_module_mock.CLIArguments.mock_calls[0], ((test_args,),)) - - -if __name__ == '__main__': - unittest.main() + def test_createsAnArgumentParserInstanceAndConfiguresIt(self): + """Creates an ArgumentParser instance and configures it""" + argparse_module_mock, argument_parser_mock, cli_arguments_module_mock = ( + set_up_mocks() + ) + with patch.dict( + "sys.modules", + { + "argparse": argparse_module_mock, + "napytau.cli.cli_arguments": cli_arguments_module_mock, + }, + ): + from napytau.cli.parser import parse_cli_arguments + + parse_cli_arguments() + self.assertEqual(len(argument_parser_mock.add_argument.mock_calls), 2) + self.assertEqual( + argument_parser_mock.add_argument.mock_calls[0], + ( + ("--headless",), + {"action": "store_true", "help": "Run the application without GUI"}, + ), + ) + self.assertEqual( + argument_parser_mock.add_argument.mock_calls[1], + ( + ("--filename",), + { + "type": str, + "help": "File to ingest (only for mockup, not used in real application)", # noqa: E501 + }, + ), + ) + + def test_returnsACLIArgumentsInstanceFromTheParsedArguments(self): + """Returns a CLIArguments instance from the parsed arguments""" + argparse_module_mock, argument_parser_mock, cli_arguments_module_mock = ( + set_up_mocks() + ) + with patch.dict( + "sys.modules", + { + "argparse": argparse_module_mock, + "napytau.cli.cli_arguments": cli_arguments_module_mock, + }, + ): + from napytau.cli.parser import parse_cli_arguments + + test_args = Namespace(headless=True, filename="test_filename") + argument_parser_mock.parse_args.return_value = test_args + cli_arguments_module_mock.CLIArguments.return_value = MagicMock() + parse_cli_arguments() + self.assertEqual( + cli_arguments_module_mock.CLIArguments.mock_calls[0], ((test_args,),) + ) + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/main_test.py b/tests/main_test.py index 42ea50f..05e7436 100644 --- a/tests/main_test.py +++ b/tests/main_test.py @@ -14,32 +14,45 @@ class MainUnitTest(unittest.TestCase): def test_callsTheHeadlessInitFunctionIfTheHeadlessFlagIsSupplied(self) -> None: """Calls the headless init function if the headless flag is supplied""" parser_mock, gui_mock, headless_mock = set_up_mocks() - with patch.dict('sys.modules', { - 'gui.ui_mockup': gui_mock, - 'headless.headless_mockup': headless_mock, - 'cli.parser': parser_mock, - }): + with patch.dict( + "sys.modules", + { + "gui.ui_mockup": gui_mock, + "headless.headless_mockup": headless_mock, + "cli.parser": parser_mock, + }, + ): headless_mock.init = MagicMock() - parser_mock.parse_cli_arguments.return_value = Namespace(headless=True, filename=None) - + parser_mock.parse_cli_arguments.return_value = Namespace( + headless=True, filename=None + ) + from napytau.main import main + main() self.assertEqual(len(headless_mock.init.mock_calls), 1) - + def test_callsTheGuiInitFunctionIfTheHeadlessFlagIsNotSupplied(self) -> None: """Calls the GUI init function if the headless flag is not supplied""" parser_mock, gui_mock, headless_mock = set_up_mocks() - with patch.dict('sys.modules', { - 'gui.ui_mockup': gui_mock, - 'headless.headless_mockup': headless_mock, - 'cli.parser': parser_mock, - }): + with patch.dict( + "sys.modules", + { + "gui.ui_mockup": gui_mock, + "headless.headless_mockup": headless_mock, + "cli.parser": parser_mock, + }, + ): gui_mock.init = MagicMock() - parser_mock.parse_cli_arguments.return_value = Namespace(headless=False, filename=None) - + parser_mock.parse_cli_arguments.return_value = Namespace( + headless=False, filename=None + ) + from napytau.main import main + main() self.assertEqual(len(gui_mock.init.mock_calls), 1) -if __name__ == '__main__': + +if __name__ == "__main__": unittest.main() diff --git a/tests/util/coalesce_test.py b/tests/util/coalesce_test.py index b264b06..974867a 100644 --- a/tests/util/coalesce_test.py +++ b/tests/util/coalesce_test.py @@ -1,17 +1,20 @@ import unittest from napytau.util.coalesce import coalesce + class CoalesceUnitTest(unittest.TestCase): - def test_returnsFirstNonNoneArgument(self): - """Returns the first non-None argument.""" - self.assertEqual(1, coalesce(None, None, None, 1, 2, 3)) - def test_returnsNoneIfAllArgumentsAreNone(self): - """Returns None if all arguments are None.""" - self.assertIsNone(coalesce(None, None, None)) - def test_returnsNoneIfNoArgumentsAreProvided(self): - """Returns None if no arguments are provided.""" - self.assertIsNone(coalesce()) + def test_returnsFirstNonNoneArgument(self): + """Returns the first non-None argument.""" + self.assertEqual(1, coalesce(None, None, None, 1, 2, 3)) + + def test_returnsNoneIfAllArgumentsAreNone(self): + """Returns None if all arguments are None.""" + self.assertIsNone(coalesce(None, None, None)) + + def test_returnsNoneIfNoArgumentsAreProvided(self): + """Returns None if no arguments are provided.""" + self.assertIsNone(coalesce()) -if __name__ == '__main__': - unittest.main() +if __name__ == "__main__": + unittest.main()