Path: blob/master/2020-spring/materials/tutorial_10/tests_tutorial_10.R
2051 views
library(testthat)1library(digest)23test_1.0 <- function(){4test_that('Did not create an object named pm_data', {5expect_true(exists("pm_data"))6})7test_that('pm_data should be a data frame.', {8expect_true('data.frame' %in% class(pm_data))9})10test_that('pm_data does not contain the correct number of rows and/or columns.', {11expect_equal(dim(pm_data), c(800, 13))12})13test_that('pm_data is missing columns.', {14expect_true('Name' %in% colnames(pm_data))15expect_true('HP' %in% colnames(pm_data))16expect_true('Attack' %in% colnames(pm_data))17expect_true('Defense' %in% colnames(pm_data))18expect_true('#' %in% colnames(pm_data))19expect_true('Type 1' %in% colnames(pm_data))20expect_true('Type 2' %in% colnames(pm_data))21expect_true('Total' %in% colnames(pm_data))22expect_true('Sp. Atk' %in% colnames(pm_data))23expect_true('Sp. Def' %in% colnames(pm_data))24expect_true('Speed' %in% colnames(pm_data))25expect_true('Generation' %in% colnames(pm_data))26expect_true('Legendary' %in% colnames(pm_data))27})28print("Success!")29}3031test_1.1 <- function(){32test_that('Did not create a plot named pm_pairs', {33expect_true(exists("pm_pairs"))34})35test_that('pm_pairs should be using data from pm_data', {36expect_equal(nrow(pm_pairs$data), 800)37expect_equal(ncol(pm_pairs$data), 7)38})39test_that('pm_pairs should be a pairwise plot matrix.', {40expect_true('ggmatrix' %in% c(class(pm_pairs)))41})42test_that('pm_pairs should plot columns 5 to 11', {43expect_equal(pm_pairs$yAxisLabels %in% c("Total", "HP", "Attack", "Defense", "Sp. Atk", "Sp. Def", "Speed"), c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE))44})45print("Success!")46}4748test_1.2 <- function(){49properties <- c(pm_scatter$layers[[1]]$mapping, pm_scatter$mapping)50labels <- pm_scatter$labels51test_that('Did not create a plot named pm_scatter', {52expect_true(exists("pm_scatter"))53})54test_that('Speed should be on the x-axis.', {55expect_true("Speed" == rlang::get_expr(properties$x))56})57test_that('Defense should be on the y-axis.', {58expect_true("Defense" == rlang::get_expr(properties$y))59})60test_that('pm_scatter should be a scatter plot.', {61expect_that("GeomPoint" %in% c(class(pm_scatter$layers[[1]]$geom)) , is_true())62})63test_that('Labels on the axes and legend need to be changed to be descriptive, nicely formatted, and human readable.', {64expect_that((labels$y) == 'Defense', is_false())65expect_that((labels$x) == 'Speed', is_false())66})67print("Success!")68}69707172test_1.3 <- function(){73test_that('km_data should contain the columns Speed and Defense', {74expect_true('Speed' %in% colnames(km_data))75expect_true('Defense' %in% colnames(km_data))76})77test_that('km_data should contain 800 rows and 2 columns.', {78expect_equal(ncol(km_data), 2)79expect_equal(nrow(km_data), 800)80})81print("Success!")82}8384test_1.4.2 <- function(){85test_that('The pokemon_clusters model should have 4 centers.', {86expect_equal(nrow(pokemon_clusters$centers), 4)87})88test_that('The pokemon_clusters model should be using Speed and Defense to create the clusters.', {89expect_equal(ncol(pokemon_clusters$centers), 2)90expect_true('Speed' %in% colnames(pokemon_clusters$centers))91expect_true('Defense' %in% colnames(pokemon_clusters$centers))92})93test_that('The pokemon_clusters model should be of class kmeans', {94expect_equal(class(pokemon_clusters), 'kmeans')95})96print("Success!")97}9899test_1.5 <- function(){100properties <- c(answer1.5$layers[[1]]$mapping, answer1.5$mapping)101labels <- answer1.5$labels102test_that('Did not create a plot named answer1.5', {103expect_true(exists("answer1.5"))104})105test_that('Speed should be on the x-axis.', {106expect_true("Speed" == rlang::get_expr(properties$x))107})108test_that('Defense should be on the y-axis.', {109expect_true("Defense" == rlang::get_expr(properties$y))110})111test_that('answer1.5 should be a scatter plot.', {112expect_that("GeomPoint" %in% c(class(answer1.5$layers[[1]]$geom)) , is_true())113})114test_that('Labels on the axes and legend need to be changed to be descriptive, nicely formatted, and human readable.', {115expect_that((labels$y) == 'Defense', is_false())116expect_that((labels$x) == 'Speed', is_false())117expect_that((labels$colour) == '.cluster', is_false())118})119print("Success!")120}121122test_1.7 <- function(){123test_that('elbow_stats should contain k from 1 to 10', {124expect_equal(nrow(elbow_stats), 10)125})126test_that('Solution is incorrect', {127expect_equal(sum(c('k', 'tot.withinss') %in% colnames(elbow_stats)), 2)128})129print("Success!")130}131132