tests/testthat/test-workflows.R

test_that("integrates with workflows::add_variables()", {
  skip_if_not_installed("workflows")

  kmeans_spec <- k_means(num_clusters = 2)

  wf_spec <- workflows::workflow() %>%
    workflows::add_variables(outcomes = NULL, predictors = everything()) %>%
    workflows::add_model(kmeans_spec)

  expect_no_error(
    fit(wf_spec, data = mtcars)
  )

  wf_spec <- workflows::workflow() %>%
    workflows::add_variables(outcomes = mpg, predictors = everything()) %>%
    workflows::add_model(kmeans_spec)

  expect_snapshot(
    error = TRUE,
    fit(wf_spec, data = mtcars)
  )
})

test_that("integrates with workflows::add_formula()", {
  skip_if_not_installed("workflows")

  kmeans_spec <- k_means(num_clusters = 2)

  wf_spec <- workflows::workflow() %>%
    workflows::add_formula(~ .) %>%
    workflows::add_model(kmeans_spec)

  expect_no_error(
    fit(wf_spec, data = mtcars)
  )

  wf_spec <- workflows::workflow() %>%
    workflows::add_formula(mpg ~ .) %>%
    workflows::add_model(kmeans_spec)

  expect_snapshot(
    error = TRUE,
    fit(wf_spec, data = mtcars)
  )
})

test_that("integrates with workflows::add_recipe()", {
  skip_if_not_installed("workflows")
  skip_if_not_installed("recipes")

  kmeans_spec <- k_means(num_clusters = 2)

  wf_spec <- workflows::workflow() %>%
    workflows::add_recipe(recipes::recipe(~ ., data = mtcars)) %>%
    workflows::add_model(kmeans_spec)

  expect_no_error(
    fit(wf_spec, data = mtcars)
  )

  wf_spec <- workflows::workflow() %>%
    workflows::add_recipe(recipes::recipe(mpg ~ ., data = mtcars)) %>%
    workflows::add_model(kmeans_spec)

  expect_snapshot(
    error = TRUE,
    fit(wf_spec, data = mtcars)
  )
})

Try the tidyclust package in your browser

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

tidyclust documentation built on Sept. 26, 2023, 1:08 a.m.