Path: blob/master/2021-summer/materials/worksheet_07/tests_worksheet_07.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.1 <- function(){34test_that('Did not create an object named answer0.1', {35expect_true(exists('answer0.1'))36})37test_that('Solution is incorrect', {38expect_equal(digest(answer0.1), '475bf9280aab63a82af60791302736f6')39})40print("Success!")4142}4344test_1.0 <- function(){45test_that('Did not create an object named fruit_data', {46expect_true(exists("fruit_data"))47})48test_that('fruit_data does not contain the correct number of rows and/or columns.', {49expect_equal(dim(fruit_data), c(59, 7))50})51test_that('The fruit_name column in fruit_data should be of class factor.', {52expect_true(is.factor(fruit_data$fruit_name))53})54test_that('Columns in fruit_data contain incorrect values.', {55expect_equal(digest(int_round(sum(fruit_data$mass, na.rm = TRUE), 2)), '8c7433f4d278ef1e1e0f8d0ccb217614') # we hid the answer to the test here so you can't see it, but we can still run the test56})57print("Success!")58}5960# # +61test_1.1 <- function(){62test_that('Did not create an object called answer1.1', {63expect_true(exists('answer1.1'))64})65test_that('Solution is incorrect', {66expect_equal(digest(answer1.1), '75f1160e72554f4270c809f041c7a776') # we hid the answer to the test here so you can't see it, but we can still run the test6768})69print("Success!")70}71# -7273test_1.2 <- function(){74test_that('Did not create an object named fruit_dist_2', {75expect_true(exists("fruit_dist_2"))76})77test_that('fruit_dist_2 is incorrect.', {78expect_equal(digest(int_round(fruit_dist_2, 3)), 'a29a5d18050c6ce0aa2dc501684e1375')79})80print("Success!")81}8283test_1.3 <- function(){84test_that('Did not create an object named fruit_dist_44', {85expect_true(exists("fruit_dist_44"))86})87test_that('fruit_dist_44 is incorrect.', {88expect_equal(digest(int_round(fruit_dist_44, 2)), 'ea07cf8b74030ff04b56ac69dd094adc')89})90print("Success!")91}929394test_1.4 <- function(){95test_that('Did not create an object named answer1.4', {96expect_true(exists('answer1.4'))97})98test_that('Solution is incorrect', {99expect_equal(digest(answer1.4), 'c1f86f7430df7ddb256980ea6a3b57a4') # we hid the answer to the test here so you can't see it, but we can still run the test100})101print("Success!")102}103104test_1.5 <- function(){105test_that('Did not create an object named fruit_data_scaled', {106expect_true(exists("fruit_data_scaled"))107})108test_that('fruit_data_scaled does not contain the correct number of rows and/or columns.', {109expect_equal(dim(fruit_data_scaled), c(59, 11))110})111test_that('The fruit_name column in fruit_data_scaled should be of class factor.', {112expect_true(is.factor(fruit_data_scaled$fruit_name))113})114test_that('Columns in fruit_data_scaled contain incorrect values.', {115expect_equal(digest(int_round(sum(fruit_data_scaled$mass, na.rm = TRUE), 2)), '8c7433f4d278ef1e1e0f8d0ccb217614') # we hid the answer to the test here so you can't see it, but we can still run the test116})117test_that('The mass, height, color score, and width columns in fruit_data_Scaled should be scaled.', {118expect_equal(digest(int_round(sum(fruit_data_scaled$scaled_mass), 2)), '1473d70e5646a26de3c52aa1abd85b1f')119expect_equal(digest(int_round(sum(fruit_data_scaled$scaled_width), 2)), '1473d70e5646a26de3c52aa1abd85b1f')120expect_equal(digest(int_round(sum(fruit_data_scaled$scaled_height), 2)), '1473d70e5646a26de3c52aa1abd85b1f')121expect_equal(digest(int_round(sum(fruit_data_scaled$scaled_color_score), 2)), '1473d70e5646a26de3c52aa1abd85b1f')122})123print("Success!")124}125126test_1.6 <- function(){127test_that('Did not create an object named distance_44', {128expect_true(exists("distance_44"))129})130test_that('Did not create an object named distance_2', {131expect_true(exists("distance_2"))132})133test_that('distance_44 should be a distance.', {134expect_true('dist' %in% class(distance_44))135})136test_that('distance_2 should be a distance.', {137expect_true('dist' %in% class(distance_2))138})139test_that('distance_44 is incorrect.', {140expect_equal(digest(int_round(distance_2, 2)), '192b298ed4661ab6d9a4a193b2e60b49')141})142test_that('distance_2 is incorrect.', {143expect_equal(digest(int_round(distance_44, 2)), '78f799aab6957dffdfd2bfb504f8cab5')144})145print("Success!")146}147148test_2.0 <- function(){149test_that('Did not create an object named fruit_train', {150expect_true(exists("fruit_train"))151})152test_that('Did not create an object named fruit_test', {153expect_true(exists("fruit_test"))154})155test_that('fruit_train does not contain the correct number of rows and/or columns', {156expect_equal(dim(fruit_train), c(46, 7))157})158test_that('fruit_test does not contain the correct number of rows and/or columns',{159expect_equal(dim(fruit_test), c(13, 7))160})161test_that('fruit_train contains the wrong data', {162expect_equal(digest(int_round(sum(fruit_train$mass), 2)), 'a42fff2d173ac77a5198b1e8422cb9ba')163})164test_that('fruit_test contains the wrong data', {165expect_equal(digest(int_round(sum(fruit_test$mass), 2)), 'fcb11cb4ce2aa88708f4f5895d59abbe')166})167print("Success!")168}169170171test_2.1 <- function(){172test_that('Did not create an object named fruit_recipe', {173expect_true(exists("fruit_recipe"))174})175test_that('fruit_recipe should be a recipe.', {176expect_true('recipe' %in% class(fruit_recipe))177})178test_that('fruit_recipe contains the wrong columns', {179expect_true('color_score' %in% colnames(fruit_recipe$template))180expect_true('mass' %in% colnames(fruit_recipe$template))181expect_true('fruit_name' %in% colnames(fruit_recipe$template))182})183test_that('fruit_recipe contains the wrong data', {184expect_equal(digest(int_round(sum(fruit_recipe$template$mass), 2)), 'a42fff2d173ac77a5198b1e8422cb9ba')185expect_equal(digest(int_round(sum(fruit_recipe$template$color_score), 2)), '656cbb68c33ed8c769ed3fb3a423f886')186})187test_that('all_predictors() is not scaled and centered', {188expect_equal(digest(as.character(get_expr(fruit_recipe$steps[[1]]$terms))), 'f34b27deb5a8023de51b602e9aacf535')189expect_equal(digest(as.character(get_expr(fruit_recipe$steps[[2]]$terms))), 'f34b27deb5a8023de51b602e9aacf535')190})191test_that('fruit_name was not placed before predictors', {192expect_equal(digest(as.character(fruit_recipe$var_info$variable[3])), '1298acdeb848b96767603d30382d6aff')193})194print("Success!")195}196197test_2.2 <- function(){198test_that('Did not create an object named knn_spec', {199expect_true(exists("knn_spec"))200})201test_that('knn_spec should be a model specification', {202expect_true("model_spec" %in% class(knn_spec))203})204test_that('k is not 3', {205expect_equal(int_round(get_expr(knn_spec$args$neighbors), 0), 3)206})207test_that('weight_func is incorrect', {208expect_equal(digest(as.character(get_expr(knn_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')209})210test_that('set_engine is incorrect', {211expect_equal(digest(as.character(knn_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')212})213test_that('mode is incorrect', {214expect_equal(digest(as.character(knn_spec$mode)), 'f361ba6f6b32d068e56f61f53d35e26a')215})216test_that('Did not create an object named fruit_fit', {217expect_true(exists("fruit_fit"))218})219test_that('fruit_fit should be a workflow.', {220expect_true('workflow' %in% class(fruit_fit))221})222test_that('fruit_fit does not contain scaled data', {223expect_equal(digest(int_round(sum(fruit_fit$pre$mold$predictors$mass), 2)), '1473d70e5646a26de3c52aa1abd85b1f')224expect_equal(digest(int_round(sum(fruit_fit$pre$mold$predictors$color_score), 2)), '1473d70e5646a26de3c52aa1abd85b1f')225})226print("Success!")227}228229test_2.3 <- function(){230test_that('Did not create an object named fruit_test_predictions', {231expect_true(exists("fruit_test_predictions"))232})233test_that('fruit_test_predictions should be a tibble.', {234expect_true('tbl' %in% class(fruit_test_predictions))235})236test_that('fruit_test_predictions should contain the original data and the new prediction column', {237expect_equal(dim(fruit_test_predictions), c(13, 8))238expect_true('.pred_class' %in% colnames(fruit_test_predictions))239})240print("Success!")241}242243test_2.4 <- function(){244test_that('Did not create an object named fruit_prediction_accuracy', {245expect_true(exists("fruit_prediction_accuracy"))246})247test_that('fruit_prediction_accuracy should be a tibble', {248expect_true('tbl' %in% class(fruit_prediction_accuracy))249})250test_that('estimates are incorrect', {251expect_equal(digest(int_round(sum(fruit_prediction_accuracy$.estimate), 2)), '44865f1c212fb27ca7ab5b7154dcf398')252})253test_that('the estimator should be a multiclass classification', {254expect_true('multiclass' %in% fruit_prediction_accuracy$.estimator)255})256print("Success!")257}258259test_2.5 <- function(){260test_that('Did not create an object named fruit_mat', {261expect_true(exists("fruit_mat"))262})263test_that('fruit_mat is not a confusion matrix', {264expect_true('conf_mat' %in% class(fruit_mat))265})266test_that('Number of observations is incorrect', {267expect_equal(digest(int_round(sum(as.tibble(fruit_mat$table)[3]), 2)), '306a937dfa0335e74514e4c6044755f6')268})269print("Success!")270}271272273test_2.6 <- function(){274test_that('Did not create an object named answer2.6', {275expect_true(exists("answer2.6"))276})277test_that('Answer is incorrect', {278expect_equal(digest(answer2.6), 'c1f86f7430df7ddb256980ea6a3b57a4')279})280print("Success!")281}282283test_3.1 <- function(){284test_that('Did not create an object named fruit_vfold', {285expect_true(exists("fruit_vfold"))286})287test_that('fruit_vfold is not a cross validation object',{288expect_true('vfold_cv' %in% class(fruit_vfold))289})290test_that('fruit_vfold does not contain 5 folds', {291expect_equal(int_round(length(fruit_vfold$id), 0), 5)292})293test_that('fruit_vfold contains the incorrect data', {294expect_equal(dim(fruit_vfold), c(5, 2))295})296test_that('fruit_vfold does not use the training data', {297expect_equal(digest(int_round(sum(fruit_vfold$splits[[1]]$data$color_score), 2)), '656cbb68c33ed8c769ed3fb3a423f886')298expect_equal(digest(int_round(sum(fruit_vfold$splits[[1]]$data$mass), 2)), 'a42fff2d173ac77a5198b1e8422cb9ba')299})300test_that('strata argument is not fruit_name', {301expect_equal(digest(int_round(sum(fruit_vfold$splits[[1]]$in_id), 2)), 'df9b1bae6656d96dfbe896782bd9de05')302expect_equal(digest(int_round(sum(fruit_vfold$splits[[2]]$in_id), 2)), 'd01f1e59ae3a6b2db6831e601606b1c0')303expect_equal(digest(int_round(sum(fruit_vfold$splits[[3]]$in_id), 2)), '71321611fabe5aee0df74bb96fe3a545')304expect_equal(digest(int_round(sum(fruit_vfold$splits[[4]]$in_id), 2)), 'd01f1e59ae3a6b2db6831e601606b1c0')305expect_equal(digest(int_round(sum(fruit_vfold$splits[[5]]$in_id), 2)), '28db4e404a1c7e394adb8f9a21711424')306})307print("Success!")308}309310test_3.2 <- function(){311test_that('Did not create an object named fruit_resample_fit', {312expect_true(exists("fruit_resample_fit"))313})314test_that('fruit_resample_fit is not a resample_result', {315expect_true('resample_results' %in% class(fruit_resample_fit))316})317test_that('fruit_resample_fit contains the incorrect data', {318expect_equal(dim(fruit_resample_fit), c(5, 4))319})320test_that('number of splits is not 5' ,{321expect_equal(int_round(length(fruit_resample_fit$splits), 0), 5)322})323test_that('fruit_vfold should contain 5 folds', {324expect_equal(int_round(length(fruit_vfold$id), 0), 5)325})326test_that('fruit_vfold contains the incorrect data', {327expect_equal(dim(fruit_vfold), c(5, 2))328})329print("Success!")330}331332test_3.3 <- function(){333test_that('Did not create an object named fruit_metrics', {334expect_true(exists("fruit_metrics"))335})336test_that('fruit_metrics contains the wrong data', {337expect_equal(dim(fruit_metrics), c(2, 5))338expect_true('mean' %in% colnames(fruit_metrics))339expect_true('std_err' %in% colnames(fruit_metrics))340expect_equal(digest(int_round(sum(fruit_metrics$mean), 2)), '3bb12916e7f6fda4645dd4ecaedb76b9')341expect_equal(digest(int_round(sum(fruit_metrics$std_err), 2)), '8eaca7c9b35d05ab15c9125bc92372fa')342expect_equal(digest(int_round(sum(fruit_metrics$n), 2)), 'b6a6227038bf9be67533a45a6511cc7e')343})344print("Success!")345}346347test_4.0 <- function(){348test_that('Did not create an object named knn_tune', {349expect_true(exists("knn_tune"))350})351test_that('knn_tune should be a model specification', {352expect_true("model_spec" %in% class(knn_tune))353})354test_that('k is not set to tune', {355expect_equal(as.character(get_expr(knn_tune$args$neighbors)), 'tune')356})357test_that('weight_func is incorrect', {358expect_equal(digest(as.character(get_expr(knn_tune$args$weight_func))), '989de78e881829b4499af3610dfe54fd')359})360test_that('set_engine is incorrect', {361expect_equal(digest(as.character(knn_tune$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')362})363test_that('mode is incorrect', {364expect_equal(digest(as.character(knn_tune$mode)), 'f361ba6f6b32d068e56f61f53d35e26a')365})366print("Success!")367}368369test_4.1 <- function(){370test_that('Did not create an object called knn_results',{371expect_true(exists("knn_results"))372})373test_that('knn_results should be a tibble', {374expect_true('tbl' %in% class(knn_results))375})376test_that('knn_results does not contain the correct data',{377expect_equal(dim(knn_results), c(20, 7))378expect_equal(digest(int_round(sum(knn_results$neighbors), 2)), 'bc0bb1b780c5a2b3fbe18f1017288655')379expect_equal(digest(int_round(sum(knn_results$mean), 2)), '2fbed5c22f5fcb638e9cad6f0d588e47')380expect_equal(digest(int_round(sum(knn_results$std_err), 2)), '6c3a3556917f12517be89f353d7b93ff')381})382test_that('grid is not set to 10', {383expect_equal(int_round(length(unique(knn_results$.config)), 0), 10)384})385print("Success!")386}387388389test_4.2 <- function(){390test_that('Did not create an object called accuracies', {391expect_true(exists("accuracies"))392})393test_that('accuracies .metric column should only contain accuracy', {394expect_true(unique(accuracies$.metric) == "accuracy")395})396properties <- c(accuracy_versus_k$layers[[1]]$mapping, accuracy_versus_k$mapping)397labels <- accuracy_versus_k$labels398test_that('Did not create a plot named accuracy_versus_k', {399expect_true(exists("accuracy_versus_k"))400})401test_that('neighbors should be on the x-axis.', {402expect_true("neighbors" == rlang::get_expr(properties$x))403})404test_that('mean should be on the y-axis.', {405expect_true("mean" == rlang::get_expr(properties$y))406})407test_that('accuracy_versus_k should be a scatter/line plot.', {408expect_true("GeomPoint" %in% c(class(accuracy_versus_k$layers[[1]]$geom)))409expect_true("GeomLine" %in% c(class(accuracy_versus_k$layers[[2]]$geom)))410})411print("Success!")412}413414415