tests/testthat/test-validate.R

################################################################################
# testing validate_graph_input() requires ALL required attributes, not just
# one of them
################################################################################

# Regression test: validate_graph_input() used to check
# any(c("lower", "upper") %in% names(graph_attrs)) - which only fails when
# NEITHER attribute is present, silently passing when just one of them is
# missing. Fixed to use all(), matching validate_graph_input_multi(),
# which already did this correctly. See R/internal_validating_fuctions.R.

make_family_graphs <- function(..., ids = c("p1", "p2")) {
  g <- igraph::make_empty_graph(n = length(ids))
  g <- igraph::set_vertex_attr(g, "name", value = ids)
  for (attr_name in names(list(...))) {
    g <- igraph::set_vertex_attr(g, attr_name, value = list(...)[[attr_name]])
  }
  tibble::tibble(fid = "fam1", fam_graph = list(g))
}

test_that("validate_graph_input errors when upper is missing (lower present)", {
  fg <- make_family_graphs(lower = c(-Inf, 0.5))
  expect_error(
    LTFGRS:::validate_graph_input(family_graphs = fg, fid = "fid",
                                    family_graphs_col = "fam_graph", useMixture = FALSE)
  )
})

test_that("validate_graph_input errors when lower is missing (upper present)", {
  fg <- make_family_graphs(upper = c(0.5, Inf))
  expect_error(
    LTFGRS:::validate_graph_input(family_graphs = fg, fid = "fid",
                                    family_graphs_col = "fam_graph", useMixture = FALSE)
  )
})

test_that("validate_graph_input passes when both lower and upper are present", {
  fg <- make_family_graphs(lower = c(-Inf, 0.5), upper = c(0.5, Inf))
  expect_no_error(
    LTFGRS:::validate_graph_input(family_graphs = fg, fid = "fid",
                                    family_graphs_col = "fam_graph", useMixture = FALSE)
  )
})

test_that("validate_graph_input (useMixture) errors when K_pop is missing", {
  fg <- make_family_graphs(lower = c(-Inf, 0.5), upper = c(0.5, Inf), K_i = c(NA, 0.01))
  expect_error(
    LTFGRS:::validate_graph_input(family_graphs = fg, fid = "fid",
                                    family_graphs_col = "fam_graph", useMixture = TRUE)
  )
})

test_that("validate_graph_input (useMixture) passes when all four attributes are present", {
  fg <- make_family_graphs(lower = c(-Inf, 0.5), upper = c(0.5, Inf),
                           K_i = c(NA, 0.01), K_pop = c(NA, 0.05))
  expect_no_error(
    LTFGRS:::validate_graph_input(family_graphs = fg, fid = "fid",
                                    family_graphs_col = "fam_graph", useMixture = TRUE)
  )
})

################################################################################
# testing validate_graph_input() also checks the sampled graph's VALUES,
# not just which attributes exist
################################################################################

test_that("validate_graph_input errors when lower > upper in the sampled graph", {
  fg <- make_family_graphs(lower = c(Inf, 0.2), upper = c(0.5, 0.8))
  expect_error(
    LTFGRS:::validate_graph_input(family_graphs = fg, fid = "fid",
                                    family_graphs_col = "fam_graph", useMixture = FALSE),
    "lower is greater than upper"
  )
})

test_that("validate_graph_input errors when K_i > K_pop in the sampled graph", {
  fg <- make_family_graphs(lower = c(-Inf, 0.2), upper = c(Inf, 0.8),
                           K_i = c(0.1, 0.9), K_pop = c(0.05, 0.95))
  expect_error(
    LTFGRS:::validate_graph_input(family_graphs = fg, fid = "fid",
                                    family_graphs_col = "fam_graph", useMixture = TRUE),
    "K_i is greater than K_pop"
  )
})

test_that("validate_graph_input_multi errors when lower > upper for a phenotype in the sampled graph", {
  fg <- make_family_graphs(lower_height = c(-Inf, 0.2), upper_height = c(Inf, 0.8),
                           lower_bmi = c(Inf, 0.1), upper_bmi = c(0.5, 0.9))
  expect_error(
    LTFGRS:::validate_graph_input_multi(family_graphs = fg, fid = "fid",
                                          family_graphs_col = "fam_graph",
                                          phen_names = c("height", "bmi"), useMixture = FALSE),
    "bmi"
  )
})

test_that("validate_graph_input_multi errors when K_i > K_pop for a phenotype in the sampled graph", {
  fg <- make_family_graphs(lower_height = c(-Inf, 0.2), upper_height = c(Inf, 0.8),
                           K_i_height = c(0.2, 0.05), K_pop_height = c(0.1, 0.9))
  expect_error(
    LTFGRS:::validate_graph_input_multi(family_graphs = fg, fid = "fid",
                                          family_graphs_col = "fam_graph",
                                          phen_names = "height", useMixture = TRUE),
    "K_i is greater than K_pop"
  )
})

test_that("validate_graph_input_multi passes when values are consistent across phenotypes", {
  fg <- make_family_graphs(lower_height = c(-Inf, 0.2), upper_height = c(Inf, 0.8),
                           lower_bmi = c(-Inf, 0.1), upper_bmi = c(Inf, 0.9))
  expect_no_error(
    LTFGRS:::validate_graph_input_multi(family_graphs = fg, fid = "fid",
                                          family_graphs_col = "fam_graph",
                                          phen_names = c("height", "bmi"), useMixture = FALSE)
  )
})

################################################################################
# testing describe_bad_rows()
################################################################################

test_that("describe_bad_rows returns NULL when nothing is wrong", {
  expect_null(LTFGRS:::describe_bad_rows(integer(0), letters[1:5]))
})

test_that("describe_bad_rows lists all ids when at or below the cap", {
  detail <- LTFGRS:::describe_bad_rows(c(1, 3), c("A", "B", "C"))
  expect_equal(detail, "2 individual(s), including: A, C")
})

test_that("describe_bad_rows truncates and appends a count when above the cap", {
  ids <- paste0("id", 1:15)
  detail <- LTFGRS:::describe_bad_rows(1:15, ids, max_show = 10)
  expect_match(detail, "^15 individual\\(s\\), including: id1, id2")
  expect_match(detail, ", and 5 more$")
})

################################################################################
# testing validate_tbl_input() errors (rather than silently swaps/warns)
################################################################################

# Regression tests: validate_tbl_input() used to silently "fix" lower >
# upper via an add/subtract swap trick that produces NaN whenever either
# bound is Inf/-Inf, and only warn() (not stop()) when K_i > K_pop. Both are
# now reported as errors, without attempting to guess the user's intent.
# See R/internal_validating_fuctions.R.

test_that("validate_tbl_input errors when lower > upper, including with Inf bounds", {
  tbl <- tibble::tibble(
    fid = "fam1", pid = c("p1", "p2"), role = c("g", "o"),
    lower = c(Inf, 0.2), upper = c(5, 0.8) # p1: lower = Inf > upper = 5
  )
  expect_error(
    LTFGRS:::validate_tbl_input(.tbl = tbl, pid = "pid", fid = "fid", role = "role", useMixture = FALSE),
    "lower is greater than upper"
  )
})

test_that("validate_tbl_input passes when lower <= upper for all rows", {
  tbl <- tibble::tibble(
    fid = "fam1", pid = c("p1", "p2"), role = c("g", "o"),
    lower = c(-Inf, 0.2), upper = c(Inf, 0.8)
  )
  expect_no_error(
    LTFGRS:::validate_tbl_input(.tbl = tbl, pid = "pid", fid = "fid", role = "role", useMixture = FALSE)
  )
})

test_that("validate_tbl_input errors when K_i > K_pop", {
  tbl <- tibble::tibble(
    fid = "fam1", pid = c("p1", "p2"), role = c("g", "o"),
    lower = c(-Inf, 0.2), upper = c(Inf, 0.8),
    K_i = c(0.1, 0.9), K_pop = c(0.05, 0.95) # p1: K_i = 0.1 > K_pop = 0.05
  )
  expect_error(
    LTFGRS:::validate_tbl_input(.tbl = tbl, pid = "pid", fid = "fid", role = "role", useMixture = TRUE),
    "K_i is greater than K_pop"
  )
})

################################################################################
# testing validate_tbl_input_multi() errors (rather than silently swaps)
################################################################################

test_that("validate_tbl_input_multi reports every affected phenotype in one error", {
  tbl <- tibble::tibble(
    fid = "fam1", pid = c("p1", "p2", "p3"), role = c("g", "o", "m"),
    lower_height = c(Inf, 0.2, 0.3), upper_height = c(5, 0.8, 0.9),    # p1 bad
    lower_bmi    = c(-Inf, 0.1, Inf), upper_bmi    = c(Inf, 0.9, 0.2)  # p3 bad
  )
  err <- tryCatch(
    LTFGRS:::validate_tbl_input_multi(.tbl = tbl, pid = "pid", fid = "fid", role = "role",
                                        useMixture = FALSE, phen_names = c("height", "bmi")),
    error = function(e) e
  )
  expect_s3_class(err, "error")
  expect_match(conditionMessage(err), "height")
  expect_match(conditionMessage(err), "bmi")
})

test_that("validate_tbl_input_multi truncates the id list past the cap", {
  n <- 15
  tbl <- tibble::tibble(
    fid = "fam1", pid = paste0("p", 1:n), role = rep("g", n),
    lower_height = rep(Inf, n), upper_height = rep(5, n)
  )
  err <- tryCatch(
    LTFGRS:::validate_tbl_input_multi(.tbl = tbl, pid = "pid", fid = "fid", role = "role",
                                        useMixture = FALSE, phen_names = "height"),
    error = function(e) e
  )
  expect_match(conditionMessage(err), "15 individual")
  expect_match(conditionMessage(err), ", and 5 more")
})

test_that("validate_tbl_input_multi passes when lower <= upper for all phenotypes", {
  tbl <- tibble::tibble(
    fid = "fam1", pid = c("p1", "p2"), role = c("g", "o"),
    lower_height = c(-Inf, 0.2), upper_height = c(Inf, 0.8)
  )
  expect_no_error(
    LTFGRS:::validate_tbl_input_multi(.tbl = tbl, pid = "pid", fid = "fid", role = "role",
                                        useMixture = FALSE, phen_names = "height")
  )
})

test_that("validate_tbl_input_multi errors when K_i > K_pop for a phenotype", {
  tbl <- tibble::tibble(
    fid = "fam1", pid = c("p1", "p2"), role = c("g", "o"),
    lower_height = c(-Inf, 0.2), upper_height = c(Inf, 0.8),
    K_i_height = c(0.2, 0.05), K_pop_height = c(0.1, 0.9) # p1 bad
  )
  expect_error(
    LTFGRS:::validate_tbl_input_multi(.tbl = tbl, pid = "pid", fid = "fid", role = "role",
                                        useMixture = TRUE, phen_names = "height"),
    "K_i is greater than K_pop"
  )
})

################################################################################

Try the LTFGRS package in your browser

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

LTFGRS documentation built on Nov. 20, 2026, 5:10 p.m.