tests/testthat/test-generate-errors.R

context("generate() errors")

# setup ----
set.seed(1)
eg_df <- data.frame(
  id = as.character(1:100),
  a = rnorm(100),
  b = rnorm(100),
  c = rnorm(100),
  d = factor(sample(c("a", "b", "c"), 100, replace=TRUE)),
  e = sample(1:5, 100, replace=TRUE),
  f = sample(c("zzzz", "zzza", "zzaa", "zaaa", "aaaa", "zzzzz"), 100, replace = TRUE),
  g = sample(c("yyyy", "yyya", "yyaa", "yaaa", "aaaa", "yyyyy"), 100, replace = TRUE)
)

# general errors ----
testthat::test_that("general errors", {
  testthat::expect_error(
    eg_df %>%
      set_options(id_col = "id") %>%
      split_by(a, -5:0 ~ 0:5) %>%
      control_for(b, -2.5:2.5) %>%
      control_for(c, -2.5:2.5) %>%
      control_for(d) %>%
      generate("10"),
    "n must be numeric or a string of value \"all\"",
    fixed = TRUE
  )

  testthat::expect_error(
    eg_df %>%
      set_options(id_col = "id") %>%
      split_by(a, -5:0 ~ 0:5) %>%
      control_for(b, -2.5:2.5) %>%
      control_for(c, -2.5:2.5) %>%
      control_for(d) %>%
      generate(10, match_null = "irreverent"),
    "Unknown match_null; expected \"inclusive\", \"random\", \"balanced\", \"first\", or a specific condition (e.g. \"A2_B1_C1\")",
    fixed = TRUE
  )

  testthat::expect_error(
    eg_df %>%
      set_options(id_col = "id") %>%
      split_by(a, -5:0 ~ 0:5) %>%
      generate(10),
    "No controls defined - see ?LexOPS::generate for example usage of generate()",
    fixed = TRUE
  )

  testthat::expect_error(
    eg_df %>%
      set_options(id_col = "id") %>%
      split_random(2) %>%
      generate(10),
    "No controls defined - see ?LexOPS::generate for example usage of generate()",
    fixed = TRUE
  )

  testthat::expect_error(
    eg_df %>%
      set_options(id_col = "id") %>%
      control_for(b, -2.5:2.5) %>%
      control_for(c, -2.5:2.5) %>%
      control_for(d) %>%
      generate(10),
    "No splits defined - see ?LexOPS::generate for example usage of generate()",
    fixed = TRUE
  )

  testthat::expect_error(
    eg_df %>%
      set_options(id_col = "id") %>%
      control_for_euc(c(b, c), -2.5:2.5) %>%
      generate(10),
    "No splits defined - see ?LexOPS::generate for example usage of generate()",
    fixed = TRUE
  )
})
JackEdTaylor/LexOPS documentation built on July 10, 2024, 6:40 a.m.