use std::{env, fs};
use datafusion_common::Result;
use sedona_common::sedona_internal_err;
pub fn test_geoparquet(group: &str, name: &str) -> Result<String> {
let geoarrow_data = geoarrow_data_dir()?;
let path = format!("{geoarrow_data}/{group}/files/{group}_{name}_geo.parquet");
if let Ok(exists) = fs::exists(&path) {
if exists {
return Ok(path);
}
}
sedona_internal_err!(
"geoarrow-data test file '{path}' does not exist.\n{}\n{}",
"You may need to check the value of the SEDONA_GEOARROW_DATA_DIR environment variable,",
"run submodules/download-assets.py, or check the name of the file you requested"
)
}
pub fn geoarrow_data_dir() -> Result<String> {
if let Ok(from_env) = env::var("SEDONA_GEOARROW_DATA_DIR") {
if fs::exists(&from_env)? {
return Ok(from_env);
} else {
return sedona_internal_err!(
"{}\n{}{}{}",
"Can't resolve geoarrow-data from the current working directory because",
"the value of the SEDONA_GEOARROW_DATA_DIR (",
from_env,
") does not exist"
);
}
}
let likely_possibilities = [
"../../submodules/geoarrow-data".to_string(),
"submodules/geoarrow-data".to_string(),
];
for possibility in likely_possibilities.into_iter().rev() {
if let Ok(exists) = fs::exists(&possibility) {
if exists {
return Ok(possibility);
}
}
}
sedona_internal_err!(
"{}\n{}\n{}",
"Can't resolve geoarrow-data from the current working directory",
"You may need to run `git submodule init && git submodule update --recursive` or",
"set the SEDONA_GEOARROW_DATA_DIR environment variable"
)
}
pub fn sedona_testing_dir() -> Result<String> {
if let Ok(from_env) = env::var("SEDONA_TESTING_DIR") {
if fs::exists(&from_env)? {
return Ok(from_env);
} else {
return sedona_internal_err!(
"{}\n{}{}{}",
"Can't resolve sedona-testing directory because",
"the value of the SEDONA_TESTING_DIR (",
from_env,
") does not exist"
);
}
}
let likely_possibilities = [
"../../submodules/sedona-testing".to_string(),
"submodules/sedona-testing".to_string(),
];
for possibility in likely_possibilities.into_iter().rev() {
if let Ok(exists) = fs::exists(&possibility) {
if exists {
return Ok(possibility);
}
}
}
sedona_internal_err!(
"{}\n{}\n{}",
"Can't resolve sedona-testing directory from the current working directory",
"You may need to run `git submodule init && git submodule update --recursive` or",
"set the SEDONA_TESTING_DIR environment variable"
)
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn example_files() {
assert!(geoarrow_data_dir().is_ok());
assert!(test_geoparquet("natural-earth", "countries").is_ok());
let err = test_geoparquet("invalid group", "invalid name").unwrap_err();
assert!(err.message().contains("geoarrow-data test file"));
env::set_var("SEDONA_GEOARROW_DATA_DIR", "this_directory_does_not_exist");
let err = geoarrow_data_dir();
env::remove_var("SEDONA_GEOARROW_DATA_DIR");
assert!(err
.unwrap_err()
.message()
.contains("the value of the SEDONA_GEOARROW_DATA_DIR"));
env::set_var("SEDONA_GEOARROW_DATA_DIR", geoarrow_data_dir().unwrap());
let maybe_file = test_geoparquet("natural-earth", "countries");
env::remove_var("SEDONA_GEOARROW_DATA_DIR");
assert!(maybe_file.is_ok());
}
#[test]
fn sedona_testing_dir_resolves() {
assert!(sedona_testing_dir().is_ok());
env::set_var("SEDONA_TESTING_DIR", "this_directory_does_not_exist");
let err = sedona_testing_dir();
env::remove_var("SEDONA_TESTING_DIR");
assert!(err
.unwrap_err()
.message()
.contains("the value of the SEDONA_TESTING_DIR"));
env::set_var("SEDONA_TESTING_DIR", sedona_testing_dir().unwrap());
let maybe_dir = sedona_testing_dir();
env::remove_var("SEDONA_TESTING_DIR");
assert!(maybe_dir.is_ok());
}
}