tests/testthat/test-TableWare-methods.R

context("unit test for TableWare methods")

# convert_col_types ------------------------------------------------------------

test_that("convert_col_types successfully type cast characters to numeric", {
    mtcars_profile <- TableProfiler$new(mtcars)$profile
    mtcars_chr <- apply(mtcars, 2, as.character) %>% as.data.frame(stringsAsFactors = FALSE) %>% magrittr::set_rownames(rownames(mtcars))

    expect_identical(
        mtcars,
        .convert_col_types(table = mtcars_chr, table_profile = mtcars_profile)
    )
})

test_that("convert_col_types successfully type cast factors to numeric", {
    mtcars_profile <- TableProfiler$new(mtcars)$profile
    mtcars_fct <- apply(mtcars, 2, as.factor) %>% as.data.frame(stringsAsFactors = FALSE)

    expect_identical(
        mtcars,
        .convert_col_types(table = mtcars_fct, table_profile = mtcars_profile)
    )
})

test_that("convert_col_types successfully type cast characters to date and time", {
    sys_time <- as.POSIXct("2019-06-24 08:42:31", tz = "UTC")
    timestamps <- data.frame(date = seq.Date(Sys.Date(), Sys.Date() + 7, 1),
                             dttm = seq.POSIXt(sys_time, sys_time + 7, 1),
                             time = rep(lubridate::hms("12:57:50"), 8))
    timestamps_profile <- TableProfiler$new(timestamps)$profile
    timestamps_chr <- apply(timestamps, 2, as.character) %>% as.data.frame(stringsAsFactors = FALSE)

    expect_equal(
        timestamps,
        .convert_col_types(table = timestamps_chr, table_profile = timestamps_profile)
    )
})
tidylab/tableware documentation built on July 6, 2019, 1:12 a.m.