Path: blob/master/2021-fall/materials/worksheet_09/tests_worksheet_09.R
2051 views
library(testthat)1library(digest)23# Round double to precise integer4#5# `int_round` works to create an integer corresponding to a number that is6# tested up to a particular decimal point of precision. This is useful when7# there is a need to compare a numeric value using hashes.8#9# @param x Double vector of length one.10# @param digits Double vector of length one to specify decimal point of precision. Negative numbers can be used to specifying significant digits > 0.1.11#12# @return Integer vector of length one corresponding to a particular decimal point of precision.13#14# @examples15# # to get an integer up to two decimals of precision from 234.5678916# int_round(234.56789, 2)17#18# to get an integer rounded to the hundred digit from 234.5678919# int_round(234.56789, -2)20int_round <- function(x, digits){21x = x * 10^digits22xint = as.integer(x)23xint1 = xint + 1L24if (abs(xint - x) < abs(xint1 - x)){25return(xint)26}27else {28return(xint1)29}30}3132test_1.0 <- function() {33test_that('Did not create an object named answer1.0', {34expect_true(exists('answer1.0'))35})36test_that('Solution is incorrect', {37expect_equal(digest(answer1.0), '3a5505c06543876fe45598b5e5e5195d')38})39print("Success!")40}4142test_1.1 <- function() {43test_that('Did not create an object named answer1.1', {44expect_true(exists('answer1.1'))45})46test_that('Solution is incorrect', {47expect_equal(digest(answer1.1), '475bf9280aab63a82af60791302736f6')48})49print("Success!")50}5152test_1.2 <- function() {53test_that('Did not create an object named answer1.2', {54expect_true(exists('answer1.2'))55})56test_that('Solution is incorrect', {57expect_equal(digest(answer1.2), '75f1160e72554f4270c809f041c7a776')58})59print("Success!")60}6162test_2.0 <- function() {63test_that('Did not create an object named answer2.0', {64expect_true(exists('answer2.0'))65})66test_that('Solution is incorrect', {67expect_equal(digest(int_round(answer2.0, 2)), '9a6564e67167bff7e7cf99a541a641f1')68})69print("Success!")70}7172test_2.1 <- function() {73test_that('Did not create an object named answer2.1', {74expect_true(exists('answer2.1'))75})76test_that('Solution is incorrect', {77expect_equal(digest(int_round(answer2.1, 2)), '9a6564e67167bff7e7cf99a541a641f1')78})79print("Success!")80}8182test_2.2 <- function() {83test_that('Did not create an object named answer2.2', {84expect_true(exists('answer2.2'))85})86test_that('Solution is incorrect', {87expect_equal(digest(int_round(answer2.2, 2)), '40755f30599581bfb1186f077db8f580')88})89print("Success!")90}9192test_2.3 <- function() {93test_that('Did not create an object named answer2.3', {94expect_true(exists('answer2.3'))95})96test_that('Solution is incorrect', {97expect_equal(digest(answer2.3), '475bf9280aab63a82af60791302736f6')98})99print("Success!")100}101102test_3.0 <- function() {103test_that('Did not create an object named marathon', {104expect_true(exists("marathon"))105})106test_that('marathon should be a tibble.', {107expect_true('tbl' %in% class(marathon))108})109test_that('marathon does not contain the correct number of rows and/or columns.', {110expect_equal(dim(marathon), c(929, 13))111})112test_that('The marathon tibble does not contain the correct columns.', {113expect_true("time_hrs" %in% colnames(marathon))114expect_true("max" %in% colnames(marathon))115})116test_that('marathon contains the wrong data', {117expect_equal(digest(int_round(sum(marathon$max), 0)), 'b64d424699e3efa872a878b15e4615fc')118expect_equal(digest(int_round(sum(marathon$time_hrs), 0)), '0a386b4fbb992709ee886a69c311a49c')119})120print("Success!")121}122123test_3.1 <- function() {124test_that('Did not create an object named marathon_split', {125expect_true(exists('marathon_split'))126})127test_that('marathon_split is not a split object (not a tibble)', {128expect_true('rsplit' %in% class(marathon_split))129})130test_that('marathon_split does not contain marathon data', {131expect_equal(dim(marathon_split$data), c(929, 13))132expect_equal(digest(int_round(sum(marathon_split$data$max), 0)), 'b64d424699e3efa872a878b15e4615fc')133expect_equal(digest(int_round(sum(marathon_split$data$time_hrs), 0)), '0a386b4fbb992709ee886a69c311a49c')134})135test_that('Did not create an object named marathon_training', {136expect_true(exists('marathon_training'))137})138test_that('marathon_training is not a tibble', {139expect_true('tbl' %in% class(marathon_training))140})141test_that('marathon_training does not contain 0.75 of the marathon data', {142expect_equal(dim(marathon_training), c(698, 13))143expect_equal(digest(int_round(sum(marathon_training$max), 0)), '94e91e5e6573ddfedb81802729c39543')144expect_equal(digest(int_round(sum(marathon_training$time_hrs), 0)), '2213c3a0eb86305be22e0ca3b0a773c1')145})146test_that('Did not create an object named marathon_testing', {147expect_true(exists('marathon_testing'))148})149test_that('marathon_testing is not a tibble', {150expect_true('tbl' %in% class(marathon_testing))151})152test_that('marathon testing does not contain 0.25 of the marathon data', {153expect_equal(dim(marathon_testing), c(231, 13))154expect_equal(digest(int_round(sum(marathon_testing$max), 0)), '8049ebf85d7d9bc2b8baac5c119d4c21')155expect_equal(digest(int_round(sum(marathon_testing$time_hrs), 0)), 'a18d76f4b0bcad9715a7c592f28ac622')156})157print("Success!")158}159160test_3.2 <- function() {161properties <- c(marathon_eda$layers[[1]]$mapping, marathon_eda$mapping)162labels <- marathon_eda$labels163layers <- marathon_eda$layers[[1]]164test_that('Did not create a plot named marathon_eda', {165expect_true(exists("marathon_eda"))166})167test_that('max should be on the x-axis.', {168expect_true("max" %in% c(rlang::get_expr(properties$x)))169})170test_that('time_hrs should be on the y-axis.', {171expect_true("time_hrs" %in% c(rlang::get_expr(properties$y)))172})173test_that('marathon_eda should be a scatter plot.', {174expect_equal(digest(class(rlang::get_expr(layers$geom))[1]), '911e5b9debfb523f25ad2ccc01a4b2dd')175})176test_that('Labels on the axes should be descriptive and human readable.', {177expect_false((labels$y) == 'time_hrs')178expect_false((labels$x) == 'max')179})180test_that('Only the training data set should be used to create the plot', {181expect_equal(int_round(nrow(marathon_eda$data), 0), 698)182})183print("Success!")184}185186test_3.3 <- function() {187test_that('Did not create an object named lm_spec', {188expect_true(exists("lm_spec"))189})190test_that('lm_spec is not a linear regression model', {191expect_true('linear_reg' %in% class(lm_spec))192})193test_that('lm_spec does not contain the correct specifications', {194expect_equal(digest(as.character(lm_spec$mode)), 'b8bdd7015e0d1c6037512fd1396aef1a')195expect_equal(digest(as.character(lm_spec$engine)), '0995419f6f003f701c545d050292f42d')196})197print("Success!")198}199200test_3.3.1 <- function() {201test_that('Did not create an object named lm_recipe', {202expect_true(exists('lm_recipe'))203})204test_that('lm_recipe is not a recipe', {205expect_true('recipe' %in% class(lm_recipe))206})207test_that('lm_recipe does not contain the correct variables', {208expect_equal(digest(int_round(sum(lm_recipe$template$max), 0)), '94e91e5e6573ddfedb81802729c39543')209expect_equal(digest(int_round(sum(lm_recipe$template$time_hrs), 0)), '2213c3a0eb86305be22e0ca3b0a773c1')210})211test_that('Did not create an object named lm_fit', {212expect_true(exists('lm_fit'))213})214test_that('lm_fit is not a workflow', {215expect_true('workflow' %in% class(lm_fit))216})217test_that('lm_fit does not contain the correct data', {218expect_equal(digest(int_round(sum(lm_fit$pre$actions$recipe$recipe$template$max), 0)), '94e91e5e6573ddfedb81802729c39543')219expect_equal(digest(int_round(sum(lm_fit$pre$actions$recipe$recipe$template$time_hrs), 0)), '2213c3a0eb86305be22e0ca3b0a773c1')220})221test_that('lm_fit coefficients are incorrect', {222expect_equal(digest(int_round(sum(lm_fit$fit$fit$fit$coefficients), 1)), '80b0ae73fe0e882b0a24973e4e2c8203')223})224print("Success!")225}226227test_3.4 <- function() {228properties <- c(lm_predictions$layers[[1]]$mapping, lm_predictions$mapping)229labels <- lm_predictions$labels230layers <- c(lm_predictions$layers[[1]], lm_predictions$layers[[2]])231layers2 <- c(lm_predictions$layers[[2]], lm_predictions$layers[[1]])232test_that('Did not create a plot named lm_predictions', {233expect_true(exists("lm_predictions"))234})235test_that('max should be on the x-axis.', {236expect_true("max" %in% c(rlang::get_expr(properties$x)))237})238test_that('time_hrs should be on the y-axis.', {239expect_true("time_hrs" %in% c(rlang::get_expr(properties$y)))240})241test_that('lm_predictions should be a scatter plot.', {242expect_true('GeomPoint' %in% c(class(rlang::get_expr(lm_predictions$layers[[1]]$geom)),243class(rlang::get_expr(lm_predictions$layers[[2]]$geom))))244245})246test_that('lm_predictions should have a best fit line using a linear regression model.', {247expect_true('GeomSmooth' %in% c(class(rlang::get_expr(lm_predictions$layers[[2]]$geom)),248class(rlang::get_expr(lm_predictions$layers[[1]]$geom))))249})250test_that('Labels on the axes should be descriptive and human readable.', {251expect_false((labels$y) == 'time_hrs')252expect_false((labels$x) == 'max')253})254print("Success!")255}256257test_3.5 <- function() {258test_that('Did not create an object named lm_test_results', {259expect_true(exists('lm_test_results'))260})261test_that('lm_test_results is not a tibble', {262expect_true('tbl' %in% class(lm_test_results))263})264test_that('lm_test_results does not contain the correct data', {265expect_equal(dim(lm_test_results), c(3, 3))266expect_equal(digest(int_round(sum(lm_test_results$.estimate), 1)), 'e03a89536262b6a0e2beabd90a841c43')267})268test_that('Did not create an object named lm_rmspe', {269expect_true(exists('lm_rmspe'))270})271test_that('lm_rmspe is not a numeric', {272expect_true('numeric' %in% class(lm_rmspe))273})274test_that('lm_rmspe is not correct', {275expect_equal(digest(int_round(lm_rmspe, 1)), '25e6a154090e35101d7678d6f034353a')276})277print("Success!")278}279280test_3.5.1 <- function() {281properties <- c(lm_predictions_test$mapping, lm_predictions_test$layers[[1]]$mapping)282labels <- lm_predictions_test$labels283layers <- lm_predictions_test$layers[[1]]284test_that('Did not create a plot named lm_predictions_test', {285expect_true(exists("lm_predictions_test"))286})287test_that('max should be on the x-axis.', {288expect_true("max" %in% c(rlang::get_expr(properties$x)))289})290test_that('time_hrs should be on the y-axis.', {291expect_true("time_hrs" %in% c(rlang::get_expr(properties$y)))292})293test_that('lm_predictions_test should be a scatter plot.', {294expect_equal(digest(class(rlang::get_expr(layers$geom))[1]), '911e5b9debfb523f25ad2ccc01a4b2dd')295})296test_that('Labels on the axes should be descriptive and human readable.', {297expect_false((labels$y) == 'time_hrs')298expect_false((labels$x) == 'max')299})300test_that('Only the testing data set should be used to create the plot', {301expect_equal(int_round(nrow(lm_predictions_test$data), 0), 231)302})303print("Success!")304}305306test_3.6 <- function() {307test_that('Did not create an object named answer3.6', {308expect_true(exists('answer3.6'))309})310test_that('Solution is incorrect', {311expect_equal(digest(answer3.6), '75f1160e72554f4270c809f041c7a776')312})313print("Success!")314}315316test_3.7 <- function() {317test_that('Did not create an object named answer3.7', {318expect_true(exists('answer3.7'))319})320test_that('Solution is incorrect', {321expect_equal(digest(answer3.7), '3a5505c06543876fe45598b5e5e5195d')322})323print("Success!")324}325326test_3.8.1 <- function() {327test_that('Did not create an object named answer3.8.1', {328expect_true(exists('answer3.8.1'))329})330test_that('Solution is incorrect', {331expect_equal(digest(answer3.8.1), '75f1160e72554f4270c809f041c7a776')332})333print("Success!")334}335336337