Path: blob/master/2019-fall/materials/worksheet_06/tests_worksheet_06.R
2051 views
library(testthat)1library(digest)23test_0.1 <- function(){4test_that('Solution is incorrect', {5expect_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 test67})8print("Success!")9}1011test_0.2 <- function(){12test_that('Solution is incorrect', {13expect_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 test1415})16print("Success!")17}181920test_1.0 <- function(){21test_that('Did not create an object named cancer', {22expect_true(exists("cancer"))23})24test_that('cancer should be a data frame.', {25expect_true('data.frame' %in% class(cancer))26})27test_that('cancer does not contain the correct number of rows and/or columns.', {28expect_equal(dim(cancer), c(569, 12))29})30test_that('cancer does not contain the correct data.', {31expect_equal(digest(as.numeric(sum(cancer$Area))), 'a2c1855f3fa92423aa169c350fc95232')32expect_equal(colnames(cancer), c("ID", "Class", "Radius", "Texture", "Perimeter", "Area", "Smoothness", "Compactness", "Concavity", "Concave_points", "Symmetry", "Fractal_dimension"))33})34print("Success!")35}3637test_1.1 <- function(){38test_that('Solution is incorrect', {39expect_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 test4041})42print("Success!")43}4445test_1.2 <- function(){46properties <- c(cancer_plot$layers[[1]]$mapping, cancer_plot$mapping)47labels <- cancer_plot$labels48test_that('Did not create a plot named cancer_plot', {49expect_true(exists("cancer_plot"))50})51test_that('Symmetry should be on the x-axis.', {52expect_that("Symmetry" == rlang::get_expr(properties$x), is_true())53})54test_that('Radius should be on the y-axis.', {55expect_that("Radius" == rlang::get_expr(properties$y) , is_true())56})57test_that('cancer_plot should be a scatter plot.', {58expect_that("GeomPoint" %in% class(cancer_plot$layers[[1]]$geom) , is_true())59})60test_that('cancer_plot should map Class to colour.', {61expect_true(digest(rlang::get_expr(properties$colour)) %in% c('a4abb3d43fde633563dd1f5c3ea31f31', 'f9e884084b84794d762a535f3facec85'))62})63print("Success!")64}6566test_1.3 <- function(){67test_that('Solution is incorrect', {68expect_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 test6970})71print("Success!")72}7374test_1.4 <- function(){75test_that('xa is incorrect.', {76expect_equal(digest(as.numeric(xa)), '218983ef51880f784c62ff2aedc196f3')77})78test_that('ya is incorrect.', {79expect_equal(digest(as.numeric(ya)), 'a1914c10445a398934c0e0015b9b18ae')80})81test_that('xb is incorrect.', {82expect_equal(digest(as.numeric(xb)), '5b34d8796880663f75ea423ccb4ea8cd')83})84test_that('yb is incorrect.', {85expect_equal(digest(as.numeric(yb)), '4490c7a115f39cede8cd353713230e95')86})87print("Success!")88}8990test_1.5 <- function(){91test_that('answer1.5 is incorrect', {92expect_equal(digest(as.numeric(answer1.5)), 'ab39ff487bddaa92a62eadbbe3e46da6') # we hid the answer to the test here so you can't see it, but we can still run the test9394})95print("Success!")96}9798test_1.6 <- function(){99test_that('zb is incorrect.', {100expect_equal(digest(as.numeric(zb)), 'b62bcabaf783e3e2d0745ca4a41219da')101})102test_that('za is incorrect.', {103expect_equal(digest(as.numeric(za)), '8f22ef4a815b2e1bd4f7ec511bbc30f2')104})105print("Success!")106}107108test_1.7 <- function(){109test_that('answer1.7 is incorrect', {110expect_equal(digest(as.numeric(answer1.7)), '97c5e6129bc96a23ed7298d78bf7f8b2') # we hid the answer to the test here so you can't see it, but we can still run the test111112})113print("Success!")114}115116117118test_1.8 <- function(){119test_that('point_a is incorrect.', {120expect_equal(digest(as.numeric(sum(point_a))), '309d3b37c196b24341299aabdac15644')121})122test_that('point_b is incorrect.', {123expect_equal(digest(as.numeric(sum(point_b))), '00bb41bc0f538b06f627ffbd9874a6a8')124})125print("Success!")126}127128test_1.9 <- function(){129test_that('difference is incorrect', {130expect_equal(digest(as.numeric(sum(difference))), 'ef1fc2c1e06df149b42dcfb47596319f') # we hid the answer to the test here so you can't see it, but we can still run the test131132})133print("Success!")134}135136test_1.10 <- function(){137test_that('dif_square is incorrect', {138expect_equal(digest(as.numeric(sum(dif_square))), '0299530505a02b47c2a30af0ecd6026b') # we hid the answer to the test here so you can't see it, but we can still run the test139140})141print("Success!")142}143144test_1.10.1 <- function(){145test_that('dif_sum is incorrect', {146expect_equal(digest(as.numeric(dif_sum)), '0299530505a02b47c2a30af0ecd6026b') # we hid the answer to the test here so you can't see it, but we can still run the test147148})149print("Success!")150}151152test_1.10.2 <- function(){153test_that('root_dif_sum is incorrect', {154expect_equal(digest(as.numeric(root_dif_sum)), '97c5e6129bc96a23ed7298d78bf7f8b2') # we hid the answer to the test here so you can't see it, but we can still run the test155156})157print("Success!")158}159160test_1.10.3 <- function(){161test_that('dist_cancer_two_rows is incorrect', {162expect_equal(digest(as.numeric(dist_cancer_two_rows)), '97c5e6129bc96a23ed7298d78bf7f8b2') # we hid the answer to the test here so you can't see it, but we can still run the test163164})165print("Success!")166}167168169test_1.10.4 <- function(){170test_that('Solution is incorrect', {171expect_equal(digest(answer1.10.4), '05ca18b596514af73f6880309a21b5dd') # we hid the answer to the test here so you can't see it, but we can still run the test172173})174print("Success!")175}176177test_2.0 <- function(){178properties <- c(small_sample_plot$layers[[1]]$mapping, small_sample_plot$mapping)179labels <- small_sample_plot$labels180test_that('Did not create an object named small_sample', {181expect_true(exists("small_sample"))182})183test_that('small_sample should be a data frame.', {184expect_true('data.frame' %in% class(small_sample))185})186test_that('small_sample does not contain the correct number of rows and/or columns.', {187expect_equal(dim(small_sample), c(5, 3))188})189test_that('small_sample does not contain the correct data.', {190expect_equal(digest(as.numeric(sum(small_sample$Symmetry))), '9335520ab7f7f606573cad5c82a24542')191})192test_that('Did not create a plot named small_sample_plot', {193expect_true(exists("small_sample_plot"))194})195test_that('Did not use small_sample data to create small_sample_plot', {196expect_equal(digest(small_sample_plot$data$Symmetry), 'f4c490a7955304fbdeb905214ea0f3d3')197})198test_that('Symmetry should be on the x-axis.', {199expect_that("Symmetry" == rlang::get_expr(properties$x), is_true())200})201test_that('Radius should be on the y-axis.', {202expect_that("Radius" == rlang::get_expr(properties$y) , is_true())203})204test_that('small_sample_plot should be a scatter plot.', {205expect_that("GeomPoint" %in% c(class(small_sample_plot$layers[[1]]$geom)) , is_true())206})207test_that('small_sample_plot should map Benign / Malignant to colour.', {208expect_that("Class" == rlang::get_expr(properties$colour) , is_true())209})210print("Success!")211}212213test_2.1 <- function(){214test_that('Did not create an object named newData', {215expect_true(exists("newData"))216})217test_that('newData should be a data frame.', {218expect_true('data.frame' %in% class(newData))219})220test_that('The last row of the Class column should be unknown.', {221expect_equal(newData$Class[6], 'unknown')222})223test_that('newData does not contain the correct number of rows and/or columns.', {224expect_equal(dim(newData), c(6, 3))225})226test_that('small_sample does not contain the correct data.', {227expect_equal(digest(sum(as.numeric(newData$Radius))), '291661ebdc869b7f43db4135b6dff842')228expect_equal(digest(sum(as.numeric(newData$Symmetry))), '9335520ab7f7f606573cad5c82a24542')229})230print("Success!")231}232233test_2.2 <- function(){234test_that('Did not create an object named dist_matrix', {235expect_true(exists("dist_matrix"))236})237test_that('dist_matrix should be a matrix.', {238expect_true('matrix' %in% class(dist_matrix))239})240test_that('dist_matrix does not contain the correct number of rows and/or columns.', {241expect_equal(dim(dist_matrix), c(6, 6))242})243test_that('dist_matrix does not contain the correct data.', {244expect_equal(digest(as.numeric(sum(dist_matrix[1, ]))), digest(sum(dist_matrix[, 1])))245expect_equal(digest(as.numeric(sum(dist_matrix[2, ]))), digest(sum(dist_matrix[, 2])))246expect_equal(digest(as.numeric(sum(dist_matrix[5, ]))), digest(sum(dist_matrix[, 5])))247expect_equal(digest(as.numeric(sum(dist_matrix[6, ]))), digest(sum(dist_matrix[, 6])))248})249print("Success!")250}251252test_2.3 <- function(){253test_that('Solution is incorrect', {254expect_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 test255256})257print("Success!")258}259260test_2.4 <- function(){261test_that('Solution is incorrect', {262expect_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 test263264})265print("Success!")266}267268test_2.5 <- function(){269test_that('Solution is incorrect', {270expect_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 test271272})273print("Success!")274}275276test_2.6 <- function(){277test_that('Solution is incorrect', {278expect_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 test279280})281print("Success!")282}283284test_2.7 <- function(){285test_that('Solution is incorrect', {286expect_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 test287288})289print("Success!")290}291292293294test_3.0 <- function(){295test_that('Did not create an object named X_train', {296expect_true(exists("X_train"))297})298test_that('X_train should be a data frame.', {299expect_true('data.frame' %in% class(X_train))300})301test_that('X_train does not contain the correct number of rows and/or columns.', {302expect_equal(dim(X_train), c(569, 2))303})304test_that('X_train does not contain the column(s) Symmetry and/or Radius.', {305expect_true('Symmetry' %in% colnames(X_train))306expect_true('Radius' %in% colnames(X_train))307})308test_that('Did not create an object named Y_train', {309expect_true(exists("Y_train"))310})311test_that('Y_train should be a character.', {312expect_true('character' %in% class(Y_train))313})314test_that('Y_train is not the correct length.', {315expect_equal(length(Y_train), 569)316})317print("Success!")318}319320test_3.1 <- function(){321test_that('method should be knn', {322expect_equal(as.character(model_knn$method), 'knn')323})324test_that('k should be 7', {325expect_equal(as.numeric(model_knn$results$k), 7)326})327test_that('model_knn contains incorrect information.', {328expect_equal(digest(as.numeric(sum(model_knn$trainingData$Symmetry))), '47d0e881a9a1b19e57f9c068c08765fa')329expect_equal(digest(as.numeric(sum(model_knn$trainingData$Radius))), '5818709a65b4a5df9cb392b9cc66e32b')330expect_equal(as.numeric(summary(model_knn$trainingData$.outcome)[1]), 357)331})332print("Success!")333}334335test_3.2 <- function(){336test_that('Did not create an object named new_obs', {337expect_true(exists("new_obs"))338})339test_that('new_obs should be a data frame.', {340expect_true('data.frame' %in% class(new_obs))341})342test_that('Did not create an object named predicted_knn_7', {343expect_true(exists("predicted_knn_7"))344})345test_that('predicted_knn_7 should be a factor.', {346expect_true('factor' %in% class(predicted_knn_7))347})348test_that('predicted_knn_7 prediction is incorrect.', {349expect_equal(digest(as.character(predicted_knn_7)), '5f0922939c45ef1054f852e83f91c660')350})351print("Success!")352}353354test_3.3 <- function(){355test_that('Did not create an object named X_train_3', {356expect_true(exists("X_train_3"))357})358test_that('X_train_3 should be a data frame.', {359expect_true('data.frame' %in% class(X_train_3))360})361test_that('X_train_3 does not contain the correct number of rows and/or columns.', {362expect_equal(dim(X_train_3), c(569, 3))363})364test_that('Did not create an object named Y_train_3', {365expect_true(exists("Y_train_3"))366})367test_that('Y_train_3 should be a character.', {368expect_true('character' %in% class(Y_train_3))369})370test_that('Y_train_3 is not the correct length.', {371expect_equal(length(Y_train_3), 569)372})373test_that('Did not create an object named new_obs_3', {374expect_true(exists("new_obs_3"))375})376test_that('new_obs_3 should be a data frame.', {377expect_true('data.frame' %in% class(new_obs_3))378})379test_that('Did not create an object named predicted_3_knn_7', {380expect_true(exists("predicted_3_knn_7"))381})382test_that('predicted_3_knn_7 should be a factor.', {383expect_true('factor' %in% class(predicted_3_knn_7))384})385test_that('predicted_3_knn_7 prediction is incorrect.', {386expect_equal(digest(as.character(predicted_3_knn_7)), '5f0922939c45ef1054f852e83f91c660')387})388print("Success!")389}390391test_3.4 <- function(){392test_that('Did not create an object named X_train_all', {393expect_true(exists("X_train_all"))394})395test_that('X_train_all should be a data frame.', {396expect_true('data.frame' %in% class(X_train_all))397})398test_that('X_train_all does not contain the correct number of rows and/or columns.', {399expect_equal(dim(X_train_all), c(569, 10))400})401test_that('Did not create an object named Y_train_all', {402expect_true(exists("Y_train_all"))403})404test_that('Y_train_all should be a character.', {405expect_true('character' %in% class(Y_train_all))406})407test_that('Y_train_all is not the correct length.', {408expect_equal(length(Y_train_all), 569)409})410test_that('Did not create an object named new_obs_all', {411expect_true(exists("new_obs_all"))412})413test_that('new_obs_all should be a data frame.', {414expect_true('data.frame' %in% class(new_obs_all))415})416test_that('Did not create an object named predicted_all_knn_7', {417expect_true(exists("predicted_all_knn_7"))418})419test_that('predicted_all_knn_7 should be a factor.', {420expect_true('factor' %in% class(predicted_all_knn_7))421})422test_that('predicted_all_knn_7 prediction is incorrect.', {423expect_equal(digest(as.character(predicted_all_knn_7)), '3a5505c06543876fe45598b5e5e5195d')424})425print("Success!")426}427428test_4.0 <- function(){429test_that('Solution is incorrect', {430expect_equal(digest(as.character(answer4.0)), '75f1160e72554f4270c809f041c7a776')431})432print("Success!")433}434435test_4.1 <- function(){436test_that('Solution is incorrect', {437expect_equal(digest(as.character(answer4.1)), '475bf9280aab63a82af60791302736f6')438})439print("Success!")440}441442