tests/testthat/test_kmeans_report.R

# Kmeans plot tests
#
# Testing includes:
#  - graceful failure when handed null data
#  - graceful failure when cluster assignments are missing
#  - checks that a plot file is actually produced and saved

library(kmeansR)
library(ggplot2)
context("report kmeans findings")

test_that("kmeans_plot successfully plots the data and cluster assignments", {

  # test that the correct error is thrown in the absence of data
  expect_error(kmeans_report(clustered_data = NULL),
               "Data object is missing or in the wrong format.")

  # # test that the correct error is thrown in the absense of cluster assignemnts
  # expect_error(kmeans_report(clustered_data = data.frame(), clust_assigns = NULL),
  #              "Cluster assignements are in the wrong format.")

  # test that correct error is thrown when data & cluster assignments are different lengths
  # expect_error(kmeans_report(clustered_data = data.frame(x = c(1,5,1),
  #                                              y = c(2,-5,2),
  #                                              z = c(3,0,3)),
  #                            clust_assigns = data.frame(x = c(1),
  #                                                       y = c(2))),
  #              "Data and cluster assignmenments must be the same length!")

  # test that the output is a list ()
  expect_equal(
    is.list(
    kmeans_report(clustered_data = data.frame(cluster=c(1,2,1),
                                              x = c(1,5,1),
                                              y = c(2,-5,2),
                                              z = c(3,0,3)))),
               TRUE)

  # check that a plot is produced for 2 dimensional data
  expect_equal(
    is.ggplot(
      kmeans_report(clustered_data = data.frame(cluster=c(1,2),
                                                x = c(1,5),
                                                y = c(2,-5)))$plot),
    TRUE)

  # check that a tibble is returned with summary data
  expect_equal(
    is.data.frame(
      kmeans_report(clustered_data = data.frame(cluster=c(1,2),
                                                x = c(1,5),
                                                y = c(2,-5),
                                                z = c(3,0)))$summary),
    TRUE)
})
UBC-MDS/kmeans_R documentation built on May 22, 2019, 2:26 p.m.