Skip to content

Commit

Permalink
wip: adds test for better fgoxide header error. squashme after bumpin…
Browse files Browse the repository at this point in the history
…g fgoxide ver
  • Loading branch information
theJasonFan committed Apr 25, 2024
1 parent f591c42 commit 6f56eb1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ bstr = "1.0.1"
clap = { version = "4.0.25", features = ["derive"] }
enum_dispatch = "0.3.8"
env_logger = "0.9.3"
fgoxide = "0.3.0"
# TODO: change this when 0.3.1 is released with header validation.
# fgoxide = "0.3.1"
fgoxide = { git = "https://github.com/fulcrumgenomics/fgoxide.git", branch = "jf/validate-header" }
flate2 = { version = "1.0.25", features = ["zlib-ng"] } # Force the faster backend that requires a C compiler
itertools = "0.10.5"
log = "0.4.17"
Expand Down
39 changes: 30 additions & 9 deletions src/lib/samples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ mod tests {
use super::*;
use csv::DeserializeErrorKind as CsvDeserializeErrorEnum;
use csv::ErrorKind as CsvErrorEnum;
use fgoxide::FgError;
use fgoxide::{self, io::Io};
use serde::de::value::Error as SerdeError;
use serde::de::Error;
Expand All @@ -176,11 +177,35 @@ mod tests {
let samples_metadata = SampleGroup::from_file(&f1).unwrap();

assert!(samples_metadata.samples[0].sample_id == "sample1");
dbg!(samples_metadata.samples[0].ordinal);
assert!(samples_metadata.samples[1].sample_id == "sample2");
assert!(samples_metadata.samples[0].barcode == "GATTACA");
assert!(samples_metadata.samples[1].barcode == "CATGCTA");
}

#[test]
fn test_tsv_file_delim_error() {
let lines = vec![
// Sample::deserialize_header_line(),
"sample_id,barcode".to_owned(),
"sample1,GATTACA".to_owned(),
"sample2,CATGCTA".to_owned(),
];
let tempdir = TempDir::new().unwrap();
let f1 = tempdir.path().join("sample_metadata.tsv");

let io = Io::default();
io.write_lines(&f1, &lines).unwrap();
let err = SampleGroup::from_file(&f1).unwrap_err();

if let FgError::DelimFileHeaderError { expected, found } = err {
assert_eq!(expected, Sample::deserialize_header_line());
assert_eq!(found, "sample_id,barcode");
} else {
panic!()
}
}

#[test]
fn test_reading_from_file_with_empty_lines_at_end() {
let lines = vec![
Expand Down Expand Up @@ -222,16 +247,12 @@ mod tests {

let io = Io::default();
io.write_lines(&f1, &lines).unwrap();
let mut to_panic = true;
if let fgoxide::FgError::ConversionError(csv_e) = SampleGroup::from_file(&f1).unwrap_err() {
if let CsvErrorEnum::Deserialize { pos: _, err: csv_de_err } = csv_e.into_kind() {
if let CsvDeserializeErrorEnum::Message(s) = csv_de_err.kind() {
to_panic = false;
assert_eq!(s, &SerdeError::missing_field("sample_id").to_string());
}
}
if let fgoxide::FgError::DelimFileHeaderError { expected, found } = SampleGroup::from_file(&f1).unwrap_err() {
assert_eq!(expected, Sample::deserialize_header_line());
assert_eq!(found, "sample1\tGATTACA");
} else {
panic!("Different error type than expected reading from headerless file.")
}
assert!(!to_panic, "Different error type than expected reading from headerless file.");
}

#[test]
Expand Down

0 comments on commit 6f56eb1

Please sign in to comment.