# Tests for toConceptSets() and toConceptSet() # ============================================================ # Helper # ============================================================ minimal_df <- function(...) { defaults <- list(concept_id = 201826L) args <- utils::modifyList(defaults, list(...)) do.call(data.frame, c(args, stringsAsFactors = FALSE)) } full_df <- function(concept_id = 201826L) { data.frame( concept_id = concept_id, concept_name = "Type 2 diabetes mellitus", domain_id = "Condition", vocabulary_id = "SNOMED", concept_class_id = "Clinical Finding", standard_concept = "S", concept_code = "44054006", invalid_reason = "V", excluded = FALSE, descendants = TRUE, mapped = FALSE, stringsAsFactors = FALSE ) } # ============================================================ # toConceptSets — valid inputs # ============================================================ test_that("toConceptSets returns named list", { result <- toConceptSets(list(cs = minimal_df())) expect_type(result, "list") expect_named(result, "cs") }) test_that("toConceptSets returns correct number of items", { df <- data.frame(concept_id = c(1L, 2L, 3L)) result <- toConceptSets(list(cs = df)) expect_length(result$cs$items, 3L) }) test_that("toConceptSets CONCEPT_ID is integer in output", { result <- toConceptSets(list(cs = minimal_df(concept_id = 201826))) cid <- result$cs$items[[1]]$concept$CONCEPT_ID expect_type(cid, "integer") expect_equal(cid, 201826L) }) test_that("toConceptSets defaults excluded/descendants/mapped to FALSE", { result <- toConceptSets(list(cs = minimal_df())) item <- result$cs$items[[1]] expect_false(item$isExcluded) expect_false(item$includeDescendants) expect_false(item$includeMapped) }) test_that("toConceptSets respects excluded = TRUE", { df <- minimal_df(excluded = TRUE) result <- toConceptSets(list(cs = df)) expect_true(result$cs$items[[1]]$isExcluded) }) test_that("toConceptSets respects descendants = TRUE", { df <- minimal_df(descendants = TRUE) result <- toConceptSets(list(cs = df)) expect_true(result$cs$items[[1]]$includeDescendants) }) test_that("toConceptSets respects mapped = TRUE", { df <- minimal_df(mapped = TRUE) result <- toConceptSets(list(cs = df)) expect_true(result$cs$items[[1]]$includeMapped) }) test_that("toConceptSets preserves metadata columns", { result <- toConceptSets(list(cs = full_df())) concept <- result$cs$items[[1]]$concept expect_equal(concept$CONCEPT_NAME, "Type 2 diabetes mellitus") expect_equal(concept$DOMAIN_ID, "Condition") expect_equal(concept$VOCABULARY_ID, "SNOMED") expect_equal(concept$CONCEPT_CLASS_ID, "Clinical Finding") expect_equal(concept$CONCEPT_CODE, "44054006") }) test_that("toConceptSets STANDARD_CONCEPT_CAPTION is 'Standard' for 'S'", { result <- toConceptSets(list(cs = full_df())) expect_equal(result$cs$items[[1]]$concept$STANDARD_CONCEPT_CAPTION, "Standard") }) test_that("toConceptSets STANDARD_CONCEPT_CAPTION is 'Classification' for 'C'", { df <- full_df() df$standard_concept <- "C" result <- toConceptSets(list(cs = df)) expect_equal(result$cs$items[[1]]$concept$STANDARD_CONCEPT_CAPTION, "Classification") }) test_that("toConceptSets STANDARD_CONCEPT_CAPTION is 'Unknown' for empty string", { df <- full_df() df$standard_concept <- "" result <- toConceptSets(list(cs = df)) expect_equal(result$cs$items[[1]]$concept$STANDARD_CONCEPT_CAPTION, "Unknown") }) test_that("toConceptSets INVALID_REASON_CAPTION is 'Valid' for 'V'", { result <- toConceptSets(list(cs = full_df())) expect_equal(result$cs$items[[1]]$concept$INVALID_REASON_CAPTION, "Valid") }) test_that("toConceptSets INVALID_REASON_CAPTION is 'Deleted' for 'D'", { df <- full_df() df$invalid_reason <- "D" result <- toConceptSets(list(cs = df)) expect_equal(result$cs$items[[1]]$concept$INVALID_REASON_CAPTION, "Deleted") }) test_that("toConceptSets handles multiple named concept sets", { result <- toConceptSets(list( cs1 = data.frame(concept_id = 1L), cs2 = data.frame(concept_id = 2L), cs3 = data.frame(concept_id = 3L) )) expect_length(result, 3L) expect_named(result, c("cs1", "cs2", "cs3")) }) test_that("toConceptSets handles NA optional column values as defaults", { df <- data.frame( concept_id = 100L, concept_name = NA_character_, domain_id = NA_character_ ) result <- toConceptSets(list(cs = df)) expect_equal(result$cs$items[[1]]$concept$CONCEPT_NAME, "") expect_equal(result$cs$items[[1]]$concept$DOMAIN_ID, "") }) # ============================================================ # toConceptSets — error cases # ============================================================ test_that("toConceptSets errors when x is a data.frame, not a list", { expect_error(toConceptSets(data.frame(concept_id = 1L)), "named list") }) test_that("toConceptSets errors when x is an unnamed list", { expect_error(toConceptSets(list(data.frame(concept_id = 1L))), "named list") }) test_that("toConceptSets errors when any name is empty", { bad <- stats::setNames(list(data.frame(concept_id = 1L)), "") expect_error(toConceptSets(bad), "named list") }) test_that("toConceptSets errors when x is an empty list", { expect_error(toConceptSets(list()), "named list") }) test_that("toConceptSets errors when an element is not a data.frame", { expect_error(toConceptSets(list(cs = "not a df")), "data.frame") }) test_that("toConceptSets errors when a data.frame has zero rows", { expect_error( toConceptSets(list(cs = data.frame(concept_id = integer(0)))), "at least one row" ) }) test_that("toConceptSets errors when concept_id column is missing", { expect_error( toConceptSets(list(cs = data.frame(id = 1L))), "concept_id" ) }) test_that("toConceptSets errors when concept_id contains NA", { expect_error( toConceptSets(list(cs = data.frame(concept_id = NA_integer_))), "NA" ) }) test_that("toConceptSets errors when concept_id is non-numeric", { expect_error( toConceptSets(list(cs = data.frame(concept_id = "abc", stringsAsFactors = FALSE))), "numeric" ) }) # ============================================================ # toConceptSet (single concept set convenience wrapper) # ============================================================ test_that("toConceptSet returns a single expression with items", { result <- toConceptSet(data.frame(concept_id = c(100L, 200L)), name = "Test") expect_type(result, "list") expect_named(result, "items") expect_length(result$items, 2L) }) test_that("toConceptSet default name does not error", { result <- toConceptSet(data.frame(concept_id = 1L)) expect_length(result$items, 1L) }) test_that("toConceptSet errors when name is NA", { expect_error( toConceptSet(data.frame(concept_id = 1L), name = NA_character_), "`name`" ) }) test_that("toConceptSet errors when name is length > 1", { expect_error( toConceptSet(data.frame(concept_id = 1L), name = c("a", "b")), "`name`" ) }) test_that("toConceptSet result matches toConceptSets result", { df <- data.frame(concept_id = c(1L, 2L)) via_single <- toConceptSet(df, name = "cs") via_multi <- toConceptSets(list(cs = df))[["cs"]] expect_equal(via_single, via_multi) })