Add types tests for geographic types
This commit is contained in:
@@ -68,16 +68,18 @@ impl FromSql for LineString<f64> {
|
||||
|
||||
// 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))
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
|
||||
28
postgres/tests/types/geo.rs
Normal file
28
postgres/tests/types/geo.rs
Normal file
@@ -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")]);
|
||||
}
|
||||
@@ -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<T: PartialEq+FromSql+ToSql, S: fmt::Display>(sql_type: &str, checks: &[(T, S)]) {
|
||||
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", TlsMode::None));
|
||||
|
||||
Reference in New Issue
Block a user