library(testthat) library(AutoEDA) library(ggplot2) #========================================================== # cluster_analysis() #========================================================== test_that("cluster_analysis returns ClusterResult", { cl <- cluster_analysis(iris) expect_s3_class(cl, "ClusterResult") }) test_that("cluster_analysis accepts valid data.frame", { expect_no_error( cluster_analysis(iris) ) }) test_that("cluster_analysis returns requested number of clusters", { cl <- cluster_analysis( iris, centers = 3 ) expect_equal( length(unique(cl$cluster)), 3 ) }) test_that("cluster_analysis stores original data", { cl <- cluster_analysis(iris) expect_true(!is.null(cl$data)) }) #========================================================== # cluster_plot() #========================================================== test_that("cluster_plot returns ggplot", { cl <- cluster_analysis(iris) p <- cluster_plot(cl) expect_s3_class( p, "ggplot" ) }) #========================================================== # cluster_dendrogram() #========================================================== test_that("cluster_dendrogram returns hclust invisibly", { hc <- cluster_dendrogram(iris) expect_s3_class( hc, "hclust" ) }) #========================================================== # cluster_elbow() #========================================================== test_that("cluster_elbow returns ggplot", { p <- cluster_elbow(iris) expect_s3_class( p, "ggplot" ) }) #========================================================== # cluster_silhouette() #========================================================== test_that("cluster_silhouette returns ggplot", { p <- cluster_silhouette(iris) expect_s3_class( p, "ggplot" ) }) #========================================================== # Invalid cluster_analysis() #========================================================== test_that("cluster_analysis rejects non-data.frame", { expect_error( cluster_analysis(1:10) ) }) test_that("cluster_analysis rejects empty data.frame", { expect_error( cluster_analysis(data.frame()) ) }) test_that("cluster_analysis rejects one numeric variable", { expect_error( cluster_analysis( data.frame(x = 1:10) ) ) }) test_that("cluster_analysis rejects invalid centers", { expect_error( cluster_analysis( iris, centers = 1 ) ) }) test_that("cluster_analysis rejects invalid nstart", { expect_error( cluster_analysis( iris, nstart = 0 ) ) }) #========================================================== # Invalid cluster_plot() #========================================================== test_that("cluster_plot rejects invalid object", { expect_error( cluster_plot(iris) ) }) #========================================================== # Invalid cluster_dendrogram() #========================================================== test_that("cluster_dendrogram rejects invalid input", { expect_error( cluster_dendrogram(1:10) ) }) #========================================================== # Invalid cluster_elbow() #========================================================== test_that("cluster_elbow rejects invalid input", { expect_error( cluster_elbow(1:10) ) }) #========================================================== # Invalid cluster_silhouette() #========================================================== test_that("cluster_silhouette rejects invalid input", { expect_error( cluster_silhouette(1:10) ) }) #========================================================== # Additional validation tests #========================================================== test_that("cluster_dendrogram rejects empty data.frame", { expect_error( cluster_dendrogram(data.frame()) ) }) test_that("cluster_elbow rejects empty data.frame", { expect_error( cluster_elbow(data.frame()) ) }) test_that("cluster_silhouette rejects empty data.frame", { expect_error( cluster_silhouette(data.frame()) ) }) test_that("cluster_analysis rejects non-numeric data", { expect_error( cluster_analysis( data.frame( A = letters[1:10], B = LETTERS[1:10] ) ) ) }) test_that("cluster_dendrogram rejects non-numeric data", { expect_error( cluster_dendrogram( data.frame( A = letters[1:10], B = LETTERS[1:10] ) ) ) }) test_that("cluster_elbow rejects non-numeric data", { expect_error( cluster_elbow( data.frame( A = letters[1:10], B = LETTERS[1:10] ) ) ) }) test_that("cluster_silhouette rejects non-numeric data", { expect_error( cluster_silhouette( data.frame( A = letters[1:10], B = LETTERS[1:10] ) ) ) }) test_that("cluster_analysis rejects invalid scale argument", { expect_error( cluster_analysis( iris, scale = "yes" ) ) }) test_that("cluster_analysis rejects invalid nstart type", { expect_error( cluster_analysis( iris, nstart = "25" ) ) }) test_that("cluster_analysis rejects invalid centers type", { expect_error( cluster_analysis( iris, centers = "3" ) ) }) test_that("cluster_elbow returns ggplot", { p <- cluster_elbow(iris) expect_s3_class(p, "ggplot") expect_false(is.null(p)) }) test_that("cluster_silhouette returns ggplot", { p <- cluster_silhouette(iris) expect_s3_class(p, "ggplot") expect_false(is.null(p)) }) test_that("cluster_plot returns ggplot", { cl <- cluster_analysis(iris) p <- cluster_plot(cl) expect_s3_class(p, "ggplot") expect_false(is.null(p)) }) test_that("cluster_dendrogram returns hclust", { hc <- cluster_dendrogram(iris) expect_s3_class(hc, "hclust") expect_false(is.null(hc)) })