Path: blob/master/2020-fall/materials/tutorial_10/tests_tutorial_10.R
2051 views
# +1library(testthat)2library(digest)34int_round <- function(x, digits){5x = x*10^digits6xint = as.integer(x)7xint1 = xint + 1L8if (abs(xint - x) < abs(xint1 - x)){9return(xint)10}11else {12return(xint1)13}14}15# -1617test_1.0 <- function(){18test_that('Did not create an object named pm_data', {19expect_true(exists("pm_data"))20})21test_that('pm_data should be a tibble.', {22expect_true('tbl' %in% class(pm_data))23})24test_that('pm_data does not contain the correct number of rows and/or columns.', {25expect_equal(dim(pm_data), c(800, 13))26})27test_that('pm_data is missing columns.', {28expect_true('Name' %in% colnames(pm_data))29expect_true('HP' %in% colnames(pm_data))30expect_true('Attack' %in% colnames(pm_data))31expect_true('Defense' %in% colnames(pm_data))32expect_true('#' %in% colnames(pm_data))33expect_true('Type 1' %in% colnames(pm_data))34expect_true('Type 2' %in% colnames(pm_data))35expect_true('Total' %in% colnames(pm_data))36expect_true('Sp. Atk' %in% colnames(pm_data))37expect_true('Sp. Def' %in% colnames(pm_data))38expect_true('Speed' %in% colnames(pm_data))39expect_true('Generation' %in% colnames(pm_data))40expect_true('Legendary' %in% colnames(pm_data))41})42print("Success!")43}4445test_1.1 <- function(){46test_that('Did not create a plot named pm_pairs', {47expect_true(exists("pm_pairs"))48})49test_that('pm_pairs should be using data from pm_data', {50expect_equal(int_round(nrow(pm_pairs$data), 0), 800)51expect_equal(int_round(ncol(pm_pairs$data), 0), 7)52})53test_that('pm_pairs should be a pairwise plot matrix.', {54expect_true('ggmatrix' %in% c(class(pm_pairs)))55})56test_that('pm_pairs should plot columns 5 to 11', {57expect_equal(pm_pairs$yAxisLabels %in% c("Total", "HP", "Attack", "Defense", "Sp. Atk", "Sp. Def", "Speed"), c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE))58})59print("Success!")60}6162test_1.2 <- function(){63test_that('km_data should contain the columns Speed and Defense', {64expect_true('Speed' %in% colnames(km_data))65expect_true('Defense' %in% colnames(km_data))66})67test_that('km_data should contain 800 rows and 2 columns.', {68expect_equal(int_round(ncol(km_data), 0), 2)69expect_equal(int_round(nrow(km_data), 0), 800)70})71print("Success!")72}7374test_1.3 <- function(){75properties <- c(pm_scatter$layers[[1]]$mapping, pm_scatter$mapping)76labels <- pm_scatter$labels77test_that('Did not create a plot named pm_scatter', {78expect_true(exists("pm_scatter"))79})80test_that('Speed should be on the x-axis.', {81expect_true("Speed" == rlang::get_expr(properties$x))82})83test_that('Defense should be on the y-axis.', {84expect_true("Defense" == rlang::get_expr(properties$y))85})86test_that('pm_scatter should be a scatter plot.', {87expect_true("GeomPoint" %in% c(class(pm_scatter$layers[[1]]$geom)))88})89test_that('Labels on the axes and legend need to be changed to be descriptive, nicely formatted, and human readable.', {90expect_false((labels$y) == 'Defense')91expect_false((labels$x) == 'Speed')92})93print("Success!")94}9596test_1.4.2 <- function(){97test_that('The pokemon_clusters model should have 4 centers.', {98expect_equal(int_round(nrow(pokemon_clusters$centers), 0), 4)99})100test_that('The pokemon_clusters model should be using Speed and Defense to create the clusters.', {101expect_equal(int_round(ncol(pokemon_clusters$centers), 0), 2)102expect_true('Speed' %in% colnames(pokemon_clusters$centers))103expect_true('Defense' %in% colnames(pokemon_clusters$centers))104})105test_that('The pokemon_clusters model should be of class kmeans', {106expect_equal(class(pokemon_clusters), 'kmeans')107})108print("Success!")109}110111test_1.5 <- function(){112properties <- c(answer1.5$layers[[1]]$mapping, answer1.5$mapping)113labels <- answer1.5$labels114test_that('Did not create a plot named answer1.5', {115expect_true(exists("answer1.5"))116})117test_that('Speed should be on the x-axis.', {118expect_true("Speed" == rlang::get_expr(properties$x))119})120test_that('Defense should be on the y-axis.', {121expect_true("Defense" == rlang::get_expr(properties$y))122})123test_that('answer1.5 should be a scatter plot.', {124expect_true("GeomPoint" %in% c(class(answer1.5$layers[[1]]$geom)))125})126test_that('Labels on the axes and legend need to be changed to be descriptive, nicely formatted, and human readable.', {127expect_false((labels$y) == 'Defense')128expect_false((labels$x) == 'Speed')129expect_false((labels$colour) == '.cluster')130})131print("Success!")132}133134test_1.7 <- function(){135test_that('elbow_stats should contain k from 1 to 10', {136expect_equal(int_round(nrow(elbow_stats), 0), 10)137})138test_that('Solution is incorrect', {139expect_equal(digest(int_round(sum(elbow_stats$tot.withinss), 2)), '13f1e8e7d2dac6cdff06a32e20382f3b')140})141test_that('poke_clusts column should be removed', {142expect_false("poke_clusts" %in% colnames(elbow_stats))143})144print("Success!")145}146147test_1.8 <- function(){148properties <- c(elbow_plot$layers[[1]]$mapping, elbow_plot$mapping)149properties2 <- c(elbow_plot$later[[2]]$mapping, elbow_plot$mapping)150test_that('Did not create a plot called elbow_plot', {151expect_true(exists('elbow_plot'))152})153test_that('elbow_plot should be a line plot', {154expect_true("GeomPoint" %in% c(class(elbow_plot$layers[[1]]$geom)))155expect_true("GeomLine" %in% c(class(elbow_plot$layers[[2]]$geom)))156})157test_that('k should be on the x-axis', {158expect_true(rlang::get_expr(properties$x) == 'k')159})160test_that('tot.withinss should be on the y-axis', {161expect_true(rlang::get_expr(properties$y) == 'tot.withinss')162})163test_that('Labels on the axes should be descriptive and human readable.', {164# expect_false((elbow_plot$labels$x) == 'k')165expect_false((elbow_plot$labels$y) == 'tot.withinss')166})167print('Success!')168}169170171