Path: blob/master/2021-fall/materials/tutorial_06/tests_tutorial_06.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 = 2){22x = x*10^digits23xint = as.integer(x)24xint1 = xint + 1L25if (abs(xint - x) < abs(xint1 - x)){26return(xint)27}28else {29return(xint1)30}31}32# -3334test_0.1 <- function(){35test_that('Solution is incorrect', {36expect_true(exists('answer0.1'))37expect_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 test3839})40print("Success!")41}4243test_1.0 <- function(){44test_that('Did not create an object named fruit_data', {45expect_true(exists("fruit_data"))46})47test_that('fruit_data does not contain the correct number of rows and/or columns.', {48expect_equal(dim(fruit_data), c(59, 7))49})50test_that('The fruit_name column in fruit_data should be of class factor.', {51expect_true(is.factor(fruit_data$fruit_name))52})53test_that('Columns in fruit_data contain incorrect values.', {54expect_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 test55})56test_that('as_factor() function not used.',{57expect_equal(c(levels(fruit_data$fruit_name)), c("apple", "mandarin", "orange", "lemon"))58})59print("Success!")60}6162test_1.1 <- function(){63test_that('Solution is incorrect', {64expect_true(exists('answer1.1'))65expect_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 test6667})68print("Success!")69}7071test_1.0.1 <- function(){72test_that('Solution is incorrect', {73expect_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 test74})75print("Success!")76}7778test_1.2 <- function(){79test_that('Did not create an object named fruit_dist_2', {80expect_true(exists("fruit_dist_2"))81})82test_that('fruit_dist_2 is incorrect.', {83expect_equal(digest(int_round(fruit_dist_2)), 'd8ba459e9b95d6bb43cdcb8acd275179')84})85print("Success!")86}8788test_1.3 <- function(){89test_that('Did not create an object named fruit_dist_44', {90expect_true(exists("fruit_dist_44"))91})92test_that('fruit_dist_44 is incorrect.', {93expect_equal(digest(int_round(fruit_dist_44)), 'ea07cf8b74030ff04b56ac69dd094adc')94})95print("Success!")96}9798test_1.4 <- function(){99test_that('Solution is incorrect', {100expect_true(exists('answer1.4'))101expect_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 test102})103print("Success!")104}105106# # +107test_1.5 <- function(){108test_that('Did not create an object named fruit_data_scaled', {109expect_true(exists("fruit_data_scaled"))110})111test_that('fruit_data_scaled does not contain the correct number of rows and/or columns.', {112expect_equal(dim(fruit_data_scaled), c(59, 5))113})114test_that('The fruit_name column in fruit_data_scaled should be of class factor.', {115expect_true(is.factor(fruit_data_scaled$fruit_name))116})117test_that('Columns in fruit_data_scaled are not centered.', {118expect_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 test119expect_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 test120expect_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 test121expect_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 test122123})124test_that('Columns in fruit_data_scaled are not scaled.', {125expect_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 test126expect_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 test127expect_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 test128expect_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 test129})130test_that('Did not create an object named fruit_data_recipe',{131expect_true(exists("fruit_data_recipe"))132})133test_that('fruit_data_recipe does not contain the correct predictors and outcome variables', {134expect_true('color_score' %in% fruit_data_recipe$var_info$variable)135expect_true('mass' %in% fruit_data_recipe$var_info$variable)136expect_true('height' %in% fruit_data_recipe$var_info$variable)137expect_equal(digest(as.character(fruit_data_recipe$var_info$variable[[5]])), '1298acdeb848b96767603d30382d6aff')138})139print("Success!")140}141# -142143test_1.6 <- function(){144test_that('Did not create an object named distance_44', {145expect_true(exists("distance_44"))146})147test_that('Did not create an object named distance_2', {148expect_true(exists("distance_2"))149})150test_that('distance_44 should be a distance.', {151expect_true('dist' %in% class(distance_44))152})153test_that('distance_2 should be a distance.', {154expect_true('dist' %in% class(distance_2))155})156test_that('distance_44 is incorrect.', {157expect_equal(digest(int_round(distance_44)), '78f799aab6957dffdfd2bfb504f8cab5')158})159test_that('distance_2 is incorrect.', {160expect_equal(digest(int_round(distance_2)), '192b298ed4661ab6d9a4a193b2e60b49')161})162print("Success!")163}164165test_1.7 <- function(){166properties <- c(fruit_plot$layers[[1]]$mapping, fruit_plot$mapping)167labels <- fruit_plot$labels168test_that('Did not create a plot named fruit_plot', {169expect_true(exists("fruit_plot"))170})171test_that('scaled_mass should be on the x-axis.', {172expect_true("mass" == rlang::get_expr(properties$x))173})174test_that('scaled_color should be on the y-axis.', {175expect_true("color_score" == rlang::get_expr(properties$y))176})177test_that('fruit_name should be mapped to colour', {178expect_true("fruit_name" == rlang::get_expr(properties$colour))179})180test_that('fruit_plot should be a scatter plot.', {181expect_true("GeomPoint" %in% c(class(fruit_plot$layers[[1]]$geom)))182})183test_that('Labels on the axes should be descriptive and human readable.', {184expect_false((labels$y) == 'color_score')185expect_false((labels$x) == 'mass')186expect_false((labels$colour) == 'fruit_name')187})188print("Success!")189}190191test_1.9 <- function(){192test_that('Did not create an object named knn_spec', {193expect_true(exists("knn_spec"))194})195test_that('k is incorrect', {196expect_equal(digest(int_round(get_expr(knn_spec$args$neighbors))), '75e76f8b41d0944779e119afd3513844')197})198test_that('weight_func is incorrect', {199expect_equal(digest(as.character(get_expr(knn_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')200})201test_that('set_engine is incorrect', {202expect_equal(digest(as.character(knn_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')203})204test_that('mode is incorrect', {205expect_equal(digest(as.character(knn_spec$mode)), 'f361ba6f6b32d068e56f61f53d35e26a')206})207test_that('fruit_data_recipe_2 does not exist', {208expect_true(exists('fruit_data_recipe_2'))209})210test_that('fruit_recipe_2 does not contain the correct predictors and outcome variables', {211expect_true('color_score' %in% fruit_data_recipe_2$var_info$variable)212expect_true('mass' %in% fruit_data_recipe_2$var_info$variable)213expect_equal(digest(as.character(fruit_data_recipe_2$var_info$variable[[3]])), '1298acdeb848b96767603d30382d6aff')214})215test_that('Did not create an object named fruit_fit', {216expect_true(exists("fruit_fit"))217})218test_that('fruit_fit should be a fit model.', {219expect_true('workflow' %in% class(fruit_fit))220})221test_that('fruit_fit does not contain knn_spec', {222expect_equal(digest(int_round(get_expr(fruit_fit$fit$actions$model$spec$args$neighbors))), '75e76f8b41d0944779e119afd3513844')223expect_equal(digest(as.character(get_expr(fruit_fit$fit$actions$model$spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')224expect_equal(digest(fruit_fit$fit$actions$model$spec$mode), 'f361ba6f6b32d068e56f61f53d35e26a')225})226print("Success!")227}228229test_1.10 <- function(){230test_that('Did not create an object called new_fruit', {231expect_true(exists('new_fruit'))232})233test_that('new_fruit does not contain the correct data', {234expect_equal(dim(new_fruit), c(1, 2))235expect_equal(digest(int_round(new_fruit$mass, 2)), '829aba66b0d64feac09b067c4cce133c')236expect_equal(digest(int_round(new_fruit$color_score, 2)), 'e15efc0db45ebdee4bebbca843987014')237})238test_that('Prediction is incorrect', {239expect_equal(digest(as.character(fruit_predicted)), 'd19d62a873f08af0488f0df720cfd293')240})241print("Success!")242}243244test_1.12 <- function(){245test_that('fruit_all_predicted does not exist',{246expect_true(exists("fruit_all_predicted"))247})248test_that('prediction is incorrect', {249expect_equal(digest(as.character(fruit_all_predicted$.pred_class)), '5e55351cc3517b5b1031d95040455de5')250})251print("Success!")252}253254test_2.1 <- function(){255test_that('answer2.1 does not exist',{256expect_true(exists("answer2.1"))257})258test_that('Answer is incorrect', {259expect_equal(digest(answer2.1), '475bf9280aab63a82af60791302736f6')260})261print("Success!")262}263264265