-
Notifications
You must be signed in to change notification settings - Fork 1
Unittest pytest example #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
VincentJilesen
wants to merge
15
commits into
beta
Choose a base branch
from
unittest-pytest-example
base: beta
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c86d1ea
_specify_w in traject_parameters.py
VincentJilesen 4e46fab
create_project in project.py
VincentJilesen a4f93c8
added_binnenteenlijn
VincentJilesen 87bf59a
rename
VincentJilesen 74aea98
cleanup
VincentJilesen 6177e0d
Test documentatie
VincentJilesen 8d9bc12
docu
VincentJilesen 6a2765d
typing
VincentJilesen a765a8e
assemblage_functions mostly covered
VincentJilesen 41947ca
format
VincentJilesen 98fccc2
minor simplification
VincentJilesen 4bccaf4
fixture for app_settings
VincentJilesen c419623
added tests "valid boundaries" and "surroundig spaces"
skapinga fdb23f3
rename test_functions_assemblage and added documentstring
skapinga 600206d
uncommit test_system
skapinga File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| """ | ||
| Unit tests for `geoprob_pipe.cmd_app.general.project.py`. | ||
| Tests performed for: | ||
| - created_project(app_settings: ApplicationSettings) -> bool | ||
| """ | ||
|
|
||
| from unittest.mock import Mock | ||
|
|
||
| import pytest | ||
|
|
||
| import geoprob_pipe.cmd_app.general.project as module | ||
|
|
||
|
|
||
| # --- created_project(app_settings: ApplicationSettings) -> bool --- | ||
| @pytest.mark.parametrize( | ||
| "choice,function_name", | ||
| [ | ||
| ( | ||
| "Bestaand project openen", | ||
| "specify_path_to_existing_project", | ||
| ), | ||
| ( | ||
| "Nieuw project starten", | ||
| "specify_dir_for_new_project", | ||
| ), | ||
| ], | ||
| ) | ||
| def test_created_project_happy_paths( | ||
| app_settings, | ||
| monkeypatch, | ||
| choice: str, | ||
| function_name: str, | ||
| ) -> None: | ||
| """Test of the paths are correctly completed.""" | ||
| # Arrange | ||
| # Mock assigned function calls | ||
| select_mock = Mock() | ||
| select_mock.return_value.execute.return_value = choice | ||
|
|
||
| monkeypatch.setattr("InquirerPy.inquirer.select", select_mock) | ||
|
|
||
| # Mock called functions | ||
| action_mock = Mock() | ||
| monkeypatch.setattr( | ||
| module, | ||
| function_name, | ||
| action_mock, | ||
| ) | ||
| logging_mock = Mock() | ||
| monkeypatch.setattr( | ||
| module, | ||
| "enable_geopackage_logging", | ||
| logging_mock, | ||
| ) | ||
|
|
||
| # Act | ||
| result: bool = module.created_project(app_settings) | ||
|
|
||
| # Assert | ||
| # Check path reaches return | ||
| assert result is True | ||
|
|
||
| # Check functions were called once with correct arguments | ||
| action_mock.assert_called_once_with(app_settings) | ||
|
|
||
| logging_mock.assert_called_once_with(app_settings=app_settings) | ||
|
|
||
|
|
||
| def test_created_project_compare(monkeypatch) -> None: | ||
| """Test start compare function.""" | ||
| # Arrange | ||
| # Mock assigned function calls | ||
| prompt_mock = Mock() | ||
| choice: str = "Twee projectbestanden vergelijken" | ||
| prompt_mock.execute.return_value = choice | ||
| monkeypatch.setattr("InquirerPy.inquirer.select", Mock(return_value=prompt_mock)) | ||
|
|
||
| # Mock called functions | ||
| compare_mock = Mock() | ||
| monkeypatch.setattr(module, "start_comparison", compare_mock) | ||
|
|
||
| # Act | ||
| # While capturing the sysexit() run the function | ||
| with pytest.raises(SystemExit, match="Applicatie afgesloten"): | ||
| module.created_project(Mock()) | ||
|
|
||
| # Assert | ||
| # Check function call | ||
| compare_mock.assert_called_once() | ||
|
|
||
|
|
||
| def test_created_project_single_calc(monkeypatch) -> None: | ||
| # Arrange | ||
| # Mock assigned function calls | ||
| prompt_mock = Mock() | ||
| choice: str = "Inspecteer een enkele berekening" | ||
| prompt_mock.execute.return_value = choice | ||
| monkeypatch.setattr("InquirerPy.inquirer.select", Mock(return_value=prompt_mock)) | ||
|
|
||
| # Mock called functions | ||
| panel_instance = Mock() | ||
| panel_mock = Mock(return_value=panel_instance) | ||
| monkeypatch.setattr( | ||
| module, | ||
| "Panel", | ||
| panel_mock, | ||
| ) | ||
| console_instance = Mock() | ||
| console_mock = Mock(return_value=console_instance) | ||
| monkeypatch.setattr( | ||
| module, | ||
| "Console", | ||
| console_mock, | ||
| ) | ||
| # Act | ||
| # While capturing the sysexit() run the function | ||
| with pytest.raises(SystemExit, match="Applicatie afgesloten"): | ||
| module.created_project(Mock()) | ||
|
|
||
| # Assert | ||
| # Check Panel construction | ||
| panel_mock.assert_called_once_with( | ||
| module.EXPLANATION_REPRODUCING_SINGLE_CALCULATION, | ||
| title="INSPECTEER EEN ENKELE BEREKENING", | ||
| title_align="left", | ||
| border_style="bright_blue", | ||
| padding=(0, 2), | ||
| ) | ||
| # Check print call | ||
| console_instance.print.assert_called_once_with(panel_instance) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| """ | ||
| Unit tests for `geoprob_pipe.cmd_app.general.traject_parameters.py`. | ||
| Tests performed for: | ||
| - _specify_w(app_settings: ApplicationSettings) | ||
| """ | ||
|
|
||
| from unittest.mock import Mock | ||
|
|
||
| import pytest | ||
|
|
||
| import geoprob_pipe.cmd_app.general.traject_parameters as module | ||
|
|
||
|
|
||
| # --- _specify_w(app_settings: ApplicationSettings) --- | ||
| @pytest.mark.parametrize( | ||
| "input_value, expected_answer", | ||
| [ | ||
| ("", "geen w gespecificeerd"), | ||
| ("abc", "geen decimaal getal"), | ||
| ("1.5", "groter dan 1.0"), | ||
| ("0", "kleiner of gelijk aan 0.0"), | ||
| ("-1", "kleiner of gelijk aan 0.0"), | ||
| ], | ||
| ) | ||
| def test_specify_w_validations( | ||
| app_settings, | ||
| monkeypatch, | ||
| capsys, | ||
| input_value: str, | ||
| expected_answer: str, | ||
| ) -> None: | ||
| """Test for all branches of validation and correct storage of user input.""" | ||
| # Arrange | ||
| # Setup user inputs | ||
| user_inputs = iter([input_value, "0.24"]) | ||
|
|
||
| # Mock assigned function calls | ||
| prompt_mock = Mock() | ||
| prompt_mock.execute.side_effect = lambda: next(user_inputs) | ||
|
|
||
| monkeypatch.setattr( | ||
| "InquirerPy.inquirer.text", | ||
| Mock(return_value=prompt_mock), | ||
| ) | ||
|
|
||
| # Mock called functions | ||
| append_mock = Mock() | ||
| monkeypatch.setattr( | ||
| module, | ||
| "_append_to_db", | ||
| append_mock, | ||
| ) | ||
|
|
||
| # Act | ||
| # Run tested function: | ||
| module._specify_w(app_settings) | ||
|
|
||
| # Assert | ||
| # Check correct message printed | ||
| captured = capsys.readouterr() | ||
| assert expected_answer in captured.out | ||
|
|
||
| # Check continue loop | ||
| assert prompt_mock.execute.call_count == 2 | ||
|
|
||
| # Check accepted value stored correctly | ||
| append_mock.assert_called_once_with( | ||
| app_settings=app_settings, | ||
| key="w", | ||
| value=0.24, # as a float | ||
| ) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("input_value", ["0.01", "1.0"]) | ||
| def test_specify_w_valid_boundaries(app_settings, monkeypatch, input_value: str) -> None: | ||
| """Test that the valid boundaries are accepted.""" | ||
| # Arrange | ||
| prompt_mock = Mock() | ||
| prompt_mock.execute.return_value = input_value | ||
| monkeypatch.setattr( | ||
| "InquirerPy.inquirer.text", | ||
| Mock(return_value=prompt_mock), | ||
| ) | ||
|
|
||
| append_mock = Mock() | ||
| monkeypatch.setattr(module, "_append_to_db", append_mock) | ||
|
|
||
| # Act | ||
| module._specify_w(app_settings) | ||
|
|
||
| # Assert | ||
| prompt_mock.execute.assert_called_once() | ||
| append_mock.assert_called_once_with( | ||
| app_settings=app_settings, | ||
| key="w", | ||
| value=float(input_value), | ||
| ) | ||
|
|
||
|
|
||
| def test_specify_w_strips_surrounding_spaces(app_settings, monkeypatch) -> None: | ||
| """Test that surrounding spaces are removed before validation.""" | ||
| # Arrange | ||
| prompt_mock = Mock() | ||
| prompt_mock.execute.return_value = " 0.24 " | ||
| monkeypatch.setattr( | ||
| "InquirerPy.inquirer.text", | ||
| Mock(return_value=prompt_mock), | ||
| ) | ||
|
|
||
| append_mock = Mock() | ||
| monkeypatch.setattr(module, "_append_to_db", append_mock) | ||
|
|
||
| # Act | ||
| module._specify_w(app_settings) | ||
|
|
||
| # Assert | ||
| prompt_mock.execute.assert_called_once() | ||
| append_mock.assert_called_once_with( | ||
| app_settings=app_settings, | ||
| key="w", | ||
| value=0.24, | ||
| ) |
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Implementatie van Mock is ok. Alleen de routeringslogica wordt getest. De echte request_binnenteenlijn_filepath en de import/write-logica blijven nog ongetest. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| """ | ||
| Unit tests for `geoprob_pipe.cmd_app.spatial_layers.binnenteenlijn.py`. | ||
| Tests performed for: | ||
| - added_binnenteenlijn(app_settings: ApplicationSettings) -> bool | ||
| """ | ||
|
|
||
| from unittest.mock import Mock | ||
|
|
||
| import geoprob_pipe.cmd_app.spatial_layers.binnenteenlijn as module | ||
|
|
||
|
|
||
| # --- added_binnenteenlijn(app_settings: ApplicationSettings) -> bool --- | ||
| def test_added_binnenteenlijn_already_included( | ||
| app_settings, | ||
| monkeypatch, | ||
| capsys, | ||
| ) -> None: | ||
| """Test case `binnenteenlijn` already added to gpkg.""" | ||
| # Arrange | ||
| # Monkey patch assigned function calls | ||
| monkeypatch.setattr( | ||
| module.fiona, | ||
| "listlayers", | ||
| Mock( | ||
| return_value=[ | ||
| "trajectlijn", | ||
| "binnenteenlijn", | ||
| "vakindeling", | ||
| ] | ||
| ), | ||
| ) | ||
|
|
||
| # Mock called module fuctions | ||
| request_mock = Mock() | ||
| monkeypatch.setattr( | ||
| module, | ||
| "request_binnenteenlijn_filepath", | ||
| request_mock, | ||
| ) | ||
|
|
||
| # Act | ||
| # Run tested function: | ||
| result: bool = module.added_binnenteenlijn(app_settings) | ||
|
|
||
| # Assert | ||
| # Check expected return | ||
| assert result is True | ||
|
|
||
| # Check correct message printed | ||
| captured = capsys.readouterr() | ||
| assert "Binnenteenlijn al toegevoegd" in captured.out | ||
|
|
||
| # Check function in other branch not called | ||
| request_mock.assert_not_called() | ||
|
|
||
|
|
||
| def test_added_binnenteenlijn_not_yet_included( | ||
| app_settings, | ||
| monkeypatch, | ||
| ) -> None: | ||
| """Test case `binnenteenlijn` not yet added to gpkg.""" | ||
| # Arrange | ||
| # Monkey patch assigned function call(s) | ||
| monkeypatch.setattr( | ||
| module.fiona, | ||
| "listlayers", | ||
| Mock( | ||
| return_value=[ | ||
| "trajectlijn", | ||
| "vakindeling", | ||
| ] | ||
| ), | ||
| ) | ||
|
|
||
| # Mock called module fuction(s) | ||
| request_mock = Mock() | ||
| monkeypatch.setattr( | ||
| module, | ||
| "request_binnenteenlijn_filepath", | ||
| request_mock, | ||
| ) | ||
|
|
||
| # Act | ||
| # Run tested function: | ||
| result: bool = module.added_binnenteenlijn(app_settings) | ||
|
|
||
| # Assert | ||
| # Check expected return | ||
| assert result is True | ||
|
|
||
| # Check function call with correct argument | ||
| request_mock.assert_called_once_with(app_settings=app_settings) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| import pytest | ||
|
|
||
| from geoprob_pipe.cmd_app.cmd import ApplicationSettings | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def app_settings(tmp_path) -> ApplicationSettings: | ||
| settings = ApplicationSettings() | ||
|
|
||
| settings.workspace_dir = str(tmp_path) | ||
| settings.geopackage_filename = "dummy.gpkg" | ||
|
|
||
| return settings |
Empty file.
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
De test controleert de functie
created_project()in project.pyIn de test ontbreekt de keuze applicatie afsluiten. Ook wordt het standaardgeval waarbij geen bekende keuze wordt teruggegeven niet getest. Lijkt me geen probleem
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Het is een meerkeuze vraag dus er is geen onbekende keuze mogelijk.