Path: blob/master/2020-fall/materials/tutorial_06/tests_tutorial_06.R
2051 views
# +1library(testthat)2library(digest)3library(rlang)45int_round <- function(x, digits = 2){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_0.1 <- function(){19test_that('Solution is incorrect', {20expect_true(exists('answer0.1'))21expect_equal(digest(answer0.1), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test2223})24print("Success!")25}2627test_1.0 <- 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(int_round(fruit_data$mass)), 'b26195f974598925edc40c2377152892') # we hid the answer to the test here so you can't see it, but we can still run the test39})40test_that('as_factor() function not used.',{41expect_equal(c(levels(fruit_data$fruit_name)), c("apple", "mandarin", "orange", "lemon"))42})43print("Success!")44}4546test_1.1 <- function(){47test_that('Solution is incorrect', {48expect_true(exists('answer1.1'))49expect_equal(digest(answer1.1), '75f1160e72554f4270c809f041c7a776') # we hid the answer to the test here so you can't see it, but we can still run the test5051})52print("Success!")53}5455test_1.0.1 <- function(){56test_that('Solution is incorrect', {57expect_equal(digest(answer1.0.1), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test58})59print("Success!")60}6162test_1.2 <- function(){63test_that('Did not create an object named fruit_dist_2', {64expect_true(exists("fruit_dist_2"))65})66test_that('fruit_dist_2 is incorrect.', {67expect_equal(digest(int_round(fruit_dist_2)), 'd8ba459e9b95d6bb43cdcb8acd275179')68})69print("Success!")70}7172test_1.3 <- function(){73test_that('Did not create an object named fruit_dist_44', {74expect_true(exists("fruit_dist_44"))75})76test_that('fruit_dist_44 is incorrect.', {77expect_equal(digest(int_round(fruit_dist_44)), 'ea07cf8b74030ff04b56ac69dd094adc')78})79print("Success!")80}8182test_1.4 <- function(){83test_that('Solution is incorrect', {84expect_true(exists('answer1.4'))85expect_equal(digest(answer1.4), 'c1f86f7430df7ddb256980ea6a3b57a4') # we hid the answer to the test here so you can't see it, but we can still run the test86})87print("Success!")88}8990# +91test_1.5 <- function(){92test_that('Did not create an object named fruit_data_scaled', {93expect_true(exists("fruit_data_scaled"))94})95test_that('fruit_data_scaled does not contain the correct number of rows and/or columns.', {96expect_equal(dim(fruit_data_scaled), c(59, 5))97})98test_that('The fruit_name column in fruit_data_scaled should be of class factor.', {99expect_true(is.factor(fruit_data_scaled$fruit_name))100})101test_that('Columns in fruit_data_scaled are not centered.', {102expect_equal(digest(int_round(mean(fruit_data_scaled$mass, na.rm = TRUE))), '1473d70e5646a26de3c52aa1abd85b1f') # we hid the answer to the test here so you can't see it, but we can still run the test103expect_equal(digest(int_round(mean(fruit_data_scaled$height, na.rm = TRUE))), '1473d70e5646a26de3c52aa1abd85b1f') # we hid the answer to the test here so you can't see it, but we can still run the test104expect_equal(digest(int_round(mean(fruit_data_scaled$width, na.rm = TRUE))), '1473d70e5646a26de3c52aa1abd85b1f') # we hid the answer to the test here so you can't see it, but we can still run the test105expect_equal(digest(int_round(mean(fruit_data_scaled$color_score, na.rm = TRUE))), '1473d70e5646a26de3c52aa1abd85b1f') # we hid the answer to the test here so you can't see it, but we can still run the test106107})108test_that('Columns in fruit_data_scaled are not scaled.', {109expect_equal(digest(int_round(sd(fruit_data_scaled$mass, na.rm = TRUE))), '5d6e7fe43b3b73e5fd2961d5162486fa') # we hid the answer to the test here so you can't see it, but we can still run the test110expect_equal(digest(int_round(sd(fruit_data_scaled$height, na.rm = TRUE))), '5d6e7fe43b3b73e5fd2961d5162486fa') # we hid the answer to the test here so you can't see it, but we can still run the test111expect_equal(digest(int_round(sd(fruit_data_scaled$width, na.rm = TRUE))), '5d6e7fe43b3b73e5fd2961d5162486fa') # we hid the answer to the test here so you can't see it, but we can still run the test112expect_equal(digest(int_round(sd(fruit_data_scaled$color_score, na.rm = TRUE))), '5d6e7fe43b3b73e5fd2961d5162486fa') # we hid the answer to the test here so you can't see it, but we can still run the test113})114print("Success!")115}116# -117118test_1.6 <- function(){119test_that('Did not create an object named distance_44', {120expect_true(exists("distance_44"))121})122test_that('Did not create an object named distance_2', {123expect_true(exists("distance_2"))124})125test_that('distance_44 should be a distance.', {126expect_true('dist' %in% class(distance_44))127})128test_that('distance_2 should be a distance.', {129expect_true('dist' %in% class(distance_2))130})131test_that('distance_44 is incorrect.', {132expect_equal(digest(int_round(distance_44)), '78f799aab6957dffdfd2bfb504f8cab5')133})134test_that('distance_2 is incorrect.', {135expect_equal(digest(int_round(distance_2)), '192b298ed4661ab6d9a4a193b2e60b49')136})137print("Success!")138}139140test_1.7 <- function(){141properties <- c(fruit_plot$layers[[1]]$mapping, fruit_plot$mapping)142labels <- fruit_plot$labels143test_that('Did not create a plot named fruit_plot', {144expect_true(exists("fruit_plot"))145})146test_that('scaled_mass should be on the x-axis.', {147expect_true("mass" == rlang::get_expr(properties$x))148})149test_that('scaled_color should be on the y-axis.', {150expect_true("color_score" == rlang::get_expr(properties$y))151})152test_that('fruit_name should be mapped to colour', {153expect_true("fruit_name" == rlang::get_expr(properties$colour))154})155test_that('fruit_plot should be a scatter plot.', {156expect_true("GeomPoint" %in% c(class(fruit_plot$layers[[1]]$geom)))157})158test_that('Labels on the axes should be descriptive and human readable.', {159expect_false((labels$y) == 'color_score')160expect_false((labels$x) == 'mass')161expect_false((labels$colour) == 'fruit_name')162})163print("Success!")164}165166test_1.8 <- function(){167test_that('Did not create an object named answer1.8', {168expect_true(exists("answer1.8"))169})170test_that('incorrect answer', {171expect_equal(digest(answer1.8), '3a5505c06543876fe45598b5e5e5195d')172})173print("Success!")174}175176test_1.9 <- function(){177test_that('Did not create an object named knn_spec', {178expect_true(exists("knn_spec"))179})180test_that('k is incorrect', {181expect_equal(digest(int_round(get_expr(knn_spec$args$neighbors))), '75e76f8b41d0944779e119afd3513844')182})183test_that('weight_func is incorrect', {184expect_equal(digest(as.character(get_expr(knn_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')185})186test_that('set_engine is incorrect', {187expect_equal(digest(as.character(knn_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')188})189test_that('mode is incorrect', {190expect_equal(digest(as.character(knn_spec$mode)), 'f361ba6f6b32d068e56f61f53d35e26a')191})192test_that('Did not create an object named fruit_fit', {193expect_true(exists("fruit_fit"))194})195test_that('fruit_fit should be a fit model.', {196expect_true('workflow' %in% class(fruit_fit))197})198test_that('fruit_fit does not contain knn_spec', {199expect_equal(digest(int_round(get_expr(fruit_fit$fit$actions$model$spec$args$neighbors))), '75e76f8b41d0944779e119afd3513844')200expect_equal(digest(as.character(get_expr(fruit_fit$fit$actions$model$spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')201expect_equal(digest(fruit_fit$fit$actions$model$spec$mode), 'f361ba6f6b32d068e56f61f53d35e26a')202})203print("Success!")204}205206test_1.10 <- function(){207test_that('Prediction is incorrect', {208expect_equal(digest(as.character(fruit_predicted)), 'd19d62a873f08af0488f0df720cfd293')209})210print("Success!")211}212213test_1.11 <- function(){214test_that('answer1.11 does not exist', {215expect_true(exists("answer1.11"))216})217test_that('Answer is incorrect', {218expect_equal(digest(answer1.11), '475bf9280aab63a82af60791302736f6')219})220print("Success!")221}222223test_1.12 <- function(){224test_that('fruit_all_predicted does not exist',{225expect_true(exists("fruit_all_predicted"))226})227test_that('prediction is incorrect', {228expect_equal(digest(as.character(fruit_all_predicted$.pred_class)), '5e55351cc3517b5b1031d95040455de5')229})230print("Success!")231}232233test_1.13 <- function(){234test_that('Answer is incorrect', {235expect_equal(digest(answer1.13), '3a5505c06543876fe45598b5e5e5195d')236})237print("Success!")238}239240test_2.0 <- function(){241test_that('incorrect classification', {242expect_equal(digest(int_round(as.integer(seed_predict$.pred_class))), 'ca21e364698e573f63da3ecac022c6e3') # can't apply int_round243})244print("Success!")245}246247test_2.1 <- function(){248test_that('answer2.1 does not exist',{249expect_true(exists("answer2.1"))250})251test_that('Answer is incorrect', {252expect_equal(digest(answer2.1), '475bf9280aab63a82af60791302736f6')253})254print("Success!")255}256257258