Skip to content

Commit eb5b412

Browse files
committed
Add round trip tests
1 parent 72829c8 commit eb5b412

File tree

4 files changed

+174
-0
lines changed

4 files changed

+174
-0
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

geopolars/geopolars-arrow/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ geoarrow-schema = { workspace = true }
2121
polars-arrow = { workspace = true }
2222

2323
[dev-dependencies]
24+
geoarrow-array = { workspace = true, features = ["test-data"] }
2425

2526
[package.metadata.docs.rs]
2627
all-features = true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#[cfg(test)]
2+
mod tests;
13
pub mod to_arrow;
24
pub mod to_geoarrow;
35
pub mod to_polars;
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
use arrow_array::cast::AsArray;
2+
use arrow_array::types::UInt64Type;
3+
use arrow_array::{Array, UInt64Array};
4+
use arrow_schema::Field;
5+
use geoarrow_array::cast::{AsGeoArrowArray, to_wkb};
6+
use geoarrow_schema::{CoordType, Dimension};
7+
8+
use crate::to_arrow::polars_to_arrow;
9+
use crate::to_geoarrow::polars_to_geoarrow;
10+
use crate::to_polars::{arrow_to_polars, geoarrow_to_polars};
11+
12+
#[test]
13+
fn round_trip_integer() {
14+
let array = UInt64Array::from(vec![1, 2, 3, 4]);
15+
// Field name not preserved in round trip
16+
let field = Field::new("", array.data_type().clone(), true);
17+
let polars_array = arrow_to_polars(&array, &field);
18+
let (back_array, back_field) = polars_to_arrow(polars_array);
19+
20+
assert_eq!(&array, back_array.as_primitive::<UInt64Type>());
21+
assert_eq!(&field, back_field.as_ref());
22+
}
23+
24+
#[test]
25+
fn round_trip_point() {
26+
for coord_type in [CoordType::Interleaved, CoordType::Separated] {
27+
for dim in [
28+
Dimension::XY,
29+
Dimension::XYZ,
30+
Dimension::XYM,
31+
Dimension::XYZM,
32+
] {
33+
let array = geoarrow_array::test::point::array(coord_type, dim);
34+
let polars_array = geoarrow_to_polars(&array);
35+
let back = polars_to_geoarrow(polars_array);
36+
assert_eq!(&array, back.as_point());
37+
}
38+
}
39+
}
40+
41+
#[test]
42+
fn round_trip_line_string() {
43+
for coord_type in [CoordType::Interleaved, CoordType::Separated] {
44+
for dim in [
45+
Dimension::XY,
46+
Dimension::XYZ,
47+
Dimension::XYM,
48+
Dimension::XYZM,
49+
] {
50+
let array = geoarrow_array::test::linestring::array(coord_type, dim);
51+
let polars_array = geoarrow_to_polars(&array);
52+
let back = polars_to_geoarrow(polars_array);
53+
assert_eq!(&array, back.as_line_string());
54+
}
55+
}
56+
}
57+
58+
#[test]
59+
fn round_trip_polygon() {
60+
for coord_type in [CoordType::Interleaved, CoordType::Separated] {
61+
for dim in [
62+
Dimension::XY,
63+
Dimension::XYZ,
64+
Dimension::XYM,
65+
Dimension::XYZM,
66+
] {
67+
let array = geoarrow_array::test::polygon::array(coord_type, dim);
68+
let polars_array = geoarrow_to_polars(&array);
69+
let back = polars_to_geoarrow(polars_array);
70+
assert_eq!(&array, back.as_polygon());
71+
}
72+
}
73+
}
74+
75+
#[test]
76+
fn round_trip_multi_point() {
77+
for coord_type in [CoordType::Interleaved, CoordType::Separated] {
78+
for dim in [
79+
Dimension::XY,
80+
Dimension::XYZ,
81+
Dimension::XYM,
82+
Dimension::XYZM,
83+
] {
84+
let array = geoarrow_array::test::multipoint::array(coord_type, dim);
85+
let polars_array = geoarrow_to_polars(&array);
86+
let back = polars_to_geoarrow(polars_array);
87+
assert_eq!(&array, back.as_multi_point());
88+
}
89+
}
90+
}
91+
92+
#[test]
93+
fn round_trip_multi_line_string() {
94+
for coord_type in [CoordType::Interleaved, CoordType::Separated] {
95+
for dim in [
96+
Dimension::XY,
97+
Dimension::XYZ,
98+
Dimension::XYM,
99+
Dimension::XYZM,
100+
] {
101+
let array = geoarrow_array::test::multilinestring::array(coord_type, dim);
102+
let polars_array = geoarrow_to_polars(&array);
103+
let back = polars_to_geoarrow(polars_array);
104+
assert_eq!(&array, back.as_multi_line_string());
105+
}
106+
}
107+
}
108+
109+
#[test]
110+
fn round_trip_multi_polygon() {
111+
for coord_type in [CoordType::Interleaved, CoordType::Separated] {
112+
for dim in [
113+
Dimension::XY,
114+
Dimension::XYZ,
115+
Dimension::XYM,
116+
Dimension::XYZM,
117+
] {
118+
let array = geoarrow_array::test::multipolygon::array(coord_type, dim);
119+
let polars_array = geoarrow_to_polars(&array);
120+
let back = polars_to_geoarrow(polars_array);
121+
assert_eq!(&array, back.as_multi_polygon());
122+
}
123+
}
124+
}
125+
126+
#[test]
127+
fn round_trip_geometry_collection() {
128+
for coord_type in [CoordType::Interleaved, CoordType::Separated] {
129+
for dim in [
130+
Dimension::XY,
131+
Dimension::XYZ,
132+
Dimension::XYM,
133+
Dimension::XYZM,
134+
] {
135+
let array = geoarrow_array::test::geometrycollection::array(coord_type, dim, false);
136+
let polars_array = geoarrow_to_polars(&array);
137+
let back = polars_to_geoarrow(polars_array);
138+
assert_eq!(&array, back.as_geometry_collection());
139+
}
140+
}
141+
}
142+
143+
#[test]
144+
fn round_trip_geometry() {
145+
for coord_type in [CoordType::Interleaved, CoordType::Separated] {
146+
let array = geoarrow_array::test::geometry::array(coord_type, false);
147+
let polars_array = geoarrow_to_polars(&array);
148+
let back = polars_to_geoarrow(polars_array);
149+
assert_eq!(&array, back.as_geometry());
150+
}
151+
}
152+
153+
#[test]
154+
fn round_trip_wkb() {
155+
let array = geoarrow_array::test::geometry::array(CoordType::default(), false);
156+
let wkb_array = to_wkb::<i32>(&array).unwrap();
157+
let polars_array = geoarrow_to_polars(&wkb_array);
158+
let back = polars_to_geoarrow(polars_array);
159+
assert_eq!(&wkb_array, back.as_wkb::<i32>());
160+
}

0 commit comments

Comments
 (0)