-
Notifications
You must be signed in to change notification settings - Fork 6
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
tests: add first approval tests #453
Open
matthiasschaub
wants to merge
6
commits into
main
Choose a base branch
from
approval-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
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
6 commits
Select commit
Hold shift + click to select a range
29e4308
build: add dependencies for approval testing
matthiasschaub 1259ed3
tests: add first approval tests
matthiasschaub 56723c2
test(approval): add custom Namer class
matthiasschaub 50b3961
test(approval): use epsg:8857 for area comparison
matthiasschaub a8c9f91
docs: add information about approval tests
matthiasschaub ff44b91
docs: move approval test section one down
matthiasschaub File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,25 @@ | ||
from pathlib import Path | ||
|
||
import geopandas | ||
from approvaltests.core import FileComparator | ||
|
||
|
||
class GeoJSONComparator(FileComparator): | ||
def compare(self, received_path: str, approved_path: str) -> bool: | ||
if not Path(approved_path).exists() or Path(approved_path).stat().st_size == 0: | ||
return False | ||
|
||
# EPSG:8857 - small scale equal-area mapping | ||
df_received = geopandas.read_file(received_path).to_crs("EPSG:8857") | ||
df_approved = geopandas.read_file(approved_path).to_crs("EPSG:8857") | ||
|
||
if len(df_approved) != len(df_received): | ||
return False | ||
|
||
area_clipped = df_received.clip(df_approved).area | ||
area_diff = df_approved.area - area_clipped | ||
area_ratio = area_diff / df_approved.area | ||
for r in area_ratio: | ||
if r > 0.01: | ||
return False | ||
return True |
1 change: 1 addition & 0 deletions
1
tests/fixtures/approved/test_approval-test_smt_approver-esri-world-imagery.approved.txt
Large diffs are not rendered by default.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
tests/fixtures/approved/test_approval-test_smt_approver-osm.approved.txt
Large diffs are not rendered by default.
Oops, something went wrong.
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,29 @@ | ||
import pytest | ||
from approvaltests import Options, verify_binary | ||
|
||
from tests.comparator import GeoJSONComparator | ||
from tests.namer import PytestNamer | ||
from tests.reporter import SketchMapToolReporter | ||
|
||
|
||
@pytest.mark.use_fixtures("vector") | ||
@pytest.fixture(scope="session") | ||
def vector_path(tmp_path_factory, uuid_digitize) -> bytes: | ||
return tmp_path_factory.getbasetemp() / uuid_digitize / "vector.geojson" | ||
|
||
|
||
@pytest.mark.use_fixtures("sketch_map_marked") | ||
@pytest.fixture(scope="session") | ||
def sketch_map_marked_path(tmp_path_factory, uuid_create) -> bytes: | ||
return tmp_path_factory.getbasetemp() / uuid_create / "sketch-map-marked.png" | ||
|
||
|
||
def test_smt_approver(sketch_map_marked_path, vector_path): | ||
options = ( | ||
Options() | ||
.with_reporter(SketchMapToolReporter(sketch_map=sketch_map_marked_path)) | ||
.with_comparator(GeoJSONComparator()) | ||
.with_namer(PytestNamer()) | ||
) | ||
with open(vector_path, "rb") as f: | ||
verify_binary(f.read(), ".geojson", options=options) |
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,40 @@ | ||
import os | ||
|
||
from approvaltests import Namer | ||
|
||
from tests import FIXTURE_DIR | ||
|
||
APPROVED_DIR = FIXTURE_DIR / "approved" | ||
|
||
|
||
class PytestNamer(Namer): | ||
def __init__(self): | ||
"""An approval tests Namer for naming approved and received text files. | ||
|
||
The Namer includes fixture dir, module, class and function in name. | ||
|
||
This class utilizes the `PYTEST_CURRENT_TEST` environment variable, which | ||
consist of the nodeid and the current stage: | ||
`relative/path/to/test_file.py::TestClass::test_func[a] (call)` | ||
|
||
For better readability this class formats the filename to something like: | ||
`test_file-TestClass-test_func-a | ||
""" | ||
# TODO: name clashes are possible. | ||
# Include dir names (except tests/integration/) to avoid name clashes | ||
nodeid = os.environ["PYTEST_CURRENT_TEST"] | ||
nodeid_without_dir = nodeid.split("/")[-1] | ||
parts = nodeid_without_dir.split("::") | ||
raw = "-".join(parts) | ||
self.name = ( | ||
raw.replace(".py", "") | ||
.replace("[", "-") | ||
.replace("]", "") | ||
.replace(" (call)", "") | ||
) | ||
|
||
def get_received_filename(self) -> str: | ||
return str(APPROVED_DIR / str(self.name + ".received" + ".txt")) | ||
|
||
def get_approved_filename(self) -> str: | ||
return str(APPROVED_DIR / str(self.name + ".approved" + ".txt")) |
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,58 @@ | ||
from pathlib import Path | ||
|
||
import geopandas | ||
import matplotlib.patches as mpatches | ||
import numpy as np | ||
from approvaltests import Reporter | ||
from matplotlib import pyplot as plt | ||
from matplotlib.widgets import Button | ||
from PIL import Image | ||
|
||
|
||
class Approver: | ||
def __init__(self, approved_path: Path, received_path: Path, sketch_map: Path): | ||
self.approved_path: Path = approved_path # geojson | ||
self.received_path: Path = received_path # geojson | ||
self.sketch_map: Path = sketch_map # jpg | ||
|
||
def plot_difference(self, ax): | ||
"""Plot difference between approved and received GeoJSON features.""" | ||
if self.approved_path.exists() and self.approved_path.stat().st_size != 0: | ||
df_approved = geopandas.read_file(self.approved_path) | ||
df_approved.plot(ax=ax, facecolor="none", edgecolor="blue") | ||
df_received = geopandas.read_file(self.received_path) | ||
df_received.plot(ax=ax, facecolor="none", edgecolor="red") | ||
|
||
def plot_sketch_map(self, ax): | ||
"""Plot sketch map image.""" | ||
image = np.asarray(Image.open(self.sketch_map)) | ||
ax.imshow(image) | ||
|
||
def approve(self, *_): | ||
self.received_path.replace(self.approved_path) | ||
plt.close() | ||
|
||
def open(self): | ||
"""Open dialog for visual comparison.""" | ||
fig, axs = plt.subplots(1, 2) | ||
fig.subplots_adjust(bottom=0.2) | ||
self.plot_sketch_map(axs[0]) | ||
self.plot_difference(axs[1]) | ||
|
||
blue_patch = mpatches.Patch(color="blue", label="Approved") | ||
red_patch = mpatches.Patch(color="red", label="Received") | ||
axs[1].legend(handles=[blue_patch, red_patch]) | ||
|
||
ax_approve = fig.add_axes((0.45, 0.05, 0.1, 0.075)) | ||
button = Button(ax_approve, "Approve") | ||
button.on_clicked(self.approve) | ||
plt.show() | ||
|
||
|
||
class SketchMapToolReporter(Reporter): | ||
def __init__(self, sketch_map: Path): | ||
self.sketch_map: Path = sketch_map | ||
|
||
def report(self, received_path, approved_path): | ||
Approver(Path(approved_path), Path(received_path), self.sketch_map).open() | ||
return True |
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.
Where does tmp_path_factory come from? Do I just not see it?
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.
Its pytest magic. Its a global fixture defined by pytest. No import needed.
(The other fixtures are also not imported but defined by us in
conftest.py
which is discovered and parsed by pytest magic as well)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.
test_path_factory gives as a tmp dir unique to the current test run