Skip to content

Commit

Permalink
Merge pull request #91 from noritada/cleanup/helpers-for-testing
Browse files Browse the repository at this point in the history
Cleanups of helper functions for testing

This PR cleans up helper functions for testing.
  • Loading branch information
noritada committed Jul 3, 2024
2 parents 8cb8c52 + 04cbf3c commit 4be6bb8
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 218 deletions.
8 changes: 4 additions & 4 deletions cli/tests/cli/commands/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ macro_rules! test_operation_with_data_without_nan_values_and_byte_order_options
.stdout(predicate::str::is_empty())
.stderr(predicate::str::is_empty());

let actual = utils::cat_as_bytes(&out_path)?;
let actual = utils::get_uncompressed(&out_path)?;
assert_eq!(actual, $expected);

Ok(())
Expand Down Expand Up @@ -154,7 +154,7 @@ fn decoding_run_length_packing_as_big_endian() -> Result<(), Box<dyn std::error:
b => b.to_vec(),
})
.collect();
let actual = utils::cat_as_bytes(&out_path)?;
let actual = utils::get_uncompressed(&out_path)?;
assert_eq!(actual, expected);

Ok(())
Expand Down Expand Up @@ -189,7 +189,7 @@ macro_rules! test_operation_with_data_with_nan_values_as_little_endian {
b => b.to_vec(),
})
.collect();
let actual = utils::cat_as_bytes(&out_path)?;
let actual = utils::get_uncompressed(&out_path)?;
assert_eq!(actual, expected);

Ok(())
Expand Down Expand Up @@ -279,7 +279,7 @@ macro_rules! test_operation_with_data_without_nan_values_compared_using_simple_p
let dig: i16 = $dig;
let expected = $expected;
let expected = utils::encode_le_bytes_using_simple_packing(expected, ref_val, exp, dig);
let actual = utils::cat_as_bytes(&out_path)?;
let actual = utils::get_uncompressed(&out_path)?;
let actual = utils::encode_le_bytes_using_simple_packing(actual, ref_val, exp, dig);
assert_eq!(actual, expected);

Expand Down
71 changes: 17 additions & 54 deletions cli/tests/cli/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,77 +5,40 @@ use std::{
};

use tempfile::NamedTempFile;
use xz2::bufread::XzDecoder;

pub(crate) mod testdata;

fn cat_to_tempfile<P>(file_path: P) -> Result<NamedTempFile, io::Error>
fn write_uncompressed_to_tempfile<P>(file_path: P) -> Result<NamedTempFile, io::Error>
where
P: AsRef<Path>,
{
let mut buf = Vec::new();
let mut out = NamedTempFile::new()?;

let f = File::open(file_path)?;
let mut f = BufReader::new(f);
f.read_to_end(&mut buf)?;
let buf = get_uncompressed(file_path)?;
out.write_all(&buf)?;

Ok(out)
}

fn gzcat_to_tempfile<P>(file_path: P) -> Result<NamedTempFile, io::Error>
pub(crate) fn get_uncompressed<P>(file_path: P) -> Result<Vec<u8>, io::Error>
where
P: AsRef<Path>,
{
let mut buf = Vec::new();
let mut out = NamedTempFile::new()?;

let f = File::open(file_path)?;
let f = BufReader::new(f);
let mut f = flate2::read::GzDecoder::new(f);
f.read_to_end(&mut buf)?;
out.write_all(&buf)?;

Ok(out)
}

fn xzcat_to_tempfile<P>(file_path: P) -> Result<NamedTempFile, io::Error>
where
P: AsRef<Path>,
{
let mut buf = Vec::new();
let mut out = NamedTempFile::new()?;

let f = File::open(file_path)?;
let f = BufReader::new(f);
let mut f = XzDecoder::new(f);
f.read_to_end(&mut buf)?;
out.write_all(&buf)?;

Ok(out)
}

fn unxz_as_bytes<P>(file_path: P) -> Result<Vec<u8>, io::Error>
where
P: AsRef<Path>,
{
let mut buf = Vec::new();

let f = File::open(file_path)?;
let f = BufReader::new(f);
let mut f = XzDecoder::new(f);
f.read_to_end(&mut buf)?;

Ok(buf)
}

