Path: blob/master/2022-spring/materials/tutorial_clustering/tests.R
2051 views
library(testthat)1library(digest)23# Round double to precise integer4#5# `int_round` works to create an integer corresponding to a number that is6# tested up to a particular decimal point of precision. This is useful when7# there is a need to compare a numeric value using hashes.8#9# @param x Double vector of length one.10# @param digits Double vector of length one to specify decimal point of precision. Negative numbers can be used to specifying significant digits > 0.1.11#12# @return Integer vector of length one corresponding to a particular decimal point of precision.13#14# @examples15# # to get an integer up to two decimals of precision from 234.5678916# int_round(234.56789, 2)17#18# to get an integer rounded to the hundred digit from 234.5678919# int_round(234.56789, -2)20int_round <- function(x, digits){21x = x*10^digits22xint = as.integer(x)23xint1 = xint + 1L24if (abs(xint - x) < abs(xint1 - x)){25return(xint)26}27else {28return(xint1)29}30}3132test_1.0 <- function(){33test_that('Did not create an object named pm_data', {34expect_true(exists("pm_data"))35})36test_that('pm_data should be a tibble.', {37expect_true('tbl' %in% class(pm_data))38})39test_that('pm_data does not contain the correct number of rows and/or columns.', {40expect_equal(dim(pm_data), c(800, 13))41})42test_that('pm_data is missing columns.', {43expect_true('Name' %in% colnames(pm_data))44expect_true('HP' %in% colnames(pm_data))45expect_true('Attack' %in% colnames(pm_data))46expect_true('Defense' %in% colnames(pm_data))47expect_true('#' %in% colnames(pm_data))48expect_true('Type 1' %in% colnames(pm_data))49expect_true('Type 2' %in% colnames(pm_data))50expect_true('Total' %in% colnames(pm_data))51expect_true('Sp. Atk' %in% colnames(pm_data))52expect_true('Sp. Def' %in% colnames(pm_data))53expect_true('Speed' %in% colnames(pm_data))54expect_true('Generation' %in% colnames(pm_data))55expect_true('Legendary' %in% colnames(pm_data))56})57print("Success!")58}5960test_1.1 <- function(){61test_that('Did not create a plot named pm_pairs', {62expect_true(exists("pm_pairs"))63})64test_that('pm_pairs should be using data from pm_data', {65expect_equal(int_round(nrow(pm_pairs$data), 0), 800)66expect_equal(int_round(ncol(pm_pairs$data), 0), 7)67})68test_that('pm_pairs should be a pairwise plot matrix.', {69expect_true('ggmatrix' %in% c(class(pm_pairs)))70})71test_that('pm_pairs should plot columns 5 to 11', {72expect_equal(pm_pairs$yAxisLabels %in% c("Total", "HP", "Attack", "Defense", "Sp. Atk", "Sp. Def", "Speed"), c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE))73})74print("Success!")75}7677test_1.2 <- function(){78test_that('km_data should contain the columns Speed and Defense', {79expect_true('Speed' %in% colnames(km_data))80expect_true('Defense' %in% colnames(km_data))81})82test_that('km_data should contain 800 rows and 2 columns.', {83expect_equal(int_round(ncol(km_data), 0), 2)84expect_equal(int_round(nrow(km_data), 0), 800)85})86print("Success!")87}8889test_1.3 <- function(){90properties <- c(pm_scatter$layers[[1]]$mapping, pm_scatter$mapping)91labels <- pm_scatter$labels92test_that('Did not create a plot named pm_scatter', {93expect_true(exists("pm_scatter"))94})95test_that('Speed should be on the x-axis.', {96expect_true("Speed" == rlang::get_expr(properties$x))97})98test_that('Defense should be on the y-axis.', {99expect_true("Defense" == rlang::get_expr(properties$y))100})101test_that('pm_scatter should be a scatter plot.', {102expect_true("GeomPoint" %in% c(class(pm_scatter$layers[[1]]$geom)))103})104test_that('Labels on the axes and legend need to be changed to be descriptive, nicely formatted, and human readable.', {105expect_false((labels$y) == 'Defense')106expect_false((labels$x) == 'Speed')107})108print("Success!")109}110111test_1.4.2 <- function(){112test_that('The pokemon_clusters model should have 4 centers.', {113expect_equal(int_round(nrow(pokemon_clusters$centers), 0), 4)114})115test_that('The pokemon_clusters model should be using Speed and Defense to create the clusters.', {116expect_equal(int_round(ncol(pokemon_clusters$centers), 0), 2)117expect_true('Speed' %in% colnames(pokemon_clusters$centers))118expect_true('Defense' %in% colnames(pokemon_clusters$centers))119})120test_that('The pokemon_clusters model should be of class kmeans', {121expect_equal(class(pokemon_clusters), 'kmeans')122})123print("Success!")124}125126test_1.5 <- function(){127properties <- c(answer1.5$layers[[1]]$mapping, answer1.5$mapping)128labels <- answer1.5$labels129test_that('Did not create a plot named answer1.5', {130expect_true(exists("answer1.5"))131})132test_that('Speed should be on the x-axis.', {133expect_true("Speed" == rlang::get_expr(properties$x))134})135test_that('Defense should be on the y-axis.', {136expect_true("Defense" == rlang::get_expr(properties$y))137})138test_that('answer1.5 should be a scatter plot.', {139expect_true("GeomPoint" %in% c(class(answer1.5$layers[[1]]$geom)))140})141test_that('Labels on the axes and legend need to be changed to be descriptive, nicely formatted, and human readable.', {142expect_false((labels$y) == 'Defense')143expect_false((labels$x) == 'Speed')144expect_false((labels$colour) == '.cluster')145})146print("Success!")147}148149test_1.7 <- function(){150test_that('elbow_stats should contain k from 1 to 10', {151expect_equal(int_round(nrow(elbow_stats), 0), 10)152})153test_that('Solution is incorrect', {154expect_equal(digest(int_round(sum(elbow_stats$tot.withinss), 2)), 'ad3673e6aed949387c2f1f4d4b9141dc')155})156test_that('poke_clusts column should be removed', {157expect_false("poke_clusts" %in% colnames(elbow_stats))158})159print("Success!")160}161162test_1.8 <- function(){163properties <- c(elbow_plot$layers[[1]]$mapping, elbow_plot$mapping)164properties2 <- c(elbow_plot$later[[2]]$mapping, elbow_plot$mapping)165test_that('Did not create a plot called elbow_plot', {166expect_true(exists('elbow_plot'))167})168test_that('elbow_plot should be a line plot with points', {169expect_true("GeomPoint" %in% c(class(elbow_plot$layers[[1]]$geom), class(elbow_plot$layers[[2]]$geom)))170expect_true("GeomLine" %in% c(class(elbow_plot$layers[[1]]$geom), class(elbow_plot$layers[[2]]$geom)))171})172test_that('k should be on the x-axis', {173expect_true(rlang::get_expr(properties$x) == 'k')174})175test_that('tot.withinss should be on the y-axis', {176expect_true(rlang::get_expr(properties$y) == 'tot.withinss')177})178test_that('Labels on the axes should be descriptive and human readable.', {179# expect_false((elbow_plot$labels$x) == 'k')180expect_false((elbow_plot$labels$y) == 'tot.withinss')181})182print('Success!')183}184185186