Path: blob/master/2020-spring/materials/tutorial_02/tests_tutorial_02.R
2051 views
library(testthat)1library(digest)23test_1.1 <- function(){4test_that('Solution is incorrect', {5expect_equal(digest(answer1), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test6})7print("Success!")8}910test_1.2 <- function(){11test_that('Solution is incorrect', {12expect_equal(digest(answer2), 'c1f86f7430df7ddb256980ea6a3b57a4') # we hid the answer to the test here so you can't see it, but we can still run the test13})14print("Success!")15}1617test_1.3.0 <- function(){18test_that('Did not create an object named happy_df_csv', {19expect_true(exists("happy_df_csv"))20})21test_that('happy_df_csv should be a data frame.', {22expect_true('data.frame' %in% class(happy_df_csv))23})24test_that('happy_df_csv does not contain the correct data.', {25expect_equal(dim(happy_df_csv), c(1562, 19))26expect_equal(digest(as.numeric(sum(happy_df_csv$year))), '17201cb95c3eb622e848455bd480ef23') # we hid the answer to the test here so you can't see it, but we can still run the test27})28print("Success!")29}3031test_1.3.1 <- function(){32test_that('Did not create an object named happy_df', {33expect_true(exists("happy_df"))34})35test_that('happy_df should be a data frame.', {36expect_true('data.frame' %in% class(happy_df))37})38test_that('happy_df does not contain the correct data.', {39expect_equal(dim(happy_df), c(1562, 19))40expect_equal(digest(as.numeric(sum(happy_df$year))), '17201cb95c3eb622e848455bd480ef23') # we hid the answer to the test here so you can't see it, but we can still run the test41})42print("Success!")43}4445test_1.3.2 <- function(){46test_that('Did not create an object named happy_step1', {47expect_true(exists("happy_step1"))48})49test_that('Did not create an object named happy_step2', {50expect_true(exists("happy_step2"))51})52test_that('Did not create an object named reduced_happy_df', {53expect_true(exists("reduced_happy_df"))54})55test_that('reduced_happy_df does not contain the correct number of rows and/or columns.', {56expect_setequal(dim(reduced_happy_df), c(141, 2))57})58test_that('The year column in reduced_happy_df should only contain 2017', {59expect_equal(unique(happy_step1$year), 2017)60})61test_that('Columns in reduced_happy_df contain incorrect values.', {62expect_equal(digest(as.numeric(sum(reduced_happy_df$Positive.affect, na.rm = TRUE))), '3c52d24546a2dbf7437bfbf2b953614f')63expect_equal(digest(as.numeric(sum(reduced_happy_df$Healthy.life.expectancy.at.birth, na.rm = TRUE))), 'dee33d6f7860c9997daacdbfded438cf')64})65print("Success!")66}6768test_1.4 <- function(){69test_that('Did not create a plot named happy_plot', {70expect_true(exists("happy_plot"))71})72test_that('Healthy.life.expectancy.at.birth should be on the x-axis.', {73expect_that("Healthy.life.expectancy.at.birth" %in% c(rlang::get_expr(happy_plot$mapping$x),rlang::get_expr(happy_plot$layers[[1]]$mapping$x)), is_true())74})75test_that('Positive.affect should be on the y-axis.', {76expect_that("Positive.affect" %in% c(rlang::get_expr(happy_plot$mapping$y), rlang::get_expr(happy_plot$layers[[1]]$mapping$y)) , is_true())77})78test_that('happy_plot should be a scatter plot.', {79expect_that("GeomPoint" %in% c(class(happy_plot$layers[[1]]$geom)) , is_true())80})81test_that('Labels on the axes should be descriptive and human readable.', {82expect_that((happy_plot$labels$y) == 'Positive.affect', is_false())83expect_that((happy_plot$labels$x) == 'Healthy.life.expectancy.at.birth', is_false())84})85print("Success!")86}8788test_2.1 <- function(){89test_that('Solution is incorrect', {90expect_equal(digest(as.character(answer2.1)), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test91})92print("Success!")93}9495test_2.2.0 <- function(){96test_that('Did not create an object named whistler_2018', {97expect_true(exists("whistler_2018"))98})99test_that('whistler_2018 should be a data frame.', {100expect_true('data.frame' %in% class(whistler_2018))101})102test_that('whistler_2018 does not contain the correct number of rows and/or columns.', {103expect_equal(dim(whistler_2018), c(365, 27))104})105test_that('whistler_2018 does not contain the correct data.', {106expect_equal(digest(as.numeric(sum(whistler_2018$Snow_ground, na.rm = TRUE))), '908d1fd10b357ed0ceaaec823abf81bc') # we hid the answer to the test here so you can't see it, but we can still run the test107})108print("Success!")109}110111test_2.2.1 <- function(){112test_that('whistler_2018 does not contain the correct number of rows and/or columns.', {113expect_equal(dim(whistler_2018), c(365, 27))114})115test_that('whistler_2018 still contains white space in its column names.', {116expect_equal(length(grep(pattern = " ", x = colnames(whistler_2018))), 0)117})118print("Success!")119}120121test_2.3 <- function(){122test_that('Did not create a plot named whistler_2018_plot', {123expect_true(exists("whistler_2018_plot"))124})125test_that('Date.Time should be on the x-axis.', {126expect_that("Date.Time" %in% c(rlang::get_expr(whistler_2018_plot$mapping$x),rlang::get_expr(whistler_2018_plot$layers[[1]]$mapping$x)), is_true())127})128test_that('Total.Snow..cm. should be on the y-axis.', {129expect_that("Total.Snow..cm." %in% c(rlang::get_expr(whistler_2018_plot$mapping$y), rlang::get_expr(whistler_2018_plot$layers[[1]]$mapping$y)) , is_true())130})131test_that('whistler_2018_plot should be a line plot.', {132expect_that("GeomLine" %in% c(class(whistler_2018_plot$layers[[1]]$geom)) , is_true())133})134test_that('Labels on the axes should be descriptive and human readable.', {135expect_that((whistler_2018_plot$labels$y) == 'Total.Snow..cm.', is_false())136expect_that((whistler_2018_plot$labels$x) == 'Date.Time', is_false())137})138print("Success!")139}140141test_3.0 <- function(){142test_that('Did not create a database connected called connection', {143expect_true(exists("connection"))144expect_equal(digest(class(connection)), 'df66af2493a2405d1c7763f4bfbfebc0')145})146test_that('Did not create a table called project_data', {147expect_true(exists('project_data'))148expect_equal(digest(class(project_data))[[1]], '98f4e628b20def80372d0acca8b5edb0')149})150test_that('The project_data table has the wrong information in it', {151expect_equal(digest(colnames(project_data)[[13]]), 'fd0b87dfaa67d4c90844080d5d9355ec')152expect_equal(digest(colnames(project_data)[[14]]), '2f2ec36ffb7792b1f8e274387e520edd')153expect_equal(digest(length(colnames(project_data))), '4d4c1ad2286f1a7670a024467dd10808')154expect_equal(digest(as_tibble(summarize(project_data, sum=sum(goal, na.rm=TRUE)))[[1]]), 'f8d1fabeacced2ef4c82748ca212b724')155})156print('Success!')157}158159test_3.1 <- function(){160test_that('Incorrect answer', {161expect_equal(digest(answer3.1), '3a5505c06543876fe45598b5e5e5195d')162})163print('Success!')164}165166167test_3.2 <- function(){168test_that('Did not create a plot named funding_over_time_plot', {169expect_true(exists("funding_over_time_plot"))170})171test_that('The axis labels are incorrect', {172expect_equal(digest(funding_over_time_plot$labels$x), "bd1055a7d6af03e74a4462edbc5b35c4")173expect_equal(digest(funding_over_time_plot$labels$y), '48c36ecb38a110dffe3f40add37ccdc1')174})175test_that('The x/y data are incorrect', {176expect_equal(digest(funding_over_time_plot$mapping$x), 'c5a397ebe58129a88408811144869370')177expect_equal(digest(funding_over_time_plot$mapping$y), '5d75d853465e03f6828b25a1808b073b')178})179test_that('arrival_delay_plot should be a scatter plot', {180expect_equal(digest(class(funding_over_time_plot$layers[[1]]$geom)[[1]]), '911e5b9debfb523f25ad2ccc01a4b2dd')181})182print("Success!")183}184185186187test_3.4 <- function(){188test_that('Did not create project_data.csv in the data/ folder.', {189expect_true(file.exists("data/project_data.csv"))190})191test_that('the project_data.csv file does not contain the right data', { # read in data file as a string, check student's data = that string192expect_equal(digest(file.info('data/project_data.csv')$size), "8e5009fb2a1bed6bf7ae2a76deef7fea")193})194test_that('the project_df object does not exist', {195expect_true(exists('project_df'))196})197test_that('the project_df object does not have the right number of rows & columns',{198expect_equal(digest(nrow(project_df)), '40202635f105eddd66654976af5d9e1f')199expect_equal(digest(ncol(project_df)), '11946e7a3ed5e1776e81c0f0ecd383d0')200})201test_that('the project_df object does not have the right data',{202expect_equal(digest(sum(project_df)), 'daca6d59f60da17a8beacb0b8caa51b9')203})204print("Success!")205}206207208209210211212213