Skip to content

Commit 72d60ee

Browse files
committed
Add types tests for geographic types
1 parent e07b7b0 commit 72d60ee

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

postgres-shared/src/types/geo.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,18 @@ impl FromSql for LineString<f64> {
6868

6969
// let _ = types::bool_from_sql(&raw[0..1])?; // is path open or closed
7070
let n_points = types::int4_from_sql(&raw[1..5])? as usize;
71-
let raw_points = &raw[5..raw.len()-1];
71+
let raw_points = &raw[5..raw.len()];
7272
if raw_points.len() != 16 * n_points {
7373
return Err("invalid message length".into());
7474
}
7575

76+
let mut offset = 0;
7677
let mut points = Vec::with_capacity(n_points);
77-
for n in 0..n_points {
78-
let x = types::float8_from_sql(&raw[n..n+8])?;
79-
let y = types::float8_from_sql(&raw[n+8..n+16])?;
78+
for _ in 0..n_points {
79+
let x = types::float8_from_sql(&raw_points[offset..offset+8])?;
80+
let y = types::float8_from_sql(&raw_points[offset+8..offset+16])?;
8081
points.push(Point::new(x, y));
82+
offset += 16;
8183
}
8284
Ok(LineString(points))
8385
}

postgres/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ url = "1.0"
5858
bit-vec = "0.4"
5959
chrono = "0.3"
6060
eui48 = "0.1"
61+
geo = "0.4"
6162
rustc-serialize = "0.3"
6263
serde_json = "0.9"
6364
time = "0.1.14"

postgres/tests/types/geo.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
extern crate geo;
2+
3+
use self::geo::{Bbox, LineString, Point};
4+
use types::test_type;
5+
6+
#[test]
7+
fn test_point_params() {
8+
test_type("POINT",
9+
&[(Some(Point::new(0.0, 0.0)), "POINT(0, 0)"),
10+
(Some(Point::new(-3.14, 1.618)), "POINT(-3.14, 1.618)"),
11+
(None, "NULL")]);
12+
}
13+
14+
#[test]
15+
fn test_box_params() {
16+
test_type("BOX",
17+
&[(Some(Bbox{xmax: 160.0, ymax: 69701.5615, xmin: -3.14, ymin: 1.618}),
18+
"BOX(POINT(160.0, 69701.5615), POINT(-3.14, 1.618))"),
19+
(None, "NULL")]);
20+
}
21+
22+
#[test]
23+
fn test_path_params() {
24+
let points = vec![Point::new(0.0, 0.0), Point::new(-3.14, 1.618), Point::new(160.0, 69701.5615)];
25+
test_type("PATH",
26+
&[(Some(LineString(points)),"path '((0, 0), (-3.14, 1.618), (160.0, 69701.5615))'"),
27+
(None, "NULL")]);
28+
}

postgres/tests/types/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ mod rustc_serialize;
2323
mod serde_json;
2424
#[cfg(feature = "with-chrono")]
2525
mod chrono;
26+
#[cfg(feature = "with-geo")]
27+
mod geo;
2628

2729
fn test_type<T: PartialEq+FromSql+ToSql, S: fmt::Display>(sql_type: &str, checks: &[(T, S)]) {
2830
let conn = or_panic!(Connection::connect("postgres://postgres@localhost", TlsMode::None));

0 commit comments

Comments
 (0)