Skip to content

Commit

Permalink
Merge pull request #229 from MoTrPAC/fix-mw-validation
Browse files Browse the repository at this point in the history
Fix refmet name validation
  • Loading branch information
biodavidjm authored Mar 4, 2024
2 parents 22a8154 + 0fa2451 commit e41d5d5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Package: MotrpacBicQC
Type: Package
Title: QC/QA functions for the MoTrPAC community
Version: 0.9.1
Date: 2024-02-02
Version: 0.9.12
Date: 2024-02-22
Author: MoTrPAC Bioinformatics Center
Maintainer: David Jimenez-Morales <davidjm@stanford.edu>
Description: R Package for the analysis of MoTrPAC datasets.
Expand Down
2 changes: 1 addition & 1 deletion R/metabolomics_data_dictionary.R
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ validate_refmetname <- function(dataf, verbose){

search_api <- paste0("https://www.metabolomicsworkbench.org/rest/refmet/match/",URLencode(rn),"/name/")
here <- jsonlite::fromJSON(search_api)
if(length(here) == 0){
if(here$refmet_name == "-"){
if(verbose) message(paste0(" (-) `refmet_name` [`", rn, "`] not available in RefMet. Please, contact MW/BIC (Error RN1)"))
irm <- irm + 1
idna <- idna + 1
Expand Down
39 changes: 39 additions & 0 deletions tests/testthat/test-refmet.R
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 = ", ")))
})

0 comments on commit e41d5d5

Please sign in to comment.