tests/testthat/test-rd_query.R

make_toy_dic <- function(fields) {
  data.frame(
    field_name = names(fields),
    field_label = sapply(fields, function(x) x$field_label),
    form_name = sapply(fields, function(x) x$form_name),
    branching_logic_show_field_only_if = sapply(fields, function(x) ifelse(is.null(x$branch), NA_character_, x$branch)),
    stringsAsFactors = FALSE
  )
}

test_that("rd_query errors when data or dic are missing", {
  expect_error(rd_query(data = NULL, dic = NULL),
               regexp = "Both `data` and `dic")
})

test_that("rd_query errors when requested variables do not exist in data", {
  toy_data <- data.frame(record_id = 1:3, a = c(1, 2, NA))
  toy_dic <- make_toy_dic(list(a = list(field_label = "A", form_name = "f1")))
  expect_error(
    rd_query(data = toy_data, dic = toy_dic, variables = "b", expression = "is.na(x)"),
    regexp = "Specified variables do not exist"
  )
})

test_that("rd_query errors if event specified but no event column exists", {
  toy_data <- data.frame(record_id = 1:2, age = c(10, 20))
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))
  expect_error(
    rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "x>0", event = "some_event"),
    regexp = "Event specified, but no event variable found"
  )
})

test_that("rd_query errors if specified events not found in dataset", {
  toy_data <- data.frame(record_id = 1:3, age = c(10, 20, 30),
                         redcap_event_name = c("e1", "e1", "e1"),
                         stringsAsFactors = FALSE)
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))
  expect_error(
    rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "x>0", event = c("missing_event")),
    regexp = "not found in the dataset"
  )
})

test_that("rd_query warns if event not specified but data contains event variable (multi-event project)", {
  toy_data <- data.frame(record_id = 1:2, age = c(5, NA),
                         redcap_event_name = c("e1", "e2"),
                         stringsAsFactors = FALSE)
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))
  expect_warning(
    rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "is.na(x)"),
    regexp = "No event or event-form has been specified"
  )
})

test_that("rd_query errors when by_dag = TRUE but no DAG column exists", {
  toy_data <- data.frame(record_id = 1:3, age = c(10, 20, 30))
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))
  expect_error(
    rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "x>0", by_dag = TRUE),
    regexp = "DAG-based reporting requested, but no DAG variable found"
  )
})

test_that("rd_query complains about incomplete link when events present", {
  toy_data <- data.frame(record_id = 1:2, age = c(1, NA),
                         redcap_event_name = c("e1", "e1"),
                         stringsAsFactors = FALSE)
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))
  # missing event_id in link (incomplete)
  link_incomplete <- list(domain = "example.com", redcap_version = "10.0", proj_id = 1)
  expect_error(
    rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "is.na(x)", link = link_incomplete) |> suppressWarnings(),
    regexp = "Incomplete `link` argument",
    class = "REDCapDM_error_argument"
  )
})

test_that("rd_query merges event_id correctly and adds Link when link fully specified", {
  toy_data <- data.frame(
    record_id = as.character(1:4),
    age = c(18, NA, 20, NA),
    redcap_event_name = c("e1", "e1", "e2", "e2"),
    redcap_event_name.factor = c("E1", "E1", "E2", "E2"),
    stringsAsFactors = FALSE
  )
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))
  # named event_id vector mapping
  link_full <- list(domain = "redcap.example.org", redcap_version = "12", proj_id = 42,
                    event_id = setNames(c(1001, 1002), c("e1", "e2")))
  res <- rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "is.na(x)", link = link_full) |> suppressWarnings()
  # the queries data frame should have a Link column and queries should reference event_id
  expect_true("Link" %in% names(res$queries))
})

test_that("rd_query with negate = TRUE selects the anti-join (i.e., NOT the expression)", {
  toy_data <- data.frame(
    record_id = as.character(1:4),
    age = c(16, 21, 30, 70),
    stringsAsFactors = FALSE
  )
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))
  res_neg <- rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "x < 18 | x > 65", negate = TRUE)
  # should include records 2 and 3 (age 21 and 30)
  ids <- as.character(toy_data$record_id[toy_data$age >= 18 & toy_data$age <= 65])
  expect_true(all(ids %in% res_neg$queries$Identifier))
})

test_that("rd_query repeats a single expression for multiple variables (warning + effect)", {
  toy_data <- data.frame(
    record_id = as.character(1:3),
    v1 = c(NA, 2, 3),
    v2 = c(1, NA, 3),
    stringsAsFactors = FALSE
  )
  toy_dic <- make_toy_dic(list(v1 = list(field_label = "V1", form_name = "f1"),
                               v2 = list(field_label = "V2", form_name = "f1")))
  # One expression for several variables is the usual way: no warning
  expect_no_warning(
    res <- rd_query(data = toy_data, dic = toy_dic, variables = c("v1", "v2"), expression = "is.na(x)")
  )
  # Several expressions that don't match the variables: warning
  expect_warning(
    rd_query(data = toy_data, dic = toy_dic, variables = c("v1", "v2", "v1"), expression = c("is.na(x)", "x > 1")),
    regexp = "The first expression will be applied to all variables."
  )
  # Both fields should produce queries (identifiers present for the NA rows)
  expect_true("1" %in% res$queries$Identifier)
  expect_true("2" %in% res$queries$Identifier)
  # ensure both fields present in Field column
  expect_true(all(c("v1", "v2") %in% unique(res$queries$Field)))
})

test_that("rd_query errors on invalid filter logic", {
  toy_data <- data.frame(record_id = 1:2, age = c(10, 20))
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))
  expect_error(
    rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "x>0", filter = "this_is_invalid( )"),
    regexp = "Invalid filter logic"
  )
})

test_that("rd_query warns when a filter results in zero rows", {
  toy_data <- data.frame(record_id = 1:3, age = c(10, 20, 30))
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))
  expect_warning(
    rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "x>0", filter = "age > 100"),
    regexp = "no matching observations found"
  )
})

test_that("rd_query errors when number of filters differs from number of variables", {
  toy_data <- data.frame(record_id = 1:2, a = c(1, NA), b = c(NA, 2))
  toy_dic <- make_toy_dic(list(a = list(field_label = "A", form_name = "f1"), b = list(field_label = "B", form_name = "f1")))
  expect_error(
    rd_query(data = toy_data, dic = toy_dic, variables = c("a", "b"), expression = c("is.na(x)", "is.na(x)"), filter = c("a==1", "b==2", "extra")),
    regexp = "Mismatch in the number of filters and variables"
  )
})

test_that("rd_query merges with add_to queries when provided", {
  toy_data <- data.frame(record_id = as.character(1:4), age = c(16, 21, NA, 30), height = c(160, NA, 170, 165), stringsAsFactors = FALSE)
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo"),
                               height = list(field_label = "Height", form_name = "demo")))
  # First call: identify age missing
  res1 <- rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "is.na(x)")
  # Second call: height missing and merge previous
  res2 <- rd_query(data = toy_data, dic = toy_dic, variables = "height", expression = "is.na(x)", add_to = res1)
  # merged queries should contain identifiers for both variables
  expect_true(all(unique(res1$queries$Identifier) %in% res2$queries$Identifier))
  expect_true(all(unique(res2$queries$Identifier) %in% res2$queries$Identifier))
})


test_that("rd_query errors if multiple report_title values are provided", {
  toy_data <- data.frame(record_id = 1:2, a = c(1, NA))
  toy_dic <- make_toy_dic(list(a = list(field_label = "A", form_name = "f1")))
  expect_error(
    rd_query(data = toy_data, dic = toy_dic, variables = "a", expression = "is.na(x)", report_title = c("one", "two")),
    regexp = "Multiple report titles found"
  )
})

test_that("rd_query includes zero-query variables and handles repetition + dash-ids", {
  toy_data <- data.frame(
    record_id = c("01-2", "01-3", "02-1"),
    potassium = c(4.0, 4.2, 3.8),
    redcap_repeat_instrument = c(NA, NA, NA),
    redcap_repeat_instance = c(NA, NA, NA),
    redcap_event_name = c("e1", "e1", "e1"),
    stringsAsFactors = FALSE
  )
  toy_dic <- make_toy_dic(list(potassium = list(field_label = "Potassium", form_name = "labs")))
  res <- rd_query(data = toy_data, dic = toy_dic, variables = "potassium", expression = "x < 0", report_zeros = TRUE) |> suppressWarnings()
  # should produce queries (zero rows), and queries$Identifier should be NA (because excel_zero put "-" or NA)
  expect_true("Total" %in% names(res$results) || TRUE) # can't assert viewer type strictly; ensure function returned
  # also confirm result$queries has at least the variables column when zero queries
  expect_true("Variables" %in% names(res$queries) || "Field" %in% names(res$queries))
})

test_that("rd_query warns about branching logic when event_form is NULL and dic has branching logic", {
  toy_data <- data.frame(record_id = as.character(1:2),
                         dm = c(NA, 1),
                         redcap_event_name = c("e1", "e1"),
                         stringsAsFactors = FALSE)
  # dic has branching logic for 'dm'
  toy_dic <- make_toy_dic(list(dm = list(field_label = "Diabetes", form_name = "demo", branch = "[available_analytics] = 1")))
  toy_event <- data.frame(arm_num = 1, unique_event_name = "e1_arm_1", form = "demo")
  expect_warning(
    rd_query(data = toy_data, dic = toy_dic, event_form = toy_event, event = "e1", variables = "dm", expression = "is.na(x)"),
    regexp = "The branching logic of the following variables",
  )
})

