Path: blob/master/2019-fall/materials/tutorial_09/tests_tutorial_09.R
2051 views
library(testthat)1library(digest)23test_1.0 <- function() {4test_that('Did not create an object named credit', {5expect_true(exists("credit"))6})7test_that('credit should be a data frame.', {8expect_true('data.frame' %in% class(credit))9})10test_that('credit does not contain the correct number of rows and/or columns.', {11expect_equal(dim(credit), c(400, 12))12})13test_that('The credit data frame is missing columns.', {14expect_that("Income" %in% colnames(credit), is_true())15expect_that("Balance" %in% colnames(credit), is_true())16})17print("Success!")18}1920test_1.1 <- function() {21test_that('credit should be a data frame.', {22expect_true('data.frame' %in% class(credit))23})24test_that('credit does not contain the correct number of rows and/or columns.', {25expect_equal(dim(credit), c(400, 3))26})27test_that('The credit data frame should not contain the column X1', {28expect_that("X1" %in% colnames(credit), is_false())29})30test_that('The credit data frame is missing columns.', {31expect_that("Income" %in% colnames(credit), is_true())32expect_that("Balance" %in% colnames(credit), is_true())33expect_that("Rating" %in% colnames(credit), is_true())34})35print("Success!")36}3738test_1.2 <- function() {39test_that('Did not create an object named X_train', {40expect_true(exists("X_train"))41})42test_that('X_train should be a data frame.', {43expect_true('data.frame' %in% class(X_train))44})45test_that('X_train does not contain the correct number of rows and/or columns.', {46expect_equal(dim(X_train), c(241, 2))47})48test_that('X_train should not contain the column Balance.', {49expect_false("Balance" %in% colnames(X_train))50})51test_that('Did not create an object named X_test', {52expect_true(exists("X_test"))53})54test_that('X_test should be a data frame.', {55expect_true('data.frame' %in% class(X_test))56})57test_that('X_test does not contain the correct number of rows and/or columns.', {58expect_equal(dim(X_test), c(159, 2))59})60test_that('X_test should not contain the column Balance', {61expect_false("Balance" %in% colnames(X_test))62})63test_that('Did not create an object named Y_train', {64expect_true(exists("Y_train"))65})66test_that('Y_train should be a numeric list', {67expect_true('numeric' %in% class(Y_train))68})69test_that('Y_train is not the correct length.', {70expect_equal(length(Y_train), 241)71})72test_that('Did not create an object named Y_test', {73expect_true(exists("Y_test"))74})75test_that('Y_test should be a numeric list', {76expect_true('numeric' %in% class(Y_test))77})78test_that('Y_test is not the correct length.', {79expect_equal(length(Y_test), 159)80})81print("Success!")82}8384test_1.3 <- function() {85test_that('credit_eda should be a pairwise plot matrix.', {86expect_true('ggmatrix' %in% c(class(credit_eda)))87})88test_that('credit_eda should be using data from the credit data frame.', {89expect_equal(nrow(credit_eda$data), 241)90})91test_that('credit_eda should be using the Balance, Income, Rating, Limit columns.', {92expect_equal(ncol(credit_eda$data), 3)93})94print("Success!")95}9697test_1.5 <- function() {98test_that('Did not create an object named lm_model', {99expect_true(exists("lm_model"))100})101test_that('x in lm_model should be X_train', {102expect_equal(as.character(lm_model$call$x), 'X_train')103expect_equal(dim(lm_model$trainingData), c(241, 3))104})105test_that('y in lm_model should be Y_train', {106expect_equal(as.character(lm_model$call$y), 'Y_train')107expect_equal(colnames(lm_model$trainingData), c('Income', 'Rating', '.outcome'))108})109test_that('method should be lm', {110expect_equal(as.character(lm_model$method), 'lm')111})112test_that('lm_model is incorrect.', {113expect_true(lm_model$results$intercept)114})115print("Success!")116}117118test_1.6 <- function() {119test_that('Did not create an object named lm_coeffs', {120expect_true(exists("lm_coeffs"))121})122test_that('lm_coeffs should be a data frame.', {123expect_equal(class(lm_coeffs), 'data.frame')124})125test_that('lm_coeffs should have 1 row of 3 different coefficients.', {126expect_equal(dim(lm_coeffs), c(1, 3))127})128test_that('column names should include Income', {129expect_true("Income" %in% colnames(lm_coeffs))130})131test_that('column names should include Income', {132expect_true("Rating" %in% colnames(lm_coeffs))133})134print("Success!")135}136137test_1.8 <- function() {138test_that('Solution is incorrect', {139expect_equal(digest(round(lm_rmse)), '506438c6bb7e021d303ffa42da8c4f3a')140})141print("Success!")142}143144test_1.9 <- function() {145test_that('Solution is incorrect', {146expect_equal(digest(round(lm_rmspe)), 'c2c9156cd03604826326da8eab46a3e1')147})148print("Success!")149}150151test_2.1 <- function() {152test_that('X_train should be a data frame.', {153expect_true('data.frame' %in% class(X_train))154})155test_that('X_train does not contain the correct number of rows and/or columns.', {156expect_equal(dim(X_train), c(2052, 5))157})158test_that('X_train should not contain the column sale_price.', {159expect_false('sale_price' %in% colnames(X_train))160})161test_that('Did not create an object named X_test', {162expect_true(exists("X_test"))163})164test_that('X_test should be a data frame.', {165expect_true('data.frame' %in% class(X_test))166})167test_that('X_test does not contain the correct number of rows and/or columns.', {168expect_equal(dim(X_test), c(877, 5))169})170test_that('X_test should not contain the column sale_price', {171expect_false('sale_price' %in% colnames(X_test))172})173test_that('Did not create an object named Y_train', {174expect_true(exists("Y_train"))175})176test_that('Y_train should be a numeric list', {177expect_true('numeric' %in% class(Y_train))178})179test_that('Y_train is not the correct length.', {180expect_equal(length(Y_train), 2052)181})182test_that('Did not create an object named Y_test', {183expect_true(exists("Y_test"))184})185test_that('Y_test should be a numeric list', {186expect_true('numeric' %in% class(Y_test))187})188test_that('Y_test is not the correct length.', {189expect_equal(length(Y_test), 877)190})191print("Success!")192}193194195