Path: blob/master/2020-spring/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))104expect_true('Income' %in% colnames(lm_model$trainingData) && 'Rating' %in% colnames(lm_model$trainingData))105})106test_that('y in lm_model should be Y_train', {107expect_equal(as.character(lm_model$call$y), 'Y_train')108})109test_that('method should be lm', {110expect_equal(as.character(lm_model$method), 'lm')111})112test_that('lm_model should have an intercept', {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 Intercept', {129expect_true("Intercept" %in% colnames(lm_coeffs))130})131test_that('column names should include Income', {132expect_true("Income" %in% colnames(lm_coeffs))133})134test_that('column names should include Rating', {135expect_true("Rating" %in% colnames(lm_coeffs))136})137print("Success!")138}139140test_1.8 <- function() {141test_that('Solution is incorrect', {142expect_equal(digest(round(lm_rmse)), '506438c6bb7e021d303ffa42da8c4f3a')143})144print("Success!")145}146147test_1.9 <- function() {148test_that('Solution is incorrect', {149expect_equal(digest(round(lm_rmspe)), 'c2c9156cd03604826326da8eab46a3e1')150})151print("Success!")152}153154