Skip to content

Commit

Permalink
test util with TempFixture
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianBeilschmidt committed Sep 2, 2022
1 parent 3886bb2 commit cb5cf9f
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 109 deletions.
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
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
49 changes: 6 additions & 43 deletions src/programs/raster/mdimtranslate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,59 +227,22 @@ fn _multi_dim_translate(

#[cfg(test)]
mod tests {
use super::*;

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

/// Create a copy of the test file in a temporary directory
/// and returns a tuple of the temp dir (for clean-up) as well as the path to the file.
/// We can remove this when <https://github.com/OSGeo/gdal/issues/6253> is resolved.
struct TempDataset {
_temp_dir: tempfile::TempDir,
temp_path: PathBuf,
}

impl TempDataset {
pub fn fixture(name: &str) -> Self {
let path = std::path::Path::new(file!())
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap()
.join("fixtures")
.join(name);

let temp_dir = tempfile::tempdir().unwrap();
let temp_path = temp_dir.path().join(path.file_name().unwrap());

std::fs::copy(&path, &temp_path).unwrap();

Self {
_temp_dir: temp_dir,
temp_path,
}
}
use super::*;

pub fn path(&self) -> &Path {
&self.temp_path
}
}
use crate::{test_utils::TempFixture, DatasetOptions, Driver, GdalOpenFlags};

#[test]
fn test_build_tiff_from_path() {
let fixture = TempDataset::fixture("cf_nasa_4326.nc");
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(fixture.path(), dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();

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

Expand All @@ -303,15 +266,15 @@ mod tests {

#[test]
fn test_build_tiff_from_dataset() {
let fixture = TempDataset::fixture("cf_nasa_4326.nc");
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(fixture.path(), 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
95 changes: 29 additions & 66 deletions src/raster/mdarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -782,75 +782,38 @@ impl Attribute {

#[cfg(test)]
mod tests {
use std::path::{Path, PathBuf};

use super::*;

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

/// Create a copy of the test file in a temporary directory
/// and returns a tuple of the temp dir (for clean-up) as well as the path to the file.
/// We can remove this when <https://github.com/OSGeo/gdal/issues/6253> is resolved.
struct TempDataset {
_temp_dir: tempfile::TempDir,
temp_path: PathBuf,
}

impl TempDataset {
pub fn fixture(name: &str) -> Self {
let path = std::path::Path::new(file!())
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap()
.join("fixtures")
.join(name);

let temp_dir = tempfile::tempdir().unwrap();
let temp_path = temp_dir.path().join(path.file_name().unwrap());

std::fs::copy(&path, &temp_path).unwrap();

Self {
_temp_dir: temp_dir,
temp_path,
}
}

pub fn path(&self) -> &Path {
&self.temp_path
}
}
use crate::{test_utils::TempFixture, Dataset, DatasetOptions, GdalOpenFlags};

#[test]
fn test_root_group_name() {
let fixture = TempDataset::fixture("byte_no_cf.nc");
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(fixture.path(), 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 = TempDataset::fixture("byte_no_cf.nc");
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.path(), 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 @@ -859,15 +822,15 @@ mod tests {

#[test]
fn test_n_dimension() {
let fixture = TempDataset::fixture("byte_no_cf.nc");
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.path(), 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 @@ -878,15 +841,15 @@ mod tests {

#[test]
fn test_n_elements() {
let fixture = TempDataset::fixture("byte_no_cf.nc");
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.path(), 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 @@ -897,15 +860,15 @@ mod tests {

#[test]
fn test_dimension_name() {
let fixture = TempDataset::fixture("byte_no_cf.nc");
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.path(), dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();
let root_group = dataset.root_group().unwrap();

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

#[test]
fn test_dimension_size() {
let fixture = TempDataset::fixture("byte_no_cf.nc");
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.path(), 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 @@ -954,15 +917,15 @@ mod tests {

#[test]
fn test_read_data() {
let fixture = TempDataset::fixture("byte_no_cf.nc");
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.path(), 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 @@ -976,15 +939,15 @@ mod tests {

#[test]
fn test_read_string_array() {
let fixture = TempDataset::fixture("alldatatypes.nc");
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(fixture.path(), dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();

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

Expand All @@ -1004,15 +967,15 @@ mod tests {

#[test]
fn test_datatype() {
let fixture = TempDataset::fixture("byte_no_cf.nc");
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.path(), dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();

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

Expand All @@ -1029,15 +992,15 @@ mod tests {

#[test]
fn test_spatial_ref() {
let fixture = TempDataset::fixture("byte_no_cf.nc");
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.path(), 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 @@ -1053,15 +1016,15 @@ mod tests {

#[test]
fn test_no_data_value() {
let fixture = TempDataset::fixture("byte_no_cf.nc");
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.path(), 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 @@ -1073,15 +1036,15 @@ mod tests {

#[test]
fn test_attributes() {
let fixture = TempDataset::fixture("cf_nasa_4326.nc");
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(fixture.path(), dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();

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

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

#[test]
fn test_unit() {
let fixture = TempDataset::fixture("cf_nasa_4326.nc");
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(fixture.path(), dataset_options).unwrap();
let dataset = Dataset::open_ex(&fixture, dataset_options).unwrap();

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

Expand Down Expand Up @@ -1165,15 +1128,15 @@ mod tests {

#[test]
fn test_stats() {
let fixture = TempDataset::fixture("byte_no_cf.nc");
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.path(), 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 Down
Loading

0 comments on commit cb5cf9f

Please sign in to comment.