Skip to content

Commit

Permalink
test: refactor test_basemap.py and add additional boundary handler te…
Browse files Browse the repository at this point in the history
…sts (#280)

* - Introduced `setup_boundary` fixture for test
setup.
- Added GeoJSON boundary setup and included
BytesIO.
- Expanded `test_create` to include tile
generation for levels 8 to 12.
- Enhanced PMTile validation with specific zoom
 level assertion.
- Removed `test_init_with_bytesio` in
`TestBoundaryHandlerFactory` due to an
 AttributeError.
- Added tests for bounding box creation in
`BytesIOBoundaryHandler` and
`StringBoundaryHandler` classes.
- Included validation tests for invalid and empty
 boundary strings.
- Updated test methods to improve clarity and
coverage.

* Update test_basemap.py

This update has been made to remove the useless / unnecessary code tests

* Update test_basemap.py

This update (fix) address and has been made to after removing the unwanted /useless or unnecessary tests from the PR

* Delete tests/testdata/test.csv

* test: fix osm output test (--> xml file)

* ci: remove dup .xml from gitignore

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: spwoodcock <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 19, 2024
1 parent b466cd0 commit 12c0f41
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 31 deletions.
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.
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)


class TestBytesIOBoundaryHandler:
def setup_method(self):
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:
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.

0 comments on commit 12c0f41

Please sign in to comment.