Path: blob/master/2020-fall/materials/worksheet_08/tests_worksheet_08.R
2715 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# -1718test_0.0 <- function(){19test_that('Solution is incorrect', {20expect_equal(digest(answer0.0), '3a5505c06543876fe45598b5e5e5195d')21})22print("Success!")23}2425test_0.1 <- function(){26test_that('Solution is incorrect', {27expect_equal(digest(answer0.1), '475bf9280aab63a82af60791302736f6')28})29print("Success!")30}3132test_0.2 <- function(){33test_that('Solution is incorrect', {34expect_equal(digest(int_round(answer0.2, 2)), '6953b334169bd7ec7da1c1eda5aaf6a5')35})36print("Success!")37}3839test_0.3 <- function(){40test_that('Solution is incorrect', {41expect_equal(digest(answer0.3), '75f1160e72554f4270c809f041c7a776')42})43print("Success!")44}4546test_1.0 <- function(){47test_that('Did not create an object named marathon', {48expect_true(exists("marathon"))49})50test_that('marathon should be a tibble.', {51expect_true('tbl' %in% class(marathon))52})53test_that('marathon does not contain the correct number of rows and/or columns.', {54expect_equal(dim(marathon), c(929, 13))55})56test_that('The marathon tibble is missing columns.', {57expect_true("time_hrs" %in% colnames(marathon))58expect_true("max" %in% colnames(marathon))59})60print("Success!")61}6263test_2.0 <- function(){64properties <- c(answer2$mapping, answer2$layers[[1]]$mapping)65labels <- answer2$labels66test_that('Did not create a plot named answer2', {67expect_true(exists("answer2"))68})69test_that('marathon_50 does not contain the correct number of rows and/or columns.', {70expect_equal(dim(marathon_50), c(50, 13))71})72test_that('answer2 should use information from marathon_50', {73expect_equal(answer2$data, marathon_50)74})75test_that('max should be on the x-axis.', {76expect_true("max" %in% c(rlang::get_expr(properties$x),77rlang::get_expr(properties$x)))78})79test_that('time_hrs should be on the y-axis.', {80expect_true("time_hrs" %in% c(rlang::get_expr(properties$y),81rlang::get_expr(properties$y)))82})83test_that('answer2 should be a scatter plot.', {84expect_equal(digest(class(rlang::get_expr(answer2$layers[[1]]$geom))[1]),85'911e5b9debfb523f25ad2ccc01a4b2dd')86})87test_that('Labels on the axes should be descriptive and human readable.', {88expect_false((labels$y) == 'time_hrs')89expect_false((labels$x) == 'max')90})91print("Success!")92}9394test_3.0 <- function(){95test_that('Did not create an object called answer3', {96expect_true(exists('answer3'))97})98test_that('answer3 is incorrect', {99expect_equal(digest(int_round(answer3, 1)), 'a266aa4a0aa711355be22e0f3b8d91af')100})101print("Success!")102}103104test_4.0 <- function(){105test_that('Did not create an object called answer4', {106expect_true(exists('answer4'))107})108test_that('answer4 is incorrect', {109expect_equal(digest(int_round(answer4, 1)), '285d156b1b700fbb489df058fdb9e2ee')110})111print("Success!")112}113114test_5.0 <- function(){115test_that('Did not create an object called answer5', {116expect_true(exists('answer5'))117})118test_that('Solution is incorrect', {119expect_equal(digest(answer5), '475bf9280aab63a82af60791302736f6')120})121print("Success!")122}123124test_6.0 <- function(){125test_that('Did not create an object named marathon_split', {126expect_true(exists("marathon_split"))127})128test_that('marathon_split should be rsplit (not a tibble)', {129expect_true('rsplit' %in% class(marathon_split))130})131test_that('Did not create an object named marathon_training', {132expect_true(exists('marathon_training'))133})134test_that('marathon_training does not contain 0.75 of the data.', {135expect_equal(dim(marathon_training), c(698,13))136expect_equal(digest(int_round(sum(marathon_training$age), 0)), '5109988e81575a4e65652fddb747a18f')137})138test_that('Did not create an object named marathon_testing', {139expect_true(exists('marathon_testing'))140})141test_that('marathon_testing does not contain 0.25 of the data.', {142expect_equal(dim(marathon_testing), c(231, 13))143expect_equal(digest(int_round(sum(marathon_testing$age), 0)), '9ffe9b883d53974eaa9afb9bf0ec386b')144})145print("Success!")146}147148test_7.0 <- function(){149test_that('Did not create an object named marathon_spec', {150expect_true(exists("marathon_spec"))151})152test_that('neighbors argument is incorrect', {153expect_equal(digest(as.character(get_expr(marathon_spec$args$neighbors))), '4b89cff22bb78b28a0a6b7fe28d371f6')154})155test_that('weight_func is incorrect', {156expect_equal(digest(as.character(get_expr(marathon_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')157})158test_that('set_engine is incorrect', {159expect_equal(digest(as.character(marathon_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')160})161test_that('mode is incorrect', {162expect_equal(digest(as.character(marathon_spec$mode)), 'b8bdd7015e0d1c6037512fd1396aef1a')163})164test_that('Did not create an object named marathon_recipe', {165expect_true(exists("marathon_recipe"))166})167test_that('Data in marathon_recipe is not scaled and centered', {168expect_equal(digest(int_round(sum(marathon_recipe$template$max), 0)), '94e91e5e6573ddfedb81802729c39543')169expect_equal(digest(int_round(sum(marathon_recipe$template$time_hrs), 0)), '2213c3a0eb86305be22e0ca3b0a773c1')170})171print("Success!")172}173174test_7.1 <- function(){175test_that('Did not create an object called marathon_vfold', {176expect_true(exists("marathon_vfold"))177})178test_that('marathon_vfold does not contain 5 folds', {179expect_equal(int_round(length(marathon_vfold$id), 0), 5)180})181test_that('marathon_vfold should be a cross-validation object', {182expect_true('vfold_cv' %in% class(marathon_vfold))183})184test_that('Did not create an object called marathon_workflow', {185expect_true(exists("marathon_workflow"))186})187test_that('marathon_workflow is not a workflow object', {188expect_true('workflow' %in% class(marathon_workflow))189})190test_that('marathon_workflow does not contain the correct model specification', {191expect_equal(digest(as.character(marathon_workflow$fit$actions$model$spec$args$neighbors)), 'b68c9f555cfd94fe903b741afcace6c1')192expect_equal(digest(as.character(marathon_workflow$fit$actions$model$spec$mode)), 'b8bdd7015e0d1c6037512fd1396aef1a')193expect_equal(digest(as.character(marathon_workflow$fit$actions$model$spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')194expect_true('nearest_neighbor' %in% class(marathon_workflow$fit$actions$model$spec))195})196test_that('marathon_workflow does not contain the correct recipe', {197expect_true('recipe' %in% class(marathon_workflow$pre$actions$recipe$recipe))198expect_equal(digest(int_round(sum(marathon_workflow$pre$actions$recipe$recipe$template$max), 0)), '94e91e5e6573ddfedb81802729c39543')199expect_equal(digest(int_round(sum(marathon_workflow$pre$actions$recipe$recipe$template$time_hrs), 0)), '2213c3a0eb86305be22e0ca3b0a773c1')200})201print("Success!")202}203204205test_8.0 <- function(){206test_that('Did not create an object named gridvals', {207expect_true(exists('gridvals'))208})209test_that('gridvals does not contain the correct data and column name', {210expect_true('tbl' %in% class(gridvals))211expect_true('neighbors' %in% colnames(gridvals))212expect_equal(digest(int_round(sum(gridvals), 0)), '7f14b492534315a44244b698e5058e39')213})214test_that('Did not create an object named marathon_results', {215expect_true(exists('marathon_results'))216})217test_that('marathon_results is not a tibble', {218expect_true('tbl' %in% class(marathon_results))219})220test_that('marathon_results does not contain the correct data', {221expect_equal(dim(marathon_results), c(400, 7))222expect_equal(digest(int_round(sum(marathon_results$neighbors), 0)), '07d8f52916463a9c6cd99797f0531d42')223expect_equal(int_round(unique(marathon_results$n), 0), 5)224expect_equal(digest(int_round(sum(marathon_results$mean), 0)), '3c9f3a7a14786e414122855e84509f9d')225expect_equal(digest(int_round(sum(marathon_results$std_err), 0)), '7c7124efff5c7039a1b1e7cba65c5379')226})227print("Success!")228}229230231test_8.1 <- function(){232test_that('Did not create an object named marathon_min', {233expect_true(exists('marathon_min'))234})235test_that('marathon_min is not a tibble', {236expect_true('tbl' %in% class(marathon_min))237})238test_that('marathon_min does not contain the correct data', {239expect_equal(dim(marathon_min), c(1, 7))240expect_true('neighbors' %in% colnames(marathon_min))241expect_true('.metric' %in% colnames(marathon_min))242expect_true('.estimator' %in% colnames(marathon_min))243expect_true('mean' %in% colnames(marathon_min))244expect_true('n' %in% colnames(marathon_min))245expect_true('std_err' %in% colnames(marathon_min))246expect_true('.config' %in% colnames(marathon_min))247})248test_that('Best K value is incorrect', {249expect_equal(digest(int_round(marathon_min$neighbors, 2)), '9241e88f7548d793a2482a33d623b99f')250})251test_that('Metric is incorrect', {252expect_equal(digest(marathon_min$.metric), '91a8c46d46a2a25459eaabfa08f35967')253})254print("Success!")255}256257test_8.2 <- function(){258test_that('Did not create an object named k_min', {259expect_true(exists('k_min'))260})261test_that('k_min is not correct', {262expect_true('integer' %in% class(k_min))263expect_equal(digest(int_round(k_min, 2)), '9241e88f7548d793a2482a33d623b99f')264})265test_that('Did not create an object named marathon_best_spec', {266expect_true(exists('marathon_best_spec'))267})268test_that('marathon_best_spec has incorrect specifications', {269expect_equal(digest(as.character(get_expr(marathon_best_spec$args$neighbors))), '0b942c90bc01f15b084d00fa29bf4cc0')270})271test_that('weight_func is incorrect', {272expect_equal(digest(as.character(get_expr(marathon_best_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')273})274test_that('set_engine is incorrect', {275expect_equal(digest(as.character(marathon_best_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')276})277test_that('mode is incorrect', {278expect_equal(digest(as.character(marathon_best_spec$mode)), 'b8bdd7015e0d1c6037512fd1396aef1a')279})280test_that('Did not create an object named marathon_best_fit', {281expect_true(exists('marathon_best_fit'))282})283test_that('marathon_best_fit should be a workflow', {284expect_true('workflow' %in% class(marathon_best_fit))285})286test_that('marathon_best_fit does not contain the correct model specification', {287expect_equal(digest(get_expr(marathon_best_fit$fit$actions$model$spec$args$neighbors)), '7ad692ee809beafa13e6d291d0d5372f')288expect_equal(digest(as.character(marathon_best_fit$fit$actions$model$spec$mode)), 'b8bdd7015e0d1c6037512fd1396aef1a')289expect_equal(digest(as.character(marathon_best_fit$fit$actions$model$spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')290expect_true('nearest_neighbor' %in% class(marathon_best_fit$fit$actions$model$spec))291})292test_that('marathon_best_fit does not contain the correct recipe', {293expect_true('recipe' %in% class(marathon_best_fit$pre$actions$recipe$recipe))294expect_equal(digest(int_round(sum(marathon_best_fit$pre$actions$recipe$recipe$template$max), 0)), '94e91e5e6573ddfedb81802729c39543')295expect_equal(digest(int_round(sum(marathon_best_fit$pre$actions$recipe$recipe$template$time_hrs), 0)), '2213c3a0eb86305be22e0ca3b0a773c1')296})297test_that('Did not create an object named marathon_summary', {298expect_true(exists('marathon_summary'))299})300test_that('marathon_summary is not a tibble', {301expect_true('tbl' %in% class(marathon_summary))302})303test_that('marathon_summary contains the incorrect data', {304expect_true('.metric' %in% colnames(marathon_summary))305expect_true('.estimator' %in% colnames(marathon_summary))306expect_true('.estimate' %in% colnames(marathon_summary))307expect_equal(digest(int_round(sum(marathon_summary$.estimate), 0)), '4b5630ee914e848e8d07221556b0a2fb')308})309print("Success!")310}311312test_8.3 <- function(){313test_that('Did not create an objected named answer8.3', {314expect_true(exists('answer8.3'))315})316test_that('answer is incorrect', {317expect_equal(digest(answer8.3), 'd2a90307aac5ae8d0ef58e2fe730d38b')318})319print("Success!")320}321322test_9.0 <- function(){323properties <- c(marathon_plot$layers[[1]]$mapping, marathon_plot$mapping)324labels <- marathon_plot$labels325test_that('Did not create an object named marathon_preds', {326expect_true(exists('marathon_preds'))327})328test_that('marathon_preds should be a tibble', {329expect_true('tbl' %in% class(marathon_preds))330})331test_that('marathon_preds contains incorrect data', {332expect_equal(dim(marathon_preds), c(698, 14))333expect_true('.pred' %in% colnames(marathon_preds))334expect_equal(digest(int_round(sum(marathon_preds$.pred), 2)), 'f5847c263ec9596eee1ae122cdd1e347')335expect_equal(digest(int_round(sum(marathon_preds$time_hrs), 2)), 'b9b7909060cc50e65fb7ff452897a2b4')336})337test_that('Did not create an object called marathon_plot', {338expect_true(exists('marathon_plot'))339})340test_that('max should be on the x-axis.', {341expect_true("max" == rlang::get_expr(properties$x))342})343test_that('time_hrs should be on the y-axis.', {344expect_true("time_hrs" == rlang::get_expr(properties$y))345})346test_that('marathon_plot should have full_predictions plotted as a blue line over the data points.', {347expect_true('blue' %in% as.character(marathon_plot$layers[[2]]$aes_params))348expect_true('GeomLine' %in% c(class(rlang::get_expr(marathon_plot$layers[[1]]$geom)), class(rlang::get_expr(marathon_plot$layers[[2]]$geom))))349})350test_that('max should be the x argument for geom_line', {351expect_true('max' == rlang::get_expr(marathon_plot$layers[[2]]$mapping$x))352})353test_that('.pred should be the y argument for geom_line',{354expect_true('.pred' == rlang::get_expr(marathon_plot$layers[[2]]$mapping$y))355})356test_that('Labels on the axes/title and legend need to be changed to be descriptive, nicely formatted, and human readable.', {357expect_false((labels$y) == 'time_hrs')358expect_false((labels$x) == 'max')359expect_false((labels$title == 'k_min'))360})361print("Success!")362}363364365