Path: blob/master/2020-spring/materials/worksheet_07/tests_worksheet_07.R
2051 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 is incorrect.', {42expect_equal(digest(round(as.numeric(fruit_dist_2), digits = 2)), '4b66dfae2026bef9f4f46d590d07821d')43})44print("Success!")45}4647test_1.3 <- function(){48test_that('Did not create an object named fruit_dist_44', {49expect_true(exists("fruit_dist_44"))50})51test_that('fruit_dist_44 is incorrect.', {52expect_equal(digest(round(as.numeric(fruit_dist_44),2)), '2b97a3d3feaca3cc5e50cfc6bbd6414f')53})54print("Success!")55}5657test_1.4 <- function(){58test_that('Solution is incorrect', {59expect_that(exists('answer1.4'), is_true())60expect_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 test61})62print("Success!")63}6465test_1.5 <- function(){66test_that('Did not create an object named fruit_data_scaled', {67expect_true(exists("fruit_data_scaled"))68})69test_that('fruit_data_scaled does not contain the correct number of rows and/or columns.', {70expect_equal(dim(fruit_data_scaled), c(59, 11))71})72test_that('The fruit_name column in fruit_data_scaled should be of class factor.', {73expect_true(is.factor(fruit_data_scaled$fruit_name))74})75test_that('Columns in fruit_data_scaled contain incorrect values.', {76expect_equal(digest(as.numeric(sum(fruit_data_scaled$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 test77})78test_that('The mass, height, color score, and width columns in fruit_data should be scaled.', {79expect_equal(fruit_data_scaled$scaled_mass, scale(fruit_data_scaled$mass, center = TRUE))80expect_equal(fruit_data_scaled$scaled_color_score, scale(fruit_data_scaled$color_score, center = TRUE))81expect_equal(fruit_data_scaled$scaled_height, scale(fruit_data_scaled$height, center = TRUE))82expect_equal(fruit_data_scaled$scaled_width, scale(fruit_data_scaled$width, center = TRUE))83})84print("Success!")85}8687test_1.6 <- function(){88test_that('Did not create an object named distance_44', {89expect_true(exists("distance_44"))90})91test_that('Did not create an object named distance_2', {92expect_true(exists("distance_2"))93})94test_that('distance_44 should be a distance.', {95expect_true('dist' %in% class(distance_44))96})97test_that('distance_2 should be a distance.', {98expect_true('dist' %in% class(distance_2))99})100test_that('distance_44 is incorrect.', {101expect_equal(digest(distance_44), '8b4aad1050358e59d907ad361c4eb9fb')102})103test_that('distance_2 is incorrect.', {104expect_equal(digest(distance_2), '5c276c2a08b1d63009a6574aefd55212')105})106print("Success!")107}108109test_2.0 <- function(){110test_that('Did not create an object named training_rows', {111expect_true(exists("training_rows"))112})113test_that('training_rows does not contain the correct number of rows and/or columns.', {114expect_equal(dim(training_rows), c(46, 1))115})116test_that('training_rows should only contain fruit names and should be a matrix', {117expect_true(is.matrix(training_rows))118})119print("Success!")120}121122123test_2.1.0 <- function(){124test_that('Did not create an object named training_set', {125expect_true(exists("training_set"))126})127test_that('training_set should contain all the columns in fruit_data and only the rows in training_rows.', {128expect_equal(dim(training_set), c(46, 7))129})130test_that('Did not create an object named testing_set', {131expect_true(exists("testing_set"))132})133test_that('testing_set should contain all the columns in fruit_data and the rows not in training_rows.', {134expect_equal(dim(testing_set), c(13, 7))135})136print("Success!")137}138139140test_2.1.1 <- function(){141test_that('Did not create an object named scale_transformer', {142expect_true(exists("scale_transformer"))143})144test_that('Preprocess method should be scale', {145expect_true("scale" %in% names(scale_transformer$method))146})147test_that('Did not create an object named training_set', {148expect_true(exists("training_set"))149})150test_that('training_set should contain all the columns in fruit_data and only the rows in training_rows.', {151expect_equal(dim(training_set), c(46, 7))152})153test_that('training_set columns should contain be standardized', {154expect_equal(digest(round(mean(training_set$mass))),'908d1fd10b357ed0ceaaec823abf81bc')155expect_equal(digest(sd(training_set$mass)),'6717f2823d3202449301145073ab8719')156expect_equal(digest(round(sd(training_set$color_score))),'6717f2823d3202449301145073ab8719')157})158test_that('Did not create an object named testing_set', {159expect_true(exists("testing_set"))160})161test_that('testing_set should contain all the columns in fruit_data and the rows not in training_rows.', {162expect_equal(dim(testing_set), c(13, 7))163})164test_that('testing_set columns should contain be standardized', {165expect_equal(digest(round(sd(testing_set$mass))),'908d1fd10b357ed0ceaaec823abf81bc')166expect_equal(digest(as.numeric(round(mean(testing_set$color_score)))),'908d1fd10b357ed0ceaaec823abf81bc')167expect_equal(digest(as.numeric(round(sd(testing_set$color_score)))),'6717f2823d3202449301145073ab8719')168})169print("Success!")170}171172test_2.2 <- function(){173test_that('Did not create an object named X_simple', {174expect_true(exists("X_simple"))175})176test_that('X_simple should be a data frame.', {177expect_true('data.frame' %in% class(X_simple))178})179test_that('X_simple does not contain the correct number of rows and/or columns.', {180expect_equal(dim(X_simple), c(46, 2))181})182test_that('X_simple does not contain the column(s) color_score and/or mass', {183expect_true('color_score' %in% colnames(X_simple))184expect_true('mass' %in% colnames(X_simple))185})186test_that('Did not create an object named Y_fruit', {187expect_true(exists("Y_fruit"))188})189test_that('Y_fruit should be a factor', {190expect_true('factor' %in% class(Y_fruit))191})192test_that('Y_fruit is not the correct length.', {193expect_equal(length(Y_fruit), 46)194})195print("Success!")196}197198199200test_2.3 <- function(){201test_that('Did not create an object named ks', {202expect_true(exists("ks"))203})204test_that('ks should be a data frame.', {205expect_true('data.frame' %in% class(ks))206})207test_that('ks should contain a vector containing all the odd numbers from 1 to 11.', {208expect_equal(as.numeric(sum(ks)), 36)209})210print("Success!")211}212213test_2.4 <- function(){214test_that('Did not create an object named train_control', {215expect_true(exists("train_control"))216})217test_that('method should be cv (for cross-validation)', {218expect_equal(train_control$method, 'cv')219})220test_that('train_control should be set up for a 10-fold cross-validation (number should be 10))', {221expect_equal(train_control$number, 10)222})223print("Success!")224}225226test_2.5 <- function(){227test_that('Did not create an object named choose_k', {228expect_true(exists("choose_k"))229})230test_that('method should be knn', {231expect_equal(as.character(choose_k$method), 'knn')232})233test_that('k should be chosen from ks', {234expect_equal(as.numeric(sum(choose_k$results$k)), 36)235})236test_that('choose_k should use 10 fold cross-validation.', {237expect_equal(choose_k$control$method, 'cv')238expect_equal(choose_k$control$number, 10)239})240print("Success!")241}242243244test_2.6 <- function(){245properties <- c(choose_k_plot$layers[[1]]$mapping, choose_k_plot$layers[[2]]$mapping, choose_k_plot$mapping)246labels <- choose_k_plot$labels247test_that('Did not create a plot named choose_k_plot', {248expect_true(exists("choose_k_plot"))249})250test_that('choose_k_plot is not using information from k_accuracies', {251expect_equal(choose_k_plot$data, k_accuracies)252})253test_that('k should be on the x-axis.', {254expect_that("k" == rlang::get_expr(properties$x), is_true())255})256test_that('Accuracy should be on the y-axis.', {257expect_that("Accuracy" == rlang::get_expr(properties$y), is_true())258})259test_that('choose_k_plot should have both a line geometric object and point geometric object.', {260expect_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())261expect_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())262})263print("Success!")264}265266test_2.7 <- function(){267test_that('Solution is incorrect', {268expect_true(answer2.7 == 3)269})270print("Success!")271}272273test_2.8 <- function(){274test_that('Solution is incorrect', {275expect_true(answer2.8 > 0.9)276expect_true(answer2.8 < 0.95)277})278print("Success!")279}280281test_3.0 <- function(){282test_that('method should be knn', {283expect_equal(as.character(simple$method), 'knn')284})285test_that('k should be 3', {286expect_equal(as.numeric(simple$results$k), 3)287})288test_that('simple should not use cross-validation.', {289expect_equal(simple$control$method, 'boot')290expect_equal(simple$control$number, 25)291})292print("Success!")293}294295test_3.1 <- function(){296test_that('training_pred prediction is incorrect.', {297expect_equal(length(training_pred), 46)298expect_equal(class(training_pred), 'factor')299expect_equal(c("apple", "lemon", "mandarin", "orange") %in% levels(training_pred), c(TRUE, TRUE, TRUE, TRUE))300expect_equal(length(training_pred[training_pred == "apple"]), 15)301expect_equal(length(training_pred[training_pred == "mandarin"]), 4)302expect_equal(length(training_pred[training_pred == "orange"]), 14)303expect_equal(length(training_pred[training_pred == "lemon"]), 13)304})305print("Success!")306}307308test_3.2 <- function(){309test_that('Solution is incorrect', {310expect_equal(class(training_results), 'confusionMatrix')311expect_equal(c("apple", "lemon", "mandarin", "orange") %in% colnames(training_results$table), c(TRUE, TRUE, TRUE, TRUE))312expect_equal(round(training_results$overall[[1]], 2), 0.93)313})314print("Success!")315}316317test_3.3 <- function(){318test_that('Solution is incorrect', {319expect_true(answer3.3 > 0.92)320expect_true(answer3.3 < 0.94)321})322print("Success!")323}324325326327test_4.0 <- function(){328test_that('Did not create an object named X_complex', {329expect_true(exists("X_complex"))330})331test_that('X_complex should be a data frame.', {332expect_true('data.frame' %in% class(X_complex))333})334test_that('X_complex does not contain the correct columns.', {335expect_that("height" %in% colnames(X_complex), is_true())336expect_that("width" %in% colnames(X_complex), is_true())337expect_that("mass" %in% colnames(X_complex), is_true())338expect_that("color_score" %in% colnames(X_complex), is_true())339})340test_that('Did not create an object named complex', {341expect_true(exists("complex"))342})343test_that('x in complex should be X_complex', {344expect_equal(as.character(complex$call$x), 'X_complex')345})346test_that('method should be knn', {347expect_equal(as.character(complex$method), 'knn')348})349test_that('k should be chosen from ks', {350expect_equal(as.numeric(sum(complex$results$k)), 36)351})352test_that('complex should use 10 fold cross-validation.', {353expect_equal(complex$control$method, 'cv')354expect_equal(complex$control$number, 10)355})356print("Success!")357}358359test_4.1 <- function(){360test_that('Did not create an object named k_accuracies_again', {361expect_true(exists("k_accuracies_again"))362})363test_that('k_accuracies_again contains incorrect information.', {364expect_equal(k_accuracies_again, complex$results %>% select(k, Accuracy))365})366test_that('Did not create a plot named choose_k_again_plot', {367expect_true(exists("choose_k_again_plot"))368})369test_that('choose_k_again_plot is not using information from k_accuracies_again', {370expect_equal(choose_k_again_plot$data, k_accuracies_again)371})372test_that('k should be on the x-axis.', {373expect_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())374})375test_that('Accuracy should be on the y-axis.', {376expect_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())377})378test_that('choose_k_again_plot should be a scatter plot and a line plot.', {379expect_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())380expect_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())381})382print("Success!")383}384385test_4.2 <- function(){386test_that('Solution is incorrect', {387expect_equal(answer4.2, 5)388})389print("Success!")390}391392test_4.3 <- function(){393test_that('Solution is incorrect', {394expect_true(answer4.3 > 0.97)395expect_true(answer4.3 < 1)396})397print("Success!")398}399400test_4.4 <- function(){401test_that('Solution is incorrect', {402expect_equal(digest(answer4.4), '96c24a598c808db5ff9c1aa505c6aa15')403})404print("Success!")405}406407408test_5.0 <- function(){409test_that('Did not create an object named final_k', {410expect_true(exists("final_k"))411})412test_that('final_k should be a data frame.', {413expect_true('data.frame' %in% class(final_k))414})415test_that('Did not create an object named final_classifier', {416expect_true(exists("final_classifier"))417})418test_that('x in the final_classifier should be X_complex', {419expect_equal(as.character(final_classifier$call$x), 'X_complex')420})421test_that('method should be knn', {422expect_equal(as.character(final_classifier$method), 'knn')423})424test_that('k should be 5', {425expect_equal(as.numeric(sum(final_classifier$results$k)), 5)426})427test_that('final_classifier should not be using cross-validation.', {428expect_equal(final_classifier$control$method, 'boot')429expect_equal(final_classifier$control$number, 25)430})431print("Success!")432}433434test_5.1 <- function(){435test_that('Did not create an object named X_test', {436expect_true(exists("X_test"))437})438test_that('X_test should be a data frame.', {439expect_true('data.frame' %in% class(X_test))440})441test_that('X_test does not contain the correct number of rows and/or columns.', {442expect_equal(dim(X_test), c(13, 4))443})444test_that('X_test does not contain the column(s) mass, width, height, and/or color_score', {445expect_true('color_score' %in% colnames(X_test))446expect_true('mass' %in% colnames(X_test))447expect_true('width' %in% colnames(X_test))448expect_true('height' %in% colnames(X_test))449})450test_that('Did not create an object named Y_test', {451expect_true(exists("Y_test"))452})453test_that('Y_fruit should be a factor', {454expect_true('factor' %in% class(Y_fruit))455})456test_that('Y_fruit is not the correct length.', {457expect_equal(length(Y_test), 13)458})459test_that('test_results should be a confusion matrix.', {460expect_equal(class(test_results), 'confusionMatrix')461})462test_that('test_results does not contain correct information.', {463expect_equal(c("apple", "lemon", "mandarin", "orange") %in% colnames(test_results$table), c(TRUE, TRUE, TRUE, TRUE))464})465test_that('prediction accuracy is not correct', {466expect_equal(round(test_results$overall[[1]], digits=2), 1.)467})468print("Success!")469}470471test_5.2 <- function(){472test_that('Solution is incorrect', {473expect_true(answer5.2 > .95)474expect_true(answer5.2 < 1.05)475})476print("Success!")477}478479