Path: blob/master/2021-fall/materials/worksheet_06/tests_worksheet_06.R
2051 views
library(testthat)1library(digest)2library(rlang)34# Round double to precise integer5#6# `int_round` works to create an integer corresponding to a number that is7# tested up to a particular decimal point of precision. This is useful when8# there is a need to compare a numeric value using hashes.9#10# @param x Double vector of length one.11# @param digits Double vector of length one to specify decimal point of precision. Negative numbers can be used to specifying significant digits > 0.1.12#13# @return Integer vector of length one corresponding to a particular decimal point of precision.14#15# @examples16# # to get an integer up to two decimals of precision from 234.5678917# int_round(234.56789, 2)18#19# to get an integer rounded to the hundred digit from 234.5678920# int_round(234.56789, -2)21int_round <- function(x, digits){22x = x * 10^digits23xint = as.integer(x)24xint1 = xint + 1L25if (abs(xint - x) < abs(xint1 - x)){26return(xint)27}28else {29return(xint1)30}31}3233test_0.1 <- function(){34test_that('Solution is incorrect', {35expect_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 test36})37print("Success!")38}3940test_0.2 <- function(){41test_that('Solution is incorrect', {42expect_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 test43})44print("Success!")45}4647test_1.0 <- function(){48test_that('Did not create an object named cancer', {49expect_true(exists("cancer"))50})51test_that('cancer should be a data frame.', {52expect_true('data.frame' %in% class(cancer))53})54test_that('cancer does not contain the correct number of rows and/or columns.', {55expect_equal(dim(cancer), c(569, 12))56})57test_that('cancer does not contain the correct data.', {58expect_equal(digest(int_round(sum(cancer$Area), 2)), '1473d70e5646a26de3c52aa1abd85b1f')59expect_equal(colnames(cancer), c("ID", "Class", "Radius", "Texture", "Perimeter", "Area", "Smoothness", "Compactness", "Concavity", "Concave_points", "Symmetry", "Fractal_dimension"))60})61test_that('read.csv() instead of read_csv() function is used.', {62expect_true(class(cancer$Class) == 'character')63})64print("Success!")65}6667test_1.1 <- function(){68if (digest(answer1.1) == '05ca18b596514af73f6880309a21b5dd'){69print("Look at the values in the Area column - are they categorical? Remember, classification problems involve predicting class labels for categorical data.")70}71test_that('Solution is incorrect', {72expect_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 test73})74print("Success!")75}7677test_1.2 <- function(){78properties <- c(cancer_plot$layers[[1]]$mapping, cancer_plot$mapping)79labels <- cancer_plot$labels80test_that('Did not create a plot named cancer_plot', {81expect_true(exists("cancer_plot"))82})83test_that('Symmetry should be on the x-axis.', {84expect_true("Symmetry" == rlang::get_expr(properties$x))85})86test_that('Radius should be on the y-axis.', {87expect_true("Radius" == rlang::get_expr(properties$y))88})89test_that('Points should be coloured by Class.', {90expect_true("Class" == rlang::get_expr(properties$colour))91})92test_that('cancer_plot should be a scatter plot.', {93expect_true("GeomPoint" %in% class(cancer_plot$layers[[1]]$geom))94})95test_that('cancer_plot should map Class to colour.', {96expect_true(digest(rlang::get_expr(properties$colour)) %in% c('a4abb3d43fde633563dd1f5c3ea31f31', 'f9e884084b84794d762a535f3facec85'))97})98test_that('axis labels do not state that the data is standardized (which it is!)', {99expect_true(labels$x != "Symmetry")100expect_true(labels$y != "Radius")101})102print("Success!")103}104105test_1.3 <- function(){106test_that('Solution is incorrect', {107expect_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 test108})109print("Success!")110}111112test_1.4 <- function(){113test_that('xa is incorrect.', {114expect_equal(digest(int_round(xa, 2)), 'c0048c4f8677b795155d8aa41e26a54d')115})116test_that('ya is incorrect.', {117expect_equal(digest(int_round(ya, 2)), 'a6e8462a7cace5673e544d1e8d238b52')118})119test_that('xb is incorrect.', {120expect_equal(digest(int_round(xb, 2)), '10aeddd8594c6ce210c731b8b94af435')121})122test_that('yb is incorrect.', {123expect_equal(digest(int_round(yb, 2)), '48139aad2994737e7e801156a24281ed')124})125print("Success!")126}127128test_1.5 <- function(){129test_that('answer1.5 is incorrect', {130expect_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 test131})132print("Success!")133}134135test_1.6 <- function(){136test_that('Did not create an object named zb', {137expect_true(exists('zb'))138})139test_that('zb is incorrect.', {140expect_equal(digest(int_round(zb,2)), 'b78a46ebc0bb9a4cc7f4f4b962f0b2ef')141})142test_that('Did not create an object named za', {143expect_true(exists('za'))144})145test_that('za is incorrect.', {146expect_equal(digest(int_round(za,2)), 'b35d8adab2b7c839e5a8e2861080b03e')147})148print("Success!")149}150151test_1.7 <- function(){152test_that('answer1.7 is incorrect', {153expect_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 test154})155print("Success!")156}157158test_1.8 <- function(){159test_that('point_a is incorrect.', {160expect_equal(digest(int_round(sum((point_a)),2)), '44014eaa19f1aef8e92b1020c47d662b')161})162test_that('point_b is incorrect.', {163expect_equal(digest(int_round(sum((point_b)),2)), 'e064b40c9ca28b04b874bcd8bdefa41e')164})165print("Success!")166}167168test_1.09 <- function(){169test_that('dif_square is incorrect', {170expect_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 test171})172print("Success!")173}174175176test_1.09.1 <- function(){177test_that('dif_sum is incorrect', {178expect_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 test179})180print("Success!")181}182183test_1.09.2 <- function(){184test_that('root_dif_sum is incorrect', {185expect_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 test186})187print("Success!")188}189190test_1.09.3 <- function(){191test_that('dist_cancer_two_rows is incorrect', {192expect_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 test193194})195print("Success!")196}197198199test_1.09.4 <- function(){200test_that('Solution is incorrect', {201expect_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 test202203})204print("Success!")205}206207test_2.0.0 <- function(){208test_that('Did not create an object named small_sample', {209expect_true(exists("small_sample"))210})211test_that('small_sample should be a data frame.', {212expect_true('data.frame' %in% class(small_sample))213})214test_that('small_sample does not contain the correct number of rows and/or columns.', {215expect_equal(dim(small_sample), c(5, 3))216})217test_that('small_sample does not contain the correct columns.', {218expect_true('Symmetry' %in% colnames(small_sample))219expect_true('Radius' %in% colnames(small_sample))220expect_true('Class' %in% colnames(small_sample))221})222print("Success!")223}224225226test_2.0.1 <- function(){227properties <- c(small_sample_plot$layers[[1]]$mapping, small_sample_plot$mapping)228labels <- small_sample_plot$labels229test_that('Did not create a plot named small_sample_plot', {230expect_true(exists("small_sample_plot"))231})232test_that('Did not use small_sample data to create small_sample_plot', {233expect_equal(digest(int_round(sum(small_sample_plot$data$Symmetry),2)), '727b6cd45f0340de38d1cfe8403adb3e')234})235test_that('Symmetry should be on the x-axis.', {236expect_true("Symmetry" == rlang::get_expr(properties$x))237})238test_that('Radius should be on the y-axis.', {239expect_true("Radius" == rlang::get_expr(properties$y))240})241test_that('small_sample_plot should be a scatter plot.', {242expect_true("GeomPoint" %in% c(class(small_sample_plot$layers[[1]]$geom)))243})244test_that('small_sample_plot should map Benign / Malignant to colour.', {245expect_true("Class" == rlang::get_expr(properties$colour))246})247test_that('axis labels do not state that the data is standardized (which it is!)', {248expect_true(labels$x != "Symmetry")249expect_true(labels$y != "Radius")250})251print("Success!")252}253254test_2.1 <- function(){255test_that('Did not create an object named newData', {256expect_true(exists("newData"))257})258test_that('newData should be a data frame.', {259expect_true('data.frame' %in% class(newData))260})261test_that('The last row of the Class column should be "unknown".', {262expect_equal((newData$Class[6]), 'unknown')263})264test_that('newData does not contain the correct number of rows and/or columns.', {265expect_equal(dim(newData), c(6, 3))266})267test_that('small_sample does not contain the correct data.', {268expect_equal(digest(int_round(sum(newData$Radius),2)), '740dbeffda6d0ffb1b86f797df4c2a25')269expect_equal(digest(int_round(sum(newData$Symmetry),2)), 'a14e0862232f39bc203a2c5021371b54')270})271print("Success!")272}273274test_2.2 <- function(){275test_that('Did not create an object named dist_matrix', {276expect_true(exists("dist_matrix"))277})278test_that('dist_matrix should be a matrix.', {279expect_true('matrix' %in% class(dist_matrix))280})281test_that('dist_matrix does not contain the correct number of rows and/or columns.', {282expect_equal(dim(dist_matrix), c(6, 6))283})284test_that('dist_matrix does not contain the correct data.', {285expect_equal(digest(int_round(sum(dist_matrix[1, ]),2)), '8435efbd9cf83356ac4a26c6889c8fa5')286expect_equal(digest(int_round(sum(dist_matrix[2, ]),2)), 'abc3f5458b96456d63865f0922593548')287expect_equal(digest(int_round(sum(dist_matrix[5, ]),2)), 'c3ad708acb2b90a9e40e48f729083e69')288expect_equal(digest(int_round(sum(dist_matrix[6, ]),2)), 'f0f12367a5beee1f65d2633294474dc9')289})290print("Success!")291}292293test_2.3 <- function(){294if (typeof(answer2.3) == 'double') {295print("Remember to surround your answer with quotes!")296}297test_that('Solution is incorrect', {298expect_equal(digest(answer2.3), 'ee340e888492be0703f2bcc9abfb390c') # we hid the answer to the test here so you can't see it, but we can still run the test299})300print("Success!")301}302303test_2.4 <- function(){304test_that('Solution is incorrect', {305expect_equal(digest(answer2.4), '891e8a631267b478c03e25594808709d') # we hid the answer to the test here so you can't see it, but we can still run the test306})307print("Success!")308}309310test_2.5 <- function(){311test_that('Solution is incorrect', {312expect_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 test313})314print("Success!")315}316317test_2.6 <- function(){318test_that('Solution is incorrect', {319expect_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 test320321})322print("Success!")323}324325test_2.7 <- function(){326test_that('Solution is incorrect', {327expect_equal(digest(answer2.7), '863dfc36ab2bfe97404cc8fc074a5241') # we hid the answer to the test here so you can't see it, but we can still run the test328329})330print("Success!")331}332333test_3.1 <- function(){334test_that('Did not create an object named knn_spec', {335expect_true(exists("knn_spec"))336})337test_that('k should be 7', {338expect_equal(as.numeric(get_expr(knn_spec$args$neighbors)), 7)339})340test_that('weight_func is incorrect', {341expect_equal(digest(as.character(get_expr(knn_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')342})343test_that('set_engine is incorrect', {344expect_equal(digest(as.character(knn_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')345})346test_that('mode is incorrect', {347expect_equal(digest(as.character(knn_spec$mode)), 'f361ba6f6b32d068e56f61f53d35e26a')348})349print("Success!")350}351352test_3.2 <- function(){353test_that('Did not create an object named knn_fit', {354expect_true(exists("knn_fit"))355})356test_that('knn_fit should be a fit model.', {357expect_true('model_fit' %in% class(knn_fit))358})359test_that('knn_fit does not include cancer_train dataset', {360expect_equal(digest(as.character(knn_fit$fit$data$Class)), '93ecaae439b9f4e8e4297d3a851929f9')361expect_equal(digest(int_round(sum(knn_fit$fit$data$Symmetry),2)), '1473d70e5646a26de3c52aa1abd85b1f')362expect_equal(digest(int_round(sum(knn_fit$fit$data$Radius),2)), '1473d70e5646a26de3c52aa1abd85b1f')363})364test_that('knn_fit does not contain knn_spec', {365expect_equal(digest(int_round(get_expr(knn_fit$spec$args$neighbors),2)), '51465273097370367115dfe0228831f3')366expect_equal(digest(as.character(get_expr(knn_fit$spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')367expect_equal(digest(knn_fit$spec$mode), 'f361ba6f6b32d068e56f61f53d35e26a')368})369test_that('knn_fit does not use the correct columns and/or the correct model formula', {370expect_setequal(c('Class', 'Radius', 'Symmetry'), row.names(attributes(knn_fit$fit$terms)$factors))371})372print("Success!")373}374375test_3.3 <- function(){376test_that('Did not create an object named new_obs',{377expect_true(exists("new_obs"))378})379test_that('new_obs is not a tibble', {380expect_true('data.frame' %in% class(new_obs))381})382test_that('Wrong values for Symmetry and Radius', {383expect_equal(as.numeric(new_obs$Symmetry), 1)384expect_equal(as.numeric(new_obs$Radius), 0)385})386test_that('Did not create an object named class_prediction',{387expect_true(exists("class_prediction"))388})389test_that('Wrong class prediction', {390expect_equal(digest(as.character(class_prediction$.pred_class)), '5f0922939c45ef1054f852e83f91c660')391})392print("Success!")393}394395test_3.4 <- function(){396test_that('k should be 7', {397expect_equal(int_round(get_expr(knn_spec$args$neighbors),0), 7)398})399test_that('weight_func is incorrect', {400expect_equal(digest(as.character(get_expr(knn_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')401})402test_that('set_engine is incorrect', {403expect_equal(digest(as.character(knn_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')404})405test_that('mode is incorrect', {406expect_equal(digest(as.character(knn_spec$mode)), 'f361ba6f6b32d068e56f61f53d35e26a')407})408test_that('Did not create an object named knn_fit_2', {409expect_true(exists("knn_fit_2"))410})411test_that('knn_fit_2 should be a fit model.', {412expect_true('model_fit' %in% class(knn_fit_2))413})414test_that('knn_fit_2 does not use the correct columns and/or the correct model formula', {415expect_setequal(c('Class', 'Radius', 'Symmetry', 'Concavity'), row.names(attributes(knn_fit_2$fit$terms)$factors))416})417test_that('knn_fit_2 does not include knn_train_2 dataset', {418expect_equal(digest(as.character(knn_fit_2$fit$data$Class)), '93ecaae439b9f4e8e4297d3a851929f9')419expect_equal(digest(int_round(sum(knn_fit_2$fit$data$Symmetry),2)), '1473d70e5646a26de3c52aa1abd85b1f')420expect_equal(digest(int_round(sum(knn_fit_2$fit$data$Radius),2)), '1473d70e5646a26de3c52aa1abd85b1f')421expect_equal(digest(int_round(sum(knn_fit_2$fit$data$Concavity),2)), '1473d70e5646a26de3c52aa1abd85b1f')422})423test_that('knn_fit_2 does not contain knn_spec_2', {424expect_equal(digest(as.numeric(get_expr(knn_fit_2$spec$args$neighbors))), '90a7653d717dc1553ee564aa27b749b9')425expect_equal(digest(as.character(get_expr(knn_fit_2$spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')426expect_equal(digest(knn_fit_2$spec$mode), 'f361ba6f6b32d068e56f61f53d35e26a')427})428test_that('Did not create an object named new_obs_2',{429expect_true(exists("new_obs_2"))430})431test_that('new_obs_2 is not a tibble', {432expect_true('data.frame' %in% class(new_obs_2))433})434test_that('Wrong values for Symmetry, Radius, and Concavity', {435expect_equal(int_round(new_obs_2$Symmetry, 0), 1)436expect_equal(int_round(new_obs_2$Radius, 0), 0)437expect_equal(int_round(new_obs_2$Concavity, 0), 1)438})439test_that('Did not create an object named class_prediction_2',{440expect_true(exists("class_prediction_2"))441})442test_that('Wrong class prediction', {443expect_equal(digest(as.character(class_prediction_2$.pred_class)), '5f0922939c45ef1054f852e83f91c660')444})445print("Success!")446}447448test_3.5 <- function(){449test_that('Did not create a object named knn_recipe', {450expect_true(exists("knn_recipe"))451})452test_that('knn_recipe is not a recipe object', {453expect_equal(digest(class(knn_recipe)), '4b3ed1334bff94d43e32a36a1f16a2f2')454})455test_that('knn_recipe does not remove ID', {456expect_false("ID" %in% (knn_recipe %>% prep() %>% bake(cancer) %>% colnames()))457})458test_that('cancer does not contain the correct data.', {459expect_equal(dim(bake(prep(knn_recipe), cancer)), c(569,11))460})461print("Success!")462}463464test_3.6 <- function(){465test_that('Did not create an object named knn_workflow', {466expect_true(exists("knn_workflow"))467})468test_that('knn_workflow is not a workflow', {469expect_true('workflow' %in% class(knn_workflow))470})471test_that('knn_workflow does not contain the right model specification', {472expect_equal(int_round(get_expr(knn_workflow$fit$actions$model$spec$args$neighbors),0), 7)473})474test_that('Did not add knn_recipe', {475expect_true('recipe' %in% class(knn_workflow$pre$actions$recipe$recipe))476})477test_that('knn_recipe does not contain the cancer dataset', {478expect_equal(digest(int_round(sum(knn_workflow$pre$actions$recipe$recipe$template$Radius),2)), '1473d70e5646a26de3c52aa1abd85b1f')479expect_equal(digest(int_round(sum(knn_workflow$pre$actions$recipe$recipe$template$Area),2)), '1473d70e5646a26de3c52aa1abd85b1f')480})481print("Success!")482}483484test_3.7 <- function(){485test_that('Did not create an object named class_prediction_all',{486expect_true(exists("class_prediction_all"))487})488test_that('Wrong class prediction', {489expect_equal(digest(as.character(class_prediction_all$.pred_class)), '3a5505c06543876fe45598b5e5e5195d')490})491print("Success!")492}493494test_4.0 <- function(){495test_that('Solution is incorrect', {496expect_equal(digest(as.character(answer4.0)), '75f1160e72554f4270c809f041c7a776')497})498print("Success!")499}500501test_4.1 <- function(){502test_that('Solution is incorrect', {503expect_equal(digest(as.character(answer4.1)), '475bf9280aab63a82af60791302736f6')504})505print("Success!")506}507508509