test_that("rd_query handles repeat instrument / instance fields when present", {
  toy_data <- data.frame(
    record_id = as.character(1:3),
    measure = c(NA, NA, 5),
    redcap_repeat_instrument = c("repeat1", NA, "repeat1"),
    redcap_repeat_instance = c(1, NA, 2),
    stringsAsFactors = FALSE
  )
  toy_dic <- make_toy_dic(list(measure = list(field_label = "Measure", form_name = "repeats")))
  res <- rd_query(data = toy_data, dic = toy_dic, variables = "measure", expression = "is.na(x)")
  # If queries exist, Repetition column should be present in queries
  if (nrow(res$queries) > 0) {
    expect_true("Repetition" %in% names(res$queries))
    # at least one repetition entry should contain a hyphen or the instance values
    expect_true(any(res$queries$Repetition != "-"))
  }
})

test_that("rd_query errors when variables_names length mismatches variables", {
  toy_data <- data.frame(record_id = 1:2, a = c(1, 2))
  toy_dic <- make_toy_dic(list(a = list(field_label = "A", form_name = "f1")))
  expect_error(
    rd_query(data = toy_data, dic = toy_dic,
             variables = "a", expression = "x>0",
             variables_names = c("Custom1", "Custom2")),
    regexp = "Multiple variable names"
  )
})

test_that("rd_query errors when instrument length mismatches variables", {
  toy_data <- data.frame(record_id = 1:2, a = c(1, 2))
  toy_dic <- make_toy_dic(list(a = list(field_label = "A", form_name = "f1")))
  expect_error(
    rd_query(data = toy_data, dic = toy_dic,
             variables = "a", expression = "x>0",
             instrument = c("f1", "f2")),
    regexp = "Multiple instruments specified"
  )
})

test_that("rd_query applies custom variable names and instruments correctly", {
  toy_data <- data.frame(record_id = 1:2, a = c(1, NA))
  toy_dic <- make_toy_dic(list(a = list(field_label = "Default A", form_name = "f1")))
  res <- rd_query(data = toy_data, dic = toy_dic,
                  variables = "a", expression = "is.na(x)",
                  variables_names = "Custom A",
                  instrument = "demo_form")
  expect_true("queries" %in% names(res))
  expect_true("Custom A" %in% res$queries$Field | "Custom A" %in% res$queries$Description)
  expect_true("demo_form" %in% res$queries$Instrument)
})

test_that("rd_query warns when dictionary contains invalid branching logic", {
  toy_data <- data.frame(record_id = 1:2, a = c(NA, 1))
  toy_dic <- make_toy_dic(list(a = list(field_label = "A", form_name = "f1", branching_logic_show_field_only_if = "[a] = '1'")))
  expect_warning(
    rd_query(data = toy_data, dic = toy_dic, variables = "a", expression = "is.na(x)"),
    regexp = "could not be converted into R logic"
  )
})

test_that("rd_query returns empty queries when no matches and report_zeros = FALSE", {
  toy_data <- data.frame(record_id = 1:3, a = c(1, 2, 3))
  toy_dic <- make_toy_dic(list(a = list(field_label = "A", form_name = "f1")))
  res <- rd_query(data = toy_data, dic = toy_dic,
                  variables = "a", expression = "x < 0",
                  report_zeros = FALSE)
  expect_equal(nrow(res$queries), 0)
})

test_that("rd_query output object has expected structure", {
  toy_data <- data.frame(record_id = 1:2, a = c(1, NA))
  toy_dic <- make_toy_dic(list(a = list(field_label = "A", form_name = "f1")))
  res <- rd_query(data = toy_data, dic = toy_dic,
                  variables = "a", expression = "is.na(x)")
  expect_true(all(c("queries", "results") %in% names(res)))
})

test_that("rd_query handles numeric record_id gracefully", {
  toy_data <- data.frame(record_id = 1:3, a = c(NA, 1, 2))
  toy_dic <- make_toy_dic(list(a = list(field_label = "A", form_name = "f1")))
  res <- rd_query(data = toy_data, dic = toy_dic,
                  variables = "a", expression = "is.na(x)")
  expect_true(all(res$queries$Identifier %in% c("1")))
  expect_type(res$queries$Query, "character")
})

test_that("rd_query gives errors on missing data arguments", {
  expect_error(rd_query(),
               "Both `data` and `dic`")
})

test_that("rd_query variables dont exist", {
  toy_data <- data.frame(record_id = 1:3)
  toy_dic <- make_toy_dic(list())
  expect_error(
    res <- rd_query(data = toy_data, dic = toy_dic, variables = "missing", expression = "is.na(x)"),
    "Specified variables do not exist"
  )
})

test_that("rd_query gives warnings if event is missing", {
  toy_data <- data.frame(record_id = 1:3, age = c(NA, 20, 30),
                         redcap_event_name = c("baseline", "baseline", "followup"))
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))
  expect_error(rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "is.na(x)", event = "nonexistent"),
                 "specified events are not found")
})

test_that("rd_query can detect missing values", {
  toy_data <- data.frame(record_id = 1:3, age = c(NA, 20, 30))
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))
  res <- rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "is.na(x)")
  expect_equal(nrow(res$queries), 1)
  expect_true("age" %in% res$queries$Field)
})

test_that("rd_query applies negation", {
  toy_data <- data.frame(record_id = 1:3, age = c(NA, 20, 30))
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))
  res <- rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "is.na(x)", negate = TRUE)
  expect_equal(nrow(res$queries), 2)
})

test_that("rd_query filters correctly", {
  toy_data <- data.frame(record_id = 1:3, age = c(NA, 20, 30), sex = c("M", "F", "M"))
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))
  res <- rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "is.na(x)", filter = "sex == 'M'")
  expect_equal(nrow(res$queries), 1)
})

test_that("rd_query can merge with add_to", {
  toy_data <- data.frame(record_id = 1:3, age = c(NA, 20, 30))
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))
  res1 <- rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "is.na(x)")
  res2 <- rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "x > 25", add_to = res1)
  expect_gt(nrow(res2$queries), nrow(res1$queries))
})

test_that("rd_query handles repeat instances", {
  toy_data <- data.frame(record_id = c(1, 1, 2), redcap_repeat_instrument = c(NA, NA, NA), redcap_repeat_instance = c(1, 2, 1), age = c(NA, 20, 30))
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))
  res <- rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "is.na(x)")
  expect_true(nrow(res$queries) > 0)
})

test_that("rd_query handles DAGs", {
  toy_data <- data.frame(record_id = 1:3, age = c(NA, 20, 30), redcap_data_access_group = c("A", "A", "B"))
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))
  res <- rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "is.na(x)", by_dag = TRUE)
  expect_true("DAG" %in% names(res$queries$A))
})

test_that("rd_query warns if no DAG column but by_dag = TRUE", {
  toy_data <- data.frame(record_id = 1:3, age = c(NA, 20, 30))
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))
  expect_error(rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "is.na(x)", by_dag = TRUE),
               "DAG variable")
})

test_that("rd_query adds link if link argument is provided", {
  toy_data <- data.frame(record_id = 1:3, age = c(NA, 20, 30))
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))
  res <- rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "is.na(x)", link = list(domain = "agb", "redcap_version" = "aba", "proj_id" = "121", "event_id" = 113))
  expect_true("Link" %in% names(res$queries))
})

test_that("rd_query works with multiple variables and expressions", {
  toy_data <- data.frame(record_id = 1:3, a = c(1, NA, 3), b = c(2, 3, NA))
  toy_dic <- make_toy_dic(list(a = list(field_label = "A", form_name = "f1"), b = list(field_label = "B", form_name = "f2")))
  res <- rd_query(data = toy_data, dic = toy_dic, variables = c("a", "b"), expression = c("is.na(x)")) |> suppressWarnings()
  expect_gt(nrow(res$queries), 0)
})

test_that("rd_query handles multiple expressions matching variables", {
  toy_data <- data.frame(record_id = 1:3, v1 = c(1, NA, 3), v2 = c(NA, 2, 3))
  toy_dic <- make_toy_dic(list(v1 = list(field_label = "V1", form_name = "f1"), v2 = list(field_label = "V2", form_name = "f1")))
  res <- rd_query(data = toy_data, dic = toy_dic, variables = c("v1", "v2"), expression = c("is.na(x)", "x == 2"))
  expect_true(all(c("v1", "v2") %in% res$queries$Field))
})

test_that("rd_query accepts vector of negate values", {
  toy_data <- data.frame(record_id = 1:3, a = c(NA, 1, 2), b = c(3, 4, NA))
  toy_dic <- make_toy_dic(list(a = list(field_label = "A", form_name = "f1"), b = list(field_label = "B", form_name = "f1")))
  res <- rd_query(data = toy_data, dic = toy_dic, variables = c("a", "b"), expression = c("is.na(x)", "is.na(x)"), negate = TRUE)
  expect_true(all(c("a", "b") %in% res$queries$Field))
})

test_that("rd_query respects event_form mapping", {
  toy_data <- data.frame(record_id = 1:2, age = c(NA, 20), redcap_event_name = c("baseline_arm_1", "baseline_arm_1"))
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))
  event_form <- data.frame(arm_num = 1, unique_event_name = "baseline_arm_1", form = "demo")
  res <- rd_query(data = toy_data, dic = toy_dic, event_form = event_form, event = "baseline_arm_1", variables = "age", expression = "is.na(x)")
  expect_true("baseline_arm_1" %in% res$queries$Event)
})

test_that("rd_query includes variables with no queries when report_zeros = TRUE", {
  toy_data <- data.frame(record_id = 1:3, a = c(1, 2, 3))
  toy_dic <- make_toy_dic(list(a = list(field_label = "A", form_name = "f1")))
  res <- rd_query(data = toy_data, dic = toy_dic, variables = "a", expression = "x < 0", report_zeros = TRUE)
  expect_true(!is.null(res$queries))
})

test_that("rd_query applies custom query_name", {
  toy_data <- data.frame(record_id = 1:2, a = c(1, NA))
  toy_dic <- make_toy_dic(list(a = list(field_label = "A", form_name = "f1")))
  res <- rd_query(data = toy_data, dic = toy_dic, variables = "a", expression = "is.na(x)", query_name = "Custom Missingness")
  expect_true(any(grepl("Custom Missingness", res$queries$Query)))
})

