Skip to content

Commit

Permalink
Rebuild cli tests so that they no-op if binary has not already been b…
Browse files Browse the repository at this point in the history
…uilt
  • Loading branch information
curtisalexander committed Sep 19, 2022
1 parent e7f2a88 commit 22fa99d
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 97 deletions.
19 changes: 9 additions & 10 deletions crates/readstat-tests/tests/cli.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
use assert_cmd::prelude::*; // Add methods on commands
use assert_cmd::Command; // Add methods on commands
use predicates::prelude::*; // Used for writing assertions
use std::process::Command; // Run programs
// use std::process::Command; // Run programs

#[test]
fn cli_file_does_not_exist() -> Result<(), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin("readstat")?;

cmd.arg("data").arg("tests/data/adataset.sas7bdat");
cmd.assert().failure().stderr(
predicate::str::is_match(r#"^(Stopping with error: File)\s(.+)\s(does not exist!\n)$"#)
.unwrap(),
);

if let Ok(mut cmd) = Command::cargo_bin("readstat") {
cmd.arg("data").arg("tests/data/adataset.sas7bdat");
cmd.assert().failure().stderr(
predicate::str::is_match(r#"^(Stopping with error: File)\s(.+)\s(does not exist!\n)$"#)
.unwrap(),
);
}
Ok(())
}
179 changes: 92 additions & 87 deletions crates/readstat-tests/tests/cli_data_parquet.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use ::predicates::prelude::*; // Used for writing assertions
use assert_cmd::prelude::*; // Add methods on commands
use assert_cmd::Command; // Add methods on commands
use assert_fs::NamedTempFile;
use polars::prelude::*;
use std::{fs::File, path::PathBuf, process::Command, result::Result};
use std::{fs::File, path::PathBuf, result::Result};

enum OverwriteOption {
Overwrite(NamedTempFile),
Expand All @@ -14,52 +14,54 @@ fn cli_data_to_parquet(
overwrite: OverwriteOption,
rows_to_stream: Option<u32>,
) -> Result<(Command, NamedTempFile), Box<dyn std::error::Error>> {
let mut cmd = Command::cargo_bin("readstat")?;

let tempfile = match (overwrite, rows_to_stream) {
(OverwriteOption::Overwrite(tempfile), Some(rows)) => {
cmd.arg("data")
.arg(format!("tests/data/{}.sas7bdat", base_file_name))
.args(["--format", "parquet"])
.args(["--output", tempfile.as_os_str().to_str().unwrap()])
.args(["--stream-rows", rows.to_string().as_str()])
.arg("--overwrite");

tempfile
}
(OverwriteOption::DoNotOverwrite, Some(rows)) => {
let tempfile = NamedTempFile::new(format!("{}.parquet", base_file_name))?;

cmd.arg("data")
.arg(format!("tests/data/{}.sas7bdat", base_file_name))
.args(["--format", "parquet"])
.args(["--output", tempfile.as_os_str().to_str().unwrap()])
.args(["--stream-rows", rows.to_string().as_str()]);

tempfile
}
(OverwriteOption::Overwrite(tempfile), None) => {
cmd.arg("data")
.arg(format!("tests/data/{}.sas7bdat", base_file_name))
.args(["--format", "parquet"])
.args(["--output", tempfile.as_os_str().to_str().unwrap()])
.arg("--overwrite");

tempfile
}
(OverwriteOption::DoNotOverwrite, None) => {
let tempfile = NamedTempFile::new(format!("{}.parquet", base_file_name))?;

cmd.arg("data")
.arg(format!("tests/data/{}.sas7bdat", base_file_name))
.args(["--format", "parquet"])
.args(["--output", tempfile.as_os_str().to_str().unwrap()]);

tempfile
}
};

Ok((cmd, tempfile))
if let Ok(mut cmd) = Command::cargo_bin("readstat") {
let tempfile = match (overwrite, rows_to_stream) {
(OverwriteOption::Overwrite(tempfile), Some(rows)) => {
cmd.arg("data")
.arg(format!("tests/data/{}.sas7bdat", base_file_name))
.args(["--format", "parquet"])
.args(["--output", tempfile.as_os_str().to_str().unwrap()])
.args(["--stream-rows", rows.to_string().as_str()])
.arg("--overwrite");

tempfile
}
(OverwriteOption::DoNotOverwrite, Some(rows)) => {
let tempfile = NamedTempFile::new(format!("{}.parquet", base_file_name))?;

cmd.arg("data")
.arg(format!("tests/data/{}.sas7bdat", base_file_name))
.args(["--format", "parquet"])
.args(["--output", tempfile.as_os_str().to_str().unwrap()])
.args(["--stream-rows", rows.to_string().as_str()]);

tempfile
}
(OverwriteOption::Overwrite(tempfile), None) => {
cmd.arg("data")
.arg(format!("tests/data/{}.sas7bdat", base_file_name))
.args(["--format", "parquet"])
.args(["--output", tempfile.as_os_str().to_str().unwrap()])
.arg("--overwrite");

tempfile
}
(OverwriteOption::DoNotOverwrite, None) => {
let tempfile = NamedTempFile::new(format!("{}.parquet", base_file_name))?;

cmd.arg("data")
.arg(format!("tests/data/{}.sas7bdat", base_file_name))
.args(["--format", "parquet"])
.args(["--output", tempfile.as_os_str().to_str().unwrap()]);

tempfile
}
};

Ok((cmd, tempfile))
} else {
Err(From::from("readstat binary does not exist"))
}
}

fn parquet_to_df(path: PathBuf) -> Result<DataFrame, Box<dyn std::error::Error>> {
Expand All @@ -72,66 +74,69 @@ fn parquet_to_df(path: PathBuf) -> Result<DataFrame, Box<dyn std::error::Error>>

#[test]
fn cars_to_parquet() {
let (mut cmd, tempfile) =
cli_data_to_parquet("cars", OverwriteOption::DoNotOverwrite, None).unwrap();
if let Ok((mut cmd, tempfile)) =
cli_data_to_parquet("cars", OverwriteOption::DoNotOverwrite, None)
{
cmd.assert().success().stdout(predicate::str::contains(
"In total, wrote 1,081 rows from file cars.sas7bdat into cars.parquet",
));

cmd.assert().success().stdout(predicate::str::contains(
"In total, wrote 1,081 rows from file cars.sas7bdat into cars.parquet",
));
let df = parquet_to_df(tempfile.to_path_buf()).unwrap();

let df = parquet_to_df(tempfile.to_path_buf()).unwrap();
let (height, width) = df.shape();

let (height, width) = df.shape();
assert_eq!(height, 1081);
assert_eq!(width, 13);

assert_eq!(height, 1081);
assert_eq!(width, 13);

tempfile.close().unwrap();
tempfile.close().unwrap();
}
}

#[test]
fn cars_to_parquet_with_streaming() {
let (mut cmd, tempfile) =
cli_data_to_parquet("cars", OverwriteOption::DoNotOverwrite, Some(500)).unwrap();

cmd.assert().success().stdout(predicate::str::contains(
"In total, wrote 1,081 rows from file cars.sas7bdat into cars.parquet",
));
if let Ok((mut cmd, tempfile)) =
cli_data_to_parquet("cars", OverwriteOption::DoNotOverwrite, Some(500))
{
cmd.assert().success().stdout(predicate::str::contains(
"In total, wrote 1,081 rows from file cars.sas7bdat into cars.parquet",
));

let df = parquet_to_df(tempfile.to_path_buf()).unwrap();
let df = parquet_to_df(tempfile.to_path_buf()).unwrap();

let (height, width) = df.shape();
let (height, width) = df.shape();

assert_eq!(height, 1081);
assert_eq!(width, 13);
assert_eq!(height, 1081);
assert_eq!(width, 13);

tempfile.close().unwrap();
tempfile.close().unwrap();
}
}

#[test]
fn cars_to_parquet_overwrite() {
// first stream
let (mut cmd, tempfile) =
cli_data_to_parquet("cars", OverwriteOption::DoNotOverwrite, Some(500)).unwrap();

cmd.assert().success().stdout(predicate::str::contains(
"In total, wrote 1,081 rows from file cars.sas7bdat into cars.parquet",
));
if let Ok((mut cmd, tempfile)) =
cli_data_to_parquet("cars", OverwriteOption::DoNotOverwrite, Some(500))
{
cmd.assert().success().stdout(predicate::str::contains(
"In total, wrote 1,081 rows from file cars.sas7bdat into cars.parquet",
));

// next do not stream
let (mut cmd, tempfile) =
cli_data_to_parquet("cars", OverwriteOption::Overwrite(tempfile), None).unwrap();
// next do not stream
let (mut cmd, tempfile) =
cli_data_to_parquet("cars", OverwriteOption::Overwrite(tempfile), None).unwrap();

cmd.assert().success().stdout(predicate::str::contains(
"In total, wrote 1,081 rows from file cars.sas7bdat into cars.parquet",
));
cmd.assert().success().stdout(predicate::str::contains(
"In total, wrote 1,081 rows from file cars.sas7bdat into cars.parquet",
));

let df = parquet_to_df(tempfile.to_path_buf()).unwrap();
let df = parquet_to_df(tempfile.to_path_buf()).unwrap();

let (height, width) = df.shape();
let (height, width) = df.shape();

assert_eq!(height, 1081);
assert_eq!(width, 13);
assert_eq!(height, 1081);
assert_eq!(width, 13);

tempfile.close().unwrap();
tempfile.close().unwrap();
}
}

0 comments on commit 22fa99d

Please sign in to comment.