Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vemonet committed Dec 17, 2023
1 parent b4de0d5 commit f2e3dc8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
14 changes: 5 additions & 9 deletions lib/src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,9 @@ async fn fetch_url(url: &str) -> Result<String, CuriesError> {

/// Given a `Path` get the file content if it exists
async fn fetch_file(path: &Path) -> Result<String, CuriesError> {
if path.exists() {
// Read from a file path
let mut file = File::open(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(contents)
} else {
Err(CuriesError::NotFound(format!("{:?}", path.to_str())))
}
// Read from a file path
let mut file = File::open(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(contents)
}
28 changes: 22 additions & 6 deletions lib/tests/curies_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,19 @@ fn new_empty_converter() -> Result<(), Box<dyn std::error::Error>> {
// Test wrong calls
assert!(converter
.add_curie("doid", "http://purl.obolibrary.org/obo/DOID_")
.map_err(|e| assert!(e.to_string().starts_with("Duplicate record")))
.is_err());
assert!(converter
.add_record(Record::new("wrong", "http://purl.obolibrary.org/obo/DOID_"))
.is_err());
assert!(converter.expand("wrong:1234")
.map_err(|e| assert!(e.to_string().starts_with("Not found")))
.is_err());
assert!(converter.expand("wrong")
.map_err(|e| assert!(e.to_string().starts_with("Invalid CURIE")))
.is_err());
assert!(converter.find_by_uri_prefix("wrong").is_err());
assert!(converter.expand("obo:1234").is_err());
assert!(converter.expand("wrong:1234").is_err());
assert!(converter.expand("wrong").is_err());
assert!(converter.compress("wrong_1234").is_err());
Ok(())
}

Expand Down Expand Up @@ -127,7 +131,9 @@ async fn from_extended_map_file() -> Result<(), Box<dyn std::error::Error>> {
converter.compress("http://purl.obolibrary.org/obo/DOID_1234")?,
"doid:1234"
);
assert!(converter.expand("doid:AAAA").is_err()); // Test pattern
assert!(converter.expand("doid:AAAA") // Test pattern
.map_err(|e| assert!(e.to_string().starts_with("Invalid format")))
.is_err());
Ok(())
}

Expand All @@ -152,8 +158,18 @@ async fn from_extended_map_vec() -> Result<(), Box<dyn std::error::Error>> {

#[tokio::test]
async fn from_converter_errors() -> Result<(), Box<dyn std::error::Error>> {
assert!(Converter::from_jsonld("wrongplace").await.is_err());
assert!(Converter::from_jsonld("{}").await.is_err());
assert!(Converter::from_jsonld(Path::new("wrong")).await.is_err());
assert!(Converter::from_jsonld("wrong")
.await
.map_err(|e| assert!(e.to_string().starts_with("Error parsing")))
.is_err());
assert!(Converter::from_jsonld("https://wrong")
.await
.map_err(|e| assert!(e.to_string().starts_with("Error sending")))
.is_err());
assert!(Converter::from_jsonld(Path::new("wrong"))
.await
.map_err(|e| assert!(e.to_string().starts_with("Error reading")))
.is_err());
Ok(())
}
6 changes: 2 additions & 4 deletions scripts/cov.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#!/usr/bin/env bash
set -e

cargo tarpaulin -p curies --doc --tests --out html --out xml --timeout 120 \
--exclude-files lib/src/error.rs

# Remove exclude error.rs when ready to add tests for prints of errors!
cargo tarpaulin -p curies --doc --tests --out html --out xml --timeout 120

# --exclude-files lib/src/error.rs
# python -m http.server 3000 --directory .

0 comments on commit f2e3dc8

Please sign in to comment.