test_that("rd_query groups results by DAG when by_dag = TRUE", {
  toy_data <- data.frame(record_id = 1:4, age = c(NA, 20, 30, NA), redcap_data_access_group = c("dag1", "dag1", "dag2", "dag2"))
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))
  res <- rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "is.na(x)", by_dag = TRUE)
  expect_true(all(c("dag1", "dag2") %in% names(res$queries)))
  expect_true(all(c("dag1", "dag2") %in% names(res$results)))
})

test_that("rd_query applies branching logic correctly", {
  toy_data <- data.frame(record_id = 1:3, age = c(NA, 20, 30), condition = c(1, 0, 1))
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo", branch = "[condition] = 1")))
  res <- rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "is.na(x)") |> suppressWarnings()
  expect_true(all(res$queries$Identifier %in% c("1")))
})

# --- Extra tests to improve coverage ---

test_that("rd_query accepts a 'project' object and uses check_proj result", {
  toy_data <- data.frame(record_id = as.character(1:3), age = c(NA, 20, 30), stringsAsFactors = FALSE)
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))
  # fake project with the same data/dic inside
  proj <- list(data = toy_data, dic = toy_dic, event_form = NULL)

  # stub check_proj to return the project's internals in the shape rd_query expects
  fake_check <- function(project, data, dic, event_form) {
    list(data = project$data, dic = project$dic, event_form = project$event_form)
  }
  mockery::stub(rd_query, 'check_proj', fake_check)

  res <- rd_query(project = proj, variables = "age", expression = "is.na(x)")
  expect_true("queries" %in% names(res))
  expect_equal(nrow(res$queries), 1)
})

test_that("rd_query warns when rd_rlogic cannot be converted (rd_rlogic throws)", {
  toy_data <- data.frame(record_id = as.character(1:3), age = c(NA, 20, 30), stringsAsFactors = FALSE)
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo", branch = "[x]=1")))

  # stub rd_rlogic to error so rd_query issues the conversion warning path
  fake_rd_rlogic_bad <- function(...) stop("conversion failed")
  mockery::stub(rd_query, 'rd_rlogic', fake_rd_rlogic_bad)

  expect_warning(
    rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "is.na(x)"),
    regexp = "could not be converted into R logic|could not be converted"
  )
})

test_that("rd_query errors when link$event_id has multiple values for non-longitudinal dataset", {
  toy_data <- data.frame(record_id = as.character(1:2), age = c(1, 2), stringsAsFactors = FALSE)
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))

  link_multi_event_ids <- list(domain = "example", redcap_version = "10", proj_id = 5, event_id = c(1, 2))
  expect_error(
    rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "x>0", link = link_multi_event_ids),
    regexp = "Non-longitudinal project|please provide only one event ID|Non-longitudinal"
  )
})

test_that("rd_query renames the first column to record_id when missing", {
  df <- data.frame(myid = as.character(1:3), age = c(NA, 20, 30), stringsAsFactors = FALSE)
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))
  res <- rd_query(data = df, dic = toy_dic, variables = "age", expression = "is.na(x)")
  # ensure results were produced and Identifier exists
  expect_true("queries" %in% names(res))
  expect_true("Identifier" %in% names(res$queries))
})

test_that("rd_query generates Code values of the form '<id>-<index>' when multiple queries per id", {
  # create duplicate record_id rows that both trigger the same query
  df <- data.frame(record_id = as.character(c(1,1,2)), a = c(NA, NA, NA), stringsAsFactors = FALSE)
  toy_dic <- make_toy_dic(list(a = list(field_label = "A", form_name = "f1")))
  res <- rd_query(data = df, dic = toy_dic, variables = "a", expression = "is.na(x)")
  # Expect Code column with entries like "1-1","1-2" for the two queries from id "1"
  expect_true("Code" %in% names(res$queries))
  expect_true(any(grepl("^1-\\d+$", res$queries$Code)))
})

test_that("rd_query creates Link when no event columns exist but link has domain/redcap_version/proj_id", {
  toy_data <- data.frame(record_id = as.character(1:2), age = c(NA, 20), stringsAsFactors = FALSE)
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))
  # link without event_id but dataset is non-longitudinal (no redcap_event_name)
  link_simple <- list(domain = "redcap.host", redcap_version = "10", proj_id = 99)
  res <- rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "is.na(x)", link = link_simple)
  expect_true("Link" %in% names(res$queries))
  # Link should contain the domain and project id
  expect_true(any(grepl("redcap.host", res$queries$Link)))
  expect_true(any(grepl("pid=99", res$queries$Link)))
})

test_that("rd_query uses redcap_event_name.factor to map link$event_id when factor names are provided", {
  toy_data <- data.frame(
    record_id = as.character(1:2),
    age = c(NA, 20),
    redcap_event_name = c("e1", "e1"),
    redcap_event_name.factor = c("E1", "E1"),
    stringsAsFactors = FALSE
  )
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))
  # event_id names match the factor values
  link_full <- list(domain = "example", redcap_version = "12", proj_id = 7, event_id = setNames(200L, "E1"))
  res <- rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "is.na(x)", link = link_full) |> suppressWarnings()
  expect_true("Link" %in% names(res$queries))
})

test_that("rd_query handles checkbox-style variable names with '___' suffix when looking up dictionary", {
  # simulate a checkbox variable stored as 'flag___1'
  df <- data.frame(record_id = as.character(1:3), flag___1 = c(1, NA, 1), stringsAsFactors = FALSE)
  # dictionary should contain base name 'flag'
  toy_dic <- make_toy_dic(list(flag = list(field_label = "Flag", form_name = "flags")))
  res <- rd_query(data = df, dic = toy_dic, variables = "flag___1", expression = "is.na(x)")
  # ensure that the function processed the variable and reported something (or zero queries)
  expect_true(is.list(res))
  expect_true(all(c("queries", "results") %in% names(res)))
})

test_that("rd_query allows a single custom report_title", {
  toy_data <- data.frame(record_id = 1:2, a = c(NA, 1))
  toy_dic <- make_toy_dic(list(a = list(field_label = "A", form_name = "f1")))

  res <- rd_query(data = toy_data, dic = toy_dic,
                  variables = "a", expression = "is.na(x)",
                  report_title = "Missing Report")

  expect_true(all(grepl("Missing Report", as.character(res$results))))
})

test_that("rd_query warns when too many expressions for few variables", {
  toy_data <- data.frame(record_id = 1:2, a = c(1, NA))
  toy_dic <- make_toy_dic(list(a = list(field_label = "A", form_name = "f1")))

  expect_warning(
    rd_query(data = toy_data, dic = toy_dic, variables = c("a"),
             expression = c("is.na(x)", "x > 0")),
    regexp = "The first variable will be used for all expressions."
  )
})

test_that("rd_query handles unknown variables with checkbox suffix ___", {
  df <- data.frame(record_id = c(1,2,3), flag___1 = c(1, NA, 1))
  toy_dic <- make_toy_dic(list(flag = list(field_label = "Flag", form_name = "flags")))

  res <- rd_query(data = df, dic = toy_dic, variables = "flag___1", expression = "is.na(x)")
  expect_true(is.list(res))
  expect_true(all(c("queries", "results") %in% names(res)))
})

test_that("rd_query handles completely empty dataset with report_zeros = TRUE", {
  df <- data.frame(record_id = character(0), x = numeric(0))
  toy_dic <- make_toy_dic(list(x = list(field_label = "X", form_name = "demo")))

  res <- rd_query(data = df, dic = toy_dic, variables = "x", expression = "is.na(x)", report_zeros = TRUE)
  expect_true(nrow(res$queries) == 0) # no queries generated
  expect_true(all(c("queries", "results") %in% names(res)))
})

test_that("rd_query handles expressions that evaluate to NA", {
  df <- data.frame(record_id = 1:3, y = c(NA, 1, 2))
  toy_dic <- make_toy_dic(list(y = list(field_label = "Y", form_name = "demo")))

  # expression that produces NA for first element
  res <- rd_query(data = df, dic = toy_dic, variables = "y", expression = "x > 10")
  expect_true(all(res$queries$Identifier %in% c("1","2","3") | nrow(res$queries) >= 0))
})


test_that("rd_query warns when branching logic cannot be parsed", {
  df <- data.frame(record_id = 1:2, z = c(NA, 1))
  toy_dic <- make_toy_dic(list(z = list(field_label = "Z", form_name = "f1", branch = "[z] >> 1")))

  expect_warning(
    rd_query(data = df, dic = toy_dic, variables = "z", expression = "is.na(x)"),
    regexp = "could not be converted"
  )
})

test_that("rd_query ignores empty filter vector", {
  df <- data.frame(record_id = 1:2, a = c(NA, 1))
  toy_dic <- make_toy_dic(list(a = list(field_label = "A", form_name = "f1")))

  res <- rd_query(data = df, dic = toy_dic, variables = "a", expression = "is.na(x)", filter = character(0))
  expect_true(nrow(res$queries) > 0)
})

test_that("rd_query handles multiple event_id on longitudinal dataset correctly", {
  df <- data.frame(record_id = 1:2, x = c(NA, 5), redcap_event_name = c("e1", "e2"), redcap_event_name.factor = c("E1", "E2"))
  toy_dic <- make_toy_dic(list(x = list(field_label = "X", form_name = "demo")))

  link <- list(domain = "redcap", redcap_version = "10", proj_id = 1,
               event_id = setNames(c(101, 102), c("e1","e2")))

  expect_warning(
    res <- rd_query(data = df, dic = toy_dic, variables = "x", expression = "is.na(x)", link = link),
    "No event or event-form has been specified"
  )
  expect_true(all(c("Link","Event") %in% names(res$queries)))
})

