-
-
Notifications
You must be signed in to change notification settings - Fork 77
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
Added generic test case handling #117
Conversation
To give you a better impression of the idea I've added a few more test cases (all the The nice thing about storing all the expected results in the geojsons is that is quite easy to come up with a visualization of what we are testing against. For instance I'm using the attached small Python script to verify the contents of the expected test results: test_cases.pdf There is probably a similarly easy way to visualize the same with plain JS as well, just to as a starting point... This makes it quite obvious that e.g. Side notes:
Python plot script#!/usr/bin/env python
from __future__ import print_function
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
import json
import os
import sys
def extract_multi_polygon(feature):
kind = feature["geometry"]["type"]
if kind == "Polygon":
return [feature["geometry"]["coordinates"]]
elif kind == "MultiPolygon":
return feature["geometry"]["coordinates"]
else:
raise ValueError("Feature has wrong type: {}".format(kind))
def plot(ax, multi_polygon, label):
for j, polygon in enumerate(multi_polygon):
for k, ring in enumerate(polygon):
xs = [p[0] for p in ring]
ys = [p[1] for p in ring]
ax.plot(xs, ys, "o-", label="{} (poly = {}, ring = {})".format(label, j + 1, k + 1), ms=2)
def main(interactive=False):
if len(sys.argv) < 2:
print("ERROR: No geojson files specified.")
sys.exit(1)
else:
files = sys.argv[1:]
with PdfPages("test_cases.pdf") as pp:
for f in sorted(files):
data = json.load(open(f))
assert data["type"] == "FeatureCollection"
features = data["features"]
assert len(features) >= 2
p1 = extract_multi_polygon(features[0])
p2 = extract_multi_polygon(features[1])
for feature in features[2:]:
op = feature["properties"]["operation"]
p_res = extract_multi_polygon(feature)
fig, axes = plt.subplots(1, 3, figsize=(18, 10), sharex=True, sharey=True)
plot(axes[0], p1, "A")
plot(axes[0], p2, "B")
plot(axes[1], p_res, "Result")
plot(axes[2], p1, "A")
plot(axes[2], p2, "B")
plot(axes[2], p_res, "Result")
#filename_out = filename.replace(".json", ".png")
#plt.savefig(filename_out)
axes[0].legend(loc="best")
axes[1].legend(loc="best")
axes[2].legend(loc="best")
fig.suptitle("{} / {}".format(os.path.basename(f), op))
plt.tight_layout()
plt.subplots_adjust(top=0.93)
if interactive:
plt.show()
pp.savefig(fig)
plt.close(fig)
if __name__ == "__main__":
main() |
It's a very cool addition, cause basically all the edge cases go there and require adding the dummy lines of code. I thought about doing something like that for the demo app as well |
👍 Okay I spend the evening converting all manually written edge cases into test case GeoJSONs. This gives the best overview: A few notes:
|
@@ -0,0 +1,34 @@ | |||
{ |
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.
This file is just intended to make it easier to add new test cases.
This implements #116, and I'm actually quite happy with how it turned out. A few notes:
edge_cases.test.js
can be handled by the system. So adding test cases doesn't require to write any code!diff
is asymmetric, I've added a modediff_ba
which allows to test for the reverse diff as well.issueXXX
pattern, but also basic cases like thehourglasses
I've migrated.glob
.REGEN=true
mode as suggested by @rowanwins. This works really nicely, just runREGEN=true npm run test
and the test cases will be updated. This also provides "auto formatting" of the test cases as a bonus feature. The defaultJSON.stringify
is a little bit too spacious when enabling line wrapping (because it requires several lines for each[x, y]
point array). I'm usingjson-stringify-pretty-compact
which seems to fit our use case very well and produces well formatted JSON -- see thehourglasses.geojson
, which is formatted by that logic.