library(testthat) library(AutoEDA) library(ggplot2) #========================================================== # auto_plot() #========================================================== test_that("auto_plot returns histogram for numeric variable", { p <- auto_plot( iris, "Sepal.Length" ) expect_s3_class( p, "ggplot" ) }) test_that("auto_plot returns bar plot for factor variable", { p <- auto_plot( iris, "Species" ) expect_s3_class( p, "ggplot" ) }) test_that("auto_plot returns bar plot for character variable", { dat <- data.frame( Name = c("A","B","A","C"), stringsAsFactors = FALSE ) p <- auto_plot( dat, "Name" ) expect_s3_class( p, "ggplot" ) }) test_that("auto_plot works with tibble", { skip_if_not_installed("tibble") dat <- tibble::as_tibble(iris) p <- auto_plot( dat, "Sepal.Length" ) expect_s3_class( p, "ggplot" ) }) test_that("auto_plot works with missing numeric values", { dat <- iris dat$Sepal.Length[1:10] <- NA p <- auto_plot( dat, "Sepal.Length" ) expect_s3_class( p, "ggplot" ) }) test_that("auto_plot works with missing factor values", { dat <- iris dat$Species[1:5] <- NA p <- auto_plot( dat, "Species" ) expect_s3_class( p, "ggplot" ) }) #========================================================== # Invalid variable #========================================================== test_that("auto_plot rejects invalid variable", { expect_error( auto_plot( iris, "ABC" ) ) }) test_that("auto_plot rejects NULL variable", { expect_error( auto_plot( iris, NULL ) ) }) test_that("auto_plot rejects numeric variable name", { expect_error( auto_plot( iris, 1 ) ) }) #========================================================== # Invalid data #========================================================== test_that("auto_plot rejects non-data.frame", { expect_error( auto_plot( 1:10, "x" ) ) }) test_that("auto_plot rejects NULL data", { expect_error( auto_plot( NULL, "x" ) ) }) test_that("auto_plot rejects character vector", { expect_error( auto_plot( letters, "x" ) ) }) test_that("auto_plot rejects empty data.frame", { expect_error( auto_plot( data.frame(), "x" ) ) }) #========================================================== # Unsupported variable types #========================================================== test_that("auto_plot rejects Date variable", { dat <- data.frame( Date = as.Date("2025-01-01") + 0:4 ) expect_error( auto_plot( dat, "Date" ), "Unsupported variable type" ) }) test_that("auto_plot rejects POSIXct variable", { dat <- data.frame( Time = as.POSIXct( "2025-01-01" ) + 1:5 ) expect_error( auto_plot( dat, "Time" ), "Unsupported variable type" ) }) test_that("auto_plot rejects list column", { dat <- data.frame( id = 1:3 ) dat$listcol <- I(list( 1:2, 3:4, 5:6 )) expect_error( auto_plot( dat, "listcol" ), "Unsupported variable type" ) })