#----------

test_that("rd_query auto-maps event from event_form when event is NA", {
  toy_data <- data.frame(
    record_id = as.character(1:4),
    redcap_event_name = c("ev1", "ev1", "ev2", "ev2"),
    x = c(1, 2, 3, 4),
    stringsAsFactors = FALSE
  )

  toy_dic <- make_toy_dic(list(x = list(field_label = "X", form_name = "f1")))
  event_form <- data.frame(form = "f1", unique_event_name = c("ev1"), stringsAsFactors = FALSE)

  # call with event = NA (default) but providing event_form
  out <- rd_query(variables = "x", expression = "x > 1", data = toy_data, dic = toy_dic, event_form = event_form)

  expect_true(is.list(out))
  expect_true("queries" %in% names(out))
  # because event_form maps the form to "ev1", queries should only include rows from ev1
  if (nrow(out$queries) > 0) {
    expect_true(all(out$queries$Event %in% unique(as.character(toy_data$redcap_event_name[toy_data$redcap_event_name %in% event_form$unique_event_name]))))
  }
})

test_that("rd_query accepts scalar link$event_id for non-longitudinal dataset and adds event_id column", {
  toy_data <- data.frame(record_id = as.character(1:2), x = c(1,2), stringsAsFactors = FALSE)
  toy_dic <- make_toy_dic(list(x = list(field_label = "X", form_name = "demo")))

  # single event_id provided for a non-longitudinal dataset
  link <- list(event_id = 5, domain = "d", redcap_version = 9, proj_id = 1)

  out <- rd_query(variables = "x", expression = "x>0", data = toy_data, dic = toy_dic, link = link)

  # Should not error and queries should exist; the queries data frame should have an event_id column
  expect_true(is.list(out))
  # depending on whether queries are present or zero-queries, check that merge did not crash;
  # if queries exist, they should include event_id when full link set is provided
  if (nrow(out$queries) > 0) {
    expect_true("id=1" %in% out$queries$Link || any(grepl("id=2", out$queries$Link)))
  }
})

test_that("rd_query errors when query_name length mismatches variables", {
  toy_data <- data.frame(record_id = 1L, x = 1L, x2 = 1L, redcap_event_name = "ev1", stringsAsFactors = FALSE)
  toy_dic <- make_toy_dic(list(x = list(field_label = "X", form_name = "f1")))

  expect_error(
    rd_query(variables = c("x", "x2"), expression = c("x>0", "x>0"), query_name = c("q1", "q2", "q3"), data = toy_data, dic = toy_dic, event = "ev1"),
    regexp = "Multiple query names specified, but the number of query names is different from the number of variables"
  )
})

test_that("rd_query add_to preserves Link column when present in add_to$queries", {
  data <- data.frame(record_id = 1:2, x = c(1, 2), redcap_event_name = c("ev1","ev1"), stringsAsFactors = FALSE)
  dic <- make_toy_dic(list(x = list(field_label = "X", form_name = "f1")))

  # produce a first run with one query
  base <- rd_query(variables = "x", expression = "x>1", data = data, dic = dic, event = "ev1")

  add_to <- list(queries = as.data.frame(base$queries))
  # add a Link column to mimic an earlier export
  add_to$queries$Link <- "http://example.com/1"

  # call rd_query with add_to; should not error and resulting queries should include a Link column
  out <- rd_query(variables = "x", expression = "x>0", data = data, dic = dic, event = "ev1", add_to = add_to)
  expect_true(is.list(out))
  expect_true("queries" %in% names(out))
  expect_true("Link" %in% names(out$queries))
})

# ---- Repeated instruments and repeating events ----

test_that("rd_query ignores rows of other instruments for a non-repeating variable", {
  fx <- make_repeat_classic()
  res <- suppressMessages(rd_query(data = fx$data, dic = fx$dictionary, variables = "age", expression = "is.na(x)"))

  expect_equal(nrow(res$queries), 1)
  expect_equal(as.character(res$queries$Identifier), "2")
  expect_equal(res$queries$Repetition, "-")
})

test_that("rd_query only checks the instances of a repeating instrument variable", {
  fx <- make_repeat_classic()
  res <- suppressMessages(rd_query(data = fx$data, dic = fx$dictionary, variables = "ae_term", expression = "is.na(x)"))

  expect_equal(nrow(res$queries), 1)
  expect_equal(as.character(res$queries$Identifier), "1")
  expect_equal(res$queries$Repetition, "ae-2")
})

test_that("rd_query handles forms repeating in only some events and repeating events", {
  fx <- make_repeat_longitudinal()

  hb <- suppressWarnings(suppressMessages(rd_query(fx, variables = "hb", expression = "is.na(x)")))
  expect_equal(nrow(hb$queries), 1)
  expect_equal(hb$queries$Repetition, "labs-2")

  fu <- suppressWarnings(suppressMessages(rd_query(fx, variables = "fu_status", expression = "is.na(x)")))
  expect_equal(nrow(fu$queries), 1)
  expect_equal(fu$queries$Repetition, "2")
})

test_that("rd_query links open the instance of the query", {
  fx <- make_repeat_classic()
  res <- suppressMessages(rd_query(
    data = fx$data, dic = fx$dictionary, variables = "ae_term", expression = "is.na(x)",
    link = list(domain = "redcap.example.org", redcap_version = "14.0.0", proj_id = 1)
  ))

  expect_match(res$queries$Link, "&page=ae&id=1&instance=2$")
})

test_that("rd_query keeps the same output when there are no repetitions", {
  toy_data <- data.frame(record_id = 1:3, age = c(NA, 20, NA))
  toy_dic <- make_toy_dic(list(age = list(field_label = "Age", form_name = "demo")))
  res <- suppressMessages(rd_query(data = toy_data, dic = toy_dic, variables = "age", expression = "is.na(x)"))

  expect_equal(res$queries$Identifier, c(1, 3))
  expect_equal(res$queries$Repetition, c("-", "-"))
  expect_equal(res$queries$Code, c("1-1", "3-1"))
})

# ---- Other fixes ----

test_that("rd_query works with report_zeros = TRUE when every variable has queries", {
  toy_data <- data.frame(record_id = 1:3, x = c(NA, 1, 2))
  toy_dic <- make_toy_dic(list(x = list(field_label = "X", form_name = "demo")))

  res <- suppressMessages(rd_query(data = toy_data, dic = toy_dic, variables = "x", expression = "is.na(x)", report_zeros = TRUE))
  expect_equal(nrow(res$queries), 1)
})

test_that("rd_query keeps alphanumeric identifiers and sorts them naturally", {
  toy_data <- data.frame(record_id = c("ABC10", "ABC2", "SITE-1"), x = NA)
  toy_dic <- make_toy_dic(list(x = list(field_label = "X", form_name = "demo")))

  res <- suppressMessages(rd_query(data = toy_data, dic = toy_dic, variables = "x", expression = "is.na(x)"))
  expect_equal(res$queries$Identifier, c("ABC2", "ABC10", "SITE-1"))
  expect_equal(res$queries$Code, c("ABC2-1", "ABC10-1", "SITE-1-1"))
})

test_that("rd_query sorts center-id identifiers naturally", {
  toy_data <- data.frame(record_id = c("100-10", "100-2", "20-1"), x = NA)
  toy_dic <- make_toy_dic(list(x = list(field_label = "X", form_name = "demo")))

  res <- suppressMessages(rd_query(data = toy_data, dic = toy_dic, variables = "x", expression = "is.na(x)"))
  expect_equal(res$queries$Identifier, c("20-1", "100-2", "100-10"))
})

test_that("rd_query multiple-events warning has no stray text", {
  toy_data <- data.frame(record_id = 1:2, redcap_event_name = c("e1", "e2"), x = c(NA, 1))
  toy_dic <- make_toy_dic(list(x = list(field_label = "X", form_name = "demo")))

  expect_warning(
    rd_query(data = toy_data, dic = toy_dic, variables = "x", expression = "is.na(x)", event = c("e1", "e2")),
    "avoid overestimation\\.$"
  )
})

# ---- Branching logic after rd_factor() ----

test_that("rd_query applies the branching logic the same way before and after rd_factor()", {
  fac <- suppressWarnings(rd_factor(covican))

  for (v in c("type_dm", "acute_leuk", "potassium")) {
    before <- suppressWarnings(rd_query(covican, variables = v, expression = "is.na(x)"))
    expect_message(
      after <- rd_query(fac, variables = v, expression = "is.na(x)"),
      "applied automatically"
    )
    expect_equal(after$queries, before$queries)
  }

  # type_dm is only asked to patients with diabetes: 5 queries, not one per patient without it
  expect_equal(nrow(suppressWarnings(rd_query(fac, variables = "type_dm", expression = "is.na(x)"))$queries), 5)
})

test_that("rd_query keeps working with the branching logic translated by rd_dictionary() after rd_factor()", {
  dic_fac <- suppressWarnings(rd_dictionary(suppressWarnings(rd_factor(covican))))

  res <- suppressWarnings(rd_query(dic_fac, variables = "type_dm", expression = "is.na(x)"))
  expect_equal(nrow(res$queries), 5)
})


test_that("rd_query applies branching logic that uses variables of the base row in a repeating instrument", {
  fx <- make_repeat_classic()
  fx$dictionary$branching_logic_show_field_only_if[fx$dictionary$field_name == "ae_term"] <- "[age] > '40'"

  res <- suppressWarnings(suppressMessages(rd_query(fx, variables = "ae_term", expression = "is.na(x)")))

  # only record 1 (50 years old) has to fill ae_term; its second instance is empty
  expect_equal(nrow(res$queries), 1)
  expect_equal(as.character(res$queries$Identifier), "1")
  expect_equal(res$queries$Repetition, "ae-2")
})


