Skip to content
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
shell: bash
run: |
psql -P pager=off -v ON_ERROR_STOP=1 -f geozero/tests/data/postgis.sql $DATABASE_URL
cargo test -p geozero --all-features -- --ignored postgis --test-threads 1
cargo test -p geozero --all-features --test postgis -- --test-threads 1
env:
DATABASE_URL: "${{ steps.pg.outputs.connection-uri }}?sslmode=disable"

Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,3 +266,19 @@ Full source code: [kdbush.rs](./geozero/tests/kdbush.rs)
# Ubuntu/Debian/Mint
apt-get install -y libgeos-dev libgdal-dev
```

## Testing

PostGIS integration tests in `geozero/tests/postgis.rs` run automatically when `DATABASE_URL` is set.
If `DATABASE_URL` is not set, those tests are skipped with an informational message.

Examples:

```sh
# regular test run (PostGIS tests auto-skip when DATABASE_URL is unset)
cargo test -p geozero --all-features

# run PostGIS tests explicitly
DATABASE_URL=postgres://ci:ci@localhost:5432/test?sslmode=disable \
cargo test -p geozero --all-features --test postgis -- --test-threads 1
```
50 changes: 39 additions & 11 deletions geozero/tests/postgis.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
mod pg {
pub fn should_skip_postgis_tests() -> bool {
if std::env::var("DATABASE_URL").is_ok() {
false
} else {
eprintln!("Skipping PostGIS tests: DATABASE_URL is not set");
true
}
}

pub fn get_db_string() -> String {
std::env::var("DATABASE_URL").unwrap()
}
Expand All @@ -21,8 +30,10 @@ mod postgis_postgres {
use geozero::wkt::WktWriter;

#[test]
#[ignore]
fn blob_query() -> Result<(), postgres::error::Error> {
if crate::pg::should_skip_postgis_tests() {
return Ok(());
}
let mut client = postgres::Client::connect(&get_db_string(), postgres::NoTls).unwrap();
let row = client.query_one(
"SELECT 'SRID=4326;POLYGON ((0 0, 2 0, 2 2, 0 2, 0 0))'::geometry::bytea",
Expand All @@ -36,8 +47,10 @@ mod postgis_postgres {
}

#[test]
#[ignore]
fn rust_geo_query() -> Result<(), postgres::error::Error> {
if crate::pg::should_skip_postgis_tests() {
return Ok(());
}
let mut client = postgres::Client::connect(&get_db_string(), postgres::NoTls).unwrap();

let row = client.query_one(
Expand Down Expand Up @@ -73,9 +86,11 @@ mod postgis_postgres {
}

#[test]
#[ignore]
#[cfg(feature = "with-geos")]
fn geos_query() -> Result<(), postgres::error::Error> {
if crate::pg::should_skip_postgis_tests() {
return Ok(());
}
let mut client = postgres::Client::connect(&get_db_string(), postgres::NoTls).unwrap();

let row = client.query_one(
Expand Down Expand Up @@ -124,8 +139,10 @@ mod postgis_postgres {
}

#[test]
#[ignore]
fn geometry_query() -> Result<(), postgres::error::Error> {
if crate::pg::should_skip_postgis_tests() {
return Ok(());
}
let mut client = postgres::Client::connect(&get_db_string(), postgres::NoTls).unwrap();

let row = client.query_one(
Expand All @@ -148,8 +165,10 @@ mod postgis_sqlx {
use geozero::wkb;

#[tokio::test]
#[ignore]
async fn blob_query() -> Result<(), sqlx::Error> {
if pg::should_skip_postgis_tests() {
return Ok(());
}
let pool = pg::get_pool().await;

let row: (Vec<u8>,) = sqlx::query_as(
Expand All @@ -173,8 +192,10 @@ mod postgis_sqlx {
}

#[tokio::test]
#[ignore]
async fn point3d_query() -> Result<(), sqlx::Error> {
if pg::should_skip_postgis_tests() {
return Ok(());
}
let pool = pg::get_pool().await;

let row: (PointZ,) = sqlx::query_as("SELECT 'POINT(1 2 3)'::geometry")
Expand All @@ -195,8 +216,10 @@ mod postgis_sqlx {
}

#[tokio::test]
#[ignore]
async fn rust_geo_query() -> Result<(), sqlx::Error> {
if pg::should_skip_postgis_tests() {
return Ok(());
}
let pool = pg::get_pool().await;

let row: (wkb::Decode<geo_types::Geometry<f64>>,) =
Expand Down Expand Up @@ -233,8 +256,10 @@ mod postgis_sqlx {
}

#[tokio::test]
#[ignore]
async fn bulk_insert() -> Result<(), sqlx::Error> {
if pg::should_skip_postgis_tests() {
return Ok(());
}
// https://github.com/launchbadge/sqlx/blob/v0.5.13/FAQ.md#how-can-i-bind-an-array-to-a-values-clause-how-can-i-do-bulk-inserts
let pool = pg::get_pool().await;

Expand All @@ -253,7 +278,6 @@ mod postgis_sqlx {
}

#[tokio::test]
#[ignore]
// Requires DATABASE_URL at compile time
#[cfg(not(test))] // Delete this line to compile the test
async fn rust_geo_macro_query() -> Result<(), sqlx::Error> {
Expand Down Expand Up @@ -310,9 +334,11 @@ mod postgis_sqlx {
}

#[tokio::test]
#[ignore]
#[cfg(feature = "with-geos")]
async fn geos_query() -> Result<(), sqlx::Error> {
if pg::should_skip_postgis_tests() {
return Ok(());
}
let pool = pg::get_pool().await;

let row: (wkb::Decode<geos::Geometry>,) =
Expand Down Expand Up @@ -376,8 +402,10 @@ mod postgis_sqlx {
}

#[tokio::test]
#[ignore]
async fn geometry_query() -> Result<(), sqlx::Error> {
if pg::should_skip_postgis_tests() {
return Ok(());
}
let pool = pg::get_pool().await;

let row: (Text,) =
Expand Down
Loading