tests/testthat/test_batchMap.R

test_that("batchMap", {
  reg = makeTestRegistry()
  fun = function(...) list(...)
  ids = batchMap(fun, i = 1:3, more.args = list(x = 1), reg = reg)
  expect_data_table(ids, any.missing = FALSE, ncols = 1L, nrow = 3L, key = "job.id")
  expect_equal(ids$job.id, 1:3)
  if (getRversion() < "4.1.0")
    expect_equal(readRDS(fs::path(reg$file.dir, "user.function.rds")), fun)
  expect_equal(readRDS(fs::path(reg$file.dir, "more.args.rds")), list(x = 1))

  checkTables(reg)
  expect_data_table(reg$defs, nrow = 3L, any.missing = FALSE)
  expect_data_table(reg$status, nrow = 3L)
  expect_data_table(reg$resources, nrow = 0L)
  expect_equal(reg$defs$job.pars[[1L]], list(i = 1))

  reg = makeTestRegistry()
  fun = function(...) list(...)
  ids = batchMap(fun, 1:3, more.args = list(j = 1), reg = reg)
  expect_equal(readRDS(fs::path(reg$file.dir, "more.args.rds")), list(j = 1))
  expect_equal(reg$defs$job.pars, lapply(1:3, list))

  reg = makeTestRegistry()
  fun = function(...) list(...)
  ids = batchMap(fun, args = list(1:3), more.args = list(j = 1), reg = reg)
  expect_equal(readRDS(fs::path(reg$file.dir, "more.args.rds")), list(j = 1))
  expect_equal(reg$defs$job.pars, lapply(1:3, list))

  reg = makeTestRegistry()
  fun = function(...) list(...)
  ids = batchMap(fun, i = 1:3, j = 1L, reg = reg)
  expect_identical(reg$defs$job.pars[[3L]], list(i = 3L, j = 1L))

  reg = makeTestRegistry()
  fun = function(...) list(...)
  cj = CJ(a = 1:3, b = letters[1:3])
  ids = batchMap(fun, args = cj, reg = reg)
  expect_data_table(ids, nrow = 9, key = "job.id")
  expect_equivalent(unwrap(getJobPars(reg = reg))[, c("a", "b")], cj)
})

test_that("batchMap with unnamed more.args (#267)", {
  reg = makeTestRegistry()
  fun = function(...) list(...)
  ids = batchMap(fun, 1:3, more.args = list(j = 1L, 5L), reg = reg)
  expect_equal(readRDS(fs::path(reg$file.dir, "more.args.rds")), list(j = 1L, 5L))
  submitAndWait(reg)
  expect_equal(loadResult(1, reg), list(1L, j = 1L, 5L))
})

Try the batchtools package in your browser

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

batchtools documentation built on April 20, 2023, 5:09 p.m.