Skip to content

Commit

Permalink
tests(derive): all derive commands have tests
Browse files Browse the repository at this point in the history
  • Loading branch information
a-frantz committed Feb 11, 2024
1 parent b67bac8 commit e1473a6
Show file tree
Hide file tree
Showing 5 changed files with 447 additions and 186 deletions.
247 changes: 247 additions & 0 deletions src/derive/encoding/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,250 @@ pub fn predict(score_set: HashSet<u8>) -> Result<DerivedEncodingResult, anyhow::

anyhow::Ok(result)
}

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

#[test]
fn test_predict_illumina_1_3() {
let mut score_set: HashSet<u8> = HashSet::new();
score_set.insert(40);
score_set.insert(41);
score_set.insert(42);
score_set.insert(43);
score_set.insert(44);
score_set.insert(45);
score_set.insert(46);
score_set.insert(47);
score_set.insert(48);
score_set.insert(49);
score_set.insert(50);
score_set.insert(51);
score_set.insert(52);
score_set.insert(53);
score_set.insert(54);
score_set.insert(55);
score_set.insert(56);
score_set.insert(57);
score_set.insert(58);
score_set.insert(59);
score_set.insert(60);
score_set.insert(61);
score_set.insert(62);
score_set.insert(63);
score_set.insert(64);
score_set.insert(65);
score_set.insert(66);
score_set.insert(67);
score_set.insert(68);
score_set.insert(69);
score_set.insert(70);
score_set.insert(71);
score_set.insert(72);
score_set.insert(73);
score_set.insert(74);
score_set.insert(75);
score_set.insert(76);
score_set.insert(77);
score_set.insert(78);
score_set.insert(79);
score_set.insert(80);
score_set.insert(81);
score_set.insert(82);
score_set.insert(83);
score_set.insert(84);
score_set.insert(85);
score_set.insert(86);
score_set.insert(87);
score_set.insert(88);
score_set.insert(89);
score_set.insert(90);
score_set.insert(91);
score_set.insert(92);
score_set.insert(93);

let result = predict(score_set).unwrap();
assert!(result.succeeded);
assert_eq!(result.encoding, Some("Illumina 1.3".to_string()));
assert_eq!(result.observed_min, 40);
assert_eq!(result.observed_max, 93);
}

#[test]
fn test_predict_illumina_1_0() {
let mut score_set: HashSet<u8> = HashSet::new();
score_set.insert(26);
score_set.insert(27);
score_set.insert(28);
score_set.insert(29);
score_set.insert(30);
score_set.insert(31);
score_set.insert(32);
score_set.insert(33);
score_set.insert(34);
score_set.insert(35);
score_set.insert(36);
score_set.insert(37);
score_set.insert(38);
score_set.insert(39);
score_set.insert(40);
score_set.insert(41);
score_set.insert(42);
score_set.insert(43);
score_set.insert(44);
score_set.insert(45);
score_set.insert(46);
score_set.insert(47);
score_set.insert(48);
score_set.insert(49);
score_set.insert(50);
score_set.insert(51);
score_set.insert(52);
score_set.insert(53);
score_set.insert(54);
score_set.insert(55);
score_set.insert(56);
score_set.insert(57);
score_set.insert(58);
score_set.insert(59);
score_set.insert(60);
score_set.insert(61);
score_set.insert(62);
score_set.insert(63);
score_set.insert(64);
score_set.insert(65);
score_set.insert(66);
score_set.insert(67);
score_set.insert(68);
score_set.insert(69);
score_set.insert(70);
score_set.insert(71);
score_set.insert(72);
score_set.insert(73);
score_set.insert(74);
score_set.insert(75);
score_set.insert(76);
score_set.insert(77);
score_set.insert(78);
score_set.insert(79);
score_set.insert(80);
score_set.insert(81);
score_set.insert(82);
score_set.insert(83);
score_set.insert(84);
score_set.insert(85);
score_set.insert(86);
score_set.insert(87);
score_set.insert(88);
score_set.insert(89);
score_set.insert(90);
score_set.insert(91);
score_set.insert(92);
score_set.insert(93);

let result = predict(score_set).unwrap();
assert!(result.succeeded);
assert_eq!(result.encoding, Some("Illumina 1.0".to_string()));
assert_eq!(result.observed_min, 26);
assert_eq!(result.observed_max, 93);
}

#[test]
fn test_predict_sanger() {
let mut score_set: HashSet<u8> = HashSet::new();
score_set.insert(0);
score_set.insert(1);
score_set.insert(2);
score_set.insert(3);
score_set.insert(4);
score_set.insert(5);
score_set.insert(6);
score_set.insert(7);
score_set.insert(8);
score_set.insert(9);
score_set.insert(10);
score_set.insert(11);
score_set.insert(12);
score_set.insert(13);
score_set.insert(14);
score_set.insert(15);
score_set.insert(16);
score_set.insert(17);
score_set.insert(18);
score_set.insert(19);
score_set.insert(20);
score_set.insert(21);
score_set.insert(22);
score_set.insert(23);
score_set.insert(24);
score_set.insert(25);
score_set.insert(26);
score_set.insert(27);
score_set.insert(28);
score_set.insert(29);
score_set.insert(30);
score_set.insert(31);
score_set.insert(32);
score_set.insert(33);
score_set.insert(34);
score_set.insert(35);
score_set.insert(36);
score_set.insert(37);
score_set.insert(38);
score_set.insert(39);
score_set.insert(40);
score_set.insert(41);
score_set.insert(42);
score_set.insert(43);
score_set.insert(44);
score_set.insert(45);
score_set.insert(46);
score_set.insert(47);
score_set.insert(48);
score_set.insert(49);
score_set.insert(50);
score_set.insert(51);
score_set.insert(52);
score_set.insert(53);
score_set.insert(54);
score_set.insert(55);
score_set.insert(56);
score_set.insert(57);
score_set.insert(58);
score_set.insert(59);
score_set.insert(60);
score_set.insert(61);
score_set.insert(62);
score_set.insert(63);
score_set.insert(64);
score_set.insert(65);
score_set.insert(66);
score_set.insert(67);
score_set.insert(68);

let result = predict(score_set).unwrap();
assert!(result.succeeded);
assert_eq!(result.encoding, Some("Sanger/Illumina 1.8".to_string()));
assert_eq!(result.observed_min, 0);
assert_eq!(result.observed_max, 68);
}

