Path: blob/master/2019-fall/materials/worksheet_07/tests_worksheet_07.R
2707 views
library(testthat)1library(digest)23test_0.1 <- function(){4test_that('Solution is incorrect', {5expect_that(exists('answer1'), is_true())6expect_equal(digest(answer1), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test78})9print("Success!")10}1112test_1.0 <- function(){13test_that('Did not create an object named fruit_data', {14expect_true(exists("fruit_data"))15})16test_that('fruit_data does not contain the correct number of rows and/or columns.', {17expect_equal(dim(fruit_data), c(59, 7))18})19test_that('The fruit_name column in fruit_data should be of class factor.', {20expect_true(is.factor(fruit_data$fruit_name))21})22test_that('Columns in fruit_data contain incorrect values.', {23expect_equal(digest(as.numeric(sum(fruit_data$mass, na.rm = TRUE))), '351bee4c830e886e0f3a17749569fb33') # we hid the answer to the test here so you can't see it, but we can still run the test24})25print("Success!")26}2728test_1.1 <- function(){29test_that('Solution is incorrect', {30expect_that(exists('answer1.1'), is_true())31expect_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 test3233})34print("Success!")35}3637test_1.2 <- function(){38test_that('Did not create an object named fruit_dist_2', {39expect_true(exists("fruit_dist_2"))40})41test_that('fruit_dist_2 should be a distance.', {42expect_true('dist' %in% class(fruit_dist_2))43})44test_that('fruit_dist_2 is incorrect.', {45expect_equal(digest(fruit_dist_2), 'bc63ef43e7c7349f09aca14a55e2c9c0')46})47print("Success!")48}4950test_1.3 <- function(){51test_that('Did not create an object named fruit_dist_44', {52expect_true(exists("fruit_dist_44"))53})54test_that('fruit_dist_44 should be a distance.', {55expect_true('dist' %in% class(fruit_dist_44))56})57test_that('fruit_dist_44 is incorrect.', {58expect_equal(digest(fruit_dist_44), 'd775055ad9a05350f4548e1dbe872297')59})60print("Success!")61}626364test_1.5 <- function(){65test_that('Did not create an object named fruit_data', {66expect_true(exists("fruit_data"))67})68test_that('fruit_data does not contain the correct number of rows and/or columns.', {69expect_equal(dim(fruit_data), c(59, 11))70})71test_that('The fruit_name column in fruit_data should be of class factor.', {72expect_true(is.factor(fruit_data$fruit_name))73})74test_that('Columns in fruit_data contain incorrect values.', {75expect_equal(digest(as.numeric(sum(fruit_data$mass, na.rm = TRUE))), '351bee4c830e886e0f3a17749569fb33') # we hid the answer to the test here so you can't see it, but we can still run the test76})77test_that('The mass, height, color score, and width columns in fruit_data should be scaled.', {78expect_equal(fruit_data$scaled_mass, scale(fruit_data$mass, center = TRUE))79expect_equal(fruit_data$scaled_color_score, scale(fruit_data$color_score, center = TRUE))80expect_equal(fruit_data$scaled_height, scale(fruit_data$height, center = TRUE))81expect_equal(fruit_data$scaled_width, scale(fruit_data$width, center = TRUE))82})83print("Success!")84}8586test_1.6 <- function(){87test_that('Did not create an object named distance_44', {88expect_true(exists("distance_44"))89})90test_that('Did not create an object named distance_2', {91expect_true(exists("distance_2"))92})93test_that('distance_44 should be a distance.', {94expect_true('dist' %in% class(distance_44))95})96test_that('distance_2 should be a distance.', {97expect_true('dist' %in% class(distance_2))98})99test_that('distance_44 is incorrect.', {100expect_equal(digest(distance_44), '8b4aad1050358e59d907ad361c4eb9fb')101})102test_that('distance_2 is incorrect.', {103expect_equal(digest(distance_2), '5c276c2a08b1d63009a6574aefd55212')104})105print("Success!")106}107108test_2.0 <- function(){109test_that('Did not create an object named training_rows', {110expect_true(exists("training_rows"))111})112test_that('training_rows does not contain the correct number of rows and/or columns.', {113expect_equal(dim(training_rows), c(46, 1))114})115test_that('training_rows should only contain fruit names and should be a matrix', {116expect_true(is.matrix(training_rows))117})118print("Success!")119}120121122test_2.1 <- function(){123test_that('Did not create an object named training_set', {124expect_true(exists("training_set"))125})126test_that('training_set should contain all the columns in fruit_data and only the rows in training_rows.', {127expect_equal(dim(training_set), c(46, 11))128})129test_that('Did not create an object named testing_set', {130expect_true(exists("testing_set"))131})132test_that('testing_set should contain all the columns in fruit_data and the rows not in training_rows.', {133expect_equal(dim(testing_set), c(13, 11))134})135print("Success!")136}137138test_2.2 <- function(){139test_that('Did not create an object named X_simple', {140expect_true(exists("X_simple"))141})142test_that('X_simple should be a data frame.', {143expect_true('data.frame' %in% class(X_simple))144})145test_that('X_simple does not contain the correct number of rows and/or columns.', {146expect_equal(dim(X_simple), c(46, 2))147})148test_that('X_simple does not contain the column(s) scaled_color_score and/or scaled_mass', {149expect_true('scaled_color_score' %in% colnames(X_simple))150expect_true('scaled_mass' %in% colnames(X_simple))151})152test_that('Did not create an object named Y_fruit', {153expect_true(exists("Y_fruit"))154})155test_that('Y_fruit should be a factor', {156expect_true('factor' %in% class(Y_fruit))157})158test_that('Y_fruit is not the correct length.', {159expect_equal(length(Y_fruit), 46)160})161print("Success!")162}163164test_2.3 <- function(){165test_that('Did not create an object named ks', {166expect_true(exists("ks"))167})168test_that('ks should be a data frame.', {169expect_true('data.frame' %in% class(ks))170})171test_that('ks should contain a vector containing all the odd numbers from 1 to 11.', {172expect_equal(as.numeric(sum(ks)), 36)173})174print("Success!")175}176177test_2.4 <- function(){178test_that('Did not create an object named train_control', {179expect_true(exists("train_control"))180})181test_that('method should be cv (for cross-validation)', {182expect_equal(train_control$method, 'cv')183})184test_that('train_control should be set up for a 10-fold cross-validation (number should be 10))', {185expect_equal(train_control$number, 10)186})187print("Success!")188}189190test_2.5 <- function(){191test_that('Did not create an object named choose_k', {192expect_true(exists("choose_k"))193})194test_that('method should be knn', {195expect_equal(as.character(choose_k$method), 'knn')196})197test_that('k should be chosen from ks', {198expect_equal(as.numeric(sum(choose_k$results$k)), 36)199})200test_that('choose_k should use 10 fold cross-validation.', {201expect_equal(choose_k$control$method, 'cv')202expect_equal(choose_k$control$number, 10)203})204print("Success!")205}206207208test_2.6 <- function(){209properties <- c(choose_k_plot$layers[[1]]$mapping, choose_k_plot$layers[[2]]$mapping, choose_k_plot$mapping)210labels <- choose_k_plot$labels211test_that('Did not create a plot named choose_k_plot', {212expect_true(exists("choose_k_plot"))213})214test_that('choose_k_plot is not using information from k_accuracies', {215expect_equal(choose_k_plot$data, k_accuracies)216})217test_that('k should be on the x-axis.', {218expect_that("k" == rlang::get_expr(properties$x), is_true())219})220test_that('Accuracy should be on the y-axis.', {221expect_that("Accuracy" == rlang::get_expr(properties$y), is_true())222})223test_that('choose_k_plot should have both a line geometric object and point geometric object.', {224expect_that('GeomPoint' %in% c(class(rlang::get_expr(choose_k_plot$layers[[1]]$geom)), class(rlang::get_expr(choose_k_plot$layers[[2]]$geom))), is_true())225expect_that('GeomLine' %in% c(class(rlang::get_expr(choose_k_plot$layers[[1]]$geom)), class(rlang::get_expr(choose_k_plot$layers[[2]]$geom))), is_true())226})227print("Success!")228}229230test_2.7 <- function(){231test_that('Solution is incorrect', {232expect_true(answer2.7 == 3)233})234print("Success!")235}236237test_2.8 <- function(){238test_that('Solution is incorrect', {239expect_true(answer2.8 > 0.9)240expect_true(answer2.8 < 0.95)241})242print("Success!")243}244245test_3.0 <- function(){246test_that('method should be knn', {247expect_equal(as.character(simple$method), 'knn')248})249test_that('k should be 3', {250expect_equal(as.numeric(simple$results$k), 3)251})252test_that('simple should not use cross-validation.', {253expect_equal(simple$control$method, 'boot')254expect_equal(simple$control$number, 25)255})256print("Success!")257}258259test_3.1 <- function(){260test_that('training_pred prediction is incorrect.', {261expect_equal(length(training_pred), 46)262expect_equal(class(training_pred), 'factor')263expect_equal(c("apple", "lemon", "mandarin", "orange") %in% levels(training_pred), c(TRUE, TRUE, TRUE, TRUE))264expect_equal(length(training_pred[training_pred == "apple"]), 15)265expect_equal(length(training_pred[training_pred == "mandarin"]), 4)266expect_equal(length(training_pred[training_pred == "orange"]), 14)267expect_equal(length(training_pred[training_pred == "lemon"]), 13)268})269print("Success!")270}271272test_3.2 <- function(){273test_that('Solution is incorrect', {274expect_equal(class(training_results), 'confusionMatrix')275expect_equal(c("apple", "lemon", "mandarin", "orange") %in% colnames(training_results$table), c(TRUE, TRUE, TRUE, TRUE))276expect_equal(round(training_results$overall[[1]], 2), 0.93)277})278print("Success!")279}280281test_3.3 <- function(){282test_that('Solution is incorrect', {283expect_true(answer3.3 > 0.92)284expect_true(answer3.3 < 0.94)285})286print("Success!")287}288289290291test_4.0 <- function(){292test_that('Did not create an object named X_complex', {293expect_true(exists("X_complex"))294})295test_that('X_complex should be a data frame.', {296expect_true('data.frame' %in% class(X_complex))297})298test_that('X_complex does not contain the correct columns.', {299expect_that("scaled_height" %in% colnames(X_complex), is_true())300expect_that("scaled_width" %in% colnames(X_complex), is_true())301expect_that("scaled_mass" %in% colnames(X_complex), is_true())302expect_that("scaled_color_score" %in% colnames(X_complex), is_true())303})304test_that('Did not create an object named complex', {305expect_true(exists("complex"))306})307test_that('x in complex should be X_complex', {308expect_equal(as.character(complex$call$x), 'X_complex')309})310test_that('method should be knn', {311expect_equal(as.character(complex$method), 'knn')312})313test_that('k should be chosen from ks', {314expect_equal(as.numeric(sum(complex$results$k)), 36)315})316test_that('complex should use 10 fold cross-validation.', {317expect_equal(complex$control$method, 'cv')318expect_equal(complex$control$number, 10)319})320print("Success!")321}322323test_4.1 <- function(){324test_that('Did not create an object named k_accuracies_again', {325expect_true(exists("k_accuracies_again"))326})327test_that('k_accuracies_again contains incorrect information.', {328expect_equal(k_accuracies_again, complex$results %>% select(k, Accuracy))329})330test_that('Did not create a plot named choose_k_again_plot', {331expect_true(exists("choose_k_again_plot"))332})333test_that('choose_k_again_plot is not using information from k_accuracies_again', {334expect_equal(choose_k_again_plot$data, k_accuracies_again)335})336test_that('k should be on the x-axis.', {337expect_that("k" %in% c(rlang::get_expr(choose_k_again_plot$mapping$x),rlang::get_expr(choose_k_again_plot$layers[[1]]$mapping$x)), is_true())338})339test_that('Accuracy should be on the y-axis.', {340expect_that("Accuracy" %in% c(rlang::get_expr(choose_k_again_plot$mapping$y), rlang::get_expr(choose_k_again_plot$layers[[1]]$mapping$y)) , is_true())341})342test_that('choose_k_again_plot should be a scatter plot and a line plot.', {343expect_that('GeomPoint' %in% c(class(rlang::get_expr(choose_k_again_plot$layers[[1]]$geom)), class(rlang::get_expr(choose_k_again_plot$layers[[2]]$geom))), is_true())344expect_that('GeomLine' %in% c(class(rlang::get_expr(choose_k_again_plot$layers[[1]]$geom)), class(rlang::get_expr(choose_k_again_plot$layers[[2]]$geom))), is_true())345})346print("Success!")347}348349test_4.2 <- function(){350test_that('Solution is incorrect', {351expect_equal(answer4.2, 5)352})353print("Success!")354}355356test_4.3 <- function(){357test_that('Solution is incorrect', {358expect_true(answer4.3 > 0.97)359expect_true(answer4.3 < 1)360})361print("Success!")362}363364test_4.4 <- function(){365test_that('Solution is incorrect', {366expect_equal(digest(answer4.4), '96c24a598c808db5ff9c1aa505c6aa15')367})368print("Success!")369}370371372test_5.0 <- function(){373test_that('Did not create an object named final_k', {374expect_true(exists("final_k"))375})376test_that('final_k should be a data frame.', {377expect_true('data.frame' %in% class(final_k))378})379test_that('Did not create an object named final_classifier', {380expect_true(exists("final_classifier"))381})382test_that('x in the final_classifier should be X_complex', {383expect_equal(as.character(final_classifier$call$x), 'X_complex')384})385test_that('method should be knn', {386expect_equal(as.character(final_classifier$method), 'knn')387})388test_that('k should be 5', {389expect_equal(as.numeric(sum(final_classifier$results$k)), 5)390})391test_that('final_classifier should not be using cross-validation.', {392expect_equal(final_classifier$control$method, 'boot')393expect_equal(final_classifier$control$number, 25)394})395print("Success!")396}397398test_5.1 <- function(){399test_that('Did not create an object named X_test', {400expect_true(exists("X_test"))401})402test_that('X_test should be a data frame.', {403expect_true('data.frame' %in% class(X_test))404})405test_that('X_test does not contain the correct number of rows and/or columns.', {406expect_equal(dim(X_test), c(13, 4))407})408test_that('X_test does not contain the column(s) scaled_mass, scaled_width, scaled_height, and/or scaled_color_score', {409expect_true('scaled_color_score' %in% colnames(X_test))410expect_true('scaled_mass' %in% colnames(X_test))411expect_true('scaled_width' %in% colnames(X_test))412expect_true('scaled_height' %in% colnames(X_test))413})414test_that('Did not create an object named Y_test', {415expect_true(exists("Y_test"))416})417test_that('Y_fruit should be a factor', {418expect_true('factor' %in% class(Y_fruit))419})420test_that('Y_fruit is not the correct length.', {421expect_equal(length(Y_test), 13)422})423test_that('test_results should be a confusion matrix.', {424expect_equal(class(test_results), 'confusionMatrix')425})426test_that('test_results does not contain correct information.', {427expect_equal(c("apple", "lemon", "mandarin", "orange") %in% colnames(test_results$table), c(TRUE, TRUE, TRUE, TRUE))428})429test_that('prediction accuracy is not correct', {430expect_equal(round(test_results$overall[[1]], digits=2), 1.)431})432print("Success!")433}434435test_5.2 <- function(){436test_that('Solution is incorrect', {437expect_true(answer5.2 > .95)438expect_true(answer5.2 < 1.05)439})440print("Success!")441}442443