Path: blob/master/2020-fall/materials/worksheet_06/tests_worksheet_06.R
2710 views
# +1library(testthat)2library(digest)3library(rlang)45int_round <- function(x, digits){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_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 test21})22print("Success!")23}2425test_0.2 <- function(){26test_that('Solution is incorrect', {27expect_equal(digest(answer0.2), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test28})29print("Success!")30}3132test_1.0 <- function(){33test_that('Did not create an object named cancer', {34expect_true(exists("cancer"))35})36test_that('cancer should be a data frame.', {37expect_true('data.frame' %in% class(cancer))38})39test_that('cancer does not contain the correct number of rows and/or columns.', {40expect_equal(dim(cancer), c(569, 12))41})42test_that('cancer does not contain the correct data.', {43expect_equal(digest(int_round(sum(cancer$Area), 2)), '1473d70e5646a26de3c52aa1abd85b1f')44expect_equal(colnames(cancer), c("ID", "Class", "Radius", "Texture", "Perimeter", "Area", "Smoothness", "Compactness", "Concavity", "Concave_points", "Symmetry", "Fractal_dimension"))45})46test_that('read.csv() instead of read_csv() function is used.', {47expect_true(class(cancer$Class) == 'character')48})49print("Success!")50}5152test_1.1 <- function(){53test_that('Solution is incorrect', {54expect_equal(digest(answer1.1), 'd2a90307aac5ae8d0ef58e2fe730d38b') # we hid the answer to the test here so you can't see it, but we can still run the test55})56print("Success!")57}5859test_1.2 <- function(){60properties <- c(cancer_plot$layers[[1]]$mapping, cancer_plot$mapping)61labels <- cancer_plot$labels62test_that('Did not create a plot named cancer_plot', {63expect_true(exists("cancer_plot"))64})65test_that('Symmetry should be on the x-axis.', {66expect_true("Symmetry" == rlang::get_expr(properties$x))67})68test_that('Radius should be on the y-axis.', {69expect_true("Radius" == rlang::get_expr(properties$y))70})71test_that('cancer_plot should be a scatter plot.', {72expect_true("GeomPoint" %in% class(cancer_plot$layers[[1]]$geom))73})74test_that('cancer_plot should map Class to colour.', {75expect_true(digest(rlang::get_expr(properties$colour)) %in% c('a4abb3d43fde633563dd1f5c3ea31f31', 'f9e884084b84794d762a535f3facec85'))76})77print("Success!")78}7980test_1.3 <- function(){81test_that('Solution is incorrect', {82expect_equal(digest(answer1.3), '891e8a631267b478c03e25594808709d') # we hid the answer to the test here so you can't see it, but we can still run the test83})84print("Success!")85}8687test_1.4 <- function(){88test_that('xa is incorrect.', {89expect_equal(digest(int_round(xa, 2)), 'c0048c4f8677b795155d8aa41e26a54d')90})91test_that('ya is incorrect.', {92expect_equal(digest(int_round(ya, 2)), 'a6e8462a7cace5673e544d1e8d238b52')93})94test_that('xb is incorrect.', {95expect_equal(digest(int_round(xb, 2)), '10aeddd8594c6ce210c731b8b94af435')96})97test_that('yb is incorrect.', {98expect_equal(digest(int_round(yb, 2)), '48139aad2994737e7e801156a24281ed')99})100print("Success!")101}102103test_1.5 <- function(){104test_that('answer1.5 is incorrect', {105expect_equal(digest(int_round(answer1.5, 2)), 'a95ceee8390cb47bb05410a8d23c76cf') # we hid the answer to the test here so you can't see it, but we can still run the test106})107print("Success!")108}109110test_1.6 <- function(){111test_that('zb is incorrect.', {112expect_equal(digest(int_round(zb,2)), 'b78a46ebc0bb9a4cc7f4f4b962f0b2ef')113})114test_that('za is incorrect.', {115expect_equal(digest(int_round(za,2)), 'b35d8adab2b7c839e5a8e2861080b03e')116})117print("Success!")118}119120test_1.7 <- function(){121test_that('answer1.7 is incorrect', {122expect_equal(digest(int_round(answer1.7, 2)), 'c7fd80062a02f15d212704a20fae75fb') # we hid the answer to the test here so you can't see it, but we can still run the test123})124print("Success!")125}126127test_1.8 <- function(){128test_that('point_a is incorrect.', {129expect_equal(digest(int_round(sum((point_a)),2)), '44014eaa19f1aef8e92b1020c47d662b')130})131test_that('point_b is incorrect.', {132expect_equal(digest(int_round(sum((point_b)),2)), 'e064b40c9ca28b04b874bcd8bdefa41e')133})134print("Success!")135}136137test_1.09 <- function(){138test_that('dif_square is incorrect', {139expect_equal(digest(int_round(sum(dif_square),2)), 'e276884e43714ac361db1a1998bb6bc9') # we hid the answer to the test here so you can't see it, but we can still run the test140})141print("Success!")142}143144145test_1.09.1 <- function(){146test_that('dif_sum is incorrect', {147expect_equal(digest(int_round(dif_sum,2)), 'e276884e43714ac361db1a1998bb6bc9') # we hid the answer to the test here so you can't see it, but we can still run the test148})149print("Success!")150}151152test_1.09.2 <- function(){153test_that('root_dif_sum is incorrect', {154expect_equal(digest(int_round(root_dif_sum,2)), 'c7fd80062a02f15d212704a20fae75fb') # we hid the answer to the test here so you can't see it, but we can still run the test155})156print("Success!")157}158159test_1.09.3 <- function(){160test_that('dist_cancer_two_rows is incorrect', {161expect_equal(digest(int_round(dist_cancer_two_rows,2)), 'c7fd80062a02f15d212704a20fae75fb') # we hid the answer to the test here so you can't see it, but we can still run the test162163})164print("Success!")165}166167168test_1.09.4 <- function(){169test_that('Solution is incorrect', {170expect_equal(digest(answer1.09.4), '05ca18b596514af73f6880309a21b5dd') # we hid the answer to the test here so you can't see it, but we can still run the test171172})173print("Success!")174}175176test_2.0.0 <- function(){177test_that('Did not create an object named small_sample', {178expect_true(exists("small_sample"))179})180test_that('small_sample should be a data frame.', {181expect_true('data.frame' %in% class(small_sample))182})183test_that('small_sample does not contain the correct number of rows and/or columns.', {184expect_equal(dim(small_sample), c(5, 3))185})186test_that('small_sample does not contain the correct columns.', {187expect_true('Symmetry' %in% colnames(small_sample))188expect_true('Radius' %in% colnames(small_sample))189expect_true('Class' %in% colnames(small_sample))190})191print("Success!")192}193194195test_2.0.1 <- function(){196properties <- c(small_sample_plot$layers[[1]]$mapping, small_sample_plot$mapping)197labels <- small_sample_plot$labels198test_that('Did not create a plot named small_sample_plot', {199expect_true(exists("small_sample_plot"))200})201test_that('Did not use small_sample data to create small_sample_plot', {202expect_equal(digest(int_round(sum(small_sample_plot$data$Symmetry),2)), '9e13926505f026ba03c475b3a83f7a04')203})204test_that('Symmetry should be on the x-axis.', {205expect_true("Symmetry" == rlang::get_expr(properties$x))206})207test_that('Radius should be on the y-axis.', {208expect_true("Radius" == rlang::get_expr(properties$y))209})210test_that('small_sample_plot should be a scatter plot.', {211expect_true("GeomPoint" %in% c(class(small_sample_plot$layers[[1]]$geom)))212})213test_that('small_sample_plot should map Benign / Malignant to colour.', {214expect_true("Class" == rlang::get_expr(properties$colour))215})216print("Success!")217}218219test_2.1 <- function(){220test_that('Did not create an object named newData', {221expect_true(exists("newData"))222})223test_that('newData should be a data frame.', {224expect_true('data.frame' %in% class(newData))225})226test_that('The last row of the Class column should be "unknown".', {227expect_equal((newData$Class[6]), 'unknown')228})229test_that('newData does not contain the correct number of rows and/or columns.', {230expect_equal(dim(newData), c(6, 3))231})232test_that('small_sample does not contain the correct data.', {233expect_equal(digest(int_round(sum(newData$Radius),2)), 'e65288ab08a1f50a7e0e9a972d0cdad3')234expect_equal(digest(int_round(sum(newData$Symmetry),2)), '9e13926505f026ba03c475b3a83f7a04')235})236print("Success!")237}238239test_2.2 <- function(){240test_that('Did not create an object named dist_matrix', {241expect_true(exists("dist_matrix"))242})243test_that('dist_matrix should be a matrix.', {244expect_true('matrix' %in% class(dist_matrix))245})246test_that('dist_matrix does not contain the correct number of rows and/or columns.', {247expect_equal(dim(dist_matrix), c(6, 6))248})249test_that('dist_matrix does not contain the correct data.', {250expect_equal(digest(int_round(sum(dist_matrix[1, ]),2)), 'd34fef5547c4f837fe41124a8fca5527')251expect_equal(digest(int_round(sum(dist_matrix[2, ]),2)), 'cabe6c85a73de5051d70aea8c66c720f')252expect_equal(digest(int_round(sum(dist_matrix[5, ]),2)), '7145585a43115cfe7cc1e1038ef55cf0')253expect_equal(digest(int_round(sum(dist_matrix[6, ]),2)), 'e14b44a4689c4ded0d65a6b30d1bcd7f')254})255print("Success!")256}257258test_2.3 <- function(){259test_that('Solution is incorrect', {260expect_equal(digest(answer2.3), '5b58e040ee35f3bcc6023fb7836c842e') # we hid the answer to the test here so you can't see it, but we can still run the test261})262print("Success!")263}264265test_2.4 <- function(){266test_that('Solution is incorrect', {267expect_equal(digest(answer2.4), '9c8cb5538e7778bf0b1bd53e45fb78c9') # we hid the answer to the test here so you can't see it, but we can still run the test268})269print("Success!")270}271272test_2.5 <- function(){273test_that('Solution is incorrect', {274expect_equal(digest(answer2.5), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test275})276print("Success!")277}278279test_2.6 <- function(){280test_that('Solution is incorrect', {281expect_equal(digest(answer2.6), '9c8cb5538e7778bf0b1bd53e45fb78c9') # we hid the answer to the test here so you can't see it, but we can still run the test282283})284print("Success!")285}286287test_2.7 <- function(){288test_that('Solution is incorrect', {289expect_equal(digest(answer2.7), '0590b0427c1b19a6eb612d19888aa52f') # we hid the answer to the test here so you can't see it, but we can still run the test290291})292print("Success!")293}294295test_3.1 <- function(){296test_that('Did not create an object named knn_spec', {297expect_true(exists("knn_spec"))298})299test_that('k should be 7', {300expect_equal(as.numeric(get_expr(knn_spec$args$neighbors)), 7)301})302test_that('weight_func is incorrect', {303expect_equal(digest(as.character(get_expr(knn_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')304})305test_that('set_engine is incorrect', {306expect_equal(digest(as.character(knn_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')307})308test_that('mode is incorrect', {309expect_equal(digest(as.character(knn_spec$mode)), 'f361ba6f6b32d068e56f61f53d35e26a')310})311print("Success!")312}313314test_3.2 <- function(){315test_that('Did not create an object named knn_fit', {316expect_true(exists("knn_fit"))317})318test_that('knn_fit should be a fit model.', {319expect_true('model_fit' %in% class(knn_fit))320})321test_that('knn_fit does not include cancer_train dataset', {322expect_equal(digest(as.character(knn_fit$fit$data$Class)), '93ecaae439b9f4e8e4297d3a851929f9')323expect_equal(digest(int_round(sum(knn_fit$fit$data$Symmetry),2)), '1473d70e5646a26de3c52aa1abd85b1f')324expect_equal(digest(int_round(sum(knn_fit$fit$data$Radius),2)), '1473d70e5646a26de3c52aa1abd85b1f')325})326test_that('knn_fit does not contain knn_spec', {327expect_equal(digest(int_round(get_expr(knn_fit$spec$args$neighbors),2)), '51465273097370367115dfe0228831f3')328expect_equal(digest(as.character(get_expr(knn_fit$spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')329expect_equal(digest(knn_fit$spec$mode), 'f361ba6f6b32d068e56f61f53d35e26a')330})331print("Success!")332}333334test_3.3 <- function(){335test_that('Did not create an object named new_obs',{336expect_true(exists("new_obs"))337})338test_that('new_obs is not a tibble', {339expect_true('data.frame' %in% class(new_obs))340})341test_that('Wrong values for Symmetry and Radius', {342expect_equal(as.numeric(new_obs$Symmetry), 1)343expect_equal(as.numeric(new_obs$Radius), 0)344})345test_that('Did not create an object named class_prediction',{346expect_true(exists("class_prediction"))347})348test_that('Wrong class prediction', {349expect_equal(digest(as.character(class_prediction$.pred_class)), '5f0922939c45ef1054f852e83f91c660')350})351print("Success!")352}353354test_3.4 <- function(){355test_that('k should be 7', {356expect_equal(int_round(get_expr(knn_spec$args$neighbors),0), 7)357})358test_that('weight_func is incorrect', {359expect_equal(digest(as.character(get_expr(knn_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')360})361test_that('set_engine is incorrect', {362expect_equal(digest(as.character(knn_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')363})364test_that('mode is incorrect', {365expect_equal(digest(as.character(knn_spec$mode)), 'f361ba6f6b32d068e56f61f53d35e26a')366})367test_that('Did not create an object named knn_fit_2', {368expect_true(exists("knn_fit_2"))369})370test_that('knn_fit_2 should be a fit model.', {371expect_true('model_fit' %in% class(knn_fit_2))372})373test_that('knn_fit_2 does not include knn_train_2 dataset', {374expect_equal(digest(as.character(knn_fit_2$fit$data$Class)), '93ecaae439b9f4e8e4297d3a851929f9')375expect_equal(digest(int_round(sum(knn_fit_2$fit$data$Symmetry),2)), '1473d70e5646a26de3c52aa1abd85b1f')376expect_equal(digest(int_round(sum(knn_fit_2$fit$data$Radius),2)), '1473d70e5646a26de3c52aa1abd85b1f')377expect_equal(digest(int_round(sum(knn_fit_2$fit$data$Concavity),2)), '1473d70e5646a26de3c52aa1abd85b1f')378})379test_that('knn_fit_2 does not contain knn_spec_2', {380expect_equal(digest(as.numeric(get_expr(knn_fit_2$spec$args$neighbors))), '90a7653d717dc1553ee564aa27b749b9')381expect_equal(digest(as.character(get_expr(knn_fit_2$spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')382expect_equal(digest(knn_fit_2$spec$mode), 'f361ba6f6b32d068e56f61f53d35e26a')383})384test_that('Did not create an object named new_obs_2',{385expect_true(exists("new_obs_2"))386})387test_that('new_obs_2 is not a tibble', {388expect_true('data.frame' %in% class(new_obs_2))389})390test_that('Wrong values for Symmetry, Radius, and Concavity', {391expect_equal(int_round(new_obs_2$Symmetry, 0), 1)392expect_equal(int_round(new_obs_2$Radius, 0), 0)393expect_equal(int_round(new_obs_2$Concavity, 0), 1)394})395test_that('Did not create an object named class_prediction_2',{396expect_true(exists("class_prediction_2"))397})398test_that('Wrong class prediction', {399expect_equal(digest(as.character(class_prediction_2$.pred_class)), '5f0922939c45ef1054f852e83f91c660')400})401print("Success!")402}403404test_3.5 <- function(){405test_that('Did not create a object named knn_recipe', {406expect_true(exists("knn_recipe"))407})408test_that('knn_recipe is not a recipe object', {409expect_equal(digest(class(knn_recipe)), '4b3ed1334bff94d43e32a36a1f16a2f2')410})411print("Success!")412}413414test_3.6 <- function(){415test_that('Did not create an object named knn_workflow', {416expect_true(exists("knn_workflow"))417})418test_that('knn_workflow is not a workflow', {419expect_true('workflow' %in% class(knn_workflow))420})421test_that('knn_workflow does not contain the right model specification', {422expect_equal(int_round(get_expr(knn_workflow$fit$actions$model$spec$args$neighbors),0), 7)423})424test_that('Did not add knn_recipe', {425expect_true('recipe' %in% class(knn_workflow$pre$actions$recipe$recipe))426})427test_that('knn_recipe does not contain the cancer dataset', {428expect_equal(digest(int_round(sum(knn_workflow$pre$actions$recipe$recipe$template$Radius),2)), '1473d70e5646a26de3c52aa1abd85b1f')429expect_equal(digest(int_round(sum(knn_workflow$pre$actions$recipe$recipe$template$Area),2)), '1473d70e5646a26de3c52aa1abd85b1f')430})431print("Success!")432}433434test_3.7 <- function(){435test_that('Did not create an object named class_prediction_all',{436expect_true(exists("class_prediction_all"))437})438test_that('Wrong class prediction', {439expect_equal(digest(as.character(class_prediction_all$.pred_class)), '3a5505c06543876fe45598b5e5e5195d')440})441print("Success!")442}443444test_4.0 <- function(){445test_that('Solution is incorrect', {446expect_equal(digest(as.character(answer4.0)), '75f1160e72554f4270c809f041c7a776')447})448print("Success!")449}450451test_4.1 <- function(){452test_that('Solution is incorrect', {453expect_equal(digest(as.character(answer4.1)), '475bf9280aab63a82af60791302736f6')454})455print("Success!")456}457458459