Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebarron committed Sep 3, 2024
1 parent b6a0697 commit d43d43d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 36 deletions.
59 changes: 24 additions & 35 deletions tests/test_duckdb.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
from pathlib import Path
from tempfile import TemporaryDirectory
from urllib.request import urlretrieve

import duckdb
import geodatasets
import pytest

from lonboard import PolygonLayer, ScatterplotLayer, SolidPolygonLayer, viz

cities_url = "https://naciscdn.org/naturalearth/110m/cultural/ne_110m_populated_places_simple.zip"
cities_path = Path("ne_110m_populated_places_simple.zip")

def test_viz_geometry():
gpd = pytest.importorskip("geopandas")
points_path = gpd.datasets.get_path("naturalearth_cities")
if not cities_path.exists():
urlretrieve(cities_url, "ne_110m_populated_places_simple.zip")

cities_gdal_path = f"/vsizip/{cities_path}"


def test_viz_geometry():
con = duckdb.connect()
sql = f"""
INSTALL spatial;
LOAD spatial;
SELECT * FROM ST_Read("{points_path}");
SELECT * FROM ST_Read("{cities_gdal_path}");
"""
rel = con.sql(sql)
assert rel.types[-1] == "GEOMETRY"
Expand All @@ -23,14 +31,11 @@ def test_viz_geometry():


def test_viz_wkb_blob():
gpd = pytest.importorskip("geopandas")
points_path = gpd.datasets.get_path("naturalearth_cities")

con = duckdb.connect()
sql = f"""
INSTALL spatial;
LOAD spatial;
SELECT name, ST_AsWKB(geom) as geom FROM ST_Read("{points_path}");
SELECT name, ST_AsWKB(geom) as geom FROM ST_Read("{cities_gdal_path}");
"""
rel = con.sql(sql)
assert rel.types[-1] == "WKB_BLOB"
Expand All @@ -39,14 +44,11 @@ def test_viz_wkb_blob():


def test_viz_point_2d():
gpd = pytest.importorskip("geopandas")
points_path = gpd.datasets.get_path("naturalearth_cities")

con = duckdb.connect()
sql = f"""
INSTALL spatial;
LOAD spatial;
SELECT name, CAST(geom as POINT_2D) as geom FROM ST_Read("{points_path}");
SELECT name, CAST(geom as POINT_2D) as geom FROM ST_Read("{cities_gdal_path}");
"""
rel = con.sql(sql)
assert rel.types[-1] == "POINT_2D"
Expand All @@ -58,7 +60,7 @@ def test_viz_bbox_2d():
gpd = pytest.importorskip("geopandas")

with TemporaryDirectory() as tmpdir:
nybb = gpd.read_file(gpd.datasets.get_path("nybb"))
nybb = gpd.read_file(geodatasets.get_path("nybb"))
nybb = nybb.to_crs("EPSG:4326")
tmp_path = f"{tmpdir}/nybb.shp"
nybb.to_file(tmp_path)
Expand All @@ -77,14 +79,11 @@ def test_viz_bbox_2d():


def test_layer_geometry():
gpd = pytest.importorskip("geopandas")
points_path = gpd.datasets.get_path("naturalearth_cities")

con = duckdb.connect()
sql = f"""
INSTALL spatial;
LOAD spatial;
SELECT * FROM ST_Read("{points_path}");
SELECT * FROM ST_Read("{cities_gdal_path}");
"""
rel = con.sql(sql)
assert rel.types[-1] == "GEOMETRY"
Expand All @@ -93,14 +92,11 @@ def test_layer_geometry():


def test_layer_wkb_blob():
gpd = pytest.importorskip("geopandas")
points_path = gpd.datasets.get_path("naturalearth_cities")

con = duckdb.connect()
sql = f"""
INSTALL spatial;
LOAD spatial;
SELECT name, ST_AsWKB(geom) as geom FROM ST_Read("{points_path}");
SELECT name, ST_AsWKB(geom) as geom FROM ST_Read("{cities_gdal_path}");
"""
rel = con.sql(sql)
assert rel.types[-1] == "WKB_BLOB"
Expand All @@ -109,14 +105,11 @@ def test_layer_wkb_blob():


def test_layer_point_2d():
gpd = pytest.importorskip("geopandas")
points_path = gpd.datasets.get_path("naturalearth_cities")

con = duckdb.connect()
sql = f"""
INSTALL spatial;
LOAD spatial;
SELECT name, CAST(geom as POINT_2D) as geom FROM ST_Read("{points_path}");
SELECT name, CAST(geom as POINT_2D) as geom FROM ST_Read("{cities_gdal_path}");
"""
rel = con.sql(sql)
assert rel.types[-1] == "POINT_2D"
Expand All @@ -128,7 +121,8 @@ def test_layer_bbox_2d():
gpd = pytest.importorskip("geopandas")

with TemporaryDirectory() as tmpdir:
nybb = gpd.read_file(gpd.datasets.get_path("nybb"))
nybb = gpd.read_file(geodatasets.get_path("nybb"))
nybb = nybb.to_crs("EPSG:4326")
tmp_path = f"{tmpdir}/nybb.shp"
nybb.to_file(tmp_path)

Expand All @@ -149,7 +143,8 @@ def test_solid_polygon_layer_bbox_2d():
gpd = pytest.importorskip("geopandas")

with TemporaryDirectory() as tmpdir:
nybb = gpd.read_file(gpd.datasets.get_path("nybb"))
nybb = gpd.read_file(geodatasets.get_path("nybb"))
nybb = nybb.to_crs("EPSG:4326")
tmp_path = f"{tmpdir}/nybb.shp"
nybb.to_file(tmp_path)

Expand All @@ -168,27 +163,21 @@ def test_solid_polygon_layer_bbox_2d():

@pytest.mark.skip("Skip because it mutates global state")
def test_create_table_as():
gpd = pytest.importorskip("geopandas")
points_path = gpd.datasets.get_path("naturalearth_cities")

sql = f"""
INSTALL spatial;
LOAD spatial;
CREATE TABLE test AS SELECT * FROM ST_Read("{points_path}");
CREATE TABLE test AS SELECT * FROM ST_Read("{cities_gdal_path}");
"""
duckdb.execute(sql)
m = viz(duckdb.table("test"))
assert isinstance(m.layers[0], ScatterplotLayer)


def test_create_table_as_custom_con():
gpd = pytest.importorskip("geopandas")
points_path = gpd.datasets.get_path("naturalearth_cities")

sql = f"""
INSTALL spatial;
LOAD spatial;
CREATE TABLE test AS SELECT * FROM ST_Read("{points_path}");
CREATE TABLE test AS SELECT * FROM ST_Read("{cities_gdal_path}");
"""
con = duckdb.connect()
con.execute(sql)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_accessor_length_validation_extension():

points = shapely.points([1, 2], [3, 4])
gdf = gpd.GeoDataFrame(geometry=points)
extension = DataFilterExtension()
extension = DataFilterExtension(filter_size=1)

with pytest.raises(TraitError, match="same length as table"):
_layer = ScatterplotLayer.from_geopandas(
Expand Down

0 comments on commit d43d43d

Please sign in to comment.