-
Notifications
You must be signed in to change notification settings - Fork 47
/
test_tools.py
192 lines (151 loc) · 5.99 KB
/
test_tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import json
from pathlib import Path
import pytest
import geopandas as gpd
from mock import patch
import pandas as pd
# pylint: disable=unused-import
from .fixtures import (
auth_mock,
tools_mock,
auth_live,
tools_live,
)
from .context import Tools
@pytest.mark.parametrize("vector_format", ["geojson", "kml", "wkt"])
def test_read_vector_file_different_formats(tools_mock, vector_format):
fp = Path(__file__).resolve().parent / f"mock_data/aoi_berlin.{vector_format}"
fc = tools_mock.read_vector_file(filename=fp)
assert isinstance(fc, dict)
assert fc["type"] == "FeatureCollection"
def test_read_vector_file_as_df(tools_mock):
fp = Path(__file__).resolve().parent / "mock_data/aoi_berlin.geojson"
df = tools_mock.read_vector_file(filename=fp, as_dataframe=True)
assert isinstance(df, gpd.GeoDataFrame)
assert df.crs.to_epsg() == 4326
def test_get_example_aoi(tools_mock):
fc = tools_mock.get_example_aoi("Berlin")
assert isinstance(fc, dict)
assert fc["type"] == "FeatureCollection"
@patch("matplotlib.pyplot.show")
def test_plot_coverage(tools_mock):
df = gpd.read_file(
Path(__file__).resolve().parent
/ "mock_data/search_results_limited_columns.geojson",
as_dataframe=True,
)
tools_mock.plot_coverage(df)
@patch("matplotlib.pyplot.show")
def test_plot_coverage_wrong_legend_column_ignores(tools_mock):
df = gpd.read_file(
Path(__file__).resolve().parent
/ "mock_data/search_results_limited_columns.geojson",
as_dataframe=True,
)
tools_mock.plot_coverage(df, legend_column="abcdefgh")
@patch("matplotlib.pyplot.show")
def test_plot_quicklook(tools_mock):
fp_quicklook = Path(__file__).resolve().parent / "mock_data/a_quicklook.png"
tools_mock.quicklook = [fp_quicklook]
tools_mock.plot_coverage()
@patch("matplotlib.pyplot.show")
def test_plot_quicklook_alternative_filepaths(tools_mock):
fp_quicklook = Path(__file__).resolve().parent / "mock_data/a_quicklook.png"
tools_mock.plot_coverage(filepaths=[fp_quicklook, fp_quicklook, fp_quicklook])
@patch("matplotlib.pyplot.show")
def test_plot_result(tools_mock):
fp_tif = Path(__file__).resolve().parent / "mock_data/s2_128.tif"
tools_mock.result = [fp_tif]
tools_mock.plot_results()
@patch("matplotlib.pyplot.show")
def test_plot_result_alternative_filepaths_and_titles(tools_mock):
fp_tif = Path(__file__).resolve().parent / "mock_data/s2_128.tif"
tools_mock.plot_results(filepaths=[fp_tif, fp_tif, fp_tif], titles=["a", "b", "c"])
def test_get_blocks(tools_mock, requests_mock):
url_get_blocks = f"{tools_mock.auth._endpoint()}/blocks"
requests_mock.get(
url=url_get_blocks,
json={
"data": [
{"id": "789-2736-212", "name": "tiling"},
{"id": "789-2736-212", "name": "sharpening"},
],
"error": {},
},
)
blocks = tools_mock.get_blocks()
assert isinstance(blocks, dict)
assert "tiling" in list(blocks.keys())
def test_plot_result_not_accepted_file_format_raises():
filepaths = [Path("abc/123.hdf", "abc/123.json")]
with pytest.raises(ValueError):
Tools().plot_results(filepaths=filepaths)
@pytest.mark.live
def test_get_blocks_live(tools_live):
blocks = tools_live.get_blocks(basic=False)
assert isinstance(blocks, list)
blocknames = [block["name"] for block in blocks]
assert "tiling" in blocknames
def test_get_blocks_not_basic_dataframe(tools_mock, requests_mock):
url_get_blocks = f"{tools_mock.auth._endpoint()}/blocks"
json_get_blocks = {
"data": [
{"id": "789-2736-212", "name": "tiling"},
{"id": "789-2736-212", "name": "sharpening"},
],
"error": {},
}
requests_mock.get(url=url_get_blocks, json=json_get_blocks)
blocks_df = tools_mock.get_blocks(basic=False, as_dataframe=True)
assert isinstance(blocks_df, pd.DataFrame)
assert "tiling" in blocks_df["name"].to_list()
def test_get_block_details(tools_mock, requests_mock):
block_id = "273612-13"
url_get_blocks_details = f"{tools_mock.auth._endpoint()}/blocks/{block_id}"
requests_mock.get(
url=url_get_blocks_details,
json={
"data": {"id": "273612-13", "name": "tiling", "createdAt": "123"},
"error": {},
},
)
details = tools_mock.get_block_details(block_id=block_id)
assert isinstance(details, dict)
assert details["id"] == block_id
assert "createdAt" in details
@pytest.mark.live
def test_get_block_details_live(tools_live):
tiling_id = "3e146dd6-2b67-4d6e-a422-bb3d973e32ff"
details = tools_live.get_block_details(block_id=tiling_id)
assert isinstance(details, dict)
assert details["id"] == tiling_id
assert "createdAt" in details
def test_validate_manifest(tools_mock, requests_mock):
fp = Path(__file__).resolve().parent / "mock_data/manifest.json"
url_validate_mainfest = f"{tools_mock.auth._endpoint()}/validate-schema/block"
requests_mock.post(
url=url_validate_mainfest,
json={"data": {"valid": True, "errors": []}, "error": {}},
)
result = tools_mock.validate_manifest(path_or_json=fp)
assert result == {"valid": True, "errors": []}
@pytest.mark.live
def test_validate_manifest_valid_live(tools_live):
fp = Path(__file__).resolve().parent / "mock_data/manifest.json"
result = tools_live.validate_manifest(path_or_json=fp)
assert result == {"valid": True, "errors": []}
@pytest.mark.live
def test_validate_manifest_invalid_live(tools_live):
fp = Path(__file__).resolve().parent / "mock_data/manifest.json"
with open(fp) as src:
mainfest_json = json.load(src)
mainfest_json.update(
{
"_up42_specification_version": 1,
"input_capabilities": {
"invalidtype": {"up42_standard": {"format": "GTiff"}}
},
}
)
with pytest.raises(ValueError):
tools_live.validate_manifest(path_or_json=mainfest_json)