tests/testthat/test-AutoGeneratedDefinitions.R

# # Tests for AutoGeneratedDefinitions.R
# 
# with_mock_dir("mocks/AutoGeneratedDefinitions", {
#   
# test_that("exists%category%Name functions work", {
#   for(i in seq_along(StandardCategories[[1]])) {
#     category <- StandardCategories$categoryStandard[[i]]
#     categoryFirstUpper <- StandardCategories$categoryFirstUpper[[i]]
#     definitionsMetaData <- getDefinitionsMetadata(baseUrl, category)[1,]
#     nm <- definitionsMetaData$name
#     existsFunction <- sprintf("exists%sName", categoryFirstUpper)
#     pathtb <- do.call(existsFunction, list(nm, baseUrl = baseUrl))
#     expect_equal(pathtb$name, nm)
#   }
# })
# 
# test_that("detect%category%ByName functions work", {
#   for(i in seq_along(StandardCategories[[1]])) {
#     category <- StandardCategories$categoryStandard[[i]]
#     categoryFirstUpper <- StandardCategories$categoryFirstUpper[[i]]
#     definitionsMetaData <- getDefinitionsMetadata(baseUrl, category)
#     nm <- definitionsMetaData$name
#     nmPattern <- stringr::word(nm, 1)
#     detectFunction <- sprintf("detect%ssByName", categoryFirstUpper)
#     args <- list(pattern = nmPattern, negate = FALSE, baseUrl = baseUrl)
#     pathtb <- do.call(detectFunction, args)
#     expect_equal(pathtb$name, nm)
#   }
# })
# 
# 
# ## Test getDefinition getMetaData Post and Delete partially applied functions ---------
# for(i in seq_along(StandardCategories[[1]])) {
#   category <- StandardCategories$categoryStandard[[i]]
#   categoryFirstUpper <- StandardCategories$categoryFirstUpper[[i]]
#   
#   test_that(paste(category, "getDefinition/Post/Delete functions work"), {
#     # find an artifact 
#     getMetaDataFun <- sprintf("get%sDefinitionsMetaData", categoryFirstUpper)
#     metadata <- do.call(getMetaDataFun, list(baseUrl))[1,]
#     expect_s3_class(metadata, "data.frame")
#     
#     # check that it is valid
#     isValidFun <- sprintf("isValid%sId", categoryFirstUpper)
#     expect_true(do.call(isValidFun, list(metadata$id, baseUrl)))
#     
#     # get the definition
#     getFun <- sprintf("get%sDefinition", categoryFirstUpper)
#     definition <- do.call(getFun, list(metadata$id, baseUrl))
#     expect_type(definition, "list")
#     
#     if(category %in% c("cohort", "conceptSet")) {
#       
#       # post a copy 
#       postFun <- sprintf("post%sDefinition", categoryFirstUpper)
#       expect_output(df <- do.call(postFun, list("temp-test", definition, baseUrl)), "success")
#       
#       # delete the copy
#       deleteFun <- sprintf("delete%sDefinition", categoryFirstUpper)
#       expect_output(do.call(deleteFun, list(df$id, baseUrl)), "Success")
#       
#     } else {
#     
#       # try to post and get an error
#       postFun <- sprintf("post%sDefinition", categoryFirstUpper)
#       expect_error(df <- do.call(postFun, list("temp-test", definition, baseUrl)), "not supported")
#       
#       # try to delete and get an error
#       deleteFun <- sprintf("delete%sDefinition", categoryFirstUpper)
#       expect_error(do.call(deleteFun, list(1e9, baseUrl)))
#     }
#     
#   
#   })
# }
# 
# }) # close with_mock_dir
# 
# test_that("Update definitions works", {
#   for (i in seq_along(StandardCategories[[1]])) {
#     category <- StandardCategories$categoryStandard[[i]]
#     categoryFirstUpper <- StandardCategories$categoryFirstUpper[[i]]
# 
#     if (category %in% c("cohort", "conceptSet")) {
# 
#       getMetaDataFun <- sprintf("get%sDefinitionsMetaData", categoryFirstUpper)
#       metadata <- do.call(getMetaDataFun, list(baseUrl))[1,]
# 
#       # get the definition
#       getFun <- sprintf("get%sDefinition", categoryFirstUpper)
#       definition <- do.call(getFun, list(metadata$id, baseUrl))
# 
#       # Set a new name
#       definition$name <- stringi::stri_rand_strings(1, 10)
#       # update definition with random string name
#       updateFun <- sprintf("update%sDefinition", categoryFirstUpper)
#       expect_output(do.call(updateFun, list(definition, baseUrl)), "Success")
#     }
#   }
# })
OHDSI/ROhdsiWebApi documentation built on March 15, 2023, 1:23 p.m.