tests/testthat/test-traintestsplit.R

library(RUtilities)
library(testthat)
library(tibble)


test_that("shuffle_tibble function returns shuffled tibble", {
  set.seed(1234)
  df = tibble(col1 = c(1, 2, 3, 4, 5), col2 = c(6, 7, 8, 9, 10))
  rowdimension = dim(df)[1]
  shuffled_df = df[sample(rowdimension), ]

  expect_equal(shuffle_tibble(tibble = df, nrows = rowdimension), shuffled_df)
})

test_that("generate_traintestindices function yields correct indices", {

  expect_equal(generate_traintestindices(0.33, 12)[[1]], 1:4)
  expect_equal(generate_traintestindices(0.33, 12)[[2]], 5:12)

  })

test_that("generate_traintestlist returns list with two tibbles with the right indices", {
  df = tibble(col1 = c(1, 2, 3, 4, 5, 6), col2 = c(7, 8, 9, 10, 11, 12))
  indices_list = generate_traintestindices(0.33, 6)
  correct_list = list(train_tib = tibble(col1 = c(1, 2), col2 = c(7, 8)),
                      test_tib = tibble(col1 = c(3, 4, 5, 6), col2 = c(9, 10, 11, 12)))
  traintestlist <- generate_traintestlist(df, indices_list)
  colnames(traintestlist$train_tib) <- c("col1", "col2")
  colnames(traintestlist$test_tib) <- c("col1", "col2")

  expect_equal(traintestlist$train_tib, correct_list$train_tib)
  expect_equal(traintestlist$test_tib, correct_list$test_tib)
})
KThompson0308/RUtilities documentation built on Dec. 8, 2019, 8:30 a.m.