Path: blob/master/2021-spring/materials/worksheet_08/tests_worksheet_08.R
2707 views
# +1library(testthat)2library(digest)3library(rlang)45int_round <- function(x, digits){6x = x*10^digits7xint = as.integer(x)8xint1 = xint + 1L9if (abs(xint - x) < abs(xint1 - x)){10return(xint)11}12else {13return(xint1)14}15}16# -1718# Round double to precise integer19#20# `int_round` works to create an integer corresponding to a number that is21# tested up to a particular decimal point of precision. This is useful when22# there is a need to compare a numeric value using hashes.23#24# @param x Double vector of length one.25# @param digits Double vector of length one to specify decimal point of precision. Negative numbers can be used to specifying significant digits > 0.1.26#27# @return Integer vector of length one corresponding to a particular decimal point of precision.28#29# @examples30# # to get an integer up to two decimals of precision from 234.5678931# int_round(234.56789, 2)32#33# to get an integer rounded to the hundred digit from 234.5678934# int_round(234.56789, -2)3536test_0.0 <- function(){37test_that('Solution is incorrect', {38expect_equal(digest(answer0.0), '3a5505c06543876fe45598b5e5e5195d')39})40print("Success!")41}4243test_0.1 <- function(){44test_that('Solution is incorrect', {45expect_equal(digest(answer0.1), '475bf9280aab63a82af60791302736f6')46})47print("Success!")48}4950test_0.2 <- function(){51test_that('Solution is incorrect', {52expect_equal(digest(int_round(answer0.2, 2)), '6953b334169bd7ec7da1c1eda5aaf6a5')53})54print("Success!")55}5657test_0.3 <- function(){58test_that('Solution is incorrect', {59expect_equal(digest(answer0.3), '75f1160e72554f4270c809f041c7a776')60})61print("Success!")62}6364test_1.0 <- function(){65test_that('Did not create an object named marathon', {66expect_true(exists("marathon"))67})68test_that('marathon should be a tibble.', {69expect_true('tbl' %in% class(marathon))70})71test_that('marathon does not contain the correct number of rows and/or columns.', {72expect_equal(dim(marathon), c(929, 13))73})74test_that('The marathon tibble is missing columns.', {75expect_true("time_hrs" %in% colnames(marathon))76expect_true("max" %in% colnames(marathon))77})78print("Success!")79}8081test_2.0 <- function(){82properties <- c(answer2$mapping, answer2$layers[[1]]$mapping)83labels <- answer2$labels84test_that('Did not create a plot named answer2', {85expect_true(exists("answer2"))86})87test_that('marathon_50 does not contain the correct number of rows and/or columns.', {88expect_equal(dim(marathon_50), c(50, 13))89})90test_that('answer2 should use information from marathon_50', {91expect_equal(answer2$data, marathon_50)92})93test_that('max should be on the x-axis.', {94expect_true("max" %in% c(rlang::get_expr(properties$x),95rlang::get_expr(properties$x)))96})97test_that('time_hrs should be on the y-axis.', {98expect_true("time_hrs" %in% c(rlang::get_expr(properties$y),99rlang::get_expr(properties$y)))100})101test_that('answer2 should be a scatter plot.', {102expect_equal(digest(class(rlang::get_expr(answer2$layers[[1]]$geom))[1]),103'911e5b9debfb523f25ad2ccc01a4b2dd')104})105test_that('Labels on the axes should be descriptive and human readable.', {106expect_false((labels$y) == 'time_hrs')107expect_false((labels$x) == 'max')108})109print("Success!")110}111112test_3.0 <- function(){113test_that('Did not create an object called answer3', {114expect_true(exists('answer3'))115})116test_that('answer3 is incorrect', {117expect_equal(digest(int_round(answer3, 1)), 'a266aa4a0aa711355be22e0f3b8d91af')118})119print("Success!")120}121122test_4.0 <- function(){123test_that('Did not create an object called answer4', {124expect_true(exists('answer4'))125})126test_that('answer4 is incorrect', {127expect_equal(digest(int_round(answer4, 1)), '285d156b1b700fbb489df058fdb9e2ee')128})129print("Success!")130}131132test_5.0 <- function(){133test_that('Did not create an object called answer5', {134expect_true(exists('answer5'))135})136test_that('Solution is incorrect', {137expect_equal(digest(answer5), '475bf9280aab63a82af60791302736f6')138})139print("Success!")140}141142test_6.0 <- function(){143test_that('Did not create an object named marathon_split', {144expect_true(exists("marathon_split"))145})146test_that('marathon_split should be rsplit (not a tibble)', {147expect_true('rsplit' %in% class(marathon_split))148})149test_that('Did not create an object named marathon_training', {150expect_true(exists('marathon_training'))151})152test_that('marathon_training does not contain 0.75 of the data.', {153expect_equal(dim(marathon_training), c(698,13))154expect_equal(digest(int_round(sum(marathon_training$age), 0)), '5109988e81575a4e65652fddb747a18f')155})156test_that('Did not create an object named marathon_testing', {157expect_true(exists('marathon_testing'))158})159test_that('marathon_testing does not contain 0.25 of the data.', {160expect_equal(dim(marathon_testing), c(231, 13))161expect_equal(digest(int_round(sum(marathon_testing$age), 0)), '9ffe9b883d53974eaa9afb9bf0ec386b')162})163print("Success!")164}165166test_7.0 <- function(){167test_that('Did not create an object named marathon_spec', {168expect_true(exists("marathon_spec"))169})170test_that('neighbors argument is incorrect', {171expect_equal(digest(as.character(get_expr(marathon_spec$args$neighbors))), '4b89cff22bb78b28a0a6b7fe28d371f6')172})173test_that('weight_func is incorrect', {174expect_equal(digest(as.character(get_expr(marathon_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')175})176test_that('set_engine is incorrect', {177expect_equal(digest(as.character(marathon_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')178})179test_that('mode is incorrect', {180expect_equal(digest(as.character(marathon_spec$mode)), 'b8bdd7015e0d1c6037512fd1396aef1a')181})182test_that('Did not create an object named marathon_recipe', {183expect_true(exists("marathon_recipe"))184})185test_that('Data in marathon_recipe is not scaled and centered', {186expect_equal(digest(int_round(sum(marathon_recipe$template$max), 0)), '94e91e5e6573ddfedb81802729c39543')187expect_equal(digest(int_round(sum(marathon_recipe$template$time_hrs), 0)), '2213c3a0eb86305be22e0ca3b0a773c1')188})189print("Success!")190}191192test_7.1 <- function(){193test_that('Did not create an object called marathon_vfold', {194expect_true(exists("marathon_vfold"))195})196test_that('marathon_vfold does not contain 5 folds', {197expect_equal(int_round(length(marathon_vfold$id), 0), 5)198})199test_that('marathon_vfold should be a cross-validation object', {200expect_true('vfold_cv' %in% class(marathon_vfold))201})202test_that('Did not create an object called marathon_workflow', {203expect_true(exists("marathon_workflow"))204})205test_that('marathon_workflow is not a workflow object', {206expect_true('workflow' %in% class(marathon_workflow))207})208test_that('marathon_workflow does not contain the correct model specification', {209expect_equal(digest(as.character(marathon_workflow$fit$actions$model$spec$args$neighbors)), 'b68c9f555cfd94fe903b741afcace6c1')210expect_equal(digest(as.character(marathon_workflow$fit$actions$model$spec$mode)), 'b8bdd7015e0d1c6037512fd1396aef1a')211expect_equal(digest(as.character(marathon_workflow$fit$actions$model$spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')212expect_true('nearest_neighbor' %in% class(marathon_workflow$fit$actions$model$spec))213})214test_that('marathon_workflow does not contain the correct recipe', {215expect_true('recipe' %in% class(marathon_workflow$pre$actions$recipe$recipe))216expect_equal(digest(int_round(sum(marathon_workflow$pre$actions$recipe$recipe$template$max), 0)), '94e91e5e6573ddfedb81802729c39543')217expect_equal(digest(int_round(sum(marathon_workflow$pre$actions$recipe$recipe$template$time_hrs), 0)), '2213c3a0eb86305be22e0ca3b0a773c1')218})219print("Success!")220}221222223test_8.0 <- function(){224test_that('Did not create an object named gridvals', {225expect_true(exists('gridvals'))226})227test_that('gridvals does not contain the correct data and column name', {228expect_true('tbl' %in% class(gridvals))229expect_true('neighbors' %in% colnames(gridvals))230expect_equal(digest(int_round(sum(gridvals), 0)), '7f14b492534315a44244b698e5058e39')231})232test_that('Did not create an object named marathon_results', {233expect_true(exists('marathon_results'))234})235test_that('marathon_results is not a tibble', {236expect_true('tbl' %in% class(marathon_results))237})238test_that('marathon_results does not contain the correct data', {239expect_equal(dim(marathon_results), c(400, 7))240expect_equal(digest(int_round(sum(marathon_results$neighbors), 0)), '07d8f52916463a9c6cd99797f0531d42')241expect_equal(int_round(unique(marathon_results$n), 0), 5)242expect_equal(digest(int_round(sum(marathon_results$mean), 0)), '3c9f3a7a14786e414122855e84509f9d')243expect_equal(digest(int_round(sum(marathon_results$std_err), 0)), '7c7124efff5c7039a1b1e7cba65c5379')244})245print("Success!")246}247248249test_8.1 <- function(){250test_that('Did not create an object named marathon_min', {251expect_true(exists('marathon_min'))252})253test_that('marathon_min is not a tibble', {254expect_true('tbl' %in% class(marathon_min))255})256test_that('marathon_min does not contain the correct data', {257expect_equal(dim(marathon_min), c(1, 7))258expect_true('neighbors' %in% colnames(marathon_min))259expect_true('.metric' %in% colnames(marathon_min))260expect_true('.estimator' %in% colnames(marathon_min))261expect_true('mean' %in% colnames(marathon_min))262expect_true('n' %in% colnames(marathon_min))263expect_true('std_err' %in% colnames(marathon_min))264expect_true('.config' %in% colnames(marathon_min))265})266test_that('Best K value is incorrect', {267expect_equal(digest(int_round(marathon_min$neighbors, 2)), '9241e88f7548d793a2482a33d623b99f')268})269test_that('Metric is incorrect', {270expect_equal(digest(marathon_min$.metric), '91a8c46d46a2a25459eaabfa08f35967')271})272print("Success!")273}274275test_8.2 <- function(){276test_that('Did not create an object named k_min', {277expect_true(exists('k_min'))278})279test_that('k_min is not correct', {280expect_true('integer' %in% class(k_min))281expect_equal(digest(int_round(k_min, 2)), '9241e88f7548d793a2482a33d623b99f')282})283test_that('Did not create an object named marathon_best_spec', {284expect_true(exists('marathon_best_spec'))285})286test_that('marathon_best_spec has incorrect specifications', {287expect_equal(digest(as.character(get_expr(marathon_best_spec$args$neighbors))), '0b942c90bc01f15b084d00fa29bf4cc0')288})289test_that('weight_func is incorrect', {290expect_equal(digest(as.character(get_expr(marathon_best_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')291})292test_that('set_engine is incorrect', {293expect_equal(digest(as.character(marathon_best_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')294})295test_that('mode is incorrect', {296expect_equal(digest(as.character(marathon_best_spec$mode)), 'b8bdd7015e0d1c6037512fd1396aef1a')297})298test_that('Did not create an object named marathon_best_fit', {299expect_true(exists('marathon_best_fit'))300})301test_that('marathon_best_fit should be a workflow', {302expect_true('workflow' %in% class(marathon_best_fit))303})304test_that('marathon_best_fit does not contain the correct model specification', {305expect_equal(digest(get_expr(marathon_best_fit$fit$actions$model$spec$args$neighbors)), '7ad692ee809beafa13e6d291d0d5372f')306expect_equal(digest(as.character(marathon_best_fit$fit$actions$model$spec$mode)), 'b8bdd7015e0d1c6037512fd1396aef1a')307expect_equal(digest(as.character(marathon_best_fit$fit$actions$model$spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')308expect_true('nearest_neighbor' %in% class(marathon_best_fit$fit$actions$model$spec))309})310test_that('marathon_best_fit does not contain the correct recipe', {311expect_true('recipe' %in% class(marathon_best_fit$pre$actions$recipe$recipe))312expect_equal(digest(int_round(sum(marathon_best_fit$pre$actions$recipe$recipe$template$max), 0)), '94e91e5e6573ddfedb81802729c39543')313expect_equal(digest(int_round(sum(marathon_best_fit$pre$actions$recipe$recipe$template$time_hrs), 0)), '2213c3a0eb86305be22e0ca3b0a773c1')314})315test_that('Did not create an object named marathon_summary', {316expect_true(exists('marathon_summary'))317})318test_that('marathon_summary is not a tibble', {319expect_true('tbl' %in% class(marathon_summary))320})321test_that('marathon_summary contains the incorrect data', {322expect_true('.metric' %in% colnames(marathon_summary))323expect_true('.estimator' %in% colnames(marathon_summary))324expect_true('.estimate' %in% colnames(marathon_summary))325expect_equal(digest(int_round(sum(marathon_summary$.estimate), 0)), '4b5630ee914e848e8d07221556b0a2fb')326})327print("Success!")328}329330test_8.3 <- function(){331test_that('Did not create an objected named answer8.3', {332expect_true(exists('answer8.3'))333})334test_that('answer is incorrect', {335expect_equal(digest(answer8.3), 'd2a90307aac5ae8d0ef58e2fe730d38b')336})337print("Success!")338}339340test_9.0 <- function(){341properties <- c(marathon_plot$layers[[1]]$mapping, marathon_plot$mapping)342labels <- marathon_plot$labels343test_that('Did not create an object named marathon_preds', {344expect_true(exists('marathon_preds'))345})346test_that('marathon_preds should be a tibble', {347expect_true('tbl' %in% class(marathon_preds))348})349test_that('marathon_preds contains incorrect data', {350expect_equal(dim(marathon_preds), c(698, 14))351expect_true('.pred' %in% colnames(marathon_preds))352expect_equal(digest(int_round(sum(marathon_preds$.pred), 2)), 'f5847c263ec9596eee1ae122cdd1e347')353expect_equal(digest(int_round(sum(marathon_preds$time_hrs), 2)), 'b9b7909060cc50e65fb7ff452897a2b4')354})355test_that('Did not create an object called marathon_plot', {356expect_true(exists('marathon_plot'))357})358test_that('max should be on the x-axis.', {359expect_true("max" == rlang::get_expr(properties$x))360})361test_that('time_hrs should be on the y-axis.', {362expect_true("time_hrs" == rlang::get_expr(properties$y))363})364test_that('marathon_plot should have full_predictions plotted as a blue line over the data points.', {365expect_true('blue' %in% as.character(marathon_plot$layers[[2]]$aes_params))366expect_true('GeomLine' %in% c(class(rlang::get_expr(marathon_plot$layers[[1]]$geom)), class(rlang::get_expr(marathon_plot$layers[[2]]$geom))))367})368test_that('max should be the x argument for geom_line', {369expect_true('max' == rlang::get_expr(marathon_plot$layers[[2]]$mapping$x))370})371test_that('.pred should be the y argument for geom_line',{372expect_true('.pred' == rlang::get_expr(marathon_plot$layers[[2]]$mapping$y))373})374test_that('Labels on the axes/title and legend need to be changed to be descriptive, nicely formatted, and human readable.', {375expect_false((labels$y) == 'time_hrs')376expect_false((labels$x) == 'max')377expect_false((labels$title == 'k_min'))378})379print("Success!")380}381382383