Path: blob/master/2019-fall/materials/worksheet_08/tests_worksheet_08.R
2707 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)), 31375)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('choosing_k contains incorrect information.', {189expect_equal(choosing_k$data, knn_cv$results)190})191test_that('k should be on the x-axis.', {192expect_that("k" %in% c(rlang::get_expr(choosing_k$mapping$x),rlang::get_expr(choosing_k$layers[[1]]$mapping$x)), is_true())193})194test_that('RMSE should be on the y-axis.', {195expect_that("RMSE" %in% c(rlang::get_expr(choosing_k$mapping$y), rlang::get_expr(choosing_k$layers[[1]]$mapping$y)) , is_true())196})197test_that('choosing_k should be a scatter plot and a line plot.', {198expect_that('GeomPoint' %in% c(class(rlang::get_expr(choosing_k$layers[[1]]$geom)), class(rlang::get_expr(choosing_k$layers[[2]]$geom))), is_true())199expect_that('GeomLine' %in% c(class(rlang::get_expr(choosing_k$layers[[1]]$geom)), class(rlang::get_expr(choosing_k$layers[[2]]$geom))), is_true())200})201print("Success!")202}203204test_8.2 <- function(){205test_that('best_k is incorrect', {206expect_equal(digest(as.numeric(best_k[[1]])), 'dc65e64a0fb0e6b4c47b0ae79cff0459')207})208print("Success!")209}210211test_9.0 <- function(){212test_that('Did not create an object named knn_model', {213expect_true(exists("knn_model"))214})215test_that('x in knn_model should be X_train', {216expect_equal(as.character(knn_model$call$x), 'X_train')217})218test_that('y in knn_model should be Y_train', {219expect_equal(as.character(knn_model$call$y), 'Y_train')220})221test_that('method should be knn', {222expect_equal(as.character(knn_model$method), 'knn')223})224test_that('k should be 75', {225expect_equal(as.integer(knn_model$results$k), 51)226})227test_that('knn_model contains incorrect trainingData.', {228expect_equal(colnames(knn_model$trainingData), c('max', '.outcome'))229expect_equal(dim(knn_model$trainingData), c(698, 2))230})231print("Success!")232}233234test_10.0 <- function(){235test_that('predictions should be a numeric vector or length 231', {236expect_true(class(predictions) == 'numeric')237expect_equal(length(predictions), 231)238expect_equal(digest(mean(predictions)), '71ba098ce630c0890084dbe1ae18b8e8')239})240test_that('values of predictions are incorrect', {241expect_equal(digest(mean(predictions)), '71ba098ce630c0890084dbe1ae18b8e8')242})243print("Success!")244}245246test_11.0 <- function(){247test_that('test_error is incorrect', {248expect_true('e2a1b82452e5d815ae7be864f8ba1ef1' %in% map(round(test_error, 3), digest) )249})250print("Success!")251}252253test_11.1 <- function(){254test_that('Solution is incorrect', {255expect_equal(digest(answer11.1), '05ca18b596514af73f6880309a21b5dd')256})257print("Success!")258}259260test_11.2 <- function(){261test_that('Solution is incorrect', {262expect_equal(digest(answer11.2), 'd2a90307aac5ae8d0ef58e2fe730d38b')263})264print("Success!")265}266267test_12.0 <- function(){268test_that('Did not create an object named full_predictions', {269expect_true(exists("full_predictions"))270})271test_that('full_predictions should be a data frame.', {272expect_true('data.frame' %in% class(full_predictions))273})274test_that('full_predictions does not contain time_hrs and/or max', {275expect_true('time_hrs' %in% colnames(full_predictions))276expect_true('max' %in% colnames(full_predictions))277})278test_that('full_predictions contains incorrect information.', {279expect_equal(round(sum(full_predictions$time_hrs)), 434)280})281print("Success!")282}283284test_13.0 <- function(){285test_that('Did not create a plot named predict_plot', {286expect_true(exists("predict_plot"))287})288test_that('max should be on the x-axis.', {289expect_that("max" %in% c(rlang::get_expr(predict_plot$mapping$x),rlang::get_expr(predict_plot$layers[[1]]$mapping$x)), is_true())290})291test_that('time_hrs should be on the y-axis.', {292expect_that("time_hrs" %in% c(rlang::get_expr(predict_plot$mapping$y), rlang::get_expr(predict_plot$layers[[1]]$mapping$y)) , is_true())293})294test_that('predict_plot should be a scatter plot.', {295expect_true('GeomPoint' %in% c(class(rlang::get_expr(predict_plot$layers[[1]]$geom)), class(rlang::get_expr(predict_plot$layers[[2]]$geom))))296})297test_that('predict_plot should have full_predictions plotted as a blue line over the data points.', {298expect_true('GeomLine' %in% c(class(rlang::get_expr(predict_plot$layers[[1]]$geom)), class(rlang::get_expr(predict_plot$layers[[2]]$geom))))299})300test_that('Labels on the axes should be descriptive and human readable.', {301expect_that((answer2$labels$y) == 'time_hrs', is_false())302expect_that((answer2$labels$x) == 'max', is_false())303})304print("Success!")305}306307308309310311312313314315