tests/testthat/test-exportSummarisedResult.R

test_that("test export", {
  res <- dplyr::tibble(
    "result_id" = as.integer(1),
    "cdm_name" = "cprd",
    "group_name" = "cohort_name",
    "group_level" = "cohort1",
    "strata_name" = "sex",
    "strata_level" = "male",
    "variable_name" = "Age group",
    "variable_level" = "10 to 50",
    "estimate_name" = "count",
    "estimate_type" = "numeric",
    "estimate_value" = "5",
    "additional_name" = "overall",
    "additional_level" = "overall"
  ) |>
    newSummarisedResult(
      settings = dplyr::tibble(
        "result_id" = 1L,
        "result_type" = "summarised_characteristics",
        "package_name" = "PatientProfiles",
        "package_version" = "0.4.0",
      )
    )

  path <- tempdir()
  expect_no_error(exportSummarisedResult(res, path = path))

  name <- paste0("results_cprd_", format(Sys.Date(), "%Y_%m_%d"), ".csv")
  expect_true(name %in% list.files(path))

  resSuppr <- res |> newSummarisedResult(
    settings = settings(res) |> dplyr::mutate("min_cell_count" = 5L)
  )
  expect_identical(
    utils::read.csv(file = file.path(path, name)) |> newSummarisedResult(), resSuppr
  )
})

test_that("missing name-level values roundtrip through export", {
  res <- dplyr::tibble(
    "result_id" = as.integer(1),
    "cdm_name" = "cprd",
    "group_name" = "cohort_name",
    "group_level" = "cohort1",
    "strata_name" = "overall",
    "strata_level" = "overall",
    "variable_name" = "number subjects",
    "variable_level" = NA_character_,
    "estimate_name" = "count",
    "estimate_type" = "integer",
    "estimate_value" = "5",
    "additional_name" = "my_column",
    "additional_level" = NA_character_
  ) |>
    newSummarisedResult()

  tempFile <- tempfile(fileext = ".csv")
  expect_no_error(exportSummarisedResult(
    results = res,
    fileName = tempFile,
    minCellCount = 0
  ))
  expect_false(any(grepl(
    naValue(),
    readLines(tempFile),
    fixed = TRUE
  )))

  value <- importSummarisedResult(tempFile) |>
    splitAdditional() |>
    dplyr::pull("my_column")
  expect_identical(value, NA_character_)
})


test_that("test temp file", {
  res <- dplyr::tibble(
    "result_id" = as.integer(1),
    "cdm_name" = "cprd",
    "group_name" = "cohort_name",
    "group_level" = "cohort1",
    "strata_name" = "sex",
    "strata_level" = "male",
    "variable_name" = "Age group",
    "variable_level" = "10 to 50",
    "estimate_name" = "count",
    "estimate_type" = "numeric",
    "estimate_value" = "5",
    "additional_name" = "overall",
    "additional_level" = "overall"
  ) |>
    newSummarisedResult(
      settings = dplyr::tibble(
        "result_id" = 1L,
        "result_type" = "summarised_characteristics",
        "package_name" = "PatientProfiles",
        "package_version" = "0.4.0",
      )
    )

  tempFile <- tempfile(fileext = ".csv")

  expect_no_error(exportSummarisedResult(
    results = res,
    fileName = tempFile
  ))
})


test_that("empty result", {
  tempFile <- tempfile(fileext = ".csv")

  expect_warning(exportSummarisedResult(
    results = list(),
    fileName = tempFile
  ))
  expect_warning(exportSummarisedResult(
    results = NULL,
    fileName = tempFile
  ))
})

Try the omopgenerics package in your browser

Any scripts or data that you put into this service are public.

omopgenerics documentation built on Sept. 8, 2026, 9:06 a.m.