Path: blob/master/2020-fall/materials/tutorial_09/tests_tutorial_09.R
2051 views
# +1library(testthat)2library(digest)3library(rlang)45int_round <- function(x, digits){6x = x*10^digits7xint = as.integer(x)8xint1 = xint + 1L9if (abs(xint - x) < abs(xint1 - x)){10return(xint)11}12else {13return(xint1)14}15}16# -1718test_1.0 <- function() {19test_that('Did not create an object named credit', {20expect_true(exists("credit"))21})22test_that('credit should be a tibble.', {23expect_true('tbl' %in% class(credit))24})25test_that('credit does not contain the correct number of rows and/or columns.', {26expect_equal(dim(credit), c(400, 12))27})28test_that('The credit tibble is missing columns.', {29expect_true("Income" %in% colnames(credit))30expect_true("Balance" %in% colnames(credit))31})32test_that('credit does not contain the correct data.', {33expect_equal(digest(int_round(sum(credit$Income), 2)), '7b41cc2ef140f2cfb4b6eb86ccebf416')34expect_equal(digest(int_round(sum(credit$Limit), 2)), '1bc8a53a9b0cc2ea3cf99f2306872029')35})36print("Success!")37}3839test_1.1 <- function() {40test_that('credit should be a tibble.', {41expect_true('tbl' %in% class(credit))42})43test_that('credit does not contain the correct number of rows and/or columns.', {44expect_equal(dim(credit), c(400, 3))45})46test_that('The credit data frame should not contain the column X1', {47expect_false("X1" %in% colnames(credit))48})49test_that('The credit data frame is missing columns.', {50expect_true("Income" %in% colnames(credit))51expect_true("Balance" %in% colnames(credit))52expect_true("Rating" %in% colnames(credit))53})54print("Success!")55}5657test_1.2 <- function() {58test_that('Did not create an object called credit_split.', {59expect_true(exists('credit_split'))60})61test_that('credit_split is not an r_split object.', {62expect_true('rsplit' %in% class(credit_split))63})64test_that('Did not create an object called credit_training.', {65expect_true(exists('credit_training'))66})67test_that('credit_training is not a tibble.',{68expect_true('tbl' %in% class(credit_training))69})70test_that('credit_training does not contain 60% of the data.', {71expect_equal(dim(credit_training), c(241, 3))72expect_equal(digest(int_round(sum(credit_training$Balance), 2)), '273157f18270727a00bfb2d4bff79903')73expect_equal(digest(int_round(sum(credit_training$Income), 2)), 'b9f9e7fe1b89fc1fc7f54425e5688322')74expect_equal(digest(int_round(sum(credit_training$Rating), 2)), 'd34e38964a1d4ddb00ddd7f6118c5fc6')75})76test_that('Did not create an object called credit_testing.', {77expect_true(exists('credit_testing'))78})79test_that('credit_testing is not a tibble.', {80expect_true('tbl' %in% class(credit_training))81})82test_that('credit_testing does not contain the remaining 40% of the data.', {83expect_equal(dim(credit_testing), c(159, 3))84expect_equal(digest(int_round(sum(credit_testing$Balance), 2)), 'b0fe1ffce8158f92072c0c27ad1f181f')85expect_equal(digest(int_round(sum(credit_testing$Income), 2)), '581f089d90262775f263a2eb16c22480')86expect_equal(digest(int_round(sum(credit_testing$Rating), 2)), '26a325d7669edb2146d4a17935fe2636')87})88print("Success!")89}9091test_1.3 <- function() {92test_that('credit_eda should be a pairwise plot matrix.', {93expect_true('ggmatrix' %in% c(class(credit_eda)))94})95test_that('credit_eda should be using data from the credit data frame.', {96expect_equal(int_round(nrow(credit_eda$data), 0), 241)97})98test_that('credit_eda should be using the Balance, Income, Rating, Limit columns.', {99expect_equal(int_round(ncol(credit_eda$data), 0), 3)100})101print("Success!")102}103104test_1.4 <- function() {105test_that('Did not create an object called answer1.4.', {106expect_true(exists('answer1.4'))107})108test_that('Solution is incorrect.',{109expect_equal(digest(answer1.4), '475bf9280aab63a82af60791302736f6')110})111print("Success!")112}113114test_1.5 <- function() {115test_that('Did not create an object called lm_spec.', {116expect_true(exists('lm_spec'))117})118test_that('lm_spec should be a linear regression model specification.', {119expect_true('linear_reg' %in% class(lm_spec))120})121test_that('Did not create an object called credit_recipe.', {122expect_true(exists('credit_recipe'))123})124test_that('credit_recipe should be a recipe.', {125expect_true('recipe' %in% class(credit_recipe))126})127test_that('credit_recipe does not contain the training data.', {128expect_equal(dim(credit_recipe$template), c(241, 3))129expect_equal(digest(int_round(sum(credit_recipe$template$Income), 2)), 'b9f9e7fe1b89fc1fc7f54425e5688322')130expect_equal(digest(int_round(sum(credit_recipe$template$Rating), 2)), 'd34e38964a1d4ddb00ddd7f6118c5fc6')131expect_equal(digest(int_round(sum(credit_recipe$template$Balance), 2)), '273157f18270727a00bfb2d4bff79903')132})133print("Success!")134}135136test_1.6 <- function() {137test_that('Did not create an object called credit_fit.', {138expect_true(exists('credit_fit'))139})140test_that('credit_fit should be a workflow.', {141expect_true('workflow' %in% class(credit_fit))142})143test_that('credit_fit does not contain the training data.', {144expect_equal(digest(int_round(sum(credit_fit$pre$mold$predictors$Income), 2)), 'b9f9e7fe1b89fc1fc7f54425e5688322')145expect_equal(digest(int_round(sum(credit_fit$pre$mold$predictors$Rating), 2)), 'd34e38964a1d4ddb00ddd7f6118c5fc6')146})147print("Success!")148}149150test_1.7 <- function(){151test_that('Did not create an object called answer1.7.')152expect_true(exists('answer1.7'))153test_that('Solution is incorrect.', {154expect_equal(digest(answer1.7), '75f1160e72554f4270c809f041c7a776')155})156}157158test_1.8 <- function() {159test_that('Solution is incorrect.', {160expect_equal(digest(int_round(lm_rmse, 2)), 'fc3357631f940d71d5027270ad54a2cf')161})162print("Success!")163}164165test_1.9 <- function() {166test_that('Solution is incorrect.', {167expect_equal(digest(int_round(lm_rmspe, 2)), 'ec726f8469b8d46ed8231dcbcca420e2')168})169print("Success!")170}171172test_1.9.2 <- function(){173test_that('Did not create an object called answer1.9.2', {174expect_true(exists('answer1.9.2'))175})176test_that('Solution is incorrect', {177expect_equal(digest(answer1.9.2), '475bf9280aab63a82af60791302736f6')178})179print("Success!")180}181182183