Path: blob/master/2020-spring/materials/worksheet_08/tests_worksheet_08.R
2710 views
library(testthat)1library(digest)23test_0.0 <- function(){4test_that('Solution is incorrect', {5expect_equal(digest(answer0.0), '3a5505c06543876fe45598b5e5e5195d')6})7print("Success!")8}910test_0.1 <- function(){11test_that('Solution is incorrect', {12expect_equal(digest(answer0.1), '475bf9280aab63a82af60791302736f6')13})14print("Success!")15}1617test_0.2 <- function(){18test_that('Solution is incorrect', {19expect_true(digest(round(answer0.2, 2)) %in% c('651ba44efc6a75d694ff482aae958ccc', '2a1ea47875e195a421d56ae3f6621d32'))20})21print("Success!")22}2324test_0.3 <- function(){25test_that('Solution is incorrect', {26expect_equal(digest(answer0.3), '75f1160e72554f4270c809f041c7a776')27})28print("Success!")29}3031test_1.0 <- function(){32test_that('Did not create an object named marathon', {33expect_true(exists("marathon"))34})35test_that('marathon should be a data frame.', {36expect_true('data.frame' %in% class(marathon))37})38test_that('marathon does not contain the correct number of rows and/or columns.', {39expect_equal(dim(marathon), c(929, 13))40})41test_that('The marathon data frame is missing columns.', {42expect_that("time_hrs" %in% colnames(marathon), is_true())43expect_that("max" %in% colnames(marathon), is_true())44})45print("Success!")46}4748test_2.0 <- function(){49test_that('Did not create a plot named answer2', {50expect_true(exists("answer2"))51})52test_that('marathon_50 does not contain the correct number of rows and/or columns.', {53expect_equal(dim(marathon_50), c(50, 13))54})55test_that('answer2 should use information from marathon_50', {56expect_equal(answer2$data, marathon_50)57})58test_that('max should be on the x-axis.', {59expect_that("max" %in% c(rlang::get_expr(answer2$mapping$x),60rlang::get_expr(answer2$layers[[1]]$mapping$x)), is_true())61})62test_that('time_hrs should be on the y-axis.', {63expect_that("time_hrs" %in% c(rlang::get_expr(answer2$mapping$y),64rlang::get_expr(answer2$layers[[1]]$mapping$y)) , is_true())65})66test_that('answer2 should be a scatter plot.', {67expect_equal(digest(class(rlang::get_expr(answer2$layers[[1]]$geom))[1]),68'911e5b9debfb523f25ad2ccc01a4b2dd')69})70test_that('Labels on the axes should be descriptive and human readable.', {71expect_that((answer2$labels$y) == 'time_hrs', is_false())72expect_that((answer2$labels$x) == 'max', is_false())73})74print("Success!")75}7677test_3.0 <- function(){78test_that('answer3 is incorrect', {79expect_true(exists('answer3'))80expect_equal(digest(round(answer3[[1]], 1)), 'b0370aa0feee1e438e74c77cd361ae86')81})82print("Success!")83}8485test_4.0 <- function(){86test_that('answer4 is incorrect', {87expect_true(exists('answer4'))88expect_equal(digest(round(answer4[[1]], 1)), '316c6aa53246a0e994e472fb4af30fc0')89})90print("Success!")91}9293test_5.0 <- function(){94test_that('Solution is incorrect', {95expect_that(exists('answer5'), is_true())96expect_equal(digest(answer5), '475bf9280aab63a82af60791302736f6')97})98print("Success!")99}100101test_6.0 <- function(){102test_that('Did not create an object named training_rows', {103expect_true(exists("training_rows"))104})105test_that('training_rows should be a matrix (not a data frame)', {106expect_true('matrix' %in% class(training_rows))107})108test_that('training_rows is not the correct length.', {109expect_equal(length(training_rows), 698)110})111print("Success!")112}113114test_7.0 <- function(){115test_that('Did not create an object named X_train', {116expect_true(exists("X_train"))117})118test_that('X_train should be a data frame.', {119expect_true('data.frame' %in% class(X_train))120})121test_that('X_train does not contain the correct number of rows and/or columns.', {122expect_equal(dim(X_train), c(698, 1))123})124test_that('X_train does not contain the column max', {125expect_true('max' %in% colnames(X_train))126})127test_that('Did not create an object named X_test', {128expect_true(exists("X_test"))129})130test_that('X_test should be a data frame.', {131expect_true('data.frame' %in% class(X_test))132})133test_that('X_test does not contain the correct number of rows and/or columns.', {134expect_equal(dim(X_test), c(231, 1))135})136test_that('X_test does not contain the column max', {137expect_true('max' %in% colnames(X_test))138})139test_that('Did not create an object named Y_train', {140expect_true(exists("Y_train"))141})142test_that('Y_train should be a numeric list', {143expect_true('numeric' %in% class(Y_train))144})145test_that('Y_train is not the correct length.', {146expect_equal(length(Y_train), 698)147})148test_that('Did not create an object named Y_test', {149expect_true(exists("Y_test"))150})151test_that('Y_test should be a numeric list', {152expect_true('numeric' %in% class(Y_test))153})154test_that('Y_test is not the correct length.', {155expect_equal(length(Y_test), 231)156})157print("Success!")158}159160test_8.0 <- function(){161test_that('Did not create an object named knn_cv', {162expect_true(exists("knn_cv"))163})164test_that('x in knn_cv should be X_train', {165expect_equal(as.character(knn_cv$call$x), 'X_train')166})167test_that('y in knn_cv should be Y_train', {168expect_equal(as.character(knn_cv$call$y), 'Y_train')169})170test_that('method should be knn', {171expect_equal(as.character(knn_cv$method), 'knn')172})173test_that('k should be chosen from k_lots', {174expect_equal(as.numeric(sum(knn_cv$results$k)), 1065)175})176test_that('knn_cv should use 3 fold cross-validation.', {177expect_equal(knn_cv$control$method, 'cv')178expect_equal(knn_cv$control$number, 3)179})180print("Success!")181}182183184test_8.1 <- function(){185test_that('Did not create a plot named choosing_k', {186expect_true(exists("choosing_k"))187})188test_that('The plot should be based on the knn_cv object.', {189expect_equal(choosing_k$data$k, knn_cv$results$k)190expect_equal(choosing_k$data$RMSE, knn_cv$results$RMSE)191})192test_that('k should be on the x-axis.', {193expect_that("k" %in% c(rlang::get_expr(choosing_k$mapping$x),rlang::get_expr(choosing_k$layers[[1]]$mapping$x)), is_true())194})195test_that('RMSE should be on the y-axis.', {196expect_that("RMSE" %in% c(rlang::get_expr(choosing_k$mapping$y), rlang::get_expr(choosing_k$layers[[1]]$mapping$y)) , is_true())197})198test_that('choosing_k should be a scatter plot and a line plot.', {199expect_that('GeomPoint' %in% c(class(rlang::get_expr(choosing_k$layers[[1]]$geom)), class(rlang::get_expr(choosing_k$layers[[2]]$geom))), is_true())200expect_that('GeomLine' %in% c(class(rlang::get_expr(choosing_k$layers[[1]]$geom)), class(rlang::get_expr(choosing_k$layers[[2]]$geom))), is_true())201})202print("Success!")203}204205test_8.2 <- function(){206test_that('best_k is incorrect', {207expect_equal(digest(as.numeric(best_k[[1]])), 'dc65e64a0fb0e6b4c47b0ae79cff0459')208})209print("Success!")210}211212test_9.0 <- function(){213test_that('Did not create an object named knn_model', {214expect_true(exists("knn_model"))215})216test_that('x in knn_model should be X_train', {217expect_equal(as.character(knn_model$call$x), 'X_train')218})219test_that('y in knn_model should be Y_train', {220expect_equal(as.character(knn_model$call$y), 'Y_train')221})222test_that('method should be knn', {223expect_equal(as.character(knn_model$method), 'knn')224})225test_that('k should be 75', {226expect_equal(as.integer(knn_model$results$k), 51)227})228test_that('knn_model contains incorrect trainingData.', {229expect_equal(colnames(knn_model$trainingData), c('max', '.outcome'))230expect_equal(dim(knn_model$trainingData), c(698, 2))231})232print("Success!")233}234235test_10.0 <- function(){236test_that('predictions should be a numeric vector or length 231', {237expect_true(class(predictions) == 'numeric')238expect_equal(length(predictions), 231)239expect_equal(digest(mean(predictions)), '71ba098ce630c0890084dbe1ae18b8e8')240})241test_that('values of predictions are incorrect', {242expect_equal(digest(mean(predictions)), '71ba098ce630c0890084dbe1ae18b8e8')243})244print("Success!")245}246247test_11.0 <- function(){248test_that('test_error is incorrect', {249expect_true('e2a1b82452e5d815ae7be864f8ba1ef1' %in% map(round(test_error, 3), digest) )250})251print("Success!")252}253254test_11.1 <- function(){255test_that('Solution is incorrect', {256expect_equal(digest(answer11.1), '05ca18b596514af73f6880309a21b5dd')257})258print("Success!")259}260261test_11.2 <- function(){262test_that('Solution is incorrect', {263expect_equal(digest(answer11.2), 'd2a90307aac5ae8d0ef58e2fe730d38b')264})265print("Success!")266}267268test_12.0 <- function(){269test_that('Did not create an object named full_predictions', {270expect_true(exists("full_predictions"))271})272test_that('full_predictions should be a data frame.', {273expect_true('data.frame' %in% class(full_predictions))274})275test_that('full_predictions does not contain time_hrs and/or max', {276expect_true('time_hrs' %in% colnames(full_predictions))277expect_true('max' %in% colnames(full_predictions))278})279test_that('full_predictions contains incorrect information.', {280expect_equal(round(sum(full_predictions$time_hrs)), 434)281})282print("Success!")283}284285test_13.0 <- function(){286test_that('Did not create a plot named predict_plot', {287expect_true(exists("predict_plot"))288})289test_that('max should be on the x-axis.', {290expect_that("max" %in% c(rlang::get_expr(predict_plot$mapping$x),rlang::get_expr(predict_plot$layers[[1]]$mapping$x)), is_true())291})292test_that('time_hrs should be on the y-axis.', {293expect_that("time_hrs" %in% c(rlang::get_expr(predict_plot$mapping$y), rlang::get_expr(predict_plot$layers[[1]]$mapping$y)) , is_true())294})295test_that('predict_plot should be a scatter plot.', {296expect_true('GeomPoint' %in% c(class(rlang::get_expr(predict_plot$layers[[1]]$geom)), class(rlang::get_expr(predict_plot$layers[[2]]$geom))))297})298test_that('predict_plot should have full_predictions plotted as a blue line over the data points.', {299expect_true('GeomLine' %in% c(class(rlang::get_expr(predict_plot$layers[[1]]$geom)), class(rlang::get_expr(predict_plot$layers[[2]]$geom))))300})301test_that('Labels on the axes should be descriptive and human readable.', {302expect_that((answer2$labels$y) == 'time_hrs', is_false())303expect_that((answer2$labels$x) == 'max', is_false())304})305print("Success!")306}307308309310311312313314315316