#[test]
fn test_predict_fail() {
let score_set: HashSet<u8> = HashSet::new();
let result = predict(score_set);
assert!(result.is_err());
}

#[test]
fn test_predict_too_high_max_score() {
let mut score_set: HashSet<u8> = HashSet::new();
score_set.insert(94);
let result = predict(score_set).unwrap();
assert!(!result.succeeded);
assert_eq!(result.encoding, None);
assert_eq!(result.observed_min, 94);
assert_eq!(result.observed_max, 94);
}
}
49 changes: 39 additions & 10 deletions src/derive/endedness/compute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,8 @@ pub fn predict(
mod tests {
use super::*;

// TODO add tests for unsegmented reads
#[test]
fn test_predict_endedness() {
fn test_predict_endedness_from_first_and_last() {
let mut ordering_flags: HashMap<ReadGroupPtr, OrderingFlagsCounts> = HashMap::new();
ordering_flags.insert(
Arc::new("overall".to_string()),
Expand Down Expand Up @@ -340,7 +339,38 @@ mod tests {
}

#[test]
fn test_derive_endedness_from_all_zero_counts() {
fn test_predict_endedness_from_unsegmented() {
let mut ordering_flags: HashMap<ReadGroupPtr, OrderingFlagsCounts> = HashMap::new();
ordering_flags.insert(
Arc::new("overall".to_string()),
OrderingFlagsCounts {
unsegmented: 1,
first: 0,
last: 0,
both: 0,
neither: 0,
},
);
let result = predict_endedness(
"overall".to_string(),
ordering_flags
.get(&Arc::new("overall".to_string()))
.unwrap(),
0.0,
None,
false,
);
assert!(result.succeeded);
assert_eq!(result.endedness, "Single-End");
assert_eq!(result.first, 0);
assert_eq!(result.last, 0);
assert_eq!(result.both, 0);
assert_eq!(result.neither, 0);
assert_eq!(result.rpt, None);
}

#[test]
fn test_predict_endedness_from_all_zero_counts() {
let mut ordering_flags: HashMap<ReadGroupPtr, OrderingFlagsCounts> = HashMap::new();
ordering_flags.insert(Arc::new(String::from("rg1")), OrderingFlagsCounts::new());
let result = predict_endedness(
Expand All @@ -360,7 +390,7 @@ mod tests {
}

#[test]
fn test_derive_endedness_from_only_first() {
fn test_predict_from_only_first() {
let mut ordering_flags: HashMap<ReadGroupPtr, OrderingFlagsCounts> = HashMap::new();
ordering_flags.insert(
Arc::new("overall".to_string()),
Expand All @@ -384,7 +414,7 @@ mod tests {
}

#[test]
fn test_derive_endedness_from_only_last() {
fn test_predict_from_only_last() {
let mut ordering_flags: HashMap<ReadGroupPtr, OrderingFlagsCounts> = HashMap::new();
ordering_flags.insert(
Arc::new("overall".to_string()),
Expand All @@ -408,7 +438,7 @@ mod tests {
}

#[test]
fn test_derive_endedness_from_only_both() {
fn test_predict_from_only_both() {
let mut ordering_flags: HashMap<ReadGroupPtr, OrderingFlagsCounts> = HashMap::new();
ordering_flags.insert(
Arc::new("overall".to_string()),
Expand All @@ -432,7 +462,7 @@ mod tests {
}

#[test]
fn test_derive_endedness_from_only_neither() {
fn test_predict_from_only_neither() {
let mut ordering_flags: HashMap<ReadGroupPtr, OrderingFlagsCounts> = HashMap::new();
ordering_flags.insert(
Arc::new("overall".to_string()),
Expand All @@ -456,7 +486,7 @@ mod tests {
}

#[test]
fn test_derive_endedness_from_first_and_last() {
fn test_predict_from_first_and_last() {
let mut ordering_flags: HashMap<ReadGroupPtr, OrderingFlagsCounts> = HashMap::new();
ordering_flags.insert(
Arc::new("overall".to_string()),
Expand Down Expand Up @@ -518,7 +548,7 @@ mod tests {
}

#[test]
fn test_derive_endedness_from_first_and_last_with_rpt() {
fn test_predict_with_rpt_complex() {
let mut ordering_flags: HashMap<ReadGroupPtr, OrderingFlagsCounts> = HashMap::new();
let rg_paired = Arc::new("rg_paired".to_string());
let rg_single = Arc::new("rg_single".to_string());
Expand Down Expand Up @@ -580,7 +610,6 @@ mod tests {
assert_eq!(result.read_groups.len(), 2);
// We can't know which read group will be first in the vector.
// But both should succeed.
print!("{:?}", result.read_groups);
assert!(result.read_groups[0].succeeded && result.read_groups[1].succeeded);
}
}
Loading

0 comments on commit e1473a6

Please sign in to comment.