Path: blob/master/2020-fall/materials/tutorial_07/tests_tutorial_07.R
2051 views
library(testthat)1library(digest)2library(rlang)34int_round <- function(x, digits){5x = x*10^digits6xint = as.integer(x)7xint1 = xint + 1L8if (abs(xint - x) < abs(xint1 - x)){9return(xint)10}11else {12return(xint1)13}14}151617test_1.0 <- function(){18test_that('Solution is incorrect', {19expect_true(exists('answer1.0'))20expect_equal(digest(answer1.0), 'c1f86f7430df7ddb256980ea6a3b57a4') # we hid the answer to the test here so you can't see it, but we can still run the test21})22print("Success!")23}2425test_1.1 <- function(){26test_that('Solution is incorrect', {27expect_true(exists('answer1.1'))28expect_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 test29})30print("Success!")31}3233test_2.0 <- function(){34test_that('number_of_rows does not exist', {35expect_true(exists('number_of_rows'))36})37test_that('The number of rows is incorrect', {38expect_equal(digest(int_round(number_of_rows, 0)), 'a58969729b9f8713e03743154dfa7b57') # 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_2.1 <- function(){44test_that('Solution is incorrect', {45expect_equal(int_round(nrow(counts), 0), 10)46expect_equal(digest(int_round(sum(as.integer(counts$y)), 2)), 'ae80db6ce6693e364a09f88dd4e3d553')47expect_equal(dim(counts), c(10, 2))48expect_equal(digest(int_round(sum(as.integer(counts$n)), 2)), '9ecacdf4df8df05e2501eb3f18bd13ab')49})50print("Success!")51}5253test_2.2 <- function(){54test_that('answer2.2 does not exist', {55expect_true(exists('answer2.2'))56})57test_that('Solution is incorrect', {58expect_equal(digest(answer2.2), 'd2a90307aac5ae8d0ef58e2fe730d38b')59})60print("Success!")61}6263test_2.4 <- function(){64test_that('Solution is incorrect', {65expect_equal(digest(int_round(as.integer(label_102[[1]]), 2)), '069c0949cc402f30063b90e9a31f87e1') # we hid the answer to the test here so you can't see it, but we can still run the test66})67print("Success!")68}6970test_3.0 <- function(){71test_that('testing_set does not exist', {72expect_true(exists('testing_set'))73})74test_that('testing_set does not contain the correct data', {75expect_equal(dim(testing_set), c(600, 786))76})77test_that('training_set does not exist', {78expect_true(exists('training_set'))79})80test_that('training_set does not contain the correct data',{81expect_equal(dim(training_set), c(600, 785))82})83print("Success!")84}8586test_3.1 <- function(){87test_that('answer3.1 does not exist', {88expect_true(exists('answer3.1'))89})90test_that('answer3.1 is incorrect', {91expect_equal(digest(answer3.1), '75f1160e72554f4270c809f041c7a776')92})93print("Success!")94}9596test_3.2 <- function(){97properties <- c(cross_val_plot$layers[[1]]$mapping, cross_val_plot$mapping)98labels <- cross_val_plot$labels99test_that('Did not create an object named cross_val_plot',{100expect_true(exists('cross_val_plot'))101})102test_that('cross_val_plot is not a lineplot', {103expect_true('GeomPoint' %in% class(cross_val_plot$layers[[1]]$geom))104expect_true('GeomLine' %in% class(cross_val_plot$layers[[2]]$geom))105})106test_that('neighbors should be on the x-axis.', {107expect_true("neighbors" == rlang::get_expr(properties$x))108})109test_that('mean should be on the y-axis.', {110expect_true("mean" == rlang::get_expr(properties$y))111})112test_that('cross_val_plot does not contain the correct data.', {113expect_equal(digest(int_round(sum(cross_val_plot$data$mean), 2)), '088459b2025cd3f9cbf1451f8c2c1636')114expect_equal(digest(int_round(sum(cross_val_plot$data$n), 2)), '189e2f1b2fbb3743811990e9708c226a')115})116test_that('cross_val_plot should only contain accuracy.', {117expect_true('accuracy' %in% unique(cross_val_plot$data$.metric))118})119print("Success!")120}121122test_3.3 <- function(){123test_that('Did not create an object named answer3.3', {124expect_true(exists('answer3.3'))125})126test_that('Solution is incorrect', {127expect_equal(digest(answer3.3), '75f1160e72554f4270c809f041c7a776')128})129print("Success!")130}131132test_4.0 <- function(){133test_that('Did not create an object named mnist_spec', {134expect_true(exists("mnist_spec"))135})136test_that('k value is incorrect', {137expect_equal(digest(int_round(get_expr(mnist_spec$args$neighbors), 0)), '11946e7a3ed5e1776e81c0f0ecd383d0')138})139test_that('weight_func is incorrect', {140expect_equal(digest(as.character(get_expr(mnist_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')141})142test_that('set_engine is incorrect', {143expect_equal(digest(as.character(mnist_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')144})145test_that('mode is incorrect', {146expect_equal(digest(as.character(mnist_spec$mode)), 'f361ba6f6b32d068e56f61f53d35e26a')147})148test_that('Did not create an object named mnist_fit', {149expect_true(exists("mnist_fit"))150})151test_that('mnist_fit should be a fit model.', {152expect_true('workflow' %in% class(mnist_fit))153})154test_that('mnist_fit does not include training_set dataset', {155expect_equal(digest(int_round(sum(as.integer(unlist(mnist_fit$fit$fit$fit$data[785]))), 2)), '4eb0b7cdd24a983324274d66e0860365')156})157test_that('mnist_fit does not contain mnist_spec', {158expect_equal(digest(int_round(get_expr(mnist_fit$fit$fit$spec$args$neighbors), 0)), '11946e7a3ed5e1776e81c0f0ecd383d0')159expect_equal(digest(as.character(get_expr(mnist_fit$fit$fit$spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')160expect_equal(digest(as.character(mnist_fit$fit$fit$spec$mode)), 'f361ba6f6b32d068e56f61f53d35e26a')161})162print("Success!")163}164165test_4.1 <- function(){166test_that('Did not create an object named mnist_predictions', {167expect_true(exists('mnist_predictions'))168})169test_that('mnist_predictions does not contain the correct data', {170expect_equal(digest(int_round(sum(as.integer(mnist_predictions$.pred_class)), 2)), 'ad1b496bd820958116aa67cdb147352b')171expect_equal(digest(int_round(sum(as.integer(mnist_predictions$y)), 2)), '733395e0768b9479c1feffa0b067fcdd')172})173test_that('Did not create an objected named training_results', {174expect_true(exists('training_results'))175})176test_that('training_results is not a confusion matrix', {177expect_true('conf_mat' %in% class(training_results))178})179test_that('training_results contains the incorrect data', {180expect_equal(digest(int_round(sum(as.integer(training_results$table)), 2)), '9ecacdf4df8df05e2501eb3f18bd13ab')181})182print("Success!")183}184185test_4.4 <- function(){186test_that('Did not create an object named answer4.4', {187expect_true(exists('answer4.4'))188})189test_that('Solution is incorrect', {190expect_equal(digest(answer4.4), '05ca18b596514af73f6880309a21b5dd')191})192print("Success!")193}194195test_4.5 <- function(){196test_that('Did not create an object named answer4.5', {197expect_true(exists('answer4.5'))198})199test_that('Solution is incorrect', {200expect_equal(digest(answer4.5), '3a5505c06543876fe45598b5e5e5195d')201})202print("Success!")203}204205206