Skip to content

Commit

Permalink
Update Line, Point and Polygon unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mgcth committed May 4, 2024
1 parent 73ff2b8 commit bed5207
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 339 deletions.
133 changes: 22 additions & 111 deletions tests/unit/test_unit_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,120 +2,31 @@

from unittest.mock import patch

import geopandas as gpd
import pytest
from lantmateriet import config
from lantmateriet.line import Line


class TestUnitLine:
"""Unit tests of Line."""

@pytest.mark.parametrize(
"file_name, detail_level, layer, use_arrow, df, expected_result",
[
(
"path",
"50",
"vaglinje",
True,
gpd.GeoDataFrame(
{
"objekttyp": [
k for k in config.config_50.communication["vaglinje"].keys()
]
}
),
config.config_50,
),
(
"path",
"50",
"vaglinje",
True,
gpd.GeoDataFrame(
{
"objekttyp": [
k
for k in config.config_50.communication["vaglinje"].keys()
if k not in {"Motorväg"}
]
}
),
None,
),
],
)
@patch("lantmateriet.geometry.gpd.read_file")
def test_unit_communication_init(
self,
mock_gpd_read_file,
file_name,
detail_level,
layer,
use_arrow,
df,
expected_result,
):
"""Unit test of Line __init__ method.
Args;
mock_gpd_read_file: mock of gpd read_file
file_name: file_name
detail_level: detail_level
layer: layer
use_arrow: arrow flag
df: dataframe
expected_result: expected result
"""
mock_gpd_read_file.return_value = df
if expected_result is None:
with pytest.raises(KeyError):
communication = Line(file_name, detail_level, layer, use_arrow)
else:
communication = Line(file_name, detail_level, layer, use_arrow)
mock_gpd_read_file.assert_called_with(
file_name, layer=layer, use_arrow=use_arrow
)
assert communication.config == expected_result

@patch("lantmateriet.communication.Line._process")
@patch("lantmateriet.communication.Line.__init__", return_value=None)
def test_unit_communication_process(
self, mock_communication_init, mock_communication_process
):
"""Unit test of communication process method.
Args:
mock_communication_init: mock of Line __init__
mock_communication_process: mock of Line _process
"""
communication = Line("path")
communication.item_type = "communication"
communication.layer = "vaglinje"
communication.dissolve = False

communication.process()
mock_communication_process.assert_called_once_with(
"communication", "vaglinje", False, False, True
)

@patch("lantmateriet.communication.Line._save")
@patch("lantmateriet.communication.Line.__init__", return_value=None)
def test_unit_communication_save(
self, mock_communication_init, mock_communication_save
):
"""Unit test of communication save method.
Args:
mock_communication_init: mock of Line __init__
mock_communication_save: mock of Line _save
"""
communication = Line("path")
communication.item_type = "communication"
communication.layer = "vaglinje"

communication.save({}, "path")
mock_communication_save.assert_called_once_with(
"communication", "vaglinje", {}, "path"
)
@patch("lantmateriet.geometry.Geometry.__init__")
def test_unit_line_init(self, mock_geometry):
"""Unit test of Line __init__ method."""
line = Line("path", "50", "layer", "name", "field")
assert line.dissolve is False
mock_geometry.assert_called_once()

@patch("lantmateriet.geometry.Geometry._process")
@patch("lantmateriet.geometry.Geometry.__init__")
def test_unit_line_process(self, mock_geometry, mock_geometry_process):
"""Unit test of Line process method."""
line = Line("path", "50", "layer", "name", "field")
line.process(False)
mock_geometry_process.assert_called_once()

@patch("lantmateriet.geometry.Geometry._save")
@patch("lantmateriet.geometry.Geometry.__init__")
def test_unit_line_save(self, mock_geometry, mock_geometry_save):
"""Unit test of Line save method."""
line = Line("path", "50", "layer", "name", "field")
line.save("path", "file")
mock_geometry_save.assert_called_once()
133 changes: 22 additions & 111 deletions tests/unit/test_unit_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,120 +2,31 @@

from unittest.mock import patch

import geopandas as gpd
import pytest
from lantmateriet import config
from lantmateriet.point import Point


class TestUnitPoint:
"""Unit tests of Point."""

@pytest.mark.parametrize(
"file_name, detail_level, layer, use_arrow, df, expected_result",
[
(
"path",
"50",
"byggnad",
True,
gpd.GeoDataFrame(
{
"objekttyp": [
k for k in config.config_50.construction["byggnad"].keys()
]
}
),
config.config_50,
),
(
"path",
"50",
"byggnad",
True,
gpd.GeoDataFrame(
{
"objekttyp": [
k
for k in config.config_50.construction["byggnad"].keys()
if k not in {"Bostad"}
]
}
),
None,
),
],
)
@patch("lantmateriet.geometry.gpd.read_file")
def test_unit_construction_init(
self,
mock_gpd_read_file,
file_name,
detail_level,
layer,
use_arrow,
df,
expected_result,
):
"""Unit test of Point __init__ method.
Args;
mock_gpd_read_file: mock of gpd read_file
file_name: file_name
detail_level: detail_level
layer: layer
use_arrow: arrow flag
df: dataframe
expected_result: expected result
"""
mock_gpd_read_file.return_value = df
if expected_result is None:
with pytest.raises(KeyError):
construction = Point(file_name, detail_level, layer, use_arrow)
else:
construction = Point(file_name, detail_level, layer, use_arrow)
mock_gpd_read_file.assert_called_with(
file_name, layer=layer, use_arrow=use_arrow
)
assert construction.config == expected_result

@patch("lantmateriet.construction.Point._process")
@patch("lantmateriet.construction.Point.__init__", return_value=None)
def test_unit_construction_process(
self, mock_construction_init, mock_construction_process
):
"""Unit test of Point process method.
Args:
mock_construction_init: mock of Point __init__
mock_construction_process: mock of Point _process
"""
construction = Point("path")
construction.item_type = "construction"
construction.layer = "byggnad"
construction.dissolve = True

construction.process()
mock_construction_process.assert_called_once_with(
"construction", "byggnad", True, True, True
)

@patch("lantmateriet.construction.Point._save")
@patch("lantmateriet.construction.Point.__init__", return_value=None)
def test_unit_construction_save(
self, mock_construction_init, mock_construction_save
):
"""Unit test of construction save method.
Args:
mock_construction_init: mock of Point __init__
mock_construction_save: mock of Point _save
"""
construction = Point("path")
construction.item_type = "construction"
construction.layer = "byggnad"

construction.save({}, "path")
mock_construction_save.assert_called_once_with(
"construction", "byggnad", {}, "path"
)
@patch("lantmateriet.geometry.Geometry.__init__")
def test_unit_point_init(self, mock_geometry):
"""Unit test of Point __init__ method."""
point = Point("path", "50", "layer", "name", "field")
assert point.dissolve is False
mock_geometry.assert_called_once()

@patch("lantmateriet.geometry.Geometry._process")
@patch("lantmateriet.geometry.Geometry.__init__")
def test_unit_point_process(self, mock_geometry, mock_geometry_process):
"""Unit test of Point process method."""
point = Point("path", "50", "layer", "name", "field")
point.process(False)
mock_geometry_process.assert_called_once()

@patch("lantmateriet.geometry.Geometry._save")
@patch("lantmateriet.geometry.Geometry.__init__")
def test_unit_point_save(self, mock_geometry, mock_geometry_save):
"""Unit test of Point save method."""
point = Point("path", "50", "layer", "name", "field")
point.save("path", "file")
mock_geometry_save.assert_called_once()
Loading

0 comments on commit bed5207

Please sign in to comment.