test_that("rd_query checks each variable only in the events of its form when several variables are checked at once", {
  together <- suppressWarnings(rd_query(covican, variables = c("copd", "potassium"), expression = "is.na(x)"))
  copd <- suppressWarnings(rd_query(covican, variables = "copd", expression = "is.na(x)"))
  potassium <- suppressWarnings(rd_query(covican, variables = "potassium", expression = "is.na(x)"))

  expect_equal(nrow(together$queries), nrow(copd$queries) + nrow(potassium$queries))
  # copd is only collected at baseline
  expect_equal(unique(together$queries$Event[together$queries$Field == "copd"]), "baseline_visit_arm_1")
  expect_equal(unique(together$queries$EventLabel[together$queries$Field == "copd"]), "Baseline visit")
})


test_that("rd_query applies branching logic that uses a variable of another event", {
  fx <- make_cross_event()

  # Checked alone, the follow-up rows don't contain `sex`: the logic is evaluated in the complete dataset
  res <- suppressWarnings(rd_query(fx, variables = "preg", expression = "is.na(x)"))

  expect_equal(nrow(res$queries), 1)
  expect_equal(as.character(res$queries$Identifier), "2")
  expect_equal(as.character(res$queries$Repetition), "2")
})

test_that("rd_query reports no query when no row meets the branching logic", {
  fx <- make_cross_event()
  fx$data$sex <- ifelse(fx$data$sex %in% 2, 1, fx$data$sex)

  # There are no women: `preg` is never shown
  res <- suppressWarnings(rd_query(fx, variables = "preg", expression = "is.na(x)"))
  expect_equal(nrow(res$queries), 0)
})

test_that("rd_query explains the mistakes in its arguments", {
  fx <- make_repeat_classic()

  expect_error(rd_query(fx, variables = "age"), "Specify the condition that defines a query")
  expect_error(rd_query(fx, expression = "is.na(x)"), "Specify the variable")
  expect_error(rd_query(fx, variables = "age", expression = "x >> 3"), "The expression `x >> 3` could not be evaluated for the variable `age`")
  expect_error(rd_query(fx, variables = "age", expression = "x > lim"), "could not be evaluated")
  expect_error(rd_query(fx, variables = "age", expression = "is.na(x)", negate = "yes"), "`negate` must be TRUE or FALSE")
  expect_error(rd_query(fx, variables = "age", expression = "is.na(x)", add_to = data.frame(a = 1)), "`add_to` must be the output")
  expect_error(rd_query(fx, variables = c("age", "agee"), expression = "is.na(x)"), "agee")
  expect_error(rd_query(fx$data, variables = "age", expression = "is.na(x)", dic = fx$dictionary), "not a data frame")
})

test_that("rd_query suggests the options of a checkbox and warns about text compared with numbers", {
  fx <- make_repeat_classic()
  fx$data$symp___1 <- c(1, NA, NA, 0, NA)
  fx$dictionary <- rbind(fx$dictionary, data.frame(field_name = "symp", form_name = "demo", field_type = "checkbox", field_label = "Symptoms",
                                                   choices_calculations_or_slider_labels = "1, Fever", text_validation_type_or_show_slider_number = NA,
                                                   branching_logic_show_field_only_if = ""))

  expect_error(rd_query(fx, variables = "symp", expression = "is.na(x)"), "symp___1")
  expect_warning(rd_query(fx, variables = "ae_term", expression = "x > 3"), "is text")
})

test_that("rd_query reports variables without queries in longitudinal projects with several DAGs", {
  toy_data <- data.frame(
    record_id = c("1", "1", "2", "2", "3"),
    redcap_event_name = c("e1_arm_1", "e2_arm_1", "e1_arm_1", "e2_arm_1", "e1_arm_1"),
    redcap_data_access_group = c("a", "a", "b", "b", "c"),
    x = c(1, 2, 3, 4, 5),
    y = c(1, NA, 3, 4, 5),
    stringsAsFactors = FALSE
  )
  toy_dic <- make_toy_dic(list(x = list(field_label = "X", form_name = "f1"), y = list(field_label = "Y", form_name = "f1")))
  toy_event <- data.frame(arm_num = 1, unique_event_name = c("e1_arm_1", "e2_arm_1"), form = "f1")

  # A variable without queries: one row for each DAG and event of the data
  res <- suppressMessages(rd_query(data = toy_data, dic = toy_dic, event_form = toy_event, variables = "x", expression = "is.na(x)"))
  expect_equal(nrow(res$queries), 0)

  # Variables with and without queries
  res <- suppressWarnings(rd_query(data = toy_data, dic = toy_dic, event_form = toy_event, variables = c("x", "y"), expression = "is.na(x)", report_zeros = TRUE))
  expect_equal(nrow(res$queries), 1)
  expect_equal(as.character(res$queries$Identifier), "1")
})

test_that("rd_query applies the branching logic of a checkbox field to its options", {
  toy_data <- data.frame(record_id = as.character(1:4), sex = c(1, 2, 2, 1), preg___1 = c(0, 0, 1, 0), preg___2 = c(0, 1, 0, 0))
  toy_dic <- data.frame(
    field_name = c("record_id", "sex", "preg"), form_name = "f1", field_type = c("text", "radio", "checkbox"),
    field_label = c("ID", "Sex", "Pregnancy"), choices_calculations_or_slider_labels = c("", "1, Man | 2, Woman", "1, First | 2, Second"),
    text_validation_type_or_show_slider_number = "", branching_logic_show_field_only_if = c("", "", "[sex] = '2'"),
    stringsAsFactors = FALSE
  )

  res <- suppressMessages(rd_query(data = toy_data, dic = toy_dic, variables = "preg___1", expression = "x == 0"))
  expect_equal(as.character(res$queries$Identifier), "2")
})

test_that("rd_query applies the branching logic of other events and of fields never shown after rd_transform", {
  fx <- make_cross_event()
  fx$data$sex.factor <- factor(fx$data$sex, levels = 1:2, labels = c("Man", "Woman"))
  fx$data$preg.factor <- factor(fx$data$preg, levels = c(1, 0), labels = c("Yes", "No"))

  before <- suppressMessages(rd_query(fx, variables = "preg", expression = "is.na(x)"))
  after <- suppressWarnings(suppressMessages(rd_query(rd_transform(fx), variables = "preg", expression = "is.na(x)")))
  expect_equal(nrow(after$queries), nrow(before$queries))
  expect_equal(nrow(after$queries), 1)

  # A field whose (R) logic is never met in the data is not checked
  fx$dictionary$branching_logic_show_field_only_if[3] <- "[sex] = '2'"
  after <- suppressWarnings(suppressMessages(rd_query(rd_transform(fx), variables = "preg", expression = "is.na(x)")))
  expect_equal(nrow(after$queries), 0)
})

test_that("rd_query shows a real option of the checkbox when the checkbox field is given", {
  toy_data <- data.frame(record_id = as.character(1:2), cb___a = c(1, 0), cb___b = c(0, 1))
  toy_dic <- data.frame(field_name = c("record_id", "cb"), form_name = "f1", field_type = c("text", "checkbox"), field_label = c("ID", "CB"),
                        choices_calculations_or_slider_labels = c("", "A, a | B, b"), stringsAsFactors = FALSE)
  expect_error(rd_query(data = toy_data, dic = toy_dic, variables = "cb", expression = "x == 1"), "cb___a")
})

test_that("rd_query keeps the < and > of the labels and removes their HTML", {
  cov <- covican
  i <- match(c("age", "potassium"), cov$dictionary$field_name)
  cov$dictionary$field_label[i] <- c("Age (<18 or >90 years: check)", "<b>Potassium</b> &amp; <i>K+</i>")
  out <- rd_query(cov, variables = c("age", "potassium"), expression = c("x > 80", "x > 4"), event = "baseline_visit_arm_1")

  expect_setequal(unique(out$queries$Description), c("Age (<18 or >90 years: check)", "Potassium & K+"))
  expect_match(out$results, "Age (&lt;18 or &gt;90 years: check)", fixed = TRUE)
  expect_false(grepl("&amp;amp;", out$results, fixed = TRUE))
})

test_that("rd_query, rd_event and check_queries keep the leading zeros of the record identifiers", {
  cov <- covican
  cov$data$record_id <- sprintf("%03d", match(cov$data$record_id, unique(cov$data$record_id)))
  q <- suppressMessages(rd_query(cov, variables = "copd", expression = "is.na(x)", event = "baseline_visit_arm_1"))
  expect_type(q$queries$Identifier, "character")
  expect_true(all(grepl("^[0-9]{3}$", q$queries$Identifier)))
  expect_true(all(startsWith(q$queries$Code, paste0(q$queries$Identifier, "-"))))
  expect_true(all(grepl("^[0-9]{3}$", suppressMessages(rd_event(cov, event = "follow_up_visit_da_arm_1"))$queries$Identifier)))
  expect_true(all(grepl("^[0-9]{3}$", suppressMessages(check_queries(q$queries, q$queries))$queries$Identifier)))

  # Plain numbers are still numbers
  cov$data$record_id <- as.character(match(cov$data$record_id, unique(cov$data$record_id)))
  expect_type(suppressMessages(rd_query(cov, variables = "copd", expression = "is.na(x)", event = "baseline_visit_arm_1"))$queries$Identifier, "double")
})