pub(crate) fn cat_as_bytes(file_name: &str) -> Result<Vec<u8>, io::Error> {
let mut buf = Vec::new();

let f = File::open(file_name)?;
let f = File::open(&file_path)?;
let mut f = BufReader::new(f);
f.read_to_end(&mut buf)?;
match file_path.as_ref().extension().map(|s| s.as_encoded_bytes()) {
Some(b"gz") => {
let mut f = flate2::read::GzDecoder::new(f);
f.read_to_end(&mut buf)?;
}
Some(b"xz") => {
let mut f = xz2::bufread::XzDecoder::new(f);
f.read_to_end(&mut buf)?;
}
_ => {
f.read_to_end(&mut buf)?;
}
};

Ok(buf)
}
Expand Down
182 changes: 54 additions & 128 deletions cli/tests/cli/utils/testdata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{

use tempfile::NamedTempFile;

use crate::utils::{cat_to_tempfile, gzcat_to_tempfile, unxz_as_bytes, xzcat_to_tempfile};
use crate::utils::{get_uncompressed, write_uncompressed_to_tempfile};

fn testdata_dir() -> PathBuf {
Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/..")).join("testdata")
Expand Down Expand Up @@ -35,7 +35,7 @@ pub(crate) mod grib2 {
use super::*;

pub(crate) fn cmc_glb() -> Result<NamedTempFile, io::Error> {
cat_to_tempfile(cmc_glb_file())
write_uncompressed_to_tempfile(cmc_glb_file())
}

fn cmc_glb_file() -> PathBuf {
Expand All @@ -46,62 +46,32 @@ pub(crate) mod grib2 {
testdata_dir().join("icon_global_icosahedral_single-level_2021112018_000_TOT_PREC.grib2")
}

pub(crate) fn jma_kousa() -> Result<NamedTempFile, io::Error> {
xzcat_to_tempfile(
testdata_dir()
.join("Z__C_RJTD_20170221120000_MSG_GPV_Gll0p5deg_Pys_B20170221120000_F2017022115-2017022212_grib2.bin.xz"),
)
}

pub(crate) fn jma_meps() -> Result<NamedTempFile, io::Error> {
xzcat_to_tempfile(
testdata_dir()
.join("Z__C_RJTD_20190605000000_MEPS_GPV_Rjp_L-pall_FH00-15_grib2.bin.0-20.xz"),
)
}

pub(crate) fn jma_msmguid() -> Result<NamedTempFile, io::Error> {
xzcat_to_tempfile(
testdata_dir()
.join("Z__C_RJTD_20190304000000_MSM_GUID_Rjp_P-all_FH03-39_Toorg_grib2.bin.xz"),
)
}

pub(crate) fn jma_tornado_nowcast() -> Result<NamedTempFile, io::Error> {
cat_to_tempfile(
testdata_dir()
.join("Z__C_RJTD_20160822020000_NOWC_GPV_Ggis10km_Pphw10_FH0000-0100_grib2.bin"),
)
}

pub(crate) fn ncmrwf_wind_solar() -> Result<NamedTempFile, io::Error> {
xzcat_to_tempfile(testdata_dir().join("wind_solar_ind_0.125_20240521_12Z.grib2.0.xz"))
}

pub(crate) fn noaa_gdas_0_10() -> Result<NamedTempFile, io::Error> {
xzcat_to_tempfile(testdata_dir().join("gdas.t12z.pgrb2.0p25.f000.0-10.xz"))
}

pub(crate) fn noaa_gdas_12() -> Result<NamedTempFile, io::Error> {
xzcat_to_tempfile(testdata_dir().join("gdas.t12z.pgrb2.0p25.f000.12.xz"))
}

pub(crate) fn noaa_gdas_46() -> Result<NamedTempFile, io::Error> {
xzcat_to_tempfile(testdata_dir().join("gdas.t12z.pgrb2.0p25.f000.46.xz"))
}

pub(crate) fn noaa_mrms() -> Result<NamedTempFile, io::Error> {
gzcat_to_tempfile(
testdata_dir().join("MRMS_ReflectivityAtLowestAltitude_00.50_20230406-120039.grib2.gz"),
)
}

pub(crate) fn noaa_ndfd_critfireo() -> Result<NamedTempFile, io::Error> {
xzcat_to_tempfile(testdata_dir().join("ds.critfireo.bin.xz"))
}

pub(crate) fn noaa_ndfd_minrh() -> Result<NamedTempFile, io::Error> {
xzcat_to_tempfile(testdata_dir().join("ds.minrh.bin.xz"))
macro_rules! definitions_of_grib2_test_data {
($(($name:ident, $file_name:expr),)*) => ($(
pub(crate) fn $name() -> Result<NamedTempFile, io::Error> {
write_uncompressed_to_tempfile(testdata_dir().join($file_name))
}
)*);
}

definitions_of_grib2_test_data! {
(
jma_kousa,
"Z__C_RJTD_20170221120000_MSG_GPV_Gll0p5deg_Pys_B20170221120000_F2017022115-2017022212_grib2.bin.xz"
),
(jma_meps, "Z__C_RJTD_20190605000000_MEPS_GPV_Rjp_L-pall_FH00-15_grib2.bin.0-20.xz"),
(jma_msmguid, "Z__C_RJTD_20190304000000_MSM_GUID_Rjp_P-all_FH03-39_Toorg_grib2.bin.xz"),
(
jma_tornado_nowcast,
"Z__C_RJTD_20160822020000_NOWC_GPV_Ggis10km_Pphw10_FH0000-0100_grib2.bin"
),
(ncmrwf_wind_solar, "wind_solar_ind_0.125_20240521_12Z.grib2.0.xz"),
(noaa_gdas_0_10, "gdas.t12z.pgrb2.0p25.f000.0-10.xz"),
(noaa_gdas_12, "gdas.t12z.pgrb2.0p25.f000.12.xz"),
(noaa_gdas_46, "gdas.t12z.pgrb2.0p25.f000.46.xz"),
(noaa_mrms, "MRMS_ReflectivityAtLowestAltitude_00.50_20230406-120039.grib2.gz"),
(noaa_ndfd_critfireo, "ds.critfireo.bin.xz"),
(noaa_ndfd_minrh, "ds.minrh.bin.xz"),
}

pub(crate) fn multi_message_data(n: usize) -> Result<NamedTempFile, io::Error> {
Expand All @@ -122,75 +92,31 @@ pub(crate) mod grib2 {
pub(crate) mod flat_binary {
use super::*;

pub(crate) fn cmc_glb_le() -> Result<Vec<u8>, io::Error> {
unxz_as_bytes(testdata_dir().join("gen").join("cmc-glb-wgrib2-le.bin.xz"))
}

pub(crate) fn jma_kousa_be() -> Result<Vec<u8>, io::Error> {
unxz_as_bytes(testdata_dir().join("gen").join("kousa-wgrib2-be.bin.xz"))
}

pub(crate) fn jma_kousa_le() -> Result<Vec<u8>, io::Error> {
unxz_as_bytes(testdata_dir().join("gen").join("kousa-wgrib2-le.bin.xz"))
}

pub(crate) fn jma_meps_le() -> Result<Vec<u8>, io::Error> {
unxz_as_bytes(testdata_dir().join("gen").join("meps-wgrib2-le.bin.xz"))
}

pub(crate) fn jma_msmguid_le() -> Result<Vec<u8>, io::Error> {
unxz_as_bytes(testdata_dir().join("gen").join("msmguid-wgrib2-le.bin.xz"))
}

pub(crate) fn jma_tornado_nowcast_be() -> Result<Vec<u8>, io::Error> {
unxz_as_bytes(testdata_dir().join("gen").join("tornado-wgrib2-be.bin.xz"))
}

pub(crate) fn jma_tornado_nowcast_le() -> Result<Vec<u8>, io::Error> {
unxz_as_bytes(testdata_dir().join("gen").join("tornado-wgrib2-le.bin.xz"))
}

pub(crate) fn ncmrwf_wind_solar_le() -> Result<Vec<u8>, io::Error> {
unxz_as_bytes(
testdata_dir()
.join("gen")
.join("wind_solar_ind_0.125_20240521_12Z.wgrib2-le.bin.xz"),
)
}

pub(crate) fn noaa_gdas_0_le() -> Result<Vec<u8>, io::Error> {
unxz_as_bytes(testdata_dir().join("gen").join("gdas-0-wgrib2-le.bin.xz"))
}

pub(crate) fn noaa_gdas_1_le() -> Result<Vec<u8>, io::Error> {
unxz_as_bytes(testdata_dir().join("gen").join("gdas-1-wgrib2-le.bin.xz"))
}

pub(crate) fn noaa_gdas_2_le() -> Result<Vec<u8>, io::Error> {
unxz_as_bytes(testdata_dir().join("gen").join("gdas-2-wgrib2-le.bin.xz"))
}

pub(crate) fn noaa_gdas_12_le() -> Result<Vec<u8>, io::Error> {
unxz_as_bytes(testdata_dir().join("gen").join("gdas-12-wgrib2-le.bin.xz"))
}

pub(crate) fn noaa_gdas_46_le() -> Result<Vec<u8>, io::Error> {
unxz_as_bytes(testdata_dir().join("gen").join("gdas-46-wgrib2-le.bin.xz"))
}

pub(crate) fn noaa_mrms_le() -> Result<Vec<u8>, io::Error> {
unxz_as_bytes(testdata_dir().join("gen").join("mrms-wgrib2-le.bin.xz"))
}

pub(crate) fn noaa_ndfd_critfireo_0_le() -> Result<Vec<u8>, io::Error> {
unxz_as_bytes(testdata_dir().join("gen").join("ds.critfireo.bin.0.xz"))
}

pub(crate) fn noaa_ndfd_critfireo_1_le() -> Result<Vec<u8>, io::Error> {
unxz_as_bytes(testdata_dir().join("gen").join("ds.critfireo.bin.1.xz"))
}

pub(crate) fn noaa_ndfd_minrh_0_le() -> Result<Vec<u8>, io::Error> {
unxz_as_bytes(testdata_dir().join("gen").join("ds.minrh.bin.0.xz"))
macro_rules! definitions_of_flat_binary_test_data {
($(($name:ident, $file_name:expr),)*) => ($(
pub(crate) fn $name() -> Result<Vec<u8>, io::Error> {
get_uncompressed(testdata_dir().join("gen").join($file_name))
}
)*);
}

definitions_of_flat_binary_test_data! {
(cmc_glb_le, "cmc-glb-wgrib2-le.bin.xz"),
(jma_kousa_be, "kousa-wgrib2-be.bin.xz"),
(jma_kousa_le, "kousa-wgrib2-le.bin.xz"),
(jma_meps_le, "meps-wgrib2-le.bin.xz"),
(jma_msmguid_le, "msmguid-wgrib2-le.bin.xz"),
(jma_tornado_nowcast_be, "tornado-wgrib2-be.bin.xz"),
(jma_tornado_nowcast_le, "tornado-wgrib2-le.bin.xz"),
(ncmrwf_wind_solar_le, "wind_solar_ind_0.125_20240521_12Z.wgrib2-le.bin.xz"),
(noaa_gdas_0_le, "gdas-0-wgrib2-le.bin.xz"),
(noaa_gdas_1_le, "gdas-1-wgrib2-le.bin.xz"),
(noaa_gdas_2_le, "gdas-2-wgrib2-le.bin.xz"),
(noaa_gdas_12_le, "gdas-12-wgrib2-le.bin.xz"),
(noaa_gdas_46_le, "gdas-46-wgrib2-le.bin.xz"),
(noaa_mrms_le, "mrms-wgrib2-le.bin.xz"),
(noaa_ndfd_critfireo_0_le, "ds.critfireo.bin.0.xz"),
(noaa_ndfd_critfireo_1_le, "ds.critfireo.bin.1.xz"),
(noaa_ndfd_minrh_0_le, "ds.minrh.bin.0.xz"),
}
}
Loading

0 comments on commit 4be6bb8

Please sign in to comment.