Path: blob/master/2020-spring/materials/worksheet_06/tests_worksheet_06.R
2715 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.0 <- function(){178test_that('Did not create an object named small_sample', {179expect_true(exists("small_sample"))180})181test_that('small_sample should be a data frame.', {182expect_true('data.frame' %in% class(small_sample))183})184test_that('small_sample does not contain the correct number of rows and/or columns.', {185expect_equal(dim(small_sample), c(5, 3))186})187test_that('small_sample does not contain the correct data.', {188expect_equal(digest(as.numeric(sum(small_sample$Symmetry))), '9335520ab7f7f606573cad5c82a24542')189})190print("Success!")191}192193194test_2.0.1 <- function(){195properties <- c(small_sample_plot$layers[[1]]$mapping, small_sample_plot$mapping)196labels <- small_sample_plot$labels197test_that('Did not create a plot named small_sample_plot', {198expect_true(exists("small_sample_plot"))199})200test_that('Did not use small_sample data to create small_sample_plot', {201expect_equal(digest(small_sample_plot$data$Symmetry), 'f4c490a7955304fbdeb905214ea0f3d3')202})203test_that('Symmetry should be on the x-axis.', {204expect_that("Symmetry" == rlang::get_expr(properties$x), is_true())205})206test_that('Radius should be on the y-axis.', {207expect_that("Radius" == rlang::get_expr(properties$y) , is_true())208})209test_that('small_sample_plot should be a scatter plot.', {210expect_that("GeomPoint" %in% c(class(small_sample_plot$layers[[1]]$geom)) , is_true())211})212test_that('small_sample_plot should map Benign / Malignant to colour.', {213expect_that("Class" == rlang::get_expr(properties$colour) , is_true())214})215print("Success!")216}217218test_2.1 <- function(){219test_that('Did not create an object named newData', {220expect_true(exists("newData"))221})222test_that('newData should be a data frame.', {223expect_true('data.frame' %in% class(newData))224})225test_that('The last row of the Class column should be unknown.', {226expect_equal(newData$Class[6], 'unknown')227})228test_that('newData does not contain the correct number of rows and/or columns.', {229expect_equal(dim(newData), c(6, 3))230})231test_that('small_sample does not contain the correct data.', {232expect_equal(digest(sum(as.numeric(newData$Radius))), '291661ebdc869b7f43db4135b6dff842')233expect_equal(digest(sum(as.numeric(newData$Symmetry))), '9335520ab7f7f606573cad5c82a24542')234})235print("Success!")236}237238test_2.2 <- function(){239test_that('Did not create an object named dist_matrix', {240expect_true(exists("dist_matrix"))241})242test_that('dist_matrix should be a matrix.', {243expect_true('matrix' %in% class(dist_matrix))244})245test_that('dist_matrix does not contain the correct number of rows and/or columns.', {246expect_equal(dim(dist_matrix), c(6, 6))247})248test_that('dist_matrix does not contain the correct data.', {249expect_equal(digest(as.numeric(sum(dist_matrix[1, ]))), digest(sum(dist_matrix[, 1])))250expect_equal(digest(as.numeric(sum(dist_matrix[2, ]))), digest(sum(dist_matrix[, 2])))251expect_equal(digest(as.numeric(sum(dist_matrix[5, ]))), digest(sum(dist_matrix[, 5])))252expect_equal(digest(as.numeric(sum(dist_matrix[6, ]))), digest(sum(dist_matrix[, 6])))253})254print("Success!")255}256257test_2.3 <- function(){258test_that('Solution is incorrect', {259expect_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 test260261})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 test268269})270print("Success!")271}272273test_2.5 <- function(){274test_that('Solution is incorrect', {275expect_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 test276277})278print("Success!")279}280281test_2.6 <- function(){282test_that('Solution is incorrect', {283expect_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 test284285})286print("Success!")287}288289test_2.7 <- function(){290test_that('Solution is incorrect', {291expect_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 test292293})294print("Success!")295}296297298299test_3.0 <- function(){300test_that('Did not create an object named X_train', {301expect_true(exists("X_train"))302})303test_that('X_train should be a data frame.', {304expect_true('data.frame' %in% class(X_train))305})306test_that('X_train does not contain the correct number of rows and/or columns.', {307expect_equal(dim(X_train), c(569, 2))308})309test_that('X_train does not contain the column(s) Symmetry and/or Radius.', {310expect_true('Symmetry' %in% colnames(X_train))311expect_true('Radius' %in% colnames(X_train))312})313test_that('Did not create an object named Y_train', {314expect_true(exists("Y_train"))315})316test_that('Y_train should be a character.', {317expect_true('character' %in% class(Y_train))318})319test_that('Y_train is not the correct length.', {320expect_equal(length(Y_train), 569)321})322print("Success!")323}324325test_3.1 <- function(){326test_that('method should be knn', {327expect_equal(as.character(model_knn$method), 'knn')328})329test_that('k should be 7', {330expect_equal(as.numeric(model_knn$results$k), 7)331})332test_that('model_knn contains incorrect information.', {333expect_equal(digest(as.numeric(sum(model_knn$trainingData$Symmetry))), '47d0e881a9a1b19e57f9c068c08765fa')334expect_equal(digest(as.numeric(sum(model_knn$trainingData$Radius))), '5818709a65b4a5df9cb392b9cc66e32b')335expect_equal(as.numeric(summary(model_knn$trainingData$.outcome)[1]), 357)336})337print("Success!")338}339340test_3.2 <- function(){341test_that('Did not create an object named new_obs', {342expect_true(exists("new_obs"))343})344test_that('new_obs should be a data frame.', {345expect_true('data.frame' %in% class(new_obs))346})347test_that('Did not create an object named predicted_knn_7', {348expect_true(exists("predicted_knn_7"))349})350test_that('predicted_knn_7 should be a factor.', {351expect_true('factor' %in% class(predicted_knn_7))352})353test_that('predicted_knn_7 prediction is incorrect.', {354expect_equal(digest(as.character(predicted_knn_7)), '5f0922939c45ef1054f852e83f91c660')355})356print("Success!")357}358359test_3.3 <- function(){360test_that('Did not create an object named X_train_3', {361expect_true(exists("X_train_3"))362})363test_that('X_train_3 should be a data frame.', {364expect_true('data.frame' %in% class(X_train_3))365})366test_that('X_train_3 does not contain the correct number of rows and/or columns.', {367expect_equal(dim(X_train_3), c(569, 3))368})369test_that('Did not create an object named Y_train_3', {370expect_true(exists("Y_train_3"))371})372test_that('Y_train_3 should be a character.', {373expect_true('character' %in% class(Y_train_3))374})375test_that('Y_train_3 is not the correct length.', {376expect_equal(length(Y_train_3), 569)377})378test_that('Did not create an object named new_obs_3', {379expect_true(exists("new_obs_3"))380})381test_that('new_obs_3 should be a data frame.', {382expect_true('data.frame' %in% class(new_obs_3))383})384test_that('Did not create an object named predicted_3_knn_7', {385expect_true(exists("predicted_3_knn_7"))386})387test_that('predicted_3_knn_7 should be a factor.', {388expect_true('factor' %in% class(predicted_3_knn_7))389})390test_that('predicted_3_knn_7 prediction is incorrect.', {391expect_equal(digest(as.character(predicted_3_knn_7)), '5f0922939c45ef1054f852e83f91c660')392})393print("Success!")394}395396test_3.4 <- function(){397test_that('Did not create an object named X_train_all', {398expect_true(exists("X_train_all"))399})400test_that('X_train_all should be a data frame.', {401expect_true('data.frame' %in% class(X_train_all))402})403test_that('X_train_all does not contain the correct number of rows and/or columns.', {404expect_equal(dim(X_train_all), c(569, 10))405})406test_that('Did not create an object named Y_train_all', {407expect_true(exists("Y_train_all"))408})409test_that('Y_train_all should be a character.', {410expect_true('character' %in% class(Y_train_all))411})412test_that('Y_train_all is not the correct length.', {413expect_equal(length(Y_train_all), 569)414})415test_that('Did not create an object named new_obs_all', {416expect_true(exists("new_obs_all"))417})418test_that('new_obs_all should be a data frame.', {419expect_true('data.frame' %in% class(new_obs_all))420})421test_that('Did not create an object named predicted_all_knn_7', {422expect_true(exists("predicted_all_knn_7"))423})424test_that('predicted_all_knn_7 should be a factor.', {425expect_true('factor' %in% class(predicted_all_knn_7))426})427test_that('predicted_all_knn_7 prediction is incorrect.', {428expect_equal(digest(as.character(predicted_all_knn_7)), '3a5505c06543876fe45598b5e5e5195d')429})430print("Success!")431}432433test_4.0 <- function(){434test_that('Solution is incorrect', {435expect_equal(digest(as.character(answer4.0)), '75f1160e72554f4270c809f041c7a776')436})437print("Success!")438}439440test_4.1 <- function(){441test_that('Solution is incorrect', {442expect_equal(digest(as.character(answer4.1)), '475bf9280aab63a82af60791302736f6')443})444print("Success!")445}446447