Path: blob/master/2021-summer/materials/tutorial_06/tests_tutorial_06.R
2051 views
# # +1library(testthat)2library(digest)3library(rlang)45#' Round double to precise integer6#'7#' `int_round` works to create an integer corresponding to a number that is8#' tested up to a particular decimal point of precision. This is useful when9#' there is a need to compare a numeric value using hashes.10#'11#' @param x Double vector of length one.12#' @param digits Double vector of length one to specify decimal point of precision. Negative numbers can be used to specifying significant digits > 0.1.13#'14#' @return Integer vector of length one corresponding to a particular decimal point of precision.15#'16#' @examples17#' # to get an integer up to two decimals of precision from 234.5678918#' int_round(234.56789, 2)19#'20#' to get an integer rounded to the hundred digit from 234.5678921#' int_round(234.56789, -2)2223int_round <- function(x, digits = 2){24x = x*10^digits25xint = as.integer(x)26xint1 = xint + 1L27if (abs(xint - x) < abs(xint1 - x)){28return(xint)29}30else {31return(xint1)32}33}34# -3536test_0.1 <- function(){37test_that('Solution is incorrect', {38expect_true(exists('answer0.1'))39expect_equal(digest(answer0.1), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test4041})42print("Success!")43}4445test_1.0 <- function(){46test_that('Did not create an object named fruit_data', {47expect_true(exists("fruit_data"))48})49test_that('fruit_data does not contain the correct number of rows and/or columns.', {50expect_equal(dim(fruit_data), c(59, 7))51})52test_that('The fruit_name column in fruit_data should be of class factor.', {53expect_true(is.factor(fruit_data$fruit_name))54})55test_that('Columns in fruit_data contain incorrect values.', {56expect_equal(digest(int_round(fruit_data$mass)), 'b26195f974598925edc40c2377152892') # we hid the answer to the test here so you can't see it, but we can still run the test57})58test_that('as_factor() function not used.',{59expect_equal(c(levels(fruit_data$fruit_name)), c("apple", "mandarin", "orange", "lemon"))60})61print("Success!")62}6364test_1.1 <- function(){65test_that('Solution is incorrect', {66expect_true(exists('answer1.1'))67expect_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 test6869})70print("Success!")71}7273test_1.0.1 <- function(){74test_that('Solution is incorrect', {75expect_equal(digest(answer1.0.1), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test76})77print("Success!")78}7980test_1.2 <- function(){81test_that('Did not create an object named fruit_dist_2', {82expect_true(exists("fruit_dist_2"))83})84test_that('fruit_dist_2 is incorrect.', {85expect_equal(digest(int_round(fruit_dist_2)), 'd8ba459e9b95d6bb43cdcb8acd275179')86})87print("Success!")88}8990test_1.3 <- function(){91test_that('Did not create an object named fruit_dist_44', {92expect_true(exists("fruit_dist_44"))93})94test_that('fruit_dist_44 is incorrect.', {95expect_equal(digest(int_round(fruit_dist_44)), 'ea07cf8b74030ff04b56ac69dd094adc')96})97print("Success!")98}99100test_1.4 <- function(){101test_that('Solution is incorrect', {102expect_true(exists('answer1.4'))103expect_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 test104})105print("Success!")106}107108# # +109test_1.5 <- function(){110test_that('Did not create an object named fruit_data_scaled', {111expect_true(exists("fruit_data_scaled"))112})113test_that('fruit_data_scaled does not contain the correct number of rows and/or columns.', {114expect_equal(dim(fruit_data_scaled), c(59, 5))115})116test_that('The fruit_name column in fruit_data_scaled should be of class factor.', {117expect_true(is.factor(fruit_data_scaled$fruit_name))118})119test_that('Columns in fruit_data_scaled are not centered.', {120expect_equal(digest(int_round(mean(fruit_data_scaled$mass, na.rm = TRUE))), '1473d70e5646a26de3c52aa1abd85b1f') # we hid the answer to the test here so you can't see it, but we can still run the test121expect_equal(digest(int_round(mean(fruit_data_scaled$height, na.rm = TRUE))), '1473d70e5646a26de3c52aa1abd85b1f') # we hid the answer to the test here so you can't see it, but we can still run the test122expect_equal(digest(int_round(mean(fruit_data_scaled$width, na.rm = TRUE))), '1473d70e5646a26de3c52aa1abd85b1f') # we hid the answer to the test here so you can't see it, but we can still run the test123expect_equal(digest(int_round(mean(fruit_data_scaled$color_score, na.rm = TRUE))), '1473d70e5646a26de3c52aa1abd85b1f') # we hid the answer to the test here so you can't see it, but we can still run the test124125})126test_that('Columns in fruit_data_scaled are not scaled.', {127expect_equal(digest(int_round(sd(fruit_data_scaled$mass, na.rm = TRUE))), '5d6e7fe43b3b73e5fd2961d5162486fa') # we hid the answer to the test here so you can't see it, but we can still run the test128expect_equal(digest(int_round(sd(fruit_data_scaled$height, na.rm = TRUE))), '5d6e7fe43b3b73e5fd2961d5162486fa') # we hid the answer to the test here so you can't see it, but we can still run the test129expect_equal(digest(int_round(sd(fruit_data_scaled$width, na.rm = TRUE))), '5d6e7fe43b3b73e5fd2961d5162486fa') # we hid the answer to the test here so you can't see it, but we can still run the test130expect_equal(digest(int_round(sd(fruit_data_scaled$color_score, na.rm = TRUE))), '5d6e7fe43b3b73e5fd2961d5162486fa') # we hid the answer to the test here so you can't see it, but we can still run the test131})132test_that('Did not create an object named fruit_data_recipe',{133expect_true(exists("fruit_data_recipe"))134})135test_that('fruit_data_recipe does not contain the correct predictors and outcome variables', {136expect_true('color_score' %in% fruit_data_recipe$var_info$variable)137expect_true('mass' %in% fruit_data_recipe$var_info$variable)138expect_true('height' %in% fruit_data_recipe$var_info$variable)139expect_equal(digest(as.character(fruit_data_recipe$var_info$variable[[5]])), '1298acdeb848b96767603d30382d6aff')140})141print("Success!")142}143# -144145test_1.6 <- function(){146test_that('Did not create an object named distance_44', {147expect_true(exists("distance_44"))148})149test_that('Did not create an object named distance_2', {150expect_true(exists("distance_2"))151})152test_that('distance_44 should be a distance.', {153expect_true('dist' %in% class(distance_44))154})155test_that('distance_2 should be a distance.', {156expect_true('dist' %in% class(distance_2))157})158test_that('distance_44 is incorrect.', {159expect_equal(digest(int_round(distance_44)), '78f799aab6957dffdfd2bfb504f8cab5')160})161test_that('distance_2 is incorrect.', {162expect_equal(digest(int_round(distance_2)), '192b298ed4661ab6d9a4a193b2e60b49')163})164print("Success!")165}166167test_1.7 <- function(){168properties <- c(fruit_plot$layers[[1]]$mapping, fruit_plot$mapping)169labels <- fruit_plot$labels170test_that('Did not create a plot named fruit_plot', {171expect_true(exists("fruit_plot"))172})173test_that('scaled_mass should be on the x-axis.', {174expect_true("mass" == rlang::get_expr(properties$x))175})176test_that('scaled_color should be on the y-axis.', {177expect_true("color_score" == rlang::get_expr(properties$y))178})179test_that('fruit_name should be mapped to colour', {180expect_true("fruit_name" == rlang::get_expr(properties$colour))181})182test_that('fruit_plot should be a scatter plot.', {183expect_true("GeomPoint" %in% c(class(fruit_plot$layers[[1]]$geom)))184})185test_that('Labels on the axes should be descriptive and human readable.', {186expect_false((labels$y) == 'color_score')187expect_false((labels$x) == 'mass')188expect_false((labels$colour) == 'fruit_name')189})190print("Success!")191}192193test_1.9 <- function(){194test_that('Did not create an object named knn_spec', {195expect_true(exists("knn_spec"))196})197test_that('k is incorrect', {198expect_equal(digest(int_round(get_expr(knn_spec$args$neighbors))), '75e76f8b41d0944779e119afd3513844')199})200test_that('weight_func is incorrect', {201expect_equal(digest(as.character(get_expr(knn_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')202})203test_that('set_engine is incorrect', {204expect_equal(digest(as.character(knn_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')205})206test_that('mode is incorrect', {207expect_equal(digest(as.character(knn_spec$mode)), 'f361ba6f6b32d068e56f61f53d35e26a')208})209test_that('fruit_data_recipe_2 does not exist', {210expect_true(exists('fruit_data_recipe_2'))211})212test_that('fruit_recipe_2 does not contain the correct predictors and outcome variables', {213expect_true('color_score' %in% fruit_data_recipe_2$var_info$variable)214expect_true('mass' %in% fruit_data_recipe_2$var_info$variable)215expect_equal(digest(as.character(fruit_data_recipe_2$var_info$variable[[3]])), '1298acdeb848b96767603d30382d6aff')216})217test_that('Did not create an object named fruit_fit', {218expect_true(exists("fruit_fit"))219})220test_that('fruit_fit should be a fit model.', {221expect_true('workflow' %in% class(fruit_fit))222})223test_that('fruit_fit does not contain knn_spec', {224expect_equal(digest(int_round(get_expr(fruit_fit$fit$actions$model$spec$args$neighbors))), '75e76f8b41d0944779e119afd3513844')225expect_equal(digest(as.character(get_expr(fruit_fit$fit$actions$model$spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')226expect_equal(digest(fruit_fit$fit$actions$model$spec$mode), 'f361ba6f6b32d068e56f61f53d35e26a')227})228print("Success!")229}230231test_1.10 <- function(){232test_that('Did not create an object called new_fruit', {233expect_true(exists('new_fruit'))234})235test_that('new_fruit does not contain the correct data', {236expect_equal(dim(new_fruit), c(1, 2))237expect_equal(digest(int_round(new_fruit$mass, 2)), '829aba66b0d64feac09b067c4cce133c')238expect_equal(digest(int_round(new_fruit$color_score, 2)), 'e15efc0db45ebdee4bebbca843987014')239})240test_that('Prediction is incorrect', {241expect_equal(digest(as.character(fruit_predicted)), 'd19d62a873f08af0488f0df720cfd293')242})243print("Success!")244}245246test_1.12 <- function(){247test_that('fruit_all_predicted does not exist',{248expect_true(exists("fruit_all_predicted"))249})250test_that('prediction is incorrect', {251expect_equal(digest(as.character(fruit_all_predicted$.pred_class)), '5e55351cc3517b5b1031d95040455de5')252})253print("Success!")254}255256test_2.1 <- function(){257test_that('answer2.1 does not exist',{258expect_true(exists("answer2.1"))259})260test_that('Answer is incorrect', {261expect_equal(digest(answer2.1), '475bf9280aab63a82af60791302736f6')262})263print("Success!")264}265266267