Path: blob/master/2020-fall/materials/worksheet_09/tests_worksheet_09.R
2051 views
# +1library(testthat)2library(digest)34int_round <- function(x, digits){5x = x*10^digits6xint = as.integer(x)7xint1 = xint + 18if (abs(xint - x) < abs(xint1 - x)){9return(xint)10}11else {12return(xint1)13}14}15# -1617test_1.0 <- function() {18test_that('Did not create an object named answer1.0', {19expect_true(exists('answer1.0'))20})21test_that('Solution is incorrect', {22expect_equal(digest(answer1.0), '3a5505c06543876fe45598b5e5e5195d')23})24print("Success!")25}2627test_1.1 <- function() {28test_that('Did not create an object named answer1.1', {29expect_true(exists('answer1.1'))30})31test_that('Solution is incorrect', {32expect_equal(digest(answer1.1), '475bf9280aab63a82af60791302736f6')33})34print("Success!")35}3637test_1.2 <- function() {38test_that('Did not create an object named answer1.2', {39expect_true(exists('answer1.2'))40})41test_that('Solution is incorrect', {42expect_equal(digest(answer1.2), '75f1160e72554f4270c809f041c7a776')43})44print("Success!")45}4647test_2.0 <- function() {48test_that('Did not create an object named answer2.0', {49expect_true(exists('answer2.0'))50})51test_that('Solution is incorrect', {52expect_equal(digest(int_round(answer2.0, 2)), '9a6564e67167bff7e7cf99a541a641f1')53})54print("Success!")55}5657test_2.1 <- function() {58test_that('Did not create an object named answer2.1', {59expect_true(exists('answer2.1'))60})61test_that('Solution is incorrect', {62expect_equal(digest(int_round(answer2.1, 2)), '9a6564e67167bff7e7cf99a541a641f1')63})64print("Success!")65}6667test_2.2 <- function() {68test_that('Did not create an object named answer2.2', {69expect_true(exists('answer2.2'))70})71test_that('Solution is incorrect', {72expect_equal(digest(int_round(answer2.2, 2)), 'd69e7827ff0b1272336c2136df42c3f0')73})74print("Success!")75}7677test_2.3 <- function() {78test_that('Did not create an object named answer2.3', {79expect_true(exists('answer2.3'))80})81test_that('Solution is incorrect', {82expect_equal(digest(answer2.3), '475bf9280aab63a82af60791302736f6')83})84print("Success!")85}8687test_3.0 <- function() {88test_that('Did not create an object named marathon', {89expect_true(exists("marathon"))90})91test_that('marathon should be a tibble.', {92expect_true('tbl' %in% class(marathon))93})94test_that('marathon does not contain the correct number of rows and/or columns.', {95expect_equal(dim(marathon), c(929, 13))96})97test_that('The marathon tibble does not contain the correct columns.', {98expect_true("time_hrs" %in% colnames(marathon))99expect_true("max" %in% colnames(marathon))100})101test_that('marathon contains the wrong data', {102expect_equal(digest(int_round(sum(marathon$max), 0)), 'b64d424699e3efa872a878b15e4615fc')103expect_equal(digest(int_round(sum(marathon$time_hrs), 0)), '0a386b4fbb992709ee886a69c311a49c')104})105print("Success!")106}107108test_3.1 <- function() {109test_that('Did not create an object named marathon_split', {110expect_true(exists('marathon_split'))111})112test_that('marathon_split is not a split object (not a tibble)', {113expect_true('rsplit' %in% class(marathon_split))114})115test_that('marathon_split does not contain marathon data', {116expect_equal(dim(marathon_split$data), c(929, 13))117expect_equal(digest(int_round(sum(marathon_split$data$max), 0)), 'b64d424699e3efa872a878b15e4615fc')118expect_equal(digest(int_round(sum(marathon_split$data$time_hrs), 0)), '0a386b4fbb992709ee886a69c311a49c')119})120test_that('Did not create an object named marathon_training', {121expect_true(exists('marathon_training'))122})123test_that('marathon_training is not a tibble', {124expect_true('tbl' %in% class(marathon_training))125})126test_that('marathon_training does not contain 0.75 of the marathon data', {127expect_equal(dim(marathon_training), c(698, 13))128expect_equal(digest(int_round(sum(marathon_training$max), 0)), '6e85687c32809edf13dccf228f9f84e9')129expect_equal(digest(int_round(sum(marathon_training$time_hrs), 0)), '2213c3a0eb86305be22e0ca3b0a773c1')130})131test_that('Did not create an object named marathon_testing', {132expect_true(exists('marathon_testing'))133})134test_that('marathon_testing is not a tibble', {135expect_true('tbl' %in% class(marathon_testing))136})137test_that('marathon testing does not contain 0.25 of the marathon data', {138expect_equal(dim(marathon_testing), c(231, 13))139expect_equal(digest(int_round(sum(marathon_testing$max), 0)), 'e5032644f10cbf9a251aff4ed126d4af')140expect_equal(digest(int_round(sum(marathon_testing$time_hrs), 0)), 'ba825ab3d722243760b0700769f9371b')141})142print("Success!")143}144145test_3.2 <- function() {146properties <- c(marathon_eda$layers[[1]]$mapping, marathon_eda$mapping)147labels <- marathon_eda$labels148layers <- marathon_eda$layers[[1]]149test_that('Did not create a plot named marathon_eda', {150expect_true(exists("marathon_eda"))151})152test_that('max should be on the x-axis.', {153expect_true("max" %in% c(rlang::get_expr(properties$x)))154})155test_that('time_hrs should be on the y-axis.', {156expect_true("time_hrs" %in% c(rlang::get_expr(properties$y)))157})158test_that('marathon_eda should be a scatter plot.', {159expect_equal(digest(class(rlang::get_expr(layers$geom))[1]), '911e5b9debfb523f25ad2ccc01a4b2dd')160})161test_that('Labels on the axes should be descriptive and human readable.', {162expect_false((labels$y) == 'time_hrs')163expect_false((labels$x) == 'max')164})165test_that('Only the training data set should be used to create the plot', {166expect_equal(int_round(nrow(marathon_eda$data), 0), 698)167})168print("Success!")169}170171test_3.3 <- function() {172test_that('Did not create an object named lm_spec', {173expect_true(exists("lm_spec"))174})175test_that('lm_spec is not a linear regression model', {176expect_true('linear_reg' %in% class(lm_spec))177})178test_that('lm_spec does not contain the correct specifications', {179expect_equal(digest(as.character(lm_spec$mode)), 'b8bdd7015e0d1c6037512fd1396aef1a')180expect_equal(digest(as.character(lm_spec$engine)), '0995419f6f003f701c545d050292f42d')181})182print("Success!")183}184185test_3.3.1 <- function() {186test_that('Did not create an object named lm_recipe', {187expect_true(exists('lm_recipe'))188})189test_that('lm_recipe is not a recipe', {190expect_true('recipe' %in% class(lm_recipe))191})192test_that('lm_recipe does not contain the correct variables', {193expect_equal(digest(int_round(sum(lm_recipe$template$max), 0)), '6e85687c32809edf13dccf228f9f84e9')194expect_equal(digest(int_round(sum(lm_recipe$template$time_hrs), 0)), '2213c3a0eb86305be22e0ca3b0a773c1')195})196test_that('Did not create an object named lm_fit', {197expect_true(exists('lm_fit'))198})199test_that('lm_fit is not a workflow', {200expect_true('workflow' %in% class(lm_fit))201})202test_that('lm_fit does not contain the correct data', {203expect_equal(digest(int_round(sum(lm_fit$pre$actions$recipe$recipe$template$max), 0)), '6e85687c32809edf13dccf228f9f84e9')204expect_equal(digest(int_round(sum(lm_fit$pre$actions$recipe$recipe$template$time_hrs), 0)), '2213c3a0eb86305be22e0ca3b0a773c1')205})206test_that('lm_fit coefficients are incorrect', {207expect_equal(digest(int_round(sum(lm_fit$fit$fit$fit$coefficients), 1)), '80b0ae73fe0e882b0a24973e4e2c8203')208})209print("Success!")210}211212test_3.4 <- function() {213properties <- c(lm_predictions$layers[[1]]$mapping, lm_predictions$mapping)214labels <- lm_predictions$labels215layers <- c(lm_predictions$layers[[1]], lm_predictions$layers[[2]])216layers2 <- c(lm_predictions$layers[[2]], lm_predictions$layers[[1]])217test_that('Did not create a plot named lm_predictions', {218expect_true(exists("lm_predictions"))219})220test_that('max should be on the x-axis.', {221expect_true("max" %in% c(rlang::get_expr(properties$x)))222})223test_that('time_hrs should be on the y-axis.', {224expect_true("time_hrs" %in% c(rlang::get_expr(properties$y)))225})226test_that('lm_predictions should be a scatter plot.', {227expect_true('GeomPoint' %in% c(class(rlang::get_expr(lm_predictions$layers[[1]]$geom)),228class(rlang::get_expr(lm_predictions$layers[[2]]$geom))))229230})231test_that('lm_predictions should have a best fit line using a linear regression model.', {232expect_true('GeomSmooth' %in% c(class(rlang::get_expr(lm_predictions$layers[[2]]$geom)),233class(rlang::get_expr(lm_predictions$layers[[1]]$geom))))234})235test_that('Labels on the axes should be descriptive and human readable.', {236expect_false((labels$y) == 'time_hrs')237expect_false((labels$x) == 'max')238})239print("Success!")240}241242test_3.5 <- function() {243test_that('Did not create an object named lm_test_results', {244expect_true(exists('lm_test_results'))245})246test_that('lm_test_results is not a tibble', {247expect_true('tbl' %in% class(lm_test_results))248})249test_that('lm_test_results does not contain the correct data', {250expect_equal(dim(lm_test_results), c(3, 3))251expect_equal(digest(int_round(sum(lm_test_results$.estimate), 1)), 'a86d0670df7fb4f1da7b38943f5ee4e7')252})253test_that('Did not create an object named lm_rmspe', {254expect_true(exists('lm_rmspe'))255})256test_that('lm_rmspe is not a numeric', {257expect_true('numeric' %in% class(lm_rmspe))258})259test_that('lm_rmspe is not correct', {260expect_equal(digest(int_round(lm_rmspe, 1)), '25e6a154090e35101d7678d6f034353a')261})262print("Success!")263}264265test_3.5.1 <- function() {266properties <- c(lm_predictions_test$mapping, lm_predictions_test$layers[[1]]$mapping)267labels <- lm_predictions_test$labels268layers <- lm_predictions_test$layers[[1]]269test_that('Did not create a plot named lm_predictions_test', {270expect_true(exists("lm_predictions_test"))271})272test_that('max should be on the x-axis.', {273expect_true("max" %in% c(rlang::get_expr(properties$x)))274})275test_that('time_hrs should be on the y-axis.', {276expect_true("time_hrs" %in% c(rlang::get_expr(properties$y)))277})278test_that('lm_predictions_test should be a scatter plot.', {279expect_equal(digest(class(rlang::get_expr(layers$geom))[1]), '911e5b9debfb523f25ad2ccc01a4b2dd')280})281test_that('Labels on the axes should be descriptive and human readable.', {282expect_false((labels$y) == 'time_hrs')283expect_false((labels$x) == 'max')284})285test_that('Only the testing data set should be used to create the plot', {286expect_equal(int_round(nrow(lm_predictions_test$data), 0), 231)287})288print("Success!")289}290291test_3.6 <- function() {292test_that('Did not create an object named answer3.6', {293expect_true(exists('answer3.6'))294})295test_that('Solution is incorrect', {296expect_equal(digest(answer3.6), '75f1160e72554f4270c809f041c7a776')297})298print("Success!")299}300301test_3.7 <- function() {302test_that('Did not create an object named answer3.7', {303expect_true(exists('answer3.7'))304})305test_that('Solution is incorrect', {306expect_equal(digest(answer3.7), '3a5505c06543876fe45598b5e5e5195d')307})308print("Success!")309}310311test_3.8.1 <- function() {312test_that('Did not create an object named answer3.8.1', {313expect_true(exists('answer3.8.1'))314})315test_that('Solution is incorrect', {316expect_equal(digest(answer3.8.1), '75f1160e72554f4270c809f041c7a776')317})318print("Success!")319}320321322