tests/testthat/test-TableProfiler.R

context("unit test for TableProfiler")

# TableProfiler ----------------------------------------------------------------
test_that("TableProfiler generates a profile for mtcars", {
    # No input argument
    expect_error(TableProfiler$new())

    # Valid input argument
    expect_silent(TableProfiler$new(table = mtcars))
    expect_class(TableProfiler$new(table = mtcars)$profile, "list")
})

# Profiler ---------------------------------------------------------------------
test_that("generate_table_profile fails when given invalid input arguments", {
    # No input arguments
    expect_error(generate_table_profile())
    # Not a data frame
    expect_error(generate_table_profile(.data = as.list(mtcars)))
    # Data frame with no rows
    expect_error(generate_table_profile(.data = data.frame(C1 = NULL)))
})

test_that("generate_table_profile generates the table profile correctly", {
    mtcars_profile <- generate_table_profile(.data = mtcars)
    expect_class(mtcars_profile, "list")
    expect_identical(mtcars_profile$mpg$type, "numeric")
    expect_identical(mtcars_profile$mpg$na, FALSE)
})

# Find Functions ---------------------------------------------------------------
test_that("find_col_types returns the correct col classes", {
    # Numeric vars
    expected_output <- rep("numeric", ncol(mtcars))
    names(expected_output) <- names(mtcars)
    expect_identical(find_col_types(.data = mtcars), expected_output)

    # Dates vars
    date <- data.frame(Dates = seq.Date(Sys.Date(), Sys.Date()+7, 1))
    expect_equal(find_col_types(.data = date), "date", check.attributes = FALSE)

    # Date-Time vars
    timestamp <- data.frame(Timestamp = seq.POSIXt(Sys.time(), Sys.time()+7, 1))
    expect_equal(find_col_types(.data = timestamp), "dttm", check.attributes = FALSE)

    # Time vars
    time <- data.frame(Times = lubridate::hms("12:57:50"))
    expect_equal(find_col_types(.data = time), "time", check.attributes = FALSE)
})

test_that("mark_cols_with_na marks cols that do/don't have NAs values", {
    # Without NAs
    expected_output <- rep(FALSE, ncol(mtcars))
    names(expected_output) <- names(mtcars)
    expect_identical(mark_cols_with_na(.data = mtcars), expected_output)

    # With NAs
    set.seed(1623)
    expected_output <- sample(c(TRUE, FALSE), ncol(mtcars), replace = TRUE)
    names(expected_output) <- names(mtcars)
    mtcars_na <- mtcars
    mtcars_na[1, expected_output] <- NA
    expect_identical(mark_cols_with_na(.data = mtcars_na), expected_output)
})
tidylab/tidylab.dqa documentation built on June 21, 2019, 7 p.m.