tests/testthat/test-CreateUserPartition.R

library(testthat)

test_that("Required parameters are present", {
  expect_error(CreateUserPartition())
  expect_error(CreateUserPartition(validationType = "CV"))
  expect_error(CreateGroupPartition(validationType = "CV", userPartitionCol = 5))
  expect_error(CreateGroupPartition(validationType = "CV", userPartitionCol = list()))
})

test_that("validationType = 'CV' option with holdout", {
  ValidCase <- CreateUserPartition(validationType = "CV",
                                   userPartitionCol = "TVHflag",
                                   cvHoldoutLevel = "H")
  expect_equal(length(ValidCase), 4)
  expect_equal(ValidCase$cvMethod, "user")
  expect_equal(ValidCase$validationType, "CV")
  expect_equal(ValidCase$userPartitionCol, "TVHflag")
  expect_equal(ValidCase$cvHoldoutLevel, "H")
})

test_that("validationType = 'CV' option explicit without holdout", {
  ValidCase <- CreateUserPartition(validationType = "CV",
                                   userPartitionCol = "TVHflag",
                                   cvHoldoutLevel = NA)
  expect_equal(length(ValidCase), 4)
  expect_equal(ValidCase$cvMethod, "user")
  expect_equal(ValidCase$validationType, "CV")
  expect_equal(ValidCase$userPartitionCol, "TVHflag")
  expect_equal(ValidCase$cvHoldoutLevel, NA)
})

test_that("validationType = 'CV' option implicit without holdout", {
  ValidCase <- CreateUserPartition(validationType = "CV",
                                   userPartitionCol = "TVHflag")
  expect_equal(length(ValidCase), 4)
  expect_equal(ValidCase$cvMethod, "user")
  expect_equal(ValidCase$validationType, "CV")
  expect_equal(ValidCase$userPartitionCol, "TVHflag")
  expect_equal(ValidCase$cvHoldoutLevel, NA)
})

test_that("validationType = 'TVH' option", {
  expect_error(CreateUserPartition(validationType = "TVH",
                                   userPartitionCol = "TVHflag"),
               "trainingLevel must be specified")
  expect_error(CreateUserPartition(validationType = "TVH",
                                   userPartitionCol = "TVHflag",
                                   trainingLevel = "T",
                holdoutLevel = "H"), "validationLevel must be specified")
  ValidCase <- CreateUserPartition(validationType = "TVH",
                                   userPartitionCol = "TVHflag",
                                   trainingLevel = "T",
                                   holdoutLevel = "H",
                                   validationLevel = "V")
  expect_equal(length(ValidCase), 6)
  expect_equal(ValidCase$cvMethod, "user")
  expect_equal(ValidCase$validationType, "TVH")
  expect_equal(ValidCase$userPartitionCol, "TVHflag")
  expect_equal(ValidCase$trainingLevel, "T")
  expect_equal(ValidCase$holdoutLevel, "H")
  expect_equal(ValidCase$validationLevel, "V")
  CreateUserPartition(validationType = "TVH",
                      userPartitionCol = "TVHflag",
                      trainingLevel = "T",
                      validationLevel = "V")
})


test_that("validationType = 'CV' option can be used to SetTarget", {
  with_mocked_bindings(
    GetProjectStatus = function(...) { list("stage" = ProjectStage$AIM) },
    {
      userPartition <- CreateUserPartition(validationType = "CV",
                                           userPartitionCol = "TVHflag",
                                           trainingLevel = "T",
                                           holdoutLevel = "H",
                                           validationLevel = "V")
      SetTarget(project = fakeProject,
                target = fakeTarget,
                partition = userPartition)
    },
    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) },
    {
      userPartition <- CreateUserPartition(validationType = "TVH",
                                           userPartitionCol = "TVHflag",
                                           trainingLevel = "T",
                                           holdoutLevel = "H",
                                           validationLevel = "V")
      SetTarget(project = fakeProject,
                target = fakeTarget,
                partition = userPartition)
    },
    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(CreateUserPartition(validationType = "XYZ",
                                   userPartitionCol = "TVHflag"),
               "not valid")
})

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.