From 72d60ee0ba9850f0fca9e5f6eef706ede8559237 Mon Sep 17 00:00:00 2001 From: kestred Date: Fri, 10 Mar 2017 17:58:51 -0700 Subject: [PATCH] Add types tests for geographic types --- postgres-shared/src/types/geo.rs | 10 ++++++---- postgres/Cargo.toml | 1 + postgres/tests/types/geo.rs | 28 ++++++++++++++++++++++++++++ postgres/tests/types/mod.rs | 2 ++ 4 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 postgres/tests/types/geo.rs diff --git a/postgres-shared/src/types/geo.rs b/postgres-shared/src/types/geo.rs index e6bb2e11..13080495 100644 --- a/postgres-shared/src/types/geo.rs +++ b/postgres-shared/src/types/geo.rs @@ -68,16 +68,18 @@ impl FromSql for LineString { // let _ = types::bool_from_sql(&raw[0..1])?; // is path open or closed let n_points = types::int4_from_sql(&raw[1..5])? as usize; - let raw_points = &raw[5..raw.len()-1]; + let raw_points = &raw[5..raw.len()]; if raw_points.len() != 16 * n_points { return Err("invalid message length".into()); } + let mut offset = 0; let mut points = Vec::with_capacity(n_points); - for n in 0..n_points { - let x = types::float8_from_sql(&raw[n..n+8])?; - let y = types::float8_from_sql(&raw[n+8..n+16])?; + for _ in 0..n_points { + let x = types::float8_from_sql(&raw_points[offset..offset+8])?; + let y = types::float8_from_sql(&raw_points[offset+8..offset+16])?; points.push(Point::new(x, y)); + offset += 16; } Ok(LineString(points)) } diff --git a/postgres/Cargo.toml b/postgres/Cargo.toml index bb157ccc..47270e2b 100644 --- a/postgres/Cargo.toml +++ b/postgres/Cargo.toml @@ -58,6 +58,7 @@ url = "1.0" bit-vec = "0.4" chrono = "0.3" eui48 = "0.1" +geo = "0.4" rustc-serialize = "0.3" serde_json = "0.9" time = "0.1.14" diff --git a/postgres/tests/types/geo.rs b/postgres/tests/types/geo.rs new file mode 100644 index 00000000..7b6afc0d --- /dev/null +++ b/postgres/tests/types/geo.rs @@ -0,0 +1,28 @@ +extern crate geo; + +use self::geo::{Bbox, LineString, Point}; +use types::test_type; + +#[test] +fn test_point_params() { + test_type("POINT", + &[(Some(Point::new(0.0, 0.0)), "POINT(0, 0)"), + (Some(Point::new(-3.14, 1.618)), "POINT(-3.14, 1.618)"), + (None, "NULL")]); +} + +#[test] +fn test_box_params() { + test_type("BOX", + &[(Some(Bbox{xmax: 160.0, ymax: 69701.5615, xmin: -3.14, ymin: 1.618}), + "BOX(POINT(160.0, 69701.5615), POINT(-3.14, 1.618))"), + (None, "NULL")]); +} + +#[test] +fn test_path_params() { + let points = vec![Point::new(0.0, 0.0), Point::new(-3.14, 1.618), Point::new(160.0, 69701.5615)]; + test_type("PATH", + &[(Some(LineString(points)),"path '((0, 0), (-3.14, 1.618), (160.0, 69701.5615))'"), + (None, "NULL")]); +} diff --git a/postgres/tests/types/mod.rs b/postgres/tests/types/mod.rs index 719f5d39..7634452a 100644 --- a/postgres/tests/types/mod.rs +++ b/postgres/tests/types/mod.rs @@ -23,6 +23,8 @@ mod rustc_serialize; mod serde_json; #[cfg(feature = "with-chrono")] mod chrono; +#[cfg(feature = "with-geo")] +mod geo; fn test_type(sql_type: &str, checks: &[(T, S)]) { let conn = or_panic!(Connection::connect("postgres://postgres@localhost", TlsMode::None));