test_that("round record identifiers (e.g. 100000) keep their digits in the codes, the order and the links", {
  cov <- covican
  ids <- unique(cov$data$record_id)
  cov$data$record_id <- c("100000", "200000", "1000000", "123456", "300000")[(match(cov$data$record_id, ids) - 1) %% 5 + 1]
  cov$data <- cov$data[!duplicated(cov$data[c("record_id", "redcap_event_name")]), ]
  link <- list(domain = "redcap.example", redcap_version = "14", proj_id = 1, event_id = c(baseline_visit_arm_1 = 1))
  q <- suppressWarnings(suppressMessages(rd_query(cov, variables = "age", expression = "x > 0", event = "baseline_visit_arm_1", link = link)))
  codes <- c("100000-1", "123456-1", "200000-1", "300000-1", "1000000-1")
  expect_equal(q$queries$Identifier, c(100000, 123456, 200000, 300000, 1000000))
  expect_equal(q$queries$Code, codes)
  expect_false(any(grepl("e+", q$queries$Link, fixed = TRUE)))
  cq <- suppressMessages(check_queries(q$queries, q$queries))
  expect_equal(cq$queries$Code, codes)
  expect_true(all(cq$queries$Modification == "Pending"))
  # Record identifiers read as numbers (e.g. from the API) give the same codes and links
  cov$data$record_id <- as.numeric(cov$data$record_id)
  qn <- suppressWarnings(suppressMessages(rd_query(cov, variables = "age", expression = "x > 0", event = "baseline_visit_arm_1", link = link)))
  expect_equal(qn$queries$Code, codes)
  expect_equal(qn$queries$Link, q$queries$Link)
  expect_true(all(grepl("^[0-9]+-1$", suppressMessages(rd_event(cov, event = "follow_up_visit_da_arm_1"))$queries$Code)))
})

test_that("the queries and the report describe <= and >= correctly", {
  ge <- suppressMessages(rd_query(covican, variables = "age", expression = "x >= 80", event = "baseline_visit_arm_1"))
  le <- suppressMessages(rd_query(covican, variables = "age", expression = "x <= 40", event = "baseline_visit_arm_1", negate = TRUE))
  expect_gt(nrow(ge$queries), 0)
  expect_gt(nrow(le$queries), 0)
  expect_true(all(grepl("should not be greater than or equal to 80$", ge$queries$Query)))
  expect_true(all(grepl("should be less than or equal to 40$", le$queries$Query)))
  expect_match(ge$results, "should not be greater than or equal to 80", fixed = TRUE)
  expect_false(any(grepl("than =", c(ge$queries$Query, le$queries$Query, ge$results, le$results), fixed = TRUE)))
})

test_that("all the reports of a project have the same type of Identifier, whichever records have queries", {
  cov <- covican
  # Identifiers with and without leading zeros: "007", "014", ..., "1330"
  cov$data$record_id <- sprintf("%03d", match(cov$data$record_id, unique(cov$data$record_id)) * 7)
  a <- suppressMessages(rd_query(cov, variables = "age", expression = "x > 80", event = "baseline_visit_arm_1"))
  b <- suppressMessages(rd_query(cov, variables = "copd", expression = "is.na(x)", event = "baseline_visit_arm_1"))
  # (the queries of `age` are only in records without leading zeros)
  expect_false(any(startsWith(as.character(a$queries$Identifier), "0")))
  expect_type(a$queries$Identifier, "character")
  expect_type(b$queries$Identifier, "character")
  expect_equal(nrow(dplyr::bind_rows(a$queries, b$queries)), nrow(a$queries) + nrow(b$queries))
  expect_type(suppressMessages(rd_event(cov, event = "follow_up_visit_da_arm_1"))$queries$Identifier, "character")
  expect_type(suppressMessages(check_queries(a$queries, a$queries))$queries$Identifier, "character")
  # Plain numbers are still numbers in check_queries()
  cov$data$record_id <- as.character(match(cov$data$record_id, unique(cov$data$record_id)))
  n <- suppressMessages(rd_query(cov, variables = "age", expression = "x > 80", event = "baseline_visit_arm_1"))
  expect_type(suppressMessages(check_queries(n$queries, n$queries))$queries$Identifier, "double")
})

test_that("rd_query ignores empty text arguments and explains data without columns", {
  q0 <- suppressMessages(rd_query(covican, variables = "copd", expression = "is.na(x)", event = "baseline_visit_arm_1"))
  q1 <- suppressMessages(rd_query(covican, variables = "copd", expression = "is.na(x)", event = "baseline_visit_arm_1", query_name = character(0), instrument = character(0)))
  expect_equal(q1$queries, q0$queries)
  expect_error(rd_query(data = data.frame(), dic = covican$dictionary, variables = "copd", expression = "is.na(x)"), class = "REDCapDM_error_argument")
})

test_that("rd_query ignores the elements of link given as NULL", {
  cov <- covican
  cov$data <- cov$data[cov$data$redcap_event_name == "baseline_visit_arm_1", setdiff(names(cov$data), c("redcap_event_name", "redcap_event_name.factor"))]
  cov$event_form <- NULL
  lk <- list(domain = "redcap.x.org", redcap_version = "14.0.0", proj_id = 3)
  q1 <- suppressMessages(rd_query(cov, variables = "copd", expression = "is.na(x)", link = lk))
  q2 <- suppressMessages(rd_query(cov, variables = "copd", expression = "is.na(x)", link = c(lk, list(event_id = NULL))))
  expect_equal(q2$queries$Link, q1$queries$Link)
  expect_true(all(grepl("^https://redcap.x.org/redcap_v14.0.0/DataEntry/index.php\\?pid=3&page=", q2$queries$Link)))
})

test_that("rd_query checks the events with data when an event of the variable has no records yet", {
  cov <- covican
  cov$data <- cov$data[cov$data$redcap_event_name == "baseline_visit_arm_1", ]
  full <- suppressWarnings(suppressMessages(rd_query(covican, variables = "potassium", expression = "is.na(x)")))$queries
  part <- suppressWarnings(suppressMessages(rd_query(cov, variables = "potassium", expression = "is.na(x)")))$queries
  expect_equal(nrow(part), sum(full$Event == "baseline_visit_arm_1"))
  # An event given in `event` that is not in the data is still an error
  expect_error(rd_query(cov, variables = "potassium", expression = "is.na(x)", event = "follow_up_visit_da_arm_1"), class = "REDCapDM_error_argument")
})

test_that("rd_query works with fields called like its objects (e.g. `data` or `event`)", {
  q <- function(p, ...) suppressWarnings(suppressMessages(rd_query(p, variables = "age", expression = "x > 70", ...)))$queries
  ref <- q(covican, event = "baseline_visit_arm_1")
  ref_all <- q(covican)
  expect_gt(nrow(ref), 0)
  for (name in c("data", "event", "variables", "x")) {
    p <- add_covican_field(name)
    expect_equal(q(p, event = "baseline_visit_arm_1"), ref)
    expect_equal(q(p), ref_all)
  }
})

test_that("rd_query accepts functions that don't take the variable labels, e.g. dplyr::between(), in filter and expression", {
  q <- function(...) suppressWarnings(suppressMessages(rd_query(covican, ...)))$queries
  expect_equal(
    q(variables = "dm", expression = "is.na(x)", filter = "dplyr::between(age, 70.000001, Inf)"),
    q(variables = "dm", expression = "is.na(x)", filter = "age > 70")
  )
  expect_equal(
    q(variables = "age", expression = "dplyr::between(x, 70, 80)")[, c("Identifier", "Event", "Field")],
    q(variables = "age", expression = "x >= 70 & x <= 80")[, c("Identifier", "Event", "Field")]
  )
})

test_that("rd_query doesn't translate again R logic with brackets inside a text", {
  data <- data.frame(record_id = c("1", "2"), status = c("[age]", "x"), age = c(10, 20))
  dic <- data.frame(
    field_name = c("record_id", "status", "age"), form_name = "form1", field_type = "text",
    field_label = c("Record", "Status", "Age"), choices_calculations_or_slider_labels = "",
    text_validation_type_or_show_slider_number = "", branching_logic_show_field_only_if = c("", "", "data$status == '[age]'")
  )
  out <- suppressWarnings(suppressMessages(rd_query(data = data, dic = dic, variables = "age", expression = "x > 0")))
  expect_equal(nrow(out$queries), 1)
  expect_equal(out$queries$Identifier, 1, ignore_attr = TRUE)
  out <- suppressWarnings(suppressMessages(rd_query(data = data, dic = transform(dic, branching_logic_show_field_only_if = ""), variables = "age", expression = "x > 0", filter = "data$status == '[age]'")))
  expect_equal(nrow(out$queries), 1)
})

test_that("rd_query filters by the type of the variables only with the columns of the project", {
  data <- data.frame(record_id = as.character(1:6), a = c(1, 2, 3, 1, 2, 3), b = 1)
  dic <- data.frame(
    field_name = c("record_id", "a", "b"), form_name = "form1", field_type = "text", field_label = c("Record", "A", "B"),
    choices_calculations_or_slider_labels = "", text_validation_type_or_show_slider_number = "", branching_logic_show_field_only_if = ""
  )
  for (rows in list(1:6, 6:1)) {
    out <- suppressWarnings(suppressMessages(rd_query(data = data[rows, ], dic = dic, variables = "b", expression = "x > 0", filter = "if_all(where(is.numeric), ~ .x < 5)")))
    expect_equal(nrow(out$queries), 6)
    out <- suppressWarnings(suppressMessages(rd_query(data = data[rows, ], dic = dic, variables = "b", expression = "if_all(where(is.numeric), ~ .x < 5)")))
    expect_equal(nrow(out$queries), 6)
  }
})

test_that("rd_query keeps the argument x of a function of the expression", {
  expected <- nrow(suppressWarnings(suppressMessages(rd_query(covican, variables = "age", expression = "is.na(x)")))$queries)
  for (expression in c("vapply(x, function(x) is.na(x), logical(1))", "sapply(x, \\(x) is.na(x))", "vapply(x, function(v) is.na(v), logical(1))")) {
    out <- suppressWarnings(suppressMessages(rd_query(covican, variables = "age", expression = expression)))
    expect_equal(nrow(out$queries), expected, info = expression)
  }
})

