tests/testthat/test-CreateStratifiedPartition.R

library(testthat)

test_that("Required parameters are present", {
  expect_error(CreateStratifiedPartition())
  expect_error(CreateStratifiedPartition(validationType = "CV"))
})

test_that("validationType = 'CV' option", {
  expect_error(CreateStratifiedPartition(validationType = "CV",
                                         holdoutPct = 20),
                                         "reps must be specified")
  ValidCase <- CreateStratifiedPartition(validationType = "CV",
                                         holdoutPct = 20, reps = 5)
  expect_equal(length(ValidCase), 4)
  expect_equal(ValidCase$cvMethod, "stratified")
  expect_equal(ValidCase$validationType, "CV")
  expect_equal(ValidCase$holdoutPct, 20)
  expect_equal(ValidCase$reps, 5)
})

test_that("validationType = 'TVH' option", {
  expect_error(CreateStratifiedPartition(validationType = "TVH",
                                         holdoutPct = 20),
                                         "validationPct must be specified")
  ValidCase <- CreateStratifiedPartition(validationType = "TVH",
                                         holdoutPct = 20,
                                         validationPct = 16)
  expect_equal(length(ValidCase), 4)
  expect_equal(ValidCase$cvMethod, "stratified")
  expect_equal(ValidCase$validationType, "TVH")
  expect_equal(ValidCase$holdoutPct, 20)
  expect_equal(ValidCase$validationPct, 16)
})


test_that("validationType = 'CV' option can be used to SetTarget", {
  with_mocked_bindings(
    GetProjectStatus = function(...) { list("stage" = ProjectStage$AIM) },
    {
      stratifiedPartition <- CreateStratifiedPartition(validationType = "CV",
                                                       holdoutPct = 20,
                                                       reps = 5)
      SetTarget(project = fakeProject,
                target = fakeTarget,
                partition = stratifiedPartition)
    },
    DataRobotPATCH = function(...) {
      list(...) # Resolve params to test that they pass without error
    },
    WaitForAsyncReturn = function(...) { "How about not" },
    .package = "datarobot"
  )
})

test_that("validationType = 'TVH' option can be used to SetTarget", {
  with_mocked_bindings(
    GetProjectStatus = function(...) { list("stage" = ProjectStage$AIM) },
    {
      stratifiedPartition <- CreateStratifiedPartition(validationType = "TVH",
                                                       holdoutPct = 20,
                                                       validationPct = 16)
      SetTarget(project = fakeProject,
                target = fakeTarget,
                partition = stratifiedPartition)
    },
    DataRobotPATCH = function(...) {
      list(...) # Resolve params to test that they pass without error
    },
    WaitForAsyncReturn = function(...) { "How about not" },
    .package = "datarobot"
  )
})


test_that("Invalid validationType returns message", {
  expect_error(CreateStratifiedPartition(validationType = "XYZ",
                                         holdoutPct = 20,
                                         validationPct = 16))
})

Try the datarobot package in your browser

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

datarobot documentation built on Nov. 5, 2025, 6:07 p.m.