Skip to content
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

Start unit tests for cycle paths getProps, with a tool for generating… #30

Merged
merged 1 commit into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions layers/cycle_paths_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import cycle_paths
import unittest


class TestGetProps(unittest.TestCase):
def test_getProps(self):
# You can generate entries using ../osm_unit_test_tool.html. The output
# is [kind, direction, width]. You can manually remove very irrelevant
# tags (like name) at your own discretion
for testCase in [
{
"id": "29063778",
"tags": ["highway=cycleway", "oneway=yes", "sidewalk=left"],
"output": ["track", "one-way", "unknown"],
},
{
"id": "256017834",
"tags": [
"bicycle=designated",
"foot=designated",
"highway=cycleway",
"segregated=yes",
],
"output": ["shared_use_segregated", "unknown", "unknown"],
},
]:
tags = {}
for tag in testCase["tags"]:
key, value = tag.split("=")
tags[key] = value
tags["@id"] = testCase["id"]

actualResult = cycle_paths.getProps(tags)

kind, direction, width = testCase["output"]
expectedResult = {
"kind": kind,
"direction": direction,
"width": width,
"osm_id": testCase["id"],
}

self.assertEqual(actualResult, expectedResult)


if __name__ == "__main__":
unittest.main()
52 changes: 52 additions & 0 deletions osm_unit_test_tool.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<!DOCTYPE html>
<html>
<head>
<title>OpenStreetMap Unit Test tool</title>
<script>
async function generate() {
document.getElementById("output").value = "";

let wayID = document.getElementById("wayID").value;
let url = `https://api.openstreetmap.org/api/0.6/way/${wayID}.json`;
try {
let resp = await fetch(url);
let json = await resp.json();
let tags = json.elements[0].tags;

// Remove tags likely to not influence the output at all
delete tags["name"];
let tagsArray = Object.entries(tags).map(([k, v]) => `${k}=${v}`);

let output = {
id: wayID,
tags: tagsArray,
output: ["kind", "direction", "width"],
};
document.getElementById("output").value = JSON.stringify(
output,
null,
2
);
} catch (err) {
document.getElementById("output").value = `Error: ${err}`;
}
}

function copy() {
navigator.clipboard.writeText(document.getElementById("output").value);
}
</script>
</head>
<body>
<h1>OpenStreetMap Unit Test tool</h1>
<label for="wayID">Enter OpenStreetMap Way ID:</label>
<input type="text" id="wayID" />
<button onclick="generate()">Generate</button>
<br />

<h2>Output:</h2>
<button onclick="copy()">Copy</button>
<br />
<textarea id="output" rows="10" cols="100" readonly></textarea>
</body>
</html>