Skip to content

Commit

Permalink
Cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bchapuis committed Oct 25, 2023
1 parent cdb0e85 commit aaf81d1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void delete(TileCoord tileCoord) {
}


public static TileQuery prepareQuery(Tileset tileset, int zoom) {
protected static TileQuery prepareQuery(Tileset tileset, int zoom) {
// Initialize a builder for the tile query
var tileQuery = new StringBuilder();
tileQuery.append("SELECT (");
Expand Down Expand Up @@ -117,11 +117,8 @@ public static TileQuery prepareQuery(Tileset tileset, int zoom) {
.replace(";", "")
.replace("?", "??")
.replace("$zoom", String.valueOf(zoom));
var queryWithParams = String.format("""
SELECT ST_AsMVTGeom(t.geom, ST_TileEnvelope(?, ?, ?)) AS geom, t.tags, t.id
FROM (%s) AS t
WHERE t.geom && ST_TileEnvelope(?, ?, ?, margin => (64.0/4096))
""", sql);
var queryWithParams = String.format("SELECT ST_AsMVTGeom(t.geom, ST_TileEnvelope(?, ?, ?)) AS geom, t.tags, t.id " +
"FROM (%s) AS t WHERE t.geom && ST_TileEnvelope(?, ?, ?, margin => (64.0/4096))", sql);
layerQuery.append(queryWithParams);

// Increase the parameter count (e.g. ?) and query count
Expand Down Expand Up @@ -162,16 +159,7 @@ WHERE t.geom && ST_TileEnvelope(?, ?, ?, margin => (64.0/4096))
return new TileQuery(query, paramCount);
}

public static class TileQuery {

private final String query;

private final int paramCount;

public TileQuery(String query, int paramCount) {
this.query = query;
this.paramCount = paramCount;
}
public record TileQuery(String query, int paramCount) {

public ByteBuffer execute(Connection connection, TileCoord tileCoord)
throws SQLException, IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import java.util.List;
import java.util.Map;
import org.apache.baremaps.tilestore.TileCoord;
import org.apache.baremaps.vectortile.tileset.Tileset;
import org.apache.baremaps.vectortile.tileset.TilesetLayer;
import org.apache.baremaps.vectortile.tileset.TilesetQuery;
Expand All @@ -30,86 +29,18 @@
class PostgresTileStoreTest {

@Test
void sameQueries() {
Tileset tileset = new Tileset();
void prepareQuery() {
var tileset = new Tileset();
tileset.setMinzoom(0);
tileset.setMaxzoom(20);
tileset.setVectorLayers(List.of(
new TilesetLayer("a", Map.of(), "", 0, 20,
List.of(new TilesetQuery(0, 20, "SELECT id, tags, geom FROM table"))),
new TilesetLayer("b", Map.of(), "", 0, 20,
List.of(new TilesetQuery(0, 20, "SELECT id, tags, geom FROM table")))));
PostgresTileStore tileStore = new PostgresTileStore(null, tileset);
String query = tileStore.withQuery(new TileCoord(0, 0, 10));
var query = PostgresTileStore.prepareQuery(tileset, 10);
assertEquals(
"""
SELECT ((WITH mvtgeom AS (
SELECT ST_AsMVTGeom(t.geom, ST_TileEnvelope(10, 0, 0)) AS geom, t.tags, t.id
FROM (SELECT id, tags, geom FROM table) AS t
WHERE t.geom && ST_TileEnvelope(10, 0, 0, margin => (64.0/4096))
) SELECT ST_AsMVT(mvtgeom.*, 'a') FROM mvtgeom
) || (WITH mvtgeom AS (
SELECT ST_AsMVTGeom(t.geom, ST_TileEnvelope(10, 0, 0)) AS geom, t.tags, t.id
FROM (SELECT id, tags, geom FROM table) AS t
WHERE t.geom && ST_TileEnvelope(10, 0, 0, margin => (64.0/4096))
) SELECT ST_AsMVT(mvtgeom.*, 'b') FROM mvtgeom
)) mvtTile""",
query);
}

@Test
void differentConditions1() {
Tileset tileset = new Tileset();
tileset.setMinzoom(0);
tileset.setMaxzoom(20);
tileset.setVectorLayers(List.of(
new TilesetLayer("a", Map.of(), "", 0, 20,
List.of(new TilesetQuery(0, 20, "SELECT id, tags, geom FROM table"))),
new TilesetLayer("b", Map.of(), "", 0, 20, List
.of(new TilesetQuery(0, 20, "SELECT id, tags, geom FROM table WHERE condition = 1")))));
PostgresTileStore tileStore = new PostgresTileStore(null, tileset);
String query = tileStore.withQuery(new TileCoord(0, 0, 10));
assertEquals("""
SELECT ((WITH mvtgeom AS (
SELECT ST_AsMVTGeom(t.geom, ST_TileEnvelope(10, 0, 0)) AS geom, t.tags, t.id
FROM (SELECT id, tags, geom FROM table) AS t
WHERE t.geom && ST_TileEnvelope(10, 0, 0, margin => (64.0/4096))
) SELECT ST_AsMVT(mvtgeom.*, 'a') FROM mvtgeom
) || (WITH mvtgeom AS (
SELECT ST_AsMVTGeom(t.geom, ST_TileEnvelope(10, 0, 0)) AS geom, t.tags, t.id
FROM (SELECT id, tags, geom FROM table WHERE condition = 1) AS t
WHERE t.geom && ST_TileEnvelope(10, 0, 0, margin => (64.0/4096))
) SELECT ST_AsMVT(mvtgeom.*, 'b') FROM mvtgeom
)) mvtTile""",
query);
}

@Test
void differentConditions2() {
Tileset tileset = new Tileset();
tileset.setMinzoom(0);
tileset.setMaxzoom(20);
tileset.setVectorLayers(List.of(
new TilesetLayer("a", Map.of(), "", 0, 20,
List.of(
new TilesetQuery(0, 20, "SELECT id, tags, geom FROM table WHERE condition = 1"))),
new TilesetLayer("b", Map.of(), "", 0, 20, List
.of(new TilesetQuery(0, 20, "SELECT id, tags, geom FROM table WHERE condition = 2")))));
PostgresTileStore tileStore = new PostgresTileStore(null, tileset);
String query = tileStore.withQuery(new TileCoord(0, 0, 10));
assertEquals(
"""
SELECT ((WITH mvtgeom AS (
SELECT ST_AsMVTGeom(t.geom, ST_TileEnvelope(10, 0, 0)) AS geom, t.tags, t.id
FROM (SELECT id, tags, geom FROM table WHERE condition = 1) AS t
WHERE t.geom && ST_TileEnvelope(10, 0, 0, margin => (64.0/4096))
) SELECT ST_AsMVT(mvtgeom.*, 'a') FROM mvtgeom
) || (WITH mvtgeom AS (
SELECT ST_AsMVTGeom(t.geom, ST_TileEnvelope(10, 0, 0)) AS geom, t.tags, t.id
FROM (SELECT id, tags, geom FROM table WHERE condition = 2) AS t
WHERE t.geom && ST_TileEnvelope(10, 0, 0, margin => (64.0/4096))
) SELECT ST_AsMVT(mvtgeom.*, 'b') FROM mvtgeom
)) mvtTile""",
query);
"SELECT ((WITH mvtGeom AS (SELECT ST_AsMVTGeom(t.geom, ST_TileEnvelope(?, ?, ?)) AS geom, t.tags, t.id FROM (SELECT id, tags, geom FROM table) AS t WHERE t.geom && ST_TileEnvelope(?, ?, ?, margin => (64.0/4096))) SELECT ST_AsMVT(mvtGeom.*, 'a') FROM mvtGeom) || (WITH mvtGeom AS (SELECT ST_AsMVTGeom(t.geom, ST_TileEnvelope(?, ?, ?)) AS geom, t.tags, t.id FROM (SELECT id, tags, geom FROM table) AS t WHERE t.geom && ST_TileEnvelope(?, ?, ?, margin => (64.0/4096))) SELECT ST_AsMVT(mvtGeom.*, 'b') FROM mvtGeom)) mvtTile",
query.query());
}
}

0 comments on commit aaf81d1

Please sign in to comment.