Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use tempfiles to not access file multiple times #302

Merged
merged 4 commits into from Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Expand Up @@ -96,6 +96,10 @@

- <https://github.com/georust/gdal/pull/292>

- Added a workaround in multi-dim tests to not access files multiple times

- <https://github.com/georust/gdal/pull/302>

## 0.12

- Bump Rust edition to 2021
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Expand Up @@ -33,6 +33,8 @@ mod metadata;
pub mod programs;
pub mod raster;
pub mod spatial_ref;
#[cfg(test)]
pub mod test_utils;
mod utils;
pub mod vector;
pub mod version;
Expand Down
11 changes: 8 additions & 3 deletions src/programs/raster/mdimtranslate.rs
Expand Up @@ -227,19 +227,22 @@ fn _multi_dim_translate(

#[cfg(test)]
mod tests {

use super::*;

use crate::{DatasetOptions, Driver, GdalOpenFlags};
use crate::{test_utils::TempFixture, DatasetOptions, Driver, GdalOpenFlags};

#[test]
fn test_build_tiff_from_path() {
let fixture = TempFixture::fixture("cf_nasa_4326.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/cf_nasa_4326.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();

let mem_file_path = "/vsimem/2d3e9124-a7a0-413e-97b5-e79d46e50ff8";

Expand All @@ -263,13 +266,15 @@ mod tests {

#[test]
fn test_build_tiff_from_dataset() {
let fixture = TempFixture::fixture("cf_nasa_4326.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/cf_nasa_4326.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();

let driver = Driver::get_by_name("MEM").unwrap();
let output_dataset = driver.create("", 5, 7, 1).unwrap();
Expand Down
108 changes: 66 additions & 42 deletions src/raster/mdarray.rs
Expand Up @@ -782,33 +782,38 @@ impl Attribute {

#[cfg(test)]
mod tests {

use super::*;

use crate::{Dataset, DatasetOptions, GdalOpenFlags};
use crate::{test_utils::TempFixture, Dataset, DatasetOptions, GdalOpenFlags};

#[test]
fn test_root_group_name() {
let fixture = TempFixture::fixture("byte_no_cf.nc");

let options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", options).unwrap();
let dataset = Dataset::open_ex(&fixture, options).unwrap();
let root_group = dataset.root_group().unwrap();
let root_group_name = root_group.name();
assert_eq!(root_group_name, "/");
}

#[test]
fn test_array_names() {
let fixture = TempFixture::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();
let root_group = dataset.root_group().unwrap();
let options = CslStringList::new(); //Driver specific options determining how groups should be retrieved. Pass nullptr for default behavior.
let array_names = root_group.array_names(options);
Expand All @@ -817,13 +822,15 @@ mod tests {

#[test]
fn test_n_dimension() {
let fixture = TempFixture::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();
let root_group = dataset.root_group().unwrap();
let array_name = "Band1".to_string();
let options = CslStringList::new(); //Driver specific options determining how the array should be opened. Pass nullptr for default behavior.
Expand All @@ -834,13 +841,15 @@ mod tests {

#[test]
fn test_n_elements() {
let fixture = TempFixture::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();
let root_group = dataset.root_group().unwrap();
let array_name = "Band1".to_string();
let options = CslStringList::new(); //Driver specific options determining how the array should be opened. Pass nullptr for default behavior.
Expand All @@ -851,13 +860,15 @@ mod tests {

#[test]
fn test_dimension_name() {
let fixture = TempFixture::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();
let root_group = dataset.root_group().unwrap();

// group dimensions
Expand All @@ -883,13 +894,15 @@ mod tests {

#[test]
fn test_dimension_size() {
let fixture = TempFixture::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();
let root_group = dataset.root_group().unwrap();
let array_name = "Band1".to_string();
let options = CslStringList::new(); //Driver specific options determining how the array should be opened. Pass nullptr for default behavior.
Expand All @@ -904,13 +917,15 @@ mod tests {

#[test]
fn test_read_data() {
let fixture = TempFixture::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();

let root_group = dataset.root_group().unwrap();
let md_array = root_group
Expand All @@ -924,13 +939,15 @@ mod tests {

#[test]
fn test_read_string_array() {
let fixture = TempFixture::fixture("alldatatypes.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/alldatatypes.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();

let root_group = dataset.root_group().unwrap();

Expand All @@ -950,13 +967,15 @@ mod tests {

#[test]
fn test_datatype() {
let fixture = TempFixture::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();

let root_group = dataset.root_group().unwrap();

Expand All @@ -973,13 +992,15 @@ mod tests {

#[test]
fn test_spatial_ref() {
let fixture = TempFixture::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();

let root_group = dataset.root_group().unwrap();
let md_array = root_group
Expand All @@ -995,13 +1016,15 @@ mod tests {

#[test]
fn test_no_data_value() {
let fixture = TempFixture::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();

let root_group = dataset.root_group().unwrap();
let md_array = root_group
Expand All @@ -1013,13 +1036,15 @@ mod tests {

#[test]
fn test_attributes() {
let fixture = TempFixture::fixture("cf_nasa_4326.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/cf_nasa_4326.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();

let root_group = dataset.root_group().unwrap();

Expand Down Expand Up @@ -1064,13 +1089,15 @@ mod tests {

#[test]
fn test_unit() {
let fixture = TempFixture::fixture("cf_nasa_4326.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/cf_nasa_4326.nc", dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();

let root_group = dataset.root_group().unwrap();

Expand Down Expand Up @@ -1101,34 +1128,31 @@ mod tests {

#[test]
fn test_stats() {
{
let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex("fixtures/byte_no_cf.nc", dataset_options).unwrap();
let root_group = dataset.root_group().unwrap();
let array_name = "Band1".to_string();
let options = CslStringList::new(); //Driver specific options determining how the array should be opened. Pass nullptr for default behavior.
let md_array = root_group.open_md_array(&array_name, options).unwrap();

assert!(md_array.get_statistics(false, true).unwrap().is_none());

assert_eq!(
md_array.get_statistics(true, true).unwrap().unwrap(),
MdStatisticsAll {
min: 74.0,
max: 255.0,
mean: 126.76500000000001,
std_dev: 22.928470838675654,
valid_count: 400,
}
);
}
let fixture = TempFixture::fixture("byte_no_cf.nc");

let dataset_options = DatasetOptions {
open_flags: GdalOpenFlags::GDAL_OF_MULTIDIM_RASTER,
allowed_drivers: None,
open_options: None,
sibling_files: None,
};
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();
let root_group = dataset.root_group().unwrap();
let array_name = "Band1".to_string();
let options = CslStringList::new(); //Driver specific options determining how the array should be opened. Pass nullptr for default behavior.
let md_array = root_group.open_md_array(&array_name, options).unwrap();

// clean up aux file
std::fs::remove_file("fixtures/byte_no_cf.nc.aux.xml").unwrap();
assert!(md_array.get_statistics(false, true).unwrap().is_none());

assert_eq!(
md_array.get_statistics(true, true).unwrap().unwrap(),
MdStatisticsAll {
min: 74.0,
max: 255.0,
mean: 126.76500000000001,
std_dev: 22.928470838675654,
valid_count: 400,
}
);
}
}
9 changes: 4 additions & 5 deletions src/raster/tests.rs
Expand Up @@ -4,6 +4,7 @@ use crate::raster::rasterband::ResampleAlg;
use crate::raster::{
ByteBuffer, ColorInterpretation, RasterCreationOption, StatisticsAll, StatisticsMinMax,
};
use crate::test_utils::TempFixture;
use crate::vsi::unlink_mem_file;
use crate::Driver;
use gdal_sys::GDALDataType;
Expand Down Expand Up @@ -806,7 +807,9 @@ fn test_color_table() {

#[test]
fn test_raster_stats() {
let dataset = Dataset::open(fixture!("tinymarble.tif")).unwrap();
let fixture = TempFixture::fixture("tinymarble.tif");

let dataset = Dataset::open(&fixture).unwrap();
let rb = dataset.rasterband(1).unwrap();

assert!(rb.get_statistics(false, false).unwrap().is_none());
Expand All @@ -828,8 +831,4 @@ fn test_raster_stats() {
max: 255.0,
}
);

// clean up aux file
drop(dataset);
std::fs::remove_file(fixture!("tinymarble.tif.aux.xml")).unwrap();
}