tests/testthat/test-add-procedures.R

test_that("addProcedures extracts labs, imaging, and procedure occurrences", {
  cdm <- hermes_test_cdm()

  cohort_enriched <- cdm$target_cohort |>
    addProcedures(
      window = list(baseline = c(-365, -1), followup = c(0, 365))
    ) |>
    dplyr::collect()

  expect_true(is.data.frame(cohort_enriched))
  expect_equal(nrow(cohort_enriched), nrow(cdm$target_cohort |> dplyr::collect()))

  cols <- colnames(cohort_enriched)
  expect_true(all(c(
    "lab_tests_count_baseline", "procedures_count_baseline",
    "lab_tests_count_followup", "procedures_count_followup"
  ) %in% cols))

  p1 <- cohort_enriched |> dplyr::filter(.data$subject_id == 1L)
  expect_equal(p1$lab_tests_count_baseline, 1)
  expect_equal(p1$procedures_count_baseline, 1)
  expect_equal(p1$lab_tests_count_followup, 1)
  expect_equal(p1$procedures_count_followup, 1)
})

test_that("addProcedures supports infinite and NA window bounds", {
  cdm <- hermesTestCdm()

  resInf <- cdm$target_cohort |>
    addProcedures(window = c(0, Inf)) |>
    dplyr::collect()

  expect_true("lab_tests_count_0_to_inf" %in% colnames(resInf))
  expect_true("procedures_count_0_to_inf" %in% colnames(resInf))

  p1 <- resInf |> dplyr::filter(.data$subject_id == 1L)
  expect_equal(p1$lab_tests_count_0_to_inf, 1)
  expect_equal(p1$procedures_count_0_to_inf, 1)
})

Try the CohortUtilisation package in your browser

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

CohortUtilisation documentation built on Aug. 30, 2026, 9:07 a.m.