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

- Refactor test_basemap.py and add additional boundary handler tests #280

Merged
merged 7 commits into from
Aug 19, 2024
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
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ NEWS
*.xml
*.geojson
*.pbf
*.xml
*.pdf
*.html
*.mbtiles
Expand Down
123 changes: 102 additions & 21 deletions tests/test_basemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with osm_fieldwork. If not, see <https:#www.gnu.org/licenses/>.
# along with osm_fieldwork. If not, see <https://www.gnu.org/licenses/>.
#
"""Test functionalty of basemapper.py."""
"""Test functionality of basemapper.py."""

import json
import logging
import os
import shutil
Expand All @@ -29,7 +30,13 @@
from pmtiles.reader import MemorySource
from pmtiles.reader import Reader as PMTileReader

from osm_fieldwork.basemapper import BaseMapper, create_basemap_file
from osm_fieldwork.basemapper import (
BaseMapper,
BoundaryHandlerFactory,
BytesIOBoundaryHandler,
StringBoundaryHandler,
create_basemap_file,
)
from osm_fieldwork.sqlite import DataFile

log = logging.getLogger(__name__)
Expand All @@ -41,40 +48,40 @@
object_boundary = BytesIO(boundary)
outfile = f"{rootdir}/testdata/rollinsville.mbtiles"
base = "./tiles"
# boundary = open(infile, "r")
# poly = geojson.load(boundary)
# if "features" in poly:
# geometry = shape(poly["features"][0]["geometry"])
# elif "geometry" in poly:
# geometry = shape(poly["geometry"])
# else:
# geometry = shape(poly)


@pytest.fixture
def setup_boundary():
return string_boundary, object_boundary


@pytest.mark.parametrize("boundary", [string_boundary, object_boundary])
def test_create(boundary):
"""See if the file got loaded."""
"""See if the file got loaded and tiles are correct."""
hits = 0
basemap = BaseMapper(boundary, base, "topo")
tiles = list()
for level in [8, 9, 10, 11, 12]:
tiles = []
for level in range(8, 13):
basemap.getTiles(level)
tiles += basemap.tiles

assert len(tiles) > 0, "No tiles were created"

if len(tiles) == 5:
hits += 1

if tiles[0].x == 52 and tiles[1].y == 193 and tiles[2].x == 211:
if len(tiles) >= 3 and tiles[0].x == 52 and tiles[1].y == 193 and tiles[2].x == 211:
hits += 1

outf = DataFile(outfile, basemap.getFormat())
outf.writeTiles(tiles, base)

assert os.path.exists(outfile), "Output file was not created"
assert hits == 2, "Hit count does not match expected value"

os.remove(outfile)
shutil.rmtree(base)

assert hits == 2


def test_pmtiles():
"""Test PMTile creation via helper function.
RonaldRonnie marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -91,14 +98,12 @@ def test_pmtiles():
)
pmtile_file = Path(f"{rootdir}/../test.pmtiles")
assert pmtile_file.exists()

# Test reading as form of validation
with open(pmtile_file, "rb") as pmtile_file:
data = pmtile_file.read()
pmtile = PMTileReader(MemorySource(data))

data_length = pmtile.header().get("tile_data_length", 0)
assert data_length > 2000 and data_length < 80000
assert 2000 < data_length < 80000, "Data length out of expected range"
assert len(pmtile.metadata().keys()) == 1

metadata = pmtile.metadata()
Expand All @@ -112,5 +117,81 @@ def test_pmtiles():
assert max_zoom == 14


class TestBoundaryHandlerFactory:
def test_get_bounding_box(self):
boundary = "10,20,30,40"
factory = BoundaryHandlerFactory(boundary)
assert factory.get_bounding_box() == (10, 20, 30, 40)
RonaldRonnie marked this conversation as resolved.
Show resolved Hide resolved


class TestBytesIOBoundaryHandler:
def setup_method(self):
RonaldRonnie marked this conversation as resolved.
Show resolved Hide resolved
geojson_data = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-73.9876, 40.7661],
[-73.9857, 40.7661],
[-73.9857, 40.7641],
[-73.9876, 40.7641],
[-73.9876, 40.7661],
]
],
},
}
],
}
self.boundary = BytesIO(json.dumps(geojson_data).encode("utf-8"))
self.handler = BytesIOBoundaryHandler(self.boundary)

def test_make_bbox(self):
valid_geojson_data = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[-73.9876, 40.7661],
[-73.9857, 40.7661],
[-73.9857, 40.7641],
[-73.9876, 40.7641],
[-73.9876, 40.7661],
]
],
},
}
],
}
self.boundary = BytesIO(json.dumps(valid_geojson_data).encode("utf-8"))
handler = BytesIOBoundaryHandler(self.boundary)
bbox = handler.make_bbox()
assert bbox == (-73.9876, 40.7641, -73.9857, 40.7661)


class TestStringBoundaryHandler:
RonaldRonnie marked this conversation as resolved.
Show resolved Hide resolved
def test_make_bbox(self):
handler = StringBoundaryHandler("10,20,30,40")
bbox = handler.make_bbox()
assert bbox == (10, 20, 30, 40)

def test_make_bbox_invalid(self):
handler = StringBoundaryHandler("10,20,30")
with pytest.raises(ValueError):
handler.make_bbox()

def test_make_bbox_empty(self):
handler = StringBoundaryHandler("")
with pytest.raises(ValueError):
handler.make_bbox()


if __name__ == "__main__":
test_create()
pytest.main()
5 changes: 2 additions & 3 deletions tests/test_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_init():
assert len(csv.yaml.yaml) > 0


def test_osm_entry(infile=f"{rootdir}/testdata/test.csv"):
def test_osm_entry(infile=f"{rootdir}/testdata/test-out.xml"):
csv = ODKParsers()
out = OutSupport()
out.createOSM(infile)
Expand All @@ -59,12 +59,11 @@ def test_osm_entry(infile=f"{rootdir}/testdata/test.csv"):
"user": "Foobar",
}
csv.createEntry(line)
# assert tmp


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Read and parse a CSV file from ODK Central")
parser.add_argument("--infile", default=f"{rootdir}/testdata/test.csv", help="The CSV input file")
parser.add_argument("--infile", default=f"{rootdir}/testdata/test-out.xml", help="The CSV input file")
args = parser.parse_args()

test_init()
Expand Down
6 changes: 0 additions & 6 deletions tests/testdata/test.csv

This file was deleted.

Loading