Skip to content

Commit

Permalink
transpose: added tests for invalid transpose requests (and fixed them)
Browse files Browse the repository at this point in the history
  • Loading branch information
proycon committed Mar 4, 2024
1 parent 71286a2 commit a97772f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/api/transpose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,18 +164,22 @@ impl<'store> Transposable<'store> for ResultTextSelectionSet<'store> {
.transpose(remainder_begin as isize)
.expect("transposition of offset must succeed");
}
relative_offsets.push(relative_offset);
selectors_per_side[side_i].push(SelectorBuilder::TextSelector(
resource.handle().into(),
source_offset,
));
if let Some(remainder) = remainder {
if remainder.begin() < intersection.begin() {
//not a valid intersection, skip to the next
continue;
}
resegment = true;
//the text selection was not matched/consumed entirely
//add the remainder of the text selection back to the buffer
tselbuffer
.push_front((remainder, remainder.begin() - tsel.begin()));
}
relative_offsets.push(relative_offset);
selectors_per_side[side_i].push(SelectorBuilder::TextSelector(
resource.handle().into(),
source_offset,
));
break;
}
}
Expand Down Expand Up @@ -289,7 +293,14 @@ impl<'store> Transposable<'store> for ResultTextSelectionSet<'store> {
}

match selectors_per_side[source_side.expect("source side must exist at this point")].len() {
0 => unreachable!("sources must have been found at this point"),
0 =>
Err(StamError::TransposeError(
format!(
"No source fragments were found in the complex transposition {}, unable to transpose",
via.id().unwrap_or("(no-id)"),
),
"",
)),
1 if config.allow_simple && !config.no_transposition => {
//output is a simple transposition
builders.push(
Expand Down
60 changes: 60 additions & 0 deletions tests/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2148,6 +2148,35 @@ fn transpose_over_simple_transposition() -> Result<(), StamError> {
Ok(())
}

#[test]
#[cfg(feature = "transpose")]
fn transpose_over_simple_transposition_invalid() -> Result<(), StamError> {
let mut store = setup_example_8()?;
store.annotate(
AnnotationBuilder::new()
.with_id("A3")
.with_data("mydataset", "species", "homo sapiens")
.with_target(SelectorBuilder::textselector(
"humanrights",
Offset::simple(0, 9), //"all human"
)),
)?;
let transposition = store.annotation("SimpleTransposition1").or_fail()?;
let source = store.annotation("A3").or_fail()?;
assert_eq!(
source.text_simple(),
Some("all human"),
"sanity check for source annotation"
);
let config = TransposeConfig {
transposition_id: Some("NewTransposition".to_string()),
target_side_ids: vec!["A3t".to_string()],
..Default::default()
};
assert!(source.transpose(&transposition, config).is_err());
Ok(())
}

#[test]
#[cfg(feature = "transpose")]
fn transpose_over_complex_transposition() -> Result<(), StamError> {
Expand Down Expand Up @@ -2275,3 +2304,34 @@ fn transpose_over_complex_transposition_with_resegmentation() -> Result<(), Stam
);
Ok(())
}

#[test]
#[cfg(feature = "transpose")]
fn transpose_over_complex_transposition_invalid() -> Result<(), StamError> {
let mut store = setup_example_8b()?;
store.annotate(
AnnotationBuilder::new()
.with_id("A3")
.with_data("mydataset", "species", "homo sapiens")
.with_target(SelectorBuilder::textselector(
"humanrights",
Offset::simple(0, 9), //"all human"
)),
)?;
let transposition = store.annotation("ComplexTransposition1").or_fail()?;
let source = store.annotation("A3").or_fail()?;
assert_eq!(
source.text_simple(),
Some("all human"),
"sanity check for source annotation"
);
let config = TransposeConfig {
transposition_id: Some("NewTransposition".to_string()),
target_side_ids: vec!["A3t".to_string()],
..Default::default()
};
let result = source.transpose(&transposition, config);
eprintln!("{:#?}", result);
assert!(result.is_err());
Ok(())
}

0 comments on commit a97772f

Please sign in to comment.