Path: blob/master/2020-fall/materials/worksheet_10/tests_worksheet_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_0.0 <- function(){18test_that('Solution is incorrect', {19expect_equal(digest(answer0.0), '01a75cb73d67b0f895ff0e61449c7bf8')20})21print("Success!")22}23test_0.1 <- function(){24test_that('Solution is incorrect', {25expect_equal(digest(answer0.1), 'd19d62a873f08af0488f0df720cfd293')26})27print("Success!")28}2930test_1.0 <- function(){31test_that('Did not create an object named beer', {32expect_true(exists("beer"))33})34test_that('beer should be a tibble.', {35expect_true('tbl' %in% class(beer))36})37test_that('beer does not contain the correct number of rows and/or columns.', {38expect_equal(dim(beer), c(2410, 8))39})40test_that('The beer tibble is missing columns.', {41expect_true("abv" %in% colnames(beer))42expect_true("ibu" %in% colnames(beer))43expect_true("id" %in% colnames(beer))44expect_true("name" %in% colnames(beer))45expect_true("style" %in% colnames(beer))46expect_true("brewery_id" %in% colnames(beer))47expect_true("ounces" %in% colnames(beer))48})49print("Success!")50}5152test_1.1 <- function(){53properties <- c(beer_eda$layers[[1]]$mapping, beer_eda$mapping)54labels <- beer_eda$labels55test_that('Did not create a plot named beer_eda', {56expect_true(exists("beer_eda"))57})58test_that('ibu should be on the x-axis.', {59expect_true("ibu" == rlang::get_expr(properties$x))60})61test_that('abv should be on the y-axis.', {62expect_true("abv" == rlang::get_expr(properties$y))63})64test_that('beer_eda should be a scatter plot.', {65expect_true("GeomPoint" %in% c(class(beer_eda$layers[[1]]$geom)))66})67test_that('Labels on the axes should be descriptive and human readable.', {68expect_false((labels$y) == 'abv')69expect_false((labels$x) == 'ibu')70})71print("Success!")72}7374test_1.2 <- function(){75test_that('Did not create an object named clean_beer', {76expect_true(exists("clean_beer"))77})78test_that('clean_beer should be a tibble.', {79expect_true('tbl' %in% class(clean_beer))80})81test_that('clean_beer should only contain the columns ibu and abv', {82expect_true("ibu" %in% colnames(clean_beer))83expect_true("abv" %in% colnames(clean_beer))84expect_false("id" %in% colnames(clean_beer))85expect_false("name" %in% colnames(clean_beer))86expect_false("style" %in% colnames(clean_beer))87expect_false("brewery_id" %in% colnames(clean_beer))88expect_false("ounces" %in% colnames(clean_beer))89})90test_that('clean_beer does not contain the correct number of rows and/or columns.', {91expect_equal(dim(clean_beer), c(1405, 2))92})9394print("Success!")95}9697test_1.3.1 <- function(){98test_that('Solution is incorrect', {99expect_equal(digest(answer1.3.1), '75f1160e72554f4270c809f041c7a776')100})101print("Success!")102}103104test_1.3.2 <- function(){105test_that('Did not create an object named scaled_beer', {106expect_true(exists("scaled_beer"))107})108test_that('scaled_beer should be a tibble.', {109expect_true('tbl' %in% class(scaled_beer))110})111test_that('scaled_beer does not contain the correct number of rows and/or columns.', {112expect_equal(dim(scaled_beer), c(1405, 2))113})114test_that('scaled_beer should only contain the columns ibu and abv', {115expect_true("ibu" %in% colnames(clean_beer))116expect_true("abv" %in% colnames(clean_beer))117expect_false("id" %in% colnames(clean_beer))118expect_false("name" %in% colnames(clean_beer))119expect_false("style" %in% colnames(clean_beer))120expect_false("brewery_id" %in% colnames(clean_beer))121expect_false("ounces" %in% colnames(clean_beer))122})123test_that('Columns in scaled_beer are not scaled correctly.', {124expect_true(min(scaled_beer$ibu) < 1)125expect_true(max(scaled_beer$ibu) < 4)126expect_true(min(scaled_beer$abv) < -2)127expect_true(max(scaled_beer$abv) < 5)128})129print("Success!")130}131132test_1.4 <- function(){133test_that('beer_cluster_k2 class should be kmeans', {134expect_equal(class(beer_cluster_k2), 'kmeans')135})136test_that('beer_cluster_k2 should have 2 centers', {137expect_equal(int_round(nrow(beer_cluster_k2$centers), 0), 2)138})139test_that('Solution is incorrect', {140expect_equal(int_round(beer_cluster_k2$tot.withinss, 0), 1110)141})142print("Success!")143}144145test_1.5 <- function(){146test_that('tidy_beer_cluster_k2 should contain the columns: abv, ibu, and .cluster', {147expect_true('abv' %in% colnames(tidy_beer_cluster_k2))148expect_true('ibu' %in% colnames(tidy_beer_cluster_k2))149expect_true('.cluster' %in% colnames(tidy_beer_cluster_k2))150})151test_that('tidy_beer_cluster_k2 contains an incorrect number of rows and/or columns.', {152expect_equal(int_round(nrow(tidy_beer_cluster_k2), 0), 1405)153expect_equal(int_round(ncol(tidy_beer_cluster_k2), 0), 3)154})155print("Success!")156}157158test_1.6 <- function(){159properties <- c(tidy_beer_cluster_k2_plot$layers[[1]]$mapping, tidy_beer_cluster_k2_plot$mapping)160labels <- tidy_beer_cluster_k2_plot$labels161test_that('Did not create a plot named tidy_beer_cluster_k2_plot', {162expect_true(exists("tidy_beer_cluster_k2_plot"))163})164test_that('tidy_beer_cluster_k2_plot should contain information from tidy_beer_cluster_k2', {165expect_equal(tidy_beer_cluster_k2_plot$data, tidy_beer_cluster_k2)166})167test_that('ibu should be on the x-axis.', {168expect_true("ibu" == rlang::get_expr(properties$x))169})170test_that('abv should be on the y-axis.', {171expect_true("abv" == rlang::get_expr(properties$y))172})173test_that('.cluster should be used to colour the points.', {174expect_true(".cluster" == rlang::get_expr(properties$colour))175})176test_that('tidy_beer_cluster_k2_plot should be a scatter plot.', {177expect_true("GeomPoint" %in% c(class(tidy_beer_cluster_k2_plot$layers[[1]]$geom)))178})179test_that('Labels on the axes should be descriptive and human readable.', {180expect_false((labels$y) == 'abv')181expect_false((labels$x) == 'ibu')182expect_false((labels$colour) == '.cluster')183})184print("Success!")185}186187test_1.7.1 <- function(){188test_that('Solution is incorrect', {189expect_equal(digest(answer1.7.1), '475bf9280aab63a82af60791302736f6')190})191print("Success!")192}193194test_1.7.2 <- function(){195test_that('beer_cluster_k2_model_stats should be a tibble.', {196expect_true('tbl' %in% class(beer_cluster_k2_model_stats))197})198test_that('beer_cluster_k2_model_stats should have 1 row of 4 different statistics.', {199expect_equal(dim(beer_cluster_k2_model_stats), c(1, 4))200})201test_that('beer_cluster_k2_model_stats should contain total within sum of squares (tot.withinss).', {202expect_true('tot.withinss' %in% colnames(beer_cluster_k2_model_stats))203})204print("Success!")205}206207test_1.8 <- function(){208test_that('beer_ks should be a tbl.', {209expect_true('tbl' %in% class(beer_ks))210})211test_that('beer_ks should have 1 column containing k values from 1 to 10.', {212expect_equal(int_round(nrow(beer_ks), 0), 10)213expect_equal(int_round(ncol(beer_ks), 0), 1)214expect_equal(colnames(beer_ks), 'k')215})216print("Success!")217}218219test_1.9 <- function(){220test_that('beer_clustering does not contain the correct number of rows and/or columns.', {221expect_equal(dim(beer_clustering), c(10, 2))222})223test_that('beer_clustering should contain the columns k and models', {224expect_true('k' %in% colnames(beer_clustering))225expect_true('models' %in% colnames(beer_clustering))226})227test_that('The models column in beer_clustering should be of class kmeans', {228expect_equal(class(beer_clustering$models[[1]]), 'kmeans')229})230print("Success!")231}232233test_2.0 <- function(){234test_that('beer_model_stats does not contain the correct number of rows and/or columns.', {235expect_equal(dim(beer_model_stats), c(10, 3))236})237test_that('beer_model_stats should contain the columns k, models, and model_statistics', {238expect_true('k' %in% colnames(beer_model_stats))239expect_true('models' %in% colnames(beer_model_stats))240expect_true('model_statistics' %in% colnames(beer_model_stats))241})242test_that('The models column in beer_model_stats should be of class kmeans', {243expect_equal(class(beer_model_stats$models[[1]]), 'kmeans')244})245test_that('The model_statistics column in beer_model_stats should be a tibble.', {246expect_true('tbl' %in% class(beer_model_stats$model_statistics[[1]]))247})248print("Success!")249}250251test_2.1 <- function(){252test_that('Solution is incorrect', {253expect_equal(int_round(nrow(beer_clustering_unnested), 0), 10)254expect_equal(int_round(ncol(beer_clustering_unnested), 0), 6)255expect_true('k' %in% colnames(beer_clustering_unnested))256expect_true('models' %in% colnames(beer_clustering_unnested))257expect_false('model_statistics' %in% colnames(beer_clustering_unnested))258expect_equal(class(beer_clustering_unnested$models[[1]]), 'kmeans')259expect_true('tot.withinss' %in% colnames(beer_clustering_unnested))260})261print("Success!")262}263264265test_2.2 <- function(){266properties <- c(choose_beer_k$layers[[1]]$mapping, choose_beer_k$mapping)267labels <- choose_beer_k$labels268test_that('Did not create a plot named choose_beer_k', {269expect_true(exists("choose_beer_k"))270})271test_that('# clusters should be on the x-axis.', {272expect_true("k" == rlang::get_expr(properties$x))273})274test_that('total within-cluster sum-of-squares should be on the y-axis.', {275expect_true("tot.withinss" == rlang::get_expr(properties$y))276})277test_that('choose_beer_k should be a line and scatter plot.', {278expect_true("GeomLine" %in% c(class(choose_beer_k$layers[[1]]$geom),class(choose_beer_k$layers[[2]]$geom)))279})280test_that('choose_beer_k should be a line and scatter plot.', {281expect_true("GeomPoint" %in% c(class(choose_beer_k$layers[[1]]$geom),class(choose_beer_k$layers[[2]]$geom)))282})283test_that('Labels on the axes should be descriptive and human readable.', {284expect_false((labels$y) == 'tot.withinss')285expect_false((labels$x) == 'k')286})287print("Success!")288}289290test_2.3 <- function(){291test_that('Solution is incorrect', {292expect_true(digest(answer2.3) %in% c('0e4033b8c0b56afbea35dc749ced4e1d', 'd19d62a873f08af0488f0df720cfd293'))293})294print("Success!")295}296297298