Path: blob/master/2019-fall/materials/worksheet_10/tests_worksheet_10.R
2051 views
library(testthat)1library(digest)23test_0.0 <- function(){4test_that('Solution is incorrect', {5expect_equal(digest(answer0.0), '01a75cb73d67b0f895ff0e61449c7bf8')6})7print("Success!")8}9test_0.1 <- function(){10test_that('Solution is incorrect', {11expect_equal(digest(answer0.1), 'd19d62a873f08af0488f0df720cfd293')12})13print("Success!")14}1516test_1.0 <- function(){17test_that('Did not create an object named beer', {18expect_true(exists("beer"))19})20test_that('beer should be a data frame.', {21expect_true('data.frame' %in% class(beer))22})23test_that('beer does not contain the correct number of rows and/or columns.', {24expect_equal(dim(beer), c(2410, 8))25})26test_that('The beer data frame is missing columns.', {27expect_that("X1" %in% colnames(beer), is_true())28expect_that("abv" %in% colnames(beer), is_true())29expect_that("ibu" %in% colnames(beer), is_true())30expect_that("id" %in% colnames(beer), is_true())31expect_that("name" %in% colnames(beer), is_true())32expect_that("style" %in% colnames(beer), is_true())33expect_that("brewery_id" %in% colnames(beer), is_true())34expect_that("ounces" %in% colnames(beer), is_true())35})36print("Success!")37}3839test_1.1 <- function(){40properties <- c(beer_eda$layers[[1]]$mapping, beer_eda$mapping)41labels <- beer_eda$labels42test_that('Did not create a plot named beer_eda', {43expect_true(exists("beer_eda"))44})45test_that('ibu should be on the x-axis.', {46expect_true("ibu" == rlang::get_expr(properties$x))47})48test_that('abv should be on the y-axis.', {49expect_true("abv" == rlang::get_expr(properties$y))50})51test_that('beer_eda should be a scatter plot.', {52expect_that("GeomPoint" %in% c(class(beer_eda$layers[[1]]$geom)) , is_true())53})54test_that('Labels on the axes should be descriptive and human readable.', {55expect_that((labels$y) == 'abv', is_false())56expect_that((labels$x) == 'ibu', is_false())57})58print("Success!")59}6061test_1.2 <- function(){62test_that('Did not create an object named clean_beer', {63expect_true(exists("clean_beer"))64})65test_that('clean_beer should be a data frame.', {66expect_true('data.frame' %in% class(clean_beer))67})68test_that('clean_beer should only contain the columns ibu and abv', {69expect_that("ibu" %in% colnames(clean_beer), is_true())70expect_that("abv" %in% colnames(clean_beer), is_true())71expect_that("id" %in% colnames(clean_beer), is_false())72expect_that("name" %in% colnames(clean_beer), is_false())73expect_that("style" %in% colnames(clean_beer), is_false())74expect_that("brewery_id" %in% colnames(clean_beer), is_false())75expect_that("ounces" %in% colnames(clean_beer), is_false())76})77test_that('clean_beer does not contain the correct number of rows and/or columns.', {78expect_equal(dim(clean_beer), c(1405, 2))79})8081print("Success!")82}8384test_1.3.1 <- function(){85test_that('Solution is incorrect', {86expect_equal(digest(answer1.3.1), '75f1160e72554f4270c809f041c7a776')87})88print("Success!")89}9091test_1.3.2 <- function(){92test_that('Did not create an object named scaled_beer', {93expect_true(exists("scaled_beer"))94})95test_that('scaled_beer should be a data frame.', {96expect_true('data.frame' %in% class(scaled_beer))97})98test_that('scaled_beer does not contain the correct number of rows and/or columns.', {99expect_equal(dim(scaled_beer), c(1405, 2))100})101test_that('scaled_beer should only contain the columns ibu and abv', {102expect_that("ibu" %in% colnames(clean_beer), is_true())103expect_that("abv" %in% colnames(clean_beer), is_true())104expect_that("id" %in% colnames(clean_beer), is_false())105expect_that("name" %in% colnames(clean_beer), is_false())106expect_that("style" %in% colnames(clean_beer), is_false())107expect_that("brewery_id" %in% colnames(clean_beer), is_false())108expect_that("ounces" %in% colnames(clean_beer), is_false())109})110test_that('Columns in scaled_beer are not scaled correctly.', {111expect_that(min(scaled_beer$ibu) < 1, is_true())112expect_that(max(scaled_beer$ibu) < 4, is_true())113expect_that(min(scaled_beer$abv) < -2, is_true())114expect_that(max(scaled_beer$abv) < 5, is_true())115})116print("Success!")117}118119test_1.4 <- function(){120test_that('beer_cluster_k2 class should be kmeans', {121expect_equal(class(beer_cluster_k2), 'kmeans')122})123test_that('beer_cluster_k2 should have 2 centers', {124expect_equal(nrow(beer_cluster_k2$centers), 2)125})126test_that('Solution is incorrect', {127expect_equal(round(beer_cluster_k2$tot.withinss), 1110)128})129print("Success!")130}131132test_1.5 <- function(){133test_that('tidy_beer_cluster_k2 should contain the columns: abv, ibu, and .cluster', {134expect_true('abv' %in% colnames(tidy_beer_cluster_k2))135expect_true('ibu' %in% colnames(tidy_beer_cluster_k2))136expect_true('.cluster' %in% colnames(tidy_beer_cluster_k2))137})138test_that('tidy_beer_cluster_k2 contains an incorrect number of rows and/or columns.', {139expect_equal(nrow(tidy_beer_cluster_k2), 1405)140expect_equal(ncol(tidy_beer_cluster_k2), 3)141})142print("Success!")143}144145test_1.6 <- function(){146properties <- c(tidy_beer_cluster_k2_plot$layers[[1]]$mapping, tidy_beer_cluster_k2_plot$mapping)147labels <- tidy_beer_cluster_k2_plot$labels148test_that('Did not create a plot named tidy_beer_cluster_k2_plot', {149expect_true(exists("tidy_beer_cluster_k2_plot"))150})151test_that('tidy_beer_cluster_k2_plot should contain information from tidy_beer_cluster_k2', {152expect_equal(tidy_beer_cluster_k2_plot$data, tidy_beer_cluster_k2)153})154test_that('ibu should be on the x-axis.', {155expect_true("ibu" == rlang::get_expr(properties$x))156})157test_that('abv should be on the y-axis.', {158expect_true("abv" == rlang::get_expr(properties$y))159})160test_that('.cluster should be used to colour the points.', {161expect_true(".cluster" == rlang::get_expr(properties$colour))162})163test_that('tidy_beer_cluster_k2_plot should be a scatter plot.', {164expect_that("GeomPoint" %in% c(class(tidy_beer_cluster_k2_plot$layers[[1]]$geom)) , is_true())165})166test_that('Labels on the axes should be descriptive and human readable.', {167expect_that((labels$y) == 'abv', is_false())168expect_that((labels$x) == 'ibu', is_false())169expect_that((labels$colour) == '.cluster', is_false())170})171print("Success!")172}173174test_1.7.1 <- function(){175test_that('Solution is incorrect', {176expect_equal(digest(answer1.7.1), '475bf9280aab63a82af60791302736f6')177})178print("Success!")179}180181test_1.7.2 <- function(){182test_that('beer_cluster_k2_model_stats should be a data frame.', {183expect_true('data.frame' %in% class(beer_cluster_k2_model_stats))184})185test_that('beer_cluster_k2_model_stats should have 1 row of 4 different statistics.', {186expect_equal(dim(beer_cluster_k2_model_stats), c(1, 4))187})188test_that('beer_cluster_k2_model_stats should contain total within sum of squares (tot.withinss).', {189expect_true('tot.withinss' %in% colnames(beer_cluster_k2_model_stats))190})191print("Success!")192}193194test_1.8 <- function(){195test_that('beer_clustering should be a data frame.', {196expect_true('data.frame' %in% class(beer_clustering))197})198test_that('beer_clustering should have 1 column containing k values from 1 to 10.', {199expect_equal(nrow(beer_clustering), 10)200expect_equal(ncol(beer_clustering), 1)201expect_equal(colnames(beer_clustering), 'k')202})203print("Success!")204}205206test_1.9 <- function(){207test_that('beer_clustering does not contain the correct number of rows and/or columns.', {208expect_equal(dim(beer_clustering), c(10, 2))209})210test_that('beer_clustering should contain the columns k and models', {211expect_true('k' %in% colnames(beer_clustering))212expect_true('models' %in% colnames(beer_clustering))213})214test_that('The models column in beer_clustering should be of class kmeans', {215expect_equal(class(beer_clustering$models[[1]]), 'kmeans')216})217print("Success!")218}219220test_2.0 <- function(){221test_that('beer_clustering does not contain the correct number of rows and/or columns.', {222expect_equal(dim(beer_clustering), c(10, 3))223})224test_that('beer_clustering should contain the columns k, models, and model_statistics', {225expect_true('k' %in% colnames(beer_clustering))226expect_true('models' %in% colnames(beer_clustering))227expect_true('model_statistics' %in% colnames(beer_clustering))228})229test_that('The models column in beer_clustering should be of class kmeans', {230expect_equal(class(beer_clustering$models[[1]]), 'kmeans')231})232test_that('The model_statistics column in beer_clustering should be a data frame.', {233expect_true('data.frame' %in% class(beer_clustering$model_statistics[[1]]))234})235print("Success!")236}237238test_2.1 <- function(){239test_that('Solution is incorrect', {240expect_equal(nrow(beer_clustering_unnested), 10)241expect_equal(ncol(beer_clustering_unnested), 6)242expect_true('k' %in% colnames(beer_clustering_unnested))243expect_true('models' %in% colnames(beer_clustering_unnested))244expect_false('model_statistics' %in% colnames(beer_clustering_unnested))245expect_equal(class(beer_clustering_unnested$models[[1]]), 'kmeans')246expect_true('tot.withinss' %in% colnames(beer_clustering_unnested))247})248print("Success!")249}250251252test_2.2 <- function(){253properties <- c(choose_beer_k$layers[[1]]$mapping, choose_beer_k$mapping)254labels <- choose_beer_k$labels255test_that('Did not create a plot named choose_beer_k', {256expect_true(exists("choose_beer_k"))257})258test_that('# clusters should be on the x-axis.', {259expect_true("k" == rlang::get_expr(properties$x))260})261test_that('total within-cluster sum-of-squares should be on the y-axis.', {262expect_true("tot.withinss" == rlang::get_expr(properties$y))263})264test_that('choose_beer_k should be a line and scatter plot.', {265expect_that("GeomLine" %in% c(class(choose_beer_k$layers[[1]]$geom),class(choose_beer_k$layers[[2]]$geom)) , is_true())266})267test_that('choose_beer_k should be a line and scatter plot.', {268expect_that("GeomPoint" %in% c(class(choose_beer_k$layers[[1]]$geom),class(choose_beer_k$layers[[2]]$geom)) , is_true())269})270test_that('Labels on the axes should be descriptive and human readable.', {271expect_that((labels$y) == 'tot.withinss', is_false())272expect_that((labels$x) == 'k', is_false())273})274print("Success!")275}276277test_2.3 <- function(){278test_that('Solution is incorrect', {279expect_true(digest(answer2.3) %in% c('0e4033b8c0b56afbea35dc749ced4e1d', 'd19d62a873f08af0488f0df720cfd293'))280})281print("Success!")282}283284285