-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Tests for every module and CI workflow
- Loading branch information
Showing
10 changed files
with
414 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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,28 @@ | ||
name: CI | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: ["3.10", "3.11"] | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: Install Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v1 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
python -m pip install ".[dev]" | ||
- name: Test with pytest | ||
run: python -m pytest |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,113 @@ | ||
import pytest | ||
from unittest.mock import patch | ||
from sigpac_tools.anotate import get_metadata | ||
|
||
# Mock data for the test | ||
layer_parcela_response = {"key": "value"} | ||
layer_recinto_response = {"key": "value2"} | ||
|
||
|
||
class TestGetMetadata: | ||
@patch("sigpac_tools.anotate.__query") | ||
def test_parcela_layer(self, mock_query): | ||
mock_query.return_value = layer_parcela_response | ||
data = { | ||
"province": 1, | ||
"municipality": 1, | ||
"polygon": 1, | ||
"parcel": 1, | ||
} | ||
|
||
result = get_metadata("parcela", data) | ||
assert result == layer_parcela_response | ||
|
||
@patch("sigpac_tools.anotate.__query") | ||
def test_recinto_layer(self, mock_query): | ||
mock_query.return_value = layer_recinto_response | ||
data = { | ||
"province": 1, | ||
"municipality": 1, | ||
"polygon": 1, | ||
"parcel": 1, | ||
"enclosure": 1, | ||
} | ||
|
||
result = get_metadata("recinto", data) | ||
assert result == layer_recinto_response | ||
|
||
def test_missing_layer(self): | ||
data = { | ||
"province": 1, | ||
"municipality": 1, | ||
"polygon": 1, | ||
"parcel": 1, | ||
} | ||
|
||
with pytest.raises(ValueError, match="Layer not specified"): | ||
get_metadata("", data) | ||
|
||
def test_invalid_layer(self): | ||
data = { | ||
"province": 1, | ||
"municipality": 1, | ||
"polygon": 1, | ||
"parcel": 1, | ||
} | ||
|
||
with pytest.raises(KeyError): | ||
get_metadata("invalid_layer", data) | ||
|
||
def test_missing_province(self): | ||
data = { | ||
"municipality": 1, | ||
"polygon": 1, | ||
"parcel": 1, | ||
} | ||
|
||
with pytest.raises(ValueError): | ||
get_metadata("parcela", data) | ||
|
||
def test_missing_municipality(self): | ||
data = { | ||
"province": 1, | ||
"polygon": 1, | ||
"parcel": 1, | ||
} | ||
|
||
with pytest.raises(ValueError): | ||
get_metadata("parcela", data) | ||
|
||
def test_missing_polygon(self): | ||
data = { | ||
"province": 1, | ||
"municipality": 1, | ||
"parcel": 1, | ||
} | ||
|
||
with pytest.raises(ValueError): | ||
get_metadata("parcela", data) | ||
|
||
def test_missing_parcel(self): | ||
data = { | ||
"province": 1, | ||
"municipality": 1, | ||
"polygon": 1, | ||
} | ||
|
||
with pytest.raises(ValueError): | ||
get_metadata("parcela", data) | ||
|
||
def test_missing_enclosure_recinto_layer(self): | ||
data = { | ||
"province": 1, | ||
"municipality": 1, | ||
"polygon": 1, | ||
"parcel": 1, | ||
} | ||
|
||
with pytest.raises(ValueError): | ||
get_metadata("recinto", data) | ||
|
||
|
||
if __name__ == "__main__": | ||
pytest.main() |
This file contains 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,101 @@ | ||
import pytest | ||
from unittest.mock import patch, Mock | ||
from sigpac_tools.locate import geometry_from_coords | ||
from sigpac_tools._globals import BASE_URL | ||
|
||
# Mock data for the geojson response | ||
mock_geojson_response = { | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"properties": { | ||
"parcela": 123, | ||
"recinto": 456, | ||
}, | ||
"geometry": { | ||
"type": "Polygon", | ||
"coordinates": [[[0, 0], [1, 1], [1, 0], [0, 0]]], | ||
}, | ||
} | ||
], | ||
} | ||
|
||
class TestGeometryFromCoords: | ||
@patch("sigpac_tools.locate.requests.get") | ||
@patch("sigpac_tools.locate.lng_lat_to_tile") | ||
@patch("sigpac_tools.locate.transform_coords") | ||
def test_geometry_from_coords_parcela(self, mock_transform_coords, mock_lng_lat_to_tile, mock_get): | ||
mock_lng_lat_to_tile.return_value = (1234, 5678) | ||
mock_response = Mock() | ||
mock_response.json.return_value = mock_geojson_response | ||
mock_get.return_value = mock_response | ||
|
||
layer = "parcela" | ||
lat = 40.0 | ||
lon = -3.0 | ||
reference = 123 | ||
|
||
result = geometry_from_coords(layer, lat, lon, reference) | ||
|
||
mock_lng_lat_to_tile.assert_called_once_with(lon, lat, 15) | ||
mock_get.assert_called_once_with(f"{BASE_URL}/vectorsdg/vector/parcela@3857/15.1234.5678.geojson") | ||
mock_transform_coords.assert_called_once() | ||
assert result == mock_geojson_response["features"][0]["geometry"] | ||
|
||
@patch("sigpac_tools.locate.requests.get") | ||
@patch("sigpac_tools.locate.lng_lat_to_tile") | ||
@patch("sigpac_tools.locate.transform_coords") | ||
def test_geometry_from_coords_recinto(self, mock_transform_coords, mock_lng_lat_to_tile, mock_get): | ||
mock_lng_lat_to_tile.return_value = (1234, 5678) | ||
mock_response = Mock() | ||
mock_response.json.return_value = mock_geojson_response | ||
mock_get.return_value = mock_response | ||
|
||
layer = "recinto" | ||
lat = 40.0 | ||
lon = -3.0 | ||
reference = 456 | ||
|
||
result = geometry_from_coords(layer, lat, lon, reference) | ||
|
||
mock_lng_lat_to_tile.assert_called_once_with(lon, lat, 15) | ||
mock_get.assert_called_once_with(f"{BASE_URL}/vectorsdg/vector/recinto@3857/15.1234.5678.geojson") | ||
mock_transform_coords.assert_called_once() | ||
assert result == mock_geojson_response["features"][0]["geometry"] | ||
|
||
@patch("sigpac_tools.locate.requests.get") | ||
@patch("sigpac_tools.locate.lng_lat_to_tile") | ||
def test_geometry_not_found(self, mock_lng_lat_to_tile, mock_get): | ||
mock_lng_lat_to_tile.return_value = (1234, 5678) | ||
mock_response = Mock() | ||
mock_response.json.return_value = {"type": "FeatureCollection", "features": []} | ||
mock_get.return_value = mock_response | ||
|
||
layer = "parcela" | ||
lat = 40.0 | ||
lon = -3.0 | ||
reference = 999 | ||
|
||
result = geometry_from_coords(layer, lat, lon, reference) | ||
|
||
mock_lng_lat_to_tile.assert_called_once_with(lon, lat, 15) | ||
mock_get.assert_called_once_with(f"{BASE_URL}/vectorsdg/vector/parcela@3857/15.1234.5678.geojson") | ||
assert result is None | ||
|
||
def test_invalid_layer(self): | ||
with pytest.raises(KeyError, match='Layer "invalid" not supported. Supported layers: "parcela", "recinto"'): | ||
geometry_from_coords("invalid", 40.0, -3.0, 123) | ||
|
||
def test_missing_parameters(self): | ||
with pytest.raises(ValueError, match="Layer, latitude, longitude or reference not specified"): | ||
geometry_from_coords("", 40.0, -3.0, 123) | ||
with pytest.raises(ValueError, match="Layer, latitude, longitude or reference not specified"): | ||
geometry_from_coords("parcela", None, -3.0, 123) | ||
with pytest.raises(ValueError, match="Layer, latitude, longitude or reference not specified"): | ||
geometry_from_coords("parcela", 40.0, None, 123) | ||
with pytest.raises(ValueError, match="Layer, latitude, longitude or reference not specified"): | ||
geometry_from_coords("parcela", 40.0, -3.0, None) | ||
|
||
if __name__ == "__main__": | ||
pytest.main() |
Oops, something went wrong.