tests/testthat/test-corr_map.R

library(palmerpenguins)

test_that("Input df should have the type as a data frame", {
  wrong_type_input <- 9
  expect_error(corr_map(wrong_type_input, c("column")),
               "Expecting a data frame for df parameter in corr_map")
})

test_that("Columns should have the type as a character vector", {
  expect_error(corr_map(penguins, penguins),
               "Expecting a character vector for columns parameter in corr_map")
  expect_error(corr_map(penguins, as.numeric(1)),
               "Expecting a character vector for columns parameter in corr_map")
})

test_that("Given columns should exist in the data frame df", {
  expect_error(corr_map(penguins, c("wrong_col_name")),
               "Column: wrong_col_name does not exist in the given df")
})

test_that("Given columns does not include any numeric column", {
  expect_error(corr_map(penguins, c("sex")),
               "None of the columns is numeric")
})

test_that("corr_map() returns plot with one single numeric column", {
  corr_plot <- corr_map(penguins, c("body_mass_g"))
  expect_true("GeomTile" %in% c(class(corr_plot$layers[[1]]$geom)))
  expect_equal(corr_plot$data$rowname, "body_mass_g")
  expect_equal(corr_plot$data$value, 1)
})

test_that("corr_map() returns map with multiple columns", {
  corr_plot <- corr_map(penguins,
                        c("body_mass_g", "sex", "bill_length_mm"))
  expect_true("GeomTile" %in% c(class(corr_plot$layers[[1]]$geom)))
  expect_true(!("sex" %in% corr_plot$data$rowname))
  expect_true("body_mass_g" %in% corr_plot$data$rowname)
  expect_true("bill_length_mm" %in% corr_plot$data$rowname)
})
UBC-MDS/slimreda documentation built on Feb. 7, 2022, 9:12 a.m.