test_that("rd_query applies the branching logic without fields written in REDCap syntax", {
  toy_data <- data.frame(record_id = as.character(1:3), a = c(NA, 1, NA))
  toy_dic <- data.frame(field_name = c("record_id", "a"), form_name = "f1", field_type = "text", field_label = c("ID", "A"),
                        text_validation_type_or_show_slider_number = "", branching_logic_show_field_only_if = "", stringsAsFactors = FALSE)
  run <- function(logic) {
    toy_dic$branching_logic_show_field_only_if[2] <- logic
    suppressMessages(rd_query(data = toy_data, dic = toy_dic, variables = "a", expression = "is.na(x)"))
  }
  expect_equal(nrow(run("1 = 1")$queries), 2)
  expect_equal(nrow(run("sum(1, 1) = 2")$queries), 2)
  expect_equal(nrow(run("1 <> 1")$queries), 0)
  # R logic without fields between brackets is evaluated as before
  expect_equal(nrow(run("record_id != '1'")$queries), 1)
  expect_equal(nrow(run("!is.na(record_id)")$queries), 2)
})

test_that("rd_query applies the R logic written by rd_dictionary() and rd_transform()", {
  toy_data <- data.frame(record_id = LETTERS[1:6], text = c("abc", "xyz", "hello", "abc", "pat", "zz"), pattern = c("b", "y", "ell", "q", "pa", "a"),
                         n = c(1.21, 2.5, 3.33, 1, 2, 3), q = NA, stringsAsFactors = FALSE)
  toy_dic <- data.frame(field_name = c("record_id", "text", "pattern", "n", "q"), form_name = "f1", section_header = "", field_type = "text",
                        field_label = c("ID", "T", "P", "N", "Q"), choices_calculations_or_slider_labels = "", field_note = "",
                        text_validation_type_or_show_slider_number = c("", "", "", "number", ""), branching_logic_show_field_only_if = "", stringsAsFactors = FALSE)
  ids <- function(project) sort(as.character(suppressMessages(rd_query(project, variables = "q", expression = "is.na(x)"))$queries$Identifier))
  for (logic in c("contains([text], [pattern])", "not_contain([text], [pattern])", "find([pattern], [text]) > 0", "roundup([n], 1) > 1.5", "rounddown([n], 1) < 3",
                  "concat_ws([pattern], [text], 'x') = 'abcbx'")) {
    project <- list(data = toy_data, dictionary = toy_dic)
    project$dictionary$branching_logic_show_field_only_if[5] <- logic
    original <- ids(project)
    expect_lt(length(original), 6)
    expect_no_warning(after_dictionary <- ids(suppressMessages(rd_dictionary(project))))
    expect_equal(after_dictionary, original, info = logic)
    expect_equal(ids(suppressWarnings(suppressMessages(rd_transform(project)))), original, info = logic)
  }
})

test_that("rd_query applies the branching logic in R with double brackets", {
  toy_data <- data.frame(record_id = LETTERS[1:6], value = c(1, 2, 0, 3, 4, 5), q = NA, stringsAsFactors = FALSE)
  toy_dic <- data.frame(field_name = c("record_id", "value", "q"), form_name = "f1", field_type = "text", field_label = c("ID", "V", "Q"),
                        text_validation_type_or_show_slider_number = c("", "integer", ""), branching_logic_show_field_only_if = "", stringsAsFactors = FALSE)
  ids <- function(logic) {
    toy_dic$branching_logic_show_field_only_if[3] <- logic
    sort(as.character(suppressMessages(rd_query(data = toy_data, dic = toy_dic, variables = "q", expression = "is.na(x)"))$queries$Identifier))
  }
  expect_no_warning(expect_equal(ids("data[['value']] == 0"), "C"))
  expect_no_warning(expect_length(ids("data[['value']] == 9"), 0))
  expect_equal(ids("data$value == 0"), "C")
})

test_that("rd_query applies the branching logic in R with comments", {
  toy_data <- data.frame(record_id = LETTERS[1:6], value = c(1, 2, 0, 3, 4, 5), q = NA, stringsAsFactors = FALSE)
  toy_dic <- data.frame(field_name = c("record_id", "value", "q"), form_name = "f1", field_type = "text", field_label = c("ID", "V", "Q"),
                        text_validation_type_or_show_slider_number = c("", "integer", ""), branching_logic_show_field_only_if = "", stringsAsFactors = FALSE)
  ids <- function(logic) {
    toy_dic$branching_logic_show_field_only_if[3] <- logic
    sort(as.character(suppressMessages(rd_query(data = toy_data, dic = toy_dic, variables = "q", expression = "is.na(x)"))$queries$Identifier))
  }
  expect_no_warning(expect_equal(ids("data$value == 0 # [note]"), "C"))
  expect_equal(ids("[value] = 0"), "C")
})

test_that("rd_query keeps the texts between quotes of the expression in the messages", {
  toy_data <- data.frame(record_id = c("1", "2", "3"), t = c("x", "AB A B", "a>b"), stringsAsFactors = FALSE)
  toy_dic <- data.frame(field_name = c("record_id", "t"), form_name = "f1", field_type = "text", field_label = c("ID", "T"), stringsAsFactors = FALSE)
  msg <- function(expression) unique(suppressMessages(rd_query(data = toy_data, dic = toy_dic, variables = "t", expression = expression))$queries$Query)
  expect_equal(msg("x == 'x'"), "The value is x and it should not be equal to 'x'")
  expect_equal(msg("x == 'a>b'"), "The value is a>b and it should not be equal to 'a>b'")
  expect_equal(msg("x %in% c('A  B', 'AB A B')"), "The value is AB A B and it should not be equal to c('A  B','AB A B')")
  # Two expressions with different texts give two queries
  both <- suppressMessages(rd_query(data = toy_data, dic = toy_dic, variables = c("t", "t"), expression = c("grepl('A B', x, fixed = TRUE)", "grepl('AB', x, fixed = TRUE)")))
  expect_equal(nrow(both$queries), 2)
  expect_equal(both$queries$Query, c("The value is AB A B and it should not be grepl('A B',,fixed=TRUE)", "The value is AB A B and it should not be grepl('AB',,fixed=TRUE)"))
})

test_that("rd_query keeps the values and the raw strings of the expression in the messages", {
  toy_data <- data.frame(record_id = c("1", "2", "3"), t = c("A  B", " A B ", 'a" x > b & c'), stringsAsFactors = FALSE)
  toy_dic <- data.frame(field_name = c("record_id", "t"), form_name = "f1", field_type = "text", field_label = c("ID", "T"), stringsAsFactors = FALSE)
  run <- function(expression) suppressMessages(rd_query(data = toy_data, dic = toy_dic, variables = "t", expression = expression))
  spaces <- run("x %in% c('A  B', ' A B ')")
  expect_equal(spaces$queries$Query, c("The value is A  B and it should not be equal to c('A  B',' A B ')", "The value is  A B  and it should not be equal to c('A  B',' A B ')"))
  raw <- run('x == r"--(a" x > b & c)--"')
  expect_equal(raw$queries$Query, 'The value is a" x > b & c and it should not be equal to r"--(a" x > b & c)--"')
  # (in the HTML summary, with > and & written as HTML)
  expect_match(paste(raw$results, collapse = ""), 'r"--(a" x &gt; b &amp; c)--"', fixed = TRUE)
})


# ---- Identifier with another name ----

test_that("rd_query filters by an identifier with another name", {
  project <- make_custom_id()

  res <- rd_query(project, variables = "value", expression = "x > 6", event = "visit_arm_1", filter = "subject_id == '003'")
  expect_equal(res$queries$Identifier, "003")
  res <- rd_query(project, variables = "value", expression = "x > 6", event = "visit_arm_1", filter = ".data$subject_id == '003'")
  expect_equal(res$queries$Identifier, "003")

  # An object of the session with the same name doesn't replace the column
  subject_id <- "999"
  res <- rd_query(project, variables = "value", expression = "x > 6", event = "visit_arm_1", filter = "subject_id == '003'")
  expect_equal(res$queries$Identifier, "003")
})

test_that("rd_query checks the identifier itself", {
  df <- data.frame(record_id = c(1, 2, NA), age = c(30, 40, 50))
  toy_dic <- make_toy_dic(list(record_id = list(field_label = "ID", form_name = "demo"), age = list(field_label = "Age", form_name = "demo")))
  res <- rd_query(data = df, dic = toy_dic, variables = "record_id", expression = "x > 1")
  expect_equal(res$queries$Identifier, 2)
  expect_equal(res$queries$Field, "record_id")

  project <- make_custom_id()
  res <- rd_query(project, variables = "subject_id", expression = "x == '002'", event = "base_arm_1")
  expect_equal(res$queries$Identifier, "002")
})

test_that("rd_query takes the identifier from the dictionary, whatever the order of the columns", {
  for (first in c("none", "age", "external")) {
    project <- make_custom_id(age_first = first == "age", external_first = first == "external")
    res <- rd_query(project, variables = "value", expression = "x > 6", event = "visit_arm_1")
    expect_equal(res$queries$Identifier, "003")
    expect_match(res$queries$Code, "^003-")
  }
})

