Path: blob/master/2020-spring/materials/tutorial_06/tests_tutorial_06.R
2051 views
library(testthat)1library(digest)23test_1.0 <- function(){4test_that('Did not create an object named fruit_data', {5expect_true(exists("fruit_data"))6})7test_that('fruit_data should be a data frame.', {8expect_true('data.frame' %in% class(fruit_data))9})10test_that('fruit_data does not contain the correct number of rows and/or columns.', {11expect_equal(dim(fruit_data), c(59, 7))12})13test_that('fruit_data does not contain the correct data.', {14expect_equal(digest(as.numeric(sum(fruit_data$scaled_width))), '3043b93a18750881f7178956ba203cfd')15expect_equal(colnames(fruit_data), c("fruit_label", "fruit_name", "fruit_subtype", "scaled_mass", "scaled_width", "scaled_height", "scaled_color"))16})17print("Success!")18}1920test_1.1 <- function(){21test_that('Solution is incorrect', {22expect_equal(digest(answer1.1), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test23})24print("Success!")25}2627test_1.2 <- function(){28test_that('Did not create an object named fruit_data', {29expect_true(exists("fruit_data"))30})31test_that('fruit_data does not contain the correct number of rows and/or columns.', {32expect_equal(dim(fruit_data), c(59, 7))33})34test_that('The fruit_name column in fruit_data should be of class factor.', {35expect_true(is.factor(fruit_data$fruit_name))36})37test_that('Columns in fruit_data contain incorrect values.', {38expect_equal(digest(as.numeric(sum(fruit_data$scaled_mass, na.rm = TRUE))), '9c46762a9ec19d9658dc07063ead8f30') # we hid the answer to the test here so you can't see it, but we can still run the test39})40print("Success!")41}4243test_1.3 <- function(){44properties <- c(fruit_plot$layers[[1]]$mapping, fruit_plot$mapping)45labels <- fruit_plot$labels46test_that('Did not create a plot named fruit_plot', {47expect_true(exists("fruit_plot"))48})49test_that('scaled_mass should be on the x-axis.', {50expect_that("scaled_mass" == rlang::get_expr(properties$x), is_true())51})52test_that('scaled_color should be on the y-axis.', {53expect_that("scaled_color" == rlang::get_expr(properties$y), is_true())54})55test_that('fruit_name should be mapped to colour', {56expect_that("fruit_name" == rlang::get_expr(properties$colour), is_true())57})58test_that('fruit_plot should be a scatter plot.', {59expect_that("GeomPoint" %in% c(class(fruit_plot$layers[[1]]$geom)) , is_true())60})61test_that('Labels on the axes should be descriptive and human readable.', {62expect_that((labels$y) == 'scaled_color', is_false())63expect_that((labels$x) == 'scaled_mass', is_false())64expect_that((labels$colour) == 'fruit_name', is_false())65})66print("Success!")67}6869test_1.4 <- function(){70properties <- c(fruit_plot_new$layers[[1]]$mapping, fruit_plot_new$mapping)71labels <- fruit_plot_new$labels72test_that('Did not create a plot named fruit_plot', {73expect_true(exists("fruit_plot_new"))74})75test_that('scaled_mass should be on the x-axis.', {76expect_that("scaled_mass" == rlang::get_expr(properties$x), is_true())77})78test_that('scaled_color should be on the y-axis.', {79expect_that("scaled_color" == rlang::get_expr(properties$y), is_true())80})81test_that('fruit_name should be mapped to colour', {82expect_that("fruit_name" == rlang::get_expr(properties$colour), is_true())83})84test_that('fruit_plot_new should be a scatter plot.', {85expect_that("GeomPoint" %in% c(class(fruit_plot_new$layers[[1]]$geom)) , is_true())86})87test_that ('There should be a new data point that has a mass and color score of 0.5', {88expect_true('GeomPoint' %in% class(rlang::get_expr(fruit_plot_new$layers[[2]]$geom)))89})90test_that('Labels on the axes should be descriptive and human readable.', {91expect_that((labels$y) == 'scaled_color', is_false())92expect_that((labels$x) == 'scaled_mass', is_false())93expect_that((labels$colour) == 'fruit_name', is_false())94})95print("Success!")96}979899test_1.6 <- function(){100test_that('Did not create an object named X_train', {101expect_true(exists("X_train"))102})103test_that('X_train should be a data frame.', {104expect_true('data.frame' %in% class(X_train))105})106test_that('X_train does not contain the correct number of rows and/or columns.', {107expect_equal(dim(X_train), c(59, 2))108})109test_that('X_train does not contain the column(s) scaled_color and/or scaled_mass', {110expect_true('scaled_color' %in% colnames(X_train))111expect_true('scaled_mass' %in% colnames(X_train))112})113test_that('Did not create an object named Y_train', {114expect_true(exists("Y_train"))115})116#test_that('Y_train should be a character.', {117# expect_true('character' %in% class(Y_train))118# })119test_that('Y_train is not the correct length.', {120expect_equal(length(Y_train), 59)121})122print("Success!")123}124125test_1.7 <- function(){126test_that('method should be knn', {127expect_equal(as.character(fruit_class$method), 'knn')128})129test_that('k should be 5', {130expect_equal(as.numeric(fruit_class$results$k), 5)131})132test_that('model_knn contains incorrect information.', {133expect_equal(digest(as.numeric(sum(fruit_class$trainingData$scaled_mass))), '9c46762a9ec19d9658dc07063ead8f30')134expect_equal(digest(as.numeric(sum(fruit_class$trainingData$scaled_color))), '2a93b1da4bcda1113ef03e891938eac4')135expect_equal(as.numeric(summary(fruit_class$trainingData$.outcome)[1]), 19)136})137print("Success!")138}139140test_1.8 <- function(){141test_that('Prediction is incorrect', {142expect_equal(digest(as.character(fruit_predicted)), '17f79d7a98f732174cc5a86dc56380d6')143})144print("Success!")145}146147