Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
130 changes: 130 additions & 0 deletions tests/cmd_app/general/test_project.py

Copy link
Copy Markdown
Collaborator

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.py

In de test ontbreekt de keuze applicatie afsluiten. Ook wordt het standaardgeval waarbij geen bekende keuze wordt teruggegeven niet getest. Lijkt me geen probleem

Copy link
Copy Markdown
Collaborator Author

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.

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)
122 changes: 122 additions & 0 deletions tests/cmd_app/general/test_traject_parameters.py
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,
)
92 changes: 92 additions & 0 deletions tests/cmd_app/spatial_layers/test_binnenteenlijn.py

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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.

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)
13 changes: 13 additions & 0 deletions tests/conftest.py
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 added tests/results/__init__.py
Empty file.
Empty file.
Loading