tests/testthat/test_plotting_numeric_buckets.R

testthat::context("Testing plotting_numerical_buckets")


testthat::test_that("check valid input of df: data frame - should error", {
  testthat::expect_error(plotting_numerical_buckets(rep("a",10)))
  testthat::expect_error(plotting_numerical_buckets(NULL))
  testthat::expect_error(plotting_numerical_buckets(NA))
})


testthat::test_that("check valid input of n_bins - should error", {
  testthat::expect_error(plotting_numerical_buckets(1:100, n_bins=-1))
  testthat::expect_error(plotting_numerical_buckets(1:100, n_bins=1))
  testthat::expect_error(plotting_numerical_buckets(1:100, n_bins=10.5))
  testthat::expect_error(plotting_numerical_buckets(1:100, n_bins="10"))

})


testthat::test_that("check valid input of weight - should error", {
  testthat::expect_error(plotting_numerical_buckets(1:100, n_bins=10, weight=NA))
  testthat::expect_error(plotting_numerical_buckets(1:100, n_bins=10, weight=1:99))

})


testthat::test_that("check valid input of df: data frame - should error", {
  testthat::expect_error(plotting_numerical_buckets(1:100, n_bins=10, include_outliers="TRUE"))
  testthat::expect_error(plotting_numerical_buckets(1:100, n_bins=10, include_outliers=c(TRUE,TRUE)))

})


testthat::test_that("Check output - No outliers", {

  out <- plotting_numerical_buckets(0:100, n_bins=10)

  testthat::expect_equal(out$lower, seq(0,90,10))
  testthat::expect_equal(out$upper, seq(10,100,10))
  testthat::expect_equal(out$center, seq(5,95,10))
  testthat::expect_equal(out$width, rep(10,10))


})


testthat::test_that("Check output - With outliers", {

  out_1 <- plotting_numerical_buckets(0:100, n_bins=10)
  out_2 <- plotting_numerical_buckets(c(0:100, 99999), n_bins=10, include_outliers=FALSE)
  out_3 <- plotting_numerical_buckets(c(0:100, 99999), n_bins=10, include_outliers=TRUE)

  testthat::expect_equal(out_2, out_1)
  testthat::expect_equal(out_3$labels[1:10], out_1$labels)
  testthat::expect_equal(out_3[11,]$center, 99999)
  testthat::expect_equal(out_3[11,]$width, 0)



})


testthat::test_that("Check output - With categorical", {

  out <- plotting_numerical_buckets(sample(seq(0,90,10), 100, replace=TRUE), n_bins=10)

  testthat::expect_equal(out$lower, seq(0,90,10) - 0.5 )
  testthat::expect_equal(out$upper, seq(0,90,10) + 0.5)
  testthat::expect_equal(out$center, seq(0,90,10))
  testthat::expect_equal(out$width, rep(1,10))



})


testthat::test_that("Check output - With categorical - tight", {

  out <- plotting_numerical_buckets(sample(seq(0,.90,.10), 100, replace=TRUE), n_bins=10)

  testthat::expect_equal(out$lower, seq(0,.90,.10) - 0.05 )
  testthat::expect_equal(out$upper, seq(0,.90,.10) + 0.05)
  testthat::expect_equal(out$center, seq(0,.90,.10))
  testthat::expect_equal(out$width, rep(.1,10))



})
gloverd2/admr documentation built on Dec. 2, 2020, 11:16 p.m.