Path: blob/master/2021-summer/materials/worksheet_08/tests_worksheet_08.R
2051 views
library(testthat)1library(digest)2library(rlang)34#' Round double to precise integer5#'6#' `int_round` works to create an integer corresponding to a number that is7#' tested up to a particular decimal point of precision. This is useful when8#' there is a need to compare a numeric value using hashes.9#'10#' @param x Double vector of length one.11#' @param digits Double vector of length one to specify decimal point of precision. Negative numbers can be used to specifying significant digits > 0.1.12#'13#' @return Integer vector of length one corresponding to a particular decimal point of precision.14#'15#' @examples16#' # to get an integer up to two decimals of precision from 234.5678917#' int_round(234.56789, 2)18#'19#' to get an integer rounded to the hundred digit from 234.5678920#' int_round(234.56789, -2)21int_round <- function(x, digits){22x = x*10^digits23xint = as.integer(x)24xint1 = xint + 1L25if (abs(xint - x) < abs(xint1 - x)){26return(xint)27}28else {29return(xint1)30}31}3233test_0.0 <- function(){34test_that('Solution is incorrect', {35expect_equal(digest(answer0.0), '3a5505c06543876fe45598b5e5e5195d')36})37print("Success!")38}3940test_0.1 <- function(){41test_that('Solution is incorrect', {42expect_equal(digest(answer0.1), '475bf9280aab63a82af60791302736f6')43})44print("Success!")45}4647test_0.2 <- function(){48test_that('Solution is incorrect', {49expect_equal(digest(int_round(answer0.2, 2)), '6953b334169bd7ec7da1c1eda5aaf6a5')50})51print("Success!")52}5354test_0.3 <- function(){55test_that('Solution is incorrect', {56expect_equal(digest(answer0.3), '75f1160e72554f4270c809f041c7a776')57})58print("Success!")59}6061test_1.0 <- function(){62test_that('Did not create an object named marathon', {63expect_true(exists("marathon"))64})65test_that('marathon should be a tibble.', {66expect_true('tbl' %in% class(marathon))67})68test_that('marathon does not contain the correct number of rows and/or columns.', {69expect_equal(dim(marathon), c(929, 13))70})71test_that('The marathon tibble is missing columns.', {72expect_true("time_hrs" %in% colnames(marathon))73expect_true("max" %in% colnames(marathon))74})75print("Success!")76}7778test_2.0 <- function(){79properties <- c(answer2$mapping, answer2$layers[[1]]$mapping)80labels <- answer2$labels81test_that('Did not create a plot named answer2', {82expect_true(exists("answer2"))83})84test_that('marathon_50 does not contain the correct number of rows and/or columns.', {85expect_equal(dim(marathon_50), c(50, 13))86})87test_that('answer2 should use information from marathon_50', {88expect_equal(answer2$data, marathon_50)89})90test_that('max should be on the x-axis.', {91expect_true("max" %in% c(rlang::get_expr(properties$x),92rlang::get_expr(properties$x)))93})94test_that('time_hrs should be on the y-axis.', {95expect_true("time_hrs" %in% c(rlang::get_expr(properties$y),96rlang::get_expr(properties$y)))97})98test_that('answer2 should be a scatter plot.', {99expect_equal(digest(class(rlang::get_expr(answer2$layers[[1]]$geom))[1]),100'911e5b9debfb523f25ad2ccc01a4b2dd')101})102test_that('Labels on the axes should be descriptive and human readable.', {103expect_false((labels$y) == 'time_hrs')104expect_false((labels$x) == 'max')105})106print("Success!")107}108109test_3.0 <- function(){110test_that('Did not create an object called answer3', {111expect_true(exists('answer3'))112})113test_that('answer3 is incorrect', {114expect_equal(digest(int_round(answer3, 1)), 'a266aa4a0aa711355be22e0f3b8d91af')115})116print("Success!")117}118119test_4.0 <- function(){120test_that('Did not create an object called answer4', {121expect_true(exists('answer4'))122})123test_that('answer4 is incorrect', {124expect_equal(digest(int_round(answer4, 1)), '285d156b1b700fbb489df058fdb9e2ee')125})126print("Success!")127}128129test_5.0 <- function(){130test_that('Did not create an object called answer5', {131expect_true(exists('answer5'))132})133test_that('Solution is incorrect', {134expect_equal(digest(answer5), '475bf9280aab63a82af60791302736f6')135})136print("Success!")137}138139test_6.0 <- function(){140test_that('Did not create an object named marathon_split', {141expect_true(exists("marathon_split"))142})143test_that('marathon_split should be rsplit (not a tibble)', {144expect_true('rsplit' %in% class(marathon_split))145})146test_that('Did not create an object named marathon_training', {147expect_true(exists('marathon_training'))148})149test_that('marathon_training does not contain 0.75 of the data.', {150expect_equal(dim(marathon_training), c(698,13))151expect_equal(digest(int_round(sum(marathon_training$age), 0)), '5109988e81575a4e65652fddb747a18f')152})153test_that('Did not create an object named marathon_testing', {154expect_true(exists('marathon_testing'))155})156test_that('marathon_testing does not contain 0.25 of the data.', {157expect_equal(dim(marathon_testing), c(231, 13))158expect_equal(digest(int_round(sum(marathon_testing$age), 0)), '9ffe9b883d53974eaa9afb9bf0ec386b')159})160print("Success!")161}162163test_7.0 <- function(){164test_that('Did not create an object named marathon_spec', {165expect_true(exists("marathon_spec"))166})167test_that('neighbors argument is incorrect', {168expect_equal(digest(as.character(get_expr(marathon_spec$args$neighbors))), '4b89cff22bb78b28a0a6b7fe28d371f6')169})170test_that('weight_func is incorrect', {171expect_equal(digest(as.character(get_expr(marathon_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')172})173test_that('set_engine is incorrect', {174expect_equal(digest(as.character(marathon_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')175})176test_that('mode is incorrect', {177expect_equal(digest(as.character(marathon_spec$mode)), 'b8bdd7015e0d1c6037512fd1396aef1a')178})179test_that('Did not create an object named marathon_recipe', {180expect_true(exists("marathon_recipe"))181})182test_that('Data in marathon_recipe is not scaled and centered', {183expect_equal(digest(int_round(sum(marathon_recipe$template$max), 0)), '94e91e5e6573ddfedb81802729c39543')184expect_equal(digest(int_round(sum(marathon_recipe$template$time_hrs), 0)), '2213c3a0eb86305be22e0ca3b0a773c1')185})186print("Success!")187}188189test_7.1 <- function(){190test_that('Did not create an object called marathon_vfold', {191expect_true(exists("marathon_vfold"))192})193test_that('marathon_vfold does not contain 5 folds', {194expect_equal(int_round(length(marathon_vfold$id), 0), 5)195})196test_that('marathon_vfold should be a cross-validation object', {197expect_true('vfold_cv' %in% class(marathon_vfold))198})199test_that('Did not create an object called marathon_workflow', {200expect_true(exists("marathon_workflow"))201})202test_that('marathon_workflow is not a workflow object', {203expect_true('workflow' %in% class(marathon_workflow))204})205test_that('marathon_workflow does not contain the correct model specification', {206expect_equal(digest(as.character(marathon_workflow$fit$actions$model$spec$args$neighbors)), 'b68c9f555cfd94fe903b741afcace6c1')207expect_equal(digest(as.character(marathon_workflow$fit$actions$model$spec$mode)), 'b8bdd7015e0d1c6037512fd1396aef1a')208expect_equal(digest(as.character(marathon_workflow$fit$actions$model$spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')209expect_true('nearest_neighbor' %in% class(marathon_workflow$fit$actions$model$spec))210})211test_that('marathon_workflow does not contain the correct recipe', {212expect_true('recipe' %in% class(marathon_workflow$pre$actions$recipe$recipe))213expect_equal(digest(int_round(sum(marathon_workflow$pre$actions$recipe$recipe$template$max), 0)), '94e91e5e6573ddfedb81802729c39543')214expect_equal(digest(int_round(sum(marathon_workflow$pre$actions$recipe$recipe$template$time_hrs), 0)), '2213c3a0eb86305be22e0ca3b0a773c1')215})216print("Success!")217}218219220test_8.0 <- function(){221test_that('Did not create an object named gridvals', {222expect_true(exists('gridvals'))223})224test_that('gridvals does not contain the correct data and column name', {225expect_true('tbl' %in% class(gridvals))226expect_true('neighbors' %in% colnames(gridvals))227expect_equal(digest(int_round(sum(gridvals), 0)), '7f14b492534315a44244b698e5058e39')228})229test_that('Did not create an object named marathon_results', {230expect_true(exists('marathon_results'))231})232test_that('marathon_results is not a tibble', {233expect_true('tbl' %in% class(marathon_results))234})235test_that('marathon_results does not contain the correct data', {236expect_equal(dim(marathon_results), c(400, 7))237expect_equal(digest(int_round(sum(marathon_results$neighbors), 0)), '07d8f52916463a9c6cd99797f0531d42')238expect_equal(int_round(unique(marathon_results$n), 0), 5)239expect_equal(digest(int_round(sum(marathon_results$mean), 0)), '3c9f3a7a14786e414122855e84509f9d')240expect_equal(digest(int_round(sum(marathon_results$std_err), 0)), '7c7124efff5c7039a1b1e7cba65c5379')241})242print("Success!")243}244245246test_8.1 <- function(){247test_that('Did not create an object named marathon_min', {248expect_true(exists('marathon_min'))249})250test_that('marathon_min is not a tibble', {251expect_true('tbl' %in% class(marathon_min))252})253test_that('marathon_min does not contain the correct data', {254expect_equal(dim(marathon_min), c(1, 7))255expect_true('neighbors' %in% colnames(marathon_min))256expect_true('.metric' %in% colnames(marathon_min))257expect_true('.estimator' %in% colnames(marathon_min))258expect_true('mean' %in% colnames(marathon_min))259expect_true('n' %in% colnames(marathon_min))260expect_true('std_err' %in% colnames(marathon_min))261expect_true('.config' %in% colnames(marathon_min))262})263test_that('Best K value is incorrect', {264expect_equal(digest(int_round(marathon_min$neighbors, 2)), '9241e88f7548d793a2482a33d623b99f')265})266test_that('Metric is incorrect', {267expect_equal(digest(marathon_min$.metric), '91a8c46d46a2a25459eaabfa08f35967')268})269print("Success!")270}271272test_8.2 <- function(){273test_that('Did not create an object named k_min', {274expect_true(exists('k_min'))275})276test_that('k_min is not correct', {277expect_true('integer' %in% class(k_min))278expect_equal(digest(int_round(k_min, 2)), '9241e88f7548d793a2482a33d623b99f')279})280test_that('Did not create an object named marathon_best_spec', {281expect_true(exists('marathon_best_spec'))282})283test_that('marathon_best_spec has incorrect specifications', {284expect_equal(digest(as.character(get_expr(marathon_best_spec$args$neighbors))), '0b942c90bc01f15b084d00fa29bf4cc0')285})286test_that('weight_func is incorrect', {287expect_equal(digest(as.character(get_expr(marathon_best_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')288})289test_that('set_engine is incorrect', {290expect_equal(digest(as.character(marathon_best_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')291})292test_that('mode is incorrect', {293expect_equal(digest(as.character(marathon_best_spec$mode)), 'b8bdd7015e0d1c6037512fd1396aef1a')294})295test_that('Did not create an object named marathon_best_fit', {296expect_true(exists('marathon_best_fit'))297})298test_that('marathon_best_fit should be a workflow', {299expect_true('workflow' %in% class(marathon_best_fit))300})301test_that('marathon_best_fit does not contain the correct model specification', {302expect_equal(digest(get_expr(marathon_best_fit$fit$actions$model$spec$args$neighbors)), '7ad692ee809beafa13e6d291d0d5372f')303expect_equal(digest(as.character(marathon_best_fit$fit$actions$model$spec$mode)), 'b8bdd7015e0d1c6037512fd1396aef1a')304expect_equal(digest(as.character(marathon_best_fit$fit$actions$model$spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')305expect_true('nearest_neighbor' %in% class(marathon_best_fit$fit$actions$model$spec))306})307test_that('marathon_best_fit does not contain the correct recipe', {308expect_true('recipe' %in% class(marathon_best_fit$pre$actions$recipe$recipe))309expect_equal(digest(int_round(sum(marathon_best_fit$pre$actions$recipe$recipe$template$max), 0)), '94e91e5e6573ddfedb81802729c39543')310expect_equal(digest(int_round(sum(marathon_best_fit$pre$actions$recipe$recipe$template$time_hrs), 0)), '2213c3a0eb86305be22e0ca3b0a773c1')311})312test_that('Did not create an object named marathon_summary', {313expect_true(exists('marathon_summary'))314})315test_that('marathon_summary is not a tibble', {316expect_true('tbl' %in% class(marathon_summary))317})318test_that('marathon_summary contains the incorrect data', {319expect_true('.metric' %in% colnames(marathon_summary))320expect_true('.estimator' %in% colnames(marathon_summary))321expect_true('.estimate' %in% colnames(marathon_summary))322expect_equal(digest(int_round(sum(marathon_summary$.estimate), 0)), '4b5630ee914e848e8d07221556b0a2fb')323})324print("Success!")325}326327test_8.3 <- function(){328test_that('Did not create an objected named answer8.3', {329expect_true(exists('answer8.3'))330})331test_that('answer is incorrect', {332expect_equal(digest(answer8.3), 'd2a90307aac5ae8d0ef58e2fe730d38b')333})334print("Success!")335}336337test_9.0 <- function(){338properties <- c(marathon_plot$layers[[1]]$mapping, marathon_plot$mapping)339labels <- marathon_plot$labels340test_that('Did not create an object named marathon_preds', {341expect_true(exists('marathon_preds'))342})343test_that('marathon_preds should be a tibble', {344expect_true('tbl' %in% class(marathon_preds))345})346test_that('marathon_preds contains incorrect data', {347expect_equal(dim(marathon_preds), c(698, 14))348expect_true('.pred' %in% colnames(marathon_preds))349expect_equal(digest(int_round(sum(marathon_preds$.pred), 2)), 'f5847c263ec9596eee1ae122cdd1e347')350expect_equal(digest(int_round(sum(marathon_preds$time_hrs), 2)), 'b9b7909060cc50e65fb7ff452897a2b4')351})352test_that('Did not create an object called marathon_plot', {353expect_true(exists('marathon_plot'))354})355test_that('max should be on the x-axis.', {356expect_true("max" == rlang::get_expr(properties$x))357})358test_that('time_hrs should be on the y-axis.', {359expect_true("time_hrs" == rlang::get_expr(properties$y))360})361test_that('marathon_plot should have full_predictions plotted as a blue line over the data points.', {362expect_true('blue' %in% as.character(marathon_plot$layers[[2]]$aes_params))363expect_true('GeomLine' %in% c(class(rlang::get_expr(marathon_plot$layers[[1]]$geom)), class(rlang::get_expr(marathon_plot$layers[[2]]$geom))))364})365test_that('max should be the x argument for geom_line', {366expect_true('max' == rlang::get_expr(marathon_plot$layers[[2]]$mapping$x))367})368test_that('.pred should be the y argument for geom_line',{369expect_true('.pred' == rlang::get_expr(marathon_plot$layers[[2]]$mapping$y))370})371test_that('Labels on the axes/title and legend need to be changed to be descriptive, nicely formatted, and human readable.', {372expect_false((labels$y) == 'time_hrs')373expect_false((labels$x) == 'max')374expect_false((labels$title == 'k_min'))375})376print("Success!")377}378379380