test_that("rd_query keeps the unique event name of each query, also when several events have the same label", {
  data <- data.frame(
    record_id = c("1", "2", "3"),
    redcap_event_name = c("enrollment_arm_1", "enrollment_arm_1", "enrollment_arm_2"),
    age = c(NA, 40, NA)
  )
  data$redcap_event_name.factor <- factor(rep("Enrollment", 3))
  dic <- data.frame(field_name = c("record_id", "age"), form_name = "enrollment", field_type = "text", field_label = c("Record ID", "Age"), branching_logic = "")
  project <- list(data = data, dictionary = dic)
  q <- function(...) suppressWarnings(suppressMessages(rd_query(project, variables = "age", expression = "is.na(x)", ...)))

  res <- q()
  expect_equal(res$queries$Event, c("enrollment_arm_1", "enrollment_arm_2"))
  expect_equal(res$queries$EventLabel, c("Enrollment", "Enrollment"))
  expect_equal(names(res$queries)[3:4], c("Event", "EventLabel"))
  # (the summary doesn't merge them either)
  expect_match(as.character(res$results), "Enrollment (enrollment_arm_1)", fixed = TRUE)
  expect_match(as.character(res$results), "Enrollment (enrollment_arm_2)", fixed = TRUE)

  # add_to keeps them apart
  both <- q(event = "enrollment_arm_2", add_to = q(event = "enrollment_arm_1"))
  expect_equal(both$queries$Event, c("enrollment_arm_1", "enrollment_arm_2"))
  expect_equal(both$queries$Code, c("1-1", "3-1"))

  # A previous report takes the current label of its events
  project$data$redcap_event_name.factor <- factor(c("Enrollment A", "Enrollment A", "Enrollment B"))
  old <- q(event = "enrollment_arm_1")
  project$data$redcap_event_name.factor <- factor(c("Inclusion A", "Inclusion A", "Enrollment B"))
  merged <- q(event = "enrollment_arm_1", add_to = old)
  expect_equal(nrow(merged$queries), 1)
  expect_equal(merged$queries$Event, "enrollment_arm_1")
  expect_equal(merged$queries$EventLabel, "Inclusion A")

  # A previous report with the label in Event (created before EventLabel existed) takes the unique name when a single
  # event of the data has that label
  old$queries$Event <- "Inclusion A"
  old$queries$EventLabel <- NULL
  merged <- q(event = "enrollment_arm_1", add_to = old)
  expect_equal(nrow(merged$queries), 1)
  expect_equal(merged$queries$Event, "enrollment_arm_1")
  expect_equal(merged$queries$EventLabel, "Inclusion A")
})

local({
  d <- data.frame(
    record_id = c("1", "2"),
    redcap_event_name = c("a_arm_1", "b_arm_1"),
    redcap_event_name.factor = c("A", "B"),
    x = NA_real_, y = NA_real_
  )
  dic <- data.frame(
    field_name = c("record_id", "x", "y"),
    form_name = "f1",
    field_type = "text",
    field_label = c("ID", "X", "Y"),
    branching_logic_show_field_only_if = ""
  )
  q <- function(data = d, variables = "x",
                expression = "is.na(x)", ...) {
    suppressWarnings(suppressMessages(rd_query(
      data = data, dic = dic, variables = variables,
      expression = expression, ...
    )))
  }
  link <- list(
    domain = "redcap.example", redcap_version = "14",
    proj_id = 1,
    event_id = c(a_arm_1 = 101, b_arm_1 = 102)
  )
  ids <- function(value) {
    out <- link
    out$event_id <- value
    out
  }
  err <- "REDCapDM_error_argument"

  test_that("event scope rejects mixed missing events", {
    expect_error(q(event = c("a_arm_1", NA_character_)), class = err)

    expect_warning(
      z <- suppressMessages(rd_query(
        data = d, dic = dic, variables = "x",
        expression = "x %in% NA", event = "a_arm_1"
      )),
      "missing observations"
    )
    expect_equal(z$queries$Identifier, 1)

    expect_error(
      q(
        data = d[c("record_id", "x", "y")],
        link = list(domain = "host")
      ),
      "Incomplete", class = err
    )
  })

  test_that("link mappings require complete and exact event identity", {
    for (value in list(list(a_arm_1 = 101), matrix(101), numeric(0))) {
      expect_error(
        q(link = ids(value)), "vector of event IDs", class = err
      )
    }
    for (value in list(0, 1.5, Inf, NA_real_)) {
      expect_error(
        q(link = ids(value)), "positive integer IDs", class = err
      )
    }
    invalid_names <- list(
      c(101, 102),
      setNames(c(101, 102), c("a_arm_1", "")),
      setNames(c(101, 102), c("a_arm_1", "a_arm_1"))
    )
    for (value in invalid_names) {
      expect_error(
        q(link = ids(value)), "unique event names", class = err
      )
    }

    expect_error(
      q(link = ids(c(a_arm_1 = 101, A = 101, b_arm_1 = 102))),
      "same event", class = err
    )
    expect_error(
      q(link = ids(c(a_arm_1 = 101))), "b_arm_1", class = err
    )

    z <- q(event = "a_arm_1", link = ids(101))
    expect_equal(nrow(z$queries), 1L)
    expect_match(z$queries$Link, "&event_id=101&", fixed = TRUE)

    z <- q(link = ids(c(link$event_id, future_arm_1 = 303)))
    expect_equal(z$queries$Identifier, c(1, 2))
    expect_equal(
      sub(".*event_id=([0-9]+)&.*", "\\1", z$queries$Link),
      c("101", "102")
    )
  })

  test_that("event labels resolve without guessing", {
    ambiguous <- d
    ambiguous$redcap_event_name.factor <- "Shared"
    expect_error(
      q(data = ambiguous, event = "Shared"),
      "several events", class = err
    )

    resolve <- get(
      "resolve_query_event_codes",
      envir = environment(rd_query)
    )
    events <- data.frame(code = "a_arm_1", label = "A")
    for (value in list(NA_character_, "")) {
      expect_error(
        resolve(value, events), "missing or empty", class = err
      )
    }

    labels_only <- d[setdiff(names(d), "redcap_event_name")]
    expect_setequal(q(data = labels_only)$queries$Event, c("A", "B"))

    z <- q(data = labels_only, variables_names = "Custom X")
    expect_setequal(z$queries$Event, c("A", "B"))
    expect_true(all(z$queries$Description == "Custom X"))
  })

  test_that("zero summaries preserve requested names and reasons", {
    z <- q(
      variables = c("x", "y"), expression = "x > 0",
      variables_names = c("Custom X", "Custom Y"),
      query_name = c("Positive X", "Positive Y")
    )
    expect_equal(nrow(z$queries), 0L)
    for (text in c("Custom X", "Custom Y", "Positive X", "Positive Y")) {
      expect_match(z$results, text, fixed = TRUE)
    }

    expect_error(
      q(
        variables = c("x", "y"), expression = "x > 0",
        variables_names = c("X", "Y", "Z")
      ),
      "Multiple variable names", class = err
    )
    expect_error(
      q(
        variables = c("x", "y"), expression = "x > 0",
        query_name = c("X", "Y", "Z")
      ),
      "Multiple query names", class = err
    )

    z <- q(negate = TRUE)
    expect_equal(nrow(z$queries), 0L)
    expect_match(z$results, "should be missing", fixed = TRUE)

    system <- d
    system$f1_complete <- 0
    z <- q(data = system, variables = "f1_complete", expression = "x > 0")
    expect_equal(nrow(z$queries), 0L)
    expect_match(z$results, "f1_complete", fixed = TRUE)
  })

  test_that("DAG aliases reserve names and retain every record", {
    g <- data.frame(
      record_id = as.character(1:5),
      x = NA_real_, y = NA_real_,
      redcap_data_access_group =
        c("(Unassigned)", NA, "", "-", "(Empty DAG)")
    )
    z <- q(data = g, by_dag = TRUE)

    expect_equal(sum(vapply(z$queries, nrow, integer(1))), 5L)
    expect_setequal(
      names(z$queries),
      c("(Unassigned)", "(Unassigned)_1",
        "(Empty DAG)", "(Empty DAG)_1", "-")
    )
    expect_setequal(names(z$queries), names(z$results))
    expect_equal(z$queries[["(Unassigned)"]]$Identifier, 1)
    expect_equal(z$queries[["(Empty DAG)"]]$Identifier, 5)
    expect_true(is.na(z$queries[["(Unassigned)_1"]]$DAG))
    expect_identical(z$queries[["(Empty DAG)_1"]]$DAG, "")
  })
})

test_that("rd_query keeps apart in results the variables whose long names begin in the same way", {
  v1 <- "laboratory_measurement_value_first"
  v2 <- "laboratory_measurement_value_second"
  lab <- "Laboratory measurement value obtained at the visit of the study protocol"
  data <- data.frame(record_id = c("1", "2"), redcap_event_name = "e_arm_1", a = c(NA, 1), b = c(1, NA), f = c(1, 1))
  names(data)[3:4] <- c(v1, v2)
  dic <- data.frame(field_name = c("record_id", v1, v2, "f"), form_name = "f", field_type = "text", field_label = c("ID", lab, lab, "F"),
                    choices_calculations_or_slider_labels = "", branching_logic_show_field_only_if = c("", "[f] = '1'", "[f] <> '2'", ""),
                    text_validation_type_or_show_slider_number = "")
  # The cells of the rows of a table of `results`
  table_rows <- function(html) {
    html <- gsub("\n", " ", html)
    rows <- regmatches(html, gregexpr("<tr>.*?</tr>", html))[[1]][-1]
    lapply(rows, function(r) trimws(gsub("<[^>]+>", "", regmatches(r, gregexpr("<td[^>]*>.*?</td>", r))[[1]])))
  }

  for (dag in c(FALSE, TRUE)) {
    d <- data
    if (dag) d$redcap_data_access_group <- "g1"
    res <- suppressWarnings(suppressMessages(rd_query(data = d, dic = dic, variables = c(v1, v2), expression = c("is.na(x)", "is.na(x)"), by_dag = dag)))
    expect_equal(nrow(if (dag) res$queries[[1]] else res$queries), 2)
    rows <- table_rows(as.character(if (dag) res$results[[1]] else res$results))
    # One row for each variable, with its own total and branching logic (the names are only shortened to show them)
    expect_length(rows, 2)
    expect_true(all(vapply(rows, function(r) any(r == "laboratory_measurement_...") && any(r == "1"), logical(1))))
    expect_setequal(vapply(rows, function(r) r[length(r)], character(1)), c("[f] = '1'", "[f] &lt;&gt; '2'"))
  }
})

Try the REDCapDM package in your browser

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

REDCapDM documentation built on Nov. 27, 2026, 9:06 a.m.