Path: blob/master/2019-fall/materials/worksheet_09/tests_worksheet_09.R
2051 views
library(testthat)1library(digest)23test_1.0 <- function() {4test_that('Solution is incorrect', {5expect_equal(digest(answer1.0), '3a5505c06543876fe45598b5e5e5195d')6})7print("Success!")8}910test_1.1 <- function() {11test_that('Solution is incorrect', {12expect_equal(digest(answer1.1), '475bf9280aab63a82af60791302736f6')13})14print("Success!")15}1617test_1.2 <- function() {18test_that('Solution is incorrect', {19expect_equal(digest(answer1.2), '75f1160e72554f4270c809f041c7a776')20})21print("Success!")22}2324test_2.0 <- function() {25test_that('Solution is incorrect', {26expect_that(digest(round(answer2.0, 2)) %in% c('75dc8b7b8724a54d1fba4cc109438cfb', '4ec4e6dd2f7793c5d566f50026f262e9', '0d8b1b67a03fc038058a25213d5e9778'), is_true())27})28print("Success!")29}3031test_2.1 <- function() {32test_that('Solution is incorrect', {33expect_that(digest(round(answer2.1, 2)) %in% c('62233ed4e6655a993784e4c0886c4550', '1825ac9b036d540ac34abdd1ecb7fc21', '354964e94313ac16ac091669a785eb4f'), is_true())34})35print("Success!")36}3738test_2.2 <- function() {39test_that('Solution is incorrect', {40expect_that(digest(round(answer2.2, 2)) %in% c('522dbf08f17812fee06f0991cf0481af', 'ee48059132b8cdd8f1a1d9abbdaead78', '37cd4e5174c65a7196eae5fed7c0a61e', '4d308066a8d7253145df19089a026b9e'), is_true())41})42print("Success!")43}4445test_2.3 <- function() {46test_that('Solution is incorrect', {47expect_equal(digest(answer2.3), '475bf9280aab63a82af60791302736f6')48})49print("Success!")50}5152test_3.0 <- function() {53test_that('Did not create an object named marathon', {54expect_true(exists("marathon"))55})56test_that('marathon should be a data frame.', {57expect_true('data.frame' %in% class(marathon))58})59test_that('marathon does not contain the correct number of rows and/or columns.', {60expect_equal(dim(marathon), c(929, 13))61})62test_that('The marathon data frame is missing columns.', {63expect_that("time_hrs" %in% colnames(marathon), is_true())64expect_that("max" %in% colnames(marathon), is_true())65})66print("Success!")67}6869test_3.1 <- function() {70test_that('Did not create an object named X_train', {71expect_true(exists("X_train"))72})73test_that('X_train should be a data frame.', {74expect_true('data.frame' %in% class(X_train))75})76test_that('X_train does not contain the correct number of rows and/or columns.', {77expect_equal(dim(X_train), c(698, 1))78})79test_that('X_train does not contain the column max', {80expect_true('max' %in% colnames(X_train))81})82test_that('Did not create an object named X_test', {83expect_true(exists("X_test"))84})85test_that('X_test should be a data frame.', {86expect_true('data.frame' %in% class(X_test))87})88test_that('X_test does not contain the correct number of rows and/or columns.', {89expect_equal(dim(X_test), c(231, 1))90})91test_that('X_test does not contain the column max', {92expect_true('max' %in% colnames(X_test))93})94test_that('Did not create an object named Y_train', {95expect_true(exists("Y_train"))96})97test_that('Y_train should be a numeric vector', {98expect_true('numeric' %in% class(Y_train))99})100test_that('Y_train is not the correct length.', {101expect_equal(length(Y_train), 698)102})103test_that('Did not create an object named Y_test', {104expect_true(exists("Y_test"))105})106test_that('Y_test should be a numeric vector', {107expect_true('numeric' %in% class(Y_test))108})109test_that('Y_test is not the correct length.', {110expect_equal(length(Y_test), 231)111})112print("Success!")113}114115test_3.2 <- function() {116test_that('Did not create a plot named marathon_eda', {117expect_true(exists("marathon_eda"))118})119test_that('max should be on the x-axis.', {120expect_that("max" %in% c(rlang::get_expr(marathon_eda$mapping$x),rlang::get_expr(marathon_eda$layers[[1]]$mapping$x)), is_true())121})122test_that('time_hrs should be on the y-axis.', {123expect_that("time_hrs" %in% c(rlang::get_expr(marathon_eda$mapping$y), rlang::get_expr(marathon_eda$layers[[1]]$mapping$y)) , is_true())124})125test_that('marathon_eda should be a scatter plot.', {126expect_equal(digest(class(rlang::get_expr(marathon_eda$layers[[1]]$geom))[1]), '911e5b9debfb523f25ad2ccc01a4b2dd')127})128test_that('Labels on the axes should be descriptive and human readable.', {129expect_that((marathon_eda$labels$y) == 'time_hrs', is_false())130expect_that((marathon_eda$labels$x) == 'max', is_false())131})132test_that('Only the training data set should be used to create the plot', {133expect_equal(nrow(marathon_eda$data), 698)134})135print("Success!")136}137138test_3.3 <- function() {139test_that('Did not create an object named lm_model', {140expect_true(exists("lm_model"))141})142test_that('x in lm_model should be X_train', {143expect_equal(as.character(lm_model$call$x), 'X_train')144expect_equal(dim(lm_model$trainingData), c(698, 2))145})146test_that('y in lm_model should be Y_train', {147expect_equal(as.character(lm_model$call$y), 'Y_train')148expect_equal(dim(lm_model$trainingData), c(698, 2))149})150test_that('method should be lm', {151expect_equal(as.character(lm_model$method), 'lm')152})153print("Success!")154}155156test_3.4 <- function() {157test_that('Did not create a plot named lm_predictions', {158expect_true(exists("lm_predictions"))159})160test_that('max should be on the x-axis.', {161expect_that("max" %in% c(rlang::get_expr(lm_predictions$mapping$x),rlang::get_expr(lm_predictions$layers[[1]]$mapping$x)), is_true())162})163test_that('time_hrs should be on the y-axis.', {164expect_that("time_hrs" %in% c(rlang::get_expr(lm_predictions$mapping$y), rlang::get_expr(lm_predictions$layers[[1]]$mapping$y)) , is_true())165})166test_that('lm_predictions should be a scatter plot.', {167expect_true('GeomPoint' %in% c(class(rlang::get_expr(lm_predictions$layers[[1]]$geom)), class(rlang::get_expr(lm_predictions$layers[[2]]$geom))))168})169test_that('lm_predictions should have a best fit line using a linear regression model.', {170expect_true('GeomSmooth' %in% c(class(rlang::get_expr(lm_predictions$layers[[2]]$geom)), class(rlang::get_expr(lm_predictions$layers[[1]]$geom))))171})172test_that('Labels on the axes should be descriptive and human readable.', {173expect_that((lm_predictions$labels$y) == 'time_hrs', is_false())174expect_that((lm_predictions$labels$x) == 'max', is_false())175})176print("Success!")177}178179test_3.5 <- function() {180test_that('lm_rmse is incorrect', {181expect_equal(digest(round(lm_rmse, 3)), '788b714543df6e5fa9954cdefb85fde8')182})183print("Success!")184}185186test_3.6 <- function() {187test_that('lm_rmspe is incorrect', {188expect_equal(digest(round(lm_rmspe, 3)), '53a185c37dade264b27beeead0c1e823')189})190print("Success!")191}192193test_3.61 <- function() {194test_that('Did not create a plot named lm_predictions', {195expect_true(exists("lm_predictions"))196})197test_that('max should be on the x-axis.', {198expect_that("max" %in% c(rlang::get_expr(lm_predictions$mapping$x),rlang::get_expr(lm_predictions$layers[[1]]$mapping$x)), is_true())199})200test_that('time_hrs should be on the y-axis.', {201expect_that("time_hrs" %in% c(rlang::get_expr(lm_predictions$mapping$y), rlang::get_expr(lm_predictions$layers[[1]]$mapping$y)) , is_true())202})203test_that('lm_predictions should be a scatter plot.', {204expect_equal(digest(class(rlang::get_expr(lm_predictions$layers[[1]]$geom))[1]), '911e5b9debfb523f25ad2ccc01a4b2dd')205})206test_that('Labels on the axes should be descriptive and human readable.', {207expect_that((lm_predictions$labels$y) == 'time_hrs', is_false())208expect_that((lm_predictions$labels$x) == 'max', is_false())209})210test_that('Only the testing data set should be used to create the plot', {211expect_equal(nrow(lm_predictions$data), 231)212})213print("Success!")214}215216test_3.7 <- function() {217test_that('Solution is incorrect', {218expect_equal(digest(answer3.7), '75f1160e72554f4270c809f041c7a776')219})220print("Success!")221}222223test_3.8 <- function() {224test_that('Solution is incorrect', {225expect_equal(digest(answer3.8), '3a5505c06543876fe45598b5e5e5195d')226})227print("Success!")228}229230231232233