Path: blob/master/2020-fall/materials/worksheet_07/tests_worksheet_07.R
2710 views
library(testthat)1library(digest)2library(rlang)34int_round <- function(x, digits){5x = x*10^digits6xint = as.integer(x)7xint1 = xint + 1L8if (abs(xint - x) < abs(xint1 - x)){9return(xint)10}11else {12return(xint1)13}14}1516test_2.0 <- function(){17test_that('Did not create an object named fruit_train', {18expect_true(exists("fruit_train"))19})20test_that('Did not create an object named fruit_test', {21expect_true(exists("fruit_test"))22})23test_that('fruit_train does not contain the correct number of rows and/or columns', {24expect_equal(dim(fruit_train), c(46, 7))25})26test_that('fruit_test does not contain the correct number of rows and/or columns',{27expect_equal(dim(fruit_test), c(13, 7))28})29test_that('fruit_train contains the wrong data', {30expect_equal(digest(int_round(sum(fruit_train$mass), 2)), 'a42fff2d173ac77a5198b1e8422cb9ba')31})32test_that('fruit_test contains the wrong data', {33expect_equal(digest(int_round(sum(fruit_test$mass), 2)), 'fcb11cb4ce2aa88708f4f5895d59abbe')34})35print("Success!")36}373839test_2.1 <- function(){40test_that('Did not create an object named fruit_recipe', {41expect_true(exists("fruit_recipe"))42})43test_that('fruit_recipe should be a recipe.', {44expect_true('recipe' %in% class(fruit_recipe))45})46test_that('fruit_recipe contains the wrong columns', {47expect_true('color_score' %in% colnames(fruit_recipe$template))48expect_true('mass' %in% colnames(fruit_recipe$template))49expect_true('fruit_name' %in% colnames(fruit_recipe$template))50})51test_that('fruit_recipe contains the wrong data', {52expect_equal(digest(int_round(sum(fruit_recipe$template$mass), 2)), 'a42fff2d173ac77a5198b1e8422cb9ba')53expect_equal(digest(int_round(sum(fruit_recipe$template$color_score), 2)), '656cbb68c33ed8c769ed3fb3a423f886')54})55print("Success!")56}5758test_2.2 <- function(){59test_that('Did not create an object named knn_spec', {60expect_true(exists("knn_spec"))61})62test_that('knn_spec should be a model specification', {63expect_true("model_spec" %in% class(knn_spec))64})65test_that('k is not 3', {66expect_equal(int_round(get_expr(knn_spec$args$neighbors), 0), 3)67})68test_that('weight_func is incorrect', {69expect_equal(digest(as.character(get_expr(knn_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')70})71test_that('set_engine is incorrect', {72expect_equal(digest(as.character(knn_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')73})74test_that('mode is incorrect', {75expect_equal(digest(as.character(knn_spec$mode)), 'f361ba6f6b32d068e56f61f53d35e26a')76})77test_that('Did not create an object named fruit_fit', {78expect_true(exists("fruit_fit"))79})80test_that('fruit_fit should be a workflow.', {81expect_true('workflow' %in% class(fruit_fit))82})83test_that('fruit_fit does not contain scaled data', {84expect_equal(digest(int_round(sum(fruit_fit$pre$mold$predictors$mass), 2)), '1473d70e5646a26de3c52aa1abd85b1f')85expect_equal(digest(int_round(sum(fruit_fit$pre$mold$predictors$color_score), 2)), '1473d70e5646a26de3c52aa1abd85b1f')86})87print("Success!")88}8990test_2.3 <- function(){91test_that('Did not create an object named fruit_test_predictions', {92expect_true(exists("fruit_test_predictions"))93})94test_that('fruit_test_predictions should be a tibble.', {95expect_true('tbl' %in% class(fruit_test_predictions))96})97test_that('fruit_test_predictions should contain the original data and the new prediction column', {98expect_equal(dim(fruit_test_predictions), c(13, 8))99expect_true('.pred_class' %in% colnames(fruit_test_predictions))100})101print("Success!")102}103104test_2.4 <- function(){105test_that('Did not create an object named fruit_prediction_accuracy', {106expect_true(exists("fruit_prediction_accuracy"))107})108test_that('fruit_prediction_accuracy should be a tibble', {109expect_true('tbl' %in% class(fruit_prediction_accuracy))110})111test_that('estimates are incorrect', {112expect_equal(digest(int_round(sum(fruit_prediction_accuracy$.estimate), 2)), '44865f1c212fb27ca7ab5b7154dcf398')113})114test_that('the estimator should be a multiclass classification', {115expect_true('multiclass' %in% fruit_prediction_accuracy$.estimator)116})117print("Success!")118}119120test_2.5 <- function(){121test_that('Did not create an object named fruit_mat', {122expect_true(exists("fruit_mat"))123})124test_that('fruit_mat is not a confusion matrix', {125expect_true('conf_mat' %in% class(fruit_mat))126})127test_that('Number of observations is incorrect', {128expect_equal(digest(int_round(sum(as.tibble(fruit_mat$table)[3]), 2)), '306a937dfa0335e74514e4c6044755f6')129})130print("Success!")131}132133134test_2.6 <- function(){135test_that('Did not create an object named answer2.6', {136expect_true(exists("answer2.6"))137})138test_that('Answer is incorrect', {139expect_equal(digest(answer2.6), 'c1f86f7430df7ddb256980ea6a3b57a4')140})141print("Success!")142}143144test_3.1 <- function(){145test_that('Did not create an object named fruit_vfold', {146expect_true(exists("fruit_vfold"))147})148test_that('fruit_vfold is not a cross validation object',{149expect_true('vfold_cv' %in% class(fruit_vfold))150})151test_that('fruit_vfold does not contain 5 folds', {152expect_equal(int_round(length(fruit_vfold$id), 0), 5)153})154test_that('fruit_vfold contains the incorrect data', {155expect_equal(dim(fruit_vfold), c(5, 2))156})157print("Success!")158}159160test_3.2 <- function(){161test_that('Did not create an object named fruit_resample_fit', {162expect_true(exists("fruit_resample_fit"))163})164test_that('fruit_resample_fit is not a resample_result', {165expect_true('resample_results' %in% class(fruit_resample_fit))166})167test_that('fruit_resample_fit contains the incorrect data', {168expect_equal(dim(fruit_resample_fit), c(5, 4))169})170test_that('number of splits is not 5' ,{171expect_equal(int_round(length(fruit_resample_fit$splits), 0), 5)172})173test_that('fruit_vfold should contain 5 folds', {174expect_equal(int_round(length(fruit_vfold$id), 0), 5)175})176test_that('fruit_vfold contains the incorrect data', {177expect_equal(dim(fruit_vfold), c(5, 2))178})179print("Success!")180}181182test_3.3 <- function(){183test_that('Did not create an object named fruit_metrics', {184expect_true(exists("fruit_metrics"))185})186test_that('fruit_metrics contains the wrong data', {187expect_equal(dim(fruit_metrics), c(2, 5))188expect_true('mean' %in% colnames(fruit_metrics))189expect_true('std_err' %in% colnames(fruit_metrics))190expect_equal(digest(int_round(sum(fruit_metrics$mean), 2)), '99496b18802c49d6571120c16308addc')191expect_equal(digest(int_round(sum(fruit_metrics$std_err), 2)), '234a2a5581872457b9fe1187d1616b13')192expect_equal(digest(int_round(sum(fruit_metrics$n), 2)), 'b6a6227038bf9be67533a45a6511cc7e')193})194print("Success!")195}196197test_4.0 <- function(){198test_that('Did not create an object named knn_tune', {199expect_true(exists("knn_tune"))200})201test_that('knn_tune should be a model specification', {202expect_true("model_spec" %in% class(knn_tune))203})204test_that('k is not set to tune', {205expect_equal(as.character(get_expr(knn_tune$args$neighbors)), 'tune')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})216print("Success!")217}218219test_4.1 <- function(){220test_that('Did not create an object called knn_results',{221expect_true(exists("knn_results"))222})223test_that('knn_results should be a tibble', {224expect_true('tbl' %in% class(knn_results))225})226test_that('knn_results does not contain the correct data',{227expect_equal(dim(knn_results), c(20, 7))228expect_equal(digest(int_round(sum(knn_results$neighbors), 2)), 'bc0bb1b780c5a2b3fbe18f1017288655')229expect_equal(digest(int_round(sum(knn_results$mean), 2)), 'e948a7acb8065d6583060c83b763e871')230expect_equal(digest(int_round(sum(knn_results$std_err), 2)), '2af87dfdb66a77e47ec325e40c275f91')231})232test_that('grid is not set to 10', {233expect_equal(int_round(length(unique(knn_results$.config)), 0), 10)234})235print("Success!")236}237238239test_4.2 <- function(){240test_that('Did not create an object called accuracies', {241expect_true(exists("accuracies"))242})243test_that('accuracies .metric column should only contain accuracy', {244expect_true(unique(accuracies$.metric) == "accuracy")245})246properties <- c(accuracy_versus_k$layers[[1]]$mapping, accuracy_versus_k$mapping)247labels <- accuracy_versus_k$labels248test_that('Did not create a plot named accuracy_versus_k', {249expect_true(exists("accuracy_versus_k"))250})251test_that('neighbors should be on the x-axis.', {252expect_true("neighbors" == rlang::get_expr(properties$x))253})254test_that('mean should be on the y-axis.', {255expect_true("mean" == rlang::get_expr(properties$y))256})257test_that('accuracy_versus_k should be a scatter/line plot.', {258expect_true("GeomPoint" %in% c(class(accuracy_versus_k$layers[[1]]$geom)))259expect_true("GeomLine" %in% c(class(accuracy_versus_k$layers[[2]]$geom)))260})261print("Success!")262}263264265