-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #229 from MoTrPAC/fix-mw-validation
Fix refmet name validation
- Loading branch information
Showing
3 changed files
with
42 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
context("Testing validate_refmetname function") | ||
|
||
test_that("validate_refmetname handles known refmet_name correctly", { | ||
# Example test data | ||
test_data <- data.frame( | ||
refmet_name = c("1-Methyl nicotinamide", "11-Deoxycortisol", "This Should Fail", "I hope this as well---", "-"), | ||
stringsAsFactors = FALSE | ||
) | ||
|
||
# Call the function with verbose = FALSE to suppress messages during test | ||
actual_missed_ids <- validate_refmetname(test_data, verbose = FALSE) | ||
|
||
# Check if the actual missed IDs match the expected outcome | ||
expect_equal(actual_missed_ids, 3) | ||
}) | ||
|
||
|
||
test_that("Successful API call returns correctly structured list with no additional elements", { | ||
# Known good refmet_name that will return a successful response | ||
test_refmet_name <- "11-Deoxycortisol" | ||
|
||
# Expected elements in the response | ||
expected_elements <- c("refmet_name", "formula", "exactmass", "super_class", "main_class", "sub_class") | ||
|
||
search_api <- paste0("https://www.metabolomicsworkbench.org/rest/refmet/match/",URLencode(test_refmet_name),"/name/") | ||
response <- jsonlite::fromJSON(search_api) | ||
|
||
# Check that the response has all the expected elements | ||
expect_true(all(expected_elements %in% names(response)), "Response is missing expected elements.") | ||
|
||
# Check for no additional elements | ||
expect_equal(length(names(response)), length(expected_elements), | ||
info = "Response contains additional unexpected elements.") | ||
|
||
unexpected_elements <- setdiff(names(response), expected_elements) | ||
expect_equal(unexpected_elements, character(0), | ||
info = paste("Unexpected elements in response:", paste(unexpected_elements, collapse = ", "))) | ||
}) | ||
|