tests/testthat/test-nest_cv.R

# WARNING - Generated by {fusen} from dev/flat_cross_validation.Rmd: do not edit by hand # nolint: line_length_linter.

test_that("nest_cv broadcasts metadata and partitions every nested table", {
  nd <- w2l_nest(iris, cols = 1:2, by = "Species")
  res <- nest_cv(nd, v = 5, seed = 1)
  expect_equal(nrow(res), 6L * 5L)
  expect_identical(names(res)[1:3], c("name", "Species", "id"))
  one <- res[name == "Sepal.Length" & Species == "setosa"]
  expect_identical(sort(unlist(one$validate_idx)), 1:50)
  expect_equal(nrow(one$train[[1]]) + nrow(one$validate[[1]]), 50L)
})

test_that("nest_cv is reproducible and supports materialize = FALSE", {
  nd <- w2l_nest(iris, cols = 1, by = "Species")
  a <- nest_cv(nd, v = 3, seed = 7, materialize = FALSE)
  b <- nest_cv(nd, v = 3, seed = 7, materialize = FALSE)
  expect_identical(a, b)
  expect_false(any(c("train", "validate") %in% names(a)))
})

test_that("nest_cv out_type = 'df' gives data.frame subsets", {
  nd <- w2l_nest(iris, cols = 1, by = "Species")
  res <- nest_cv(nd, v = 2, seed = 1, out_type = "df")
  expect_true(all(vapply(c(res$train, res$validate),
                         function(d) identical(class(d), "data.frame"), logical(1L))))
})

test_that("masking with validate_idx leaves the nested data untouched", {
  nd <- w2l_nest(iris, cols = 1:2, by = "Species")
  cv <- nest_cv(nd, v = 5, seed = 1, materialize = FALSE)
  cv[nd, on = c("name", "Species"), full := i.data]
  n_masked <- mapply(function(full, vidx) {
    d <- as.data.frame(full)
    d$value[vidx] <- NA
    sum(is.na(d$value))
  }, cv$full, cv$validate_idx)
  expect_identical(n_masked, lengths(cv$validate_idx))
  expect_false(anyNA(unlist(lapply(nd$data, `[[`, "value"))))
})

test_that("nest_cv validates its input", {
  expect_error(nest_cv(iris), "no nested")
})

Try the mintyr package in your browser

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

mintyr documentation built on Oct. 5, 2026, 5:08 p.m.