tests/testthat/test-utils-summarize_int_cat_data.R

test_that("summarize_int_cat_data validates data input", {
  # The function actually accepts strings and tries to process them,
  # so it will fail deeper in the call stack
  expect_error(
    summarize_int_cat_data("not a data frame"),
    "must be a vector"
  )
})

test_that("summarize_int_cat_data validates column existence", {
  data <- data.frame(
    x = 1:5,
    y = letters[1:5],
    z = factor(c("A", "B", "A", "B", "A"))
  )

  # Invalid dep columns
  expect_error(
    summarize_int_cat_data(data, dep = "nonexistent"),
    "doesn't exist"
  )

  # Invalid indep columns
  expect_error(
    summarize_int_cat_data(data, dep = "x", indep = "nonexistent"),
    "doesn't exist"
  )
})

test_that("summarize_int_cat_data prevents dep/indep overlap", {
  data <- data.frame(
    x = 1:5,
    y = letters[1:5],
    z = factor(c("A", "B", "A", "B", "A"))
  )

  # The function has a bug - it references invalid_deps before defining it
  # Let's test the error that actually occurs
  expect_error(
    summarize_int_cat_data(data, dep = "x", indep = "x"),
    "among indep columns|invalid_deps"
  )
})

test_that("summarize_int_cat_data works with single indep variable", {
  data <- data.frame(
    x = 1:5,
    y = c(10, 20, 30, 40, 50),
    z = factor(c("A", "B", "A", "B", "A"))
  )

  result <- summarize_int_cat_data(data, dep = "x", indep = "z")

  expect_s3_class(result, "data.frame")
  expect_true(nrow(result) > 0)
})

test_that("summarize_int_cat_data works with no indep variable", {
  data <- data.frame(
    x = 1:5,
    y = c(10, 20, 30, 40, 50)
  )

  result <- summarize_int_cat_data(data, dep = "x", indep = NULL)

  expect_s3_class(result, "data.frame")
  expect_true(nrow(result) > 0)
})

test_that("summarize_int_cat_data works with multiple indep variables", {
  # The function has a bug with multiple indep - it passes the vector to .id parameter
  data <- data.frame(
    x = 1:8,
    y = c(10, 20, 30, 40, 50, 60, 70, 80),
    z = factor(c("A", "B", "A", "B", "A", "B", "A", "B")),
    w = factor(c("X", "X", "Y", "Y", "X", "X", "Y", "Y"))
  )

  # This reveals a bug in the function - .id must be a single string
  expect_error(
    summarize_int_cat_data(data, dep = "x", indep = c("z", "w")),
    ".id.*must be a single string"
  )
})

test_that("summarize_int_cat_data handles multiple dep variables", {
  data <- data.frame(
    x = 1:5,
    y = c(10, 20, 30, 40, 50),
    z = factor(c("A", "B", "A", "B", "A"))
  )

  result <- summarize_int_cat_data(data, dep = c("x", "y"), indep = "z")

  expect_s3_class(result, "data.frame")
  expect_true(nrow(result) > 0)
})

test_that("summarize_int_cat_data handles survey objects", {
  skip_if_not_installed("survey")

  # Survey objects don't work with dplyr::summarize without srvyr
  # This test reveals the incompatibility
  data <- data.frame(
    x = 1:5,
    y = c(10, 20, 30, 40, 50),
    z = factor(c("A", "B", "A", "B", "A")),
    weights = c(1, 1, 1, 1, 1)
  )

  svy_data <- survey::svydesign(ids = ~1, weights = ~weights, data = data)

  expect_error(
    summarize_int_cat_data(svy_data, dep = "x", indep = "z"),
    "no applicable method for 'summarise'"
  )
})

test_that("summarize_int_cat_data handles edge cases", {
  # Empty data frame - this will actually cause an error in data processing
  data <- data.frame(x = numeric(0), y = factor())

  expect_warning(
    expect_error(
      summarize_int_cat_data(data, dep = "x", indep = "y"),
      "replacement has 1 row, data has 0"
    ),
    "no non-missing arguments to min"
  )

  # Single row
  data <- data.frame(
    x = 1,
    z = factor("A")
  )

  expect_no_error(
    result <- summarize_int_cat_data(data, dep = "x", indep = "z")
  )
})

Try the saros package in your browser

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

saros documentation built on Nov. 10, 2025, 5:06 p.m.