Path: blob/master/2022-spring/materials/worksheet_wrangling/tests.R
2051 views
library(testthat)1library(digest)23# Round double to precise integer4#5# `int_round` works to create an integer corresponding to a number that is6# tested up to a particular decimal point of precision. This is useful when7# there is a need to compare a numeric value using hashes.8#9# @param x Double vector of length one.10# @param digits Double vector of length one to specify decimal point of precision. Negative numbers can be used to specifying significant digits > 0.1.11#12# @return Integer vector of length one corresponding to a particular decimal point of precision.13#14# @examples15# # to get an integer up to two decimals of precision from 234.5678916# int_round(234.56789, 2)17#18# to get an integer rounded to the hundred digit from 234.5678919# int_round(234.56789, -2)20int_round <- function(x, digits){21x = x * 10^digits22xint = as.integer(x)23xint1 = xint + 1L24if (abs(xint - x) < abs(xint1 - x)){25return(xint)26}27else {28return(xint1)29}30}3132test_0.0 <- function(){33test_that('Solution is incorrect', {34expect_equal(digest(as.character(answer0.0)), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test35})36print("Success!")37}383940test_0.1 <- function(){41test_that('Solution is incorrect', {42expect_equal(digest(as.character(answer0.1)), '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_0.2 <- function(){48test_that('Solution is incorrect', {49expect_equal(digest(as.character(answer0.2)), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test50})51print("Success!")52}5354test_0.3 <- function(){55test_that('Solution is incorrect', {56expect_equal(digest(as.character(answer0.3)), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test57})58print("Success!")59}6061test_1.1 <- function(){62test_that('Solution is incorrect', {63expect_equal(digest(as.character(answer1.1)), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test64})65print("Success!")66}6768test_1.2 <- function(){69test_that('Solution is incorrect', {70expect_equal(digest(as.character(answer1.2)), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test71})72print("Success!")73}7475test_1.3 <- function(){76test_that('Did not create an object named avocado', {77expect_true(exists("avocado"))78})79test_that('avocado should be a data frame.', {80expect_true('data.frame' %in% class(avocado))81})82test_that('avocado does not contain the correct data.', {83expect_equal(dim(avocado), c(17911, 9))84expect_equal(digest(int_round(sum(avocado$average_price), 2)), '925e36908ff4830300330ada3666458c') # we hid the answer to the test here so you can't see it, but we can still run the test85expect_equal(colnames(avocado), c("Date", "average_price", "small_hass_volume", "large_hass_volume", "extra_l_hass_volume", "type", "yr", "region", "wk"))86})87print("Success!")88}8990test_1.4 <- function(){91test_that('Solution is incorrect', {92expect_equal(digest(as.character(answer1.4)), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test93})94print("Success!")95}9697test_1.5 <- function(){98test_that('Did not create a plot named avocado_plot', {99expect_true(exists("avocado_plot"))100})101properties <- c(avocado_plot$layers[[1]]$mapping, avocado_plot$mapping)102test_that('Date should be on the x-axis.', {103expect_true("Date" == rlang::get_expr(properties$x))104})105test_that('average_price should be on the y-axis.', {106expect_true("average_price" == rlang::get_expr(properties$y))107})108test_that('avocado_plot should be a scatter plot.', {109expect_true("GeomPoint" %in% c(class(avocado_plot$layers[[1]]$geom)))110})111test_that('Labels on the axes should be descriptive and human readable.', {112expect_false(avocado_plot$labels$y == 'average_price')113})114print("Success!")115}116117test_1.6 <- function(){118test_that('Did not create an object named avocado_aggregate', {119expect_true(exists("avocado_aggregate"))120})121test_that('avocado_aggregate does not contain the correct number of rows and/or columns.', {122expect_equal(dim(avocado_aggregate), c(53, 2))123})124test_that('Columns in avocado_aggregate contain incorrect values.', {125expect_equal(digest(int_round(sum(avocado_aggregate$wk), 2)), '52e8770115417a331e4e2a539238bbf1') # we hid the answer to the test here so you can't see it, but we can still run the test126expect_equal(digest(int_round(sum(avocado_aggregate$average_price), 2)), '2a69e8ab5af131a648f5cb2d24e6fbcf') # we hid the answer to the test here so you can't see it, but we can still run the test127})128print("Success!")129}130131test_1.7 <- function(){132test_that('Did not create a plot named avocado_aggregate_plot', {133expect_true(exists("avocado_aggregate_plot"))134})135properties <- c(avocado_aggregate_plot$layers[[1]]$mapping, avocado_aggregate_plot$mapping)136test_that('week should be on the x-axis.', {137expect_true("wk" == rlang::get_expr(properties$x))138})139test_that('average_price should be on the y-axis.', {140expect_true("average_price" == rlang::get_expr(properties$y))141})142test_that('avocado_aggregate_plot should be a scatter plot.', {143expect_true("GeomPoint" %in% c(class(avocado_aggregate_plot$layers[[1]]$geom)))144})145test_that('Labels on the axes should be descriptive and human readable.', {146expect_false(avocado_aggregate_plot$labels$y == 'average_price')147expect_false(avocado_aggregate_plot$labels$x == 'wk')148})149print("Success!")150}151152test_1.8 <- function(){153test_that('Did not create an object named avocado', {154expect_true(exists("avocado"))155})156test_that('avocado does not contain the correct number of rows and/or columns.', {157expect_equal(dim(avocado), c(17911, 10))158})159test_that('Columns in avocado contain incorrect values.', {160expect_equal(digest(int_round(sum(avocado$total_volume, na.rm = TRUE), -2)), '7128826a55bfba26d8c2df46ab65dbcf') # we hid the answer to the test here so you can't see it, but we can still run the test161expect_equal(digest(int_round(sum(avocado$average_price, na.rm = TRUE), 2)), '925e36908ff4830300330ada3666458c') # we hid the answer to the test here so you can't see it, but we can still run the test162})163print("Success!")164}165166test_1.9 <- function(){167test_that('Did not create an object named avocado_aggregate_2', {168expect_true(exists("avocado_aggregate_2"))169})170test_that('avocado_aggregate_2 does not contain the correct number of rows and/or columns.', {171expect_equal(dim(avocado_aggregate_2), c(53, 2))172})173test_that('Columns in avocado_aggregate_2 contain incorrect values.', {174expect_equal(digest(int_round(sum(avocado_aggregate_2$total_volume), 2)), '31b3d41174825b72e1d523622ee021e6') # we hid the answer to the test here so you can't see it, but we can still run the test175expect_equal(digest(int_round(sum(avocado_aggregate_2$wk), 2)), '52e8770115417a331e4e2a539238bbf1') # we hid the answer to the test here so you can't see it, but we can still run the test176})177print("Success!")178}179180test_1.10 <- function(){181test_that('Did not create a plot named avocado_aggregate_plot_2', {182expect_true(exists("avocado_aggregate_plot_2"))183})184properties <- c(avocado_aggregate_plot_2$layers[[1]]$mapping, avocado_aggregate_plot_2$mapping)185test_that('week should be on the x-axis.', {186expect_true("wk" == rlang::get_expr(properties$x))187})188test_that('total_volume should be on the y-axis.', {189expect_true("total_volume" == rlang::get_expr(properties$y))190})191test_that('avocado_aggregate_plot_2 should be a scatter plot.', {192expect_true("GeomPoint" %in% c(class(avocado_aggregate_plot_2$layers[[1]]$geom)))193})194test_that('Labels on the axes should be descriptive and human readable.', {195expect_false(avocado_aggregate_plot_2$labels$y == 'total_volume')196expect_false(avocado_aggregate_plot_2$labels$x == 'wk')197})198print("Success!")199}200201test_2.1 <- function(){202test_that('Solution is incorrect', {203expect_equal(digest(answer2.1), 'd2a90307aac5ae8d0ef58e2fe730d38b') # we hid the answer to the test here so you can't see it, but we can still run the test204})205print("Success!")206}207208test_2.2 <- function(){209test_that('Solution is incorrect', {210expect_equal(digest(answer2.2), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test211})212print("Success!")213}214215test_2.3 <- function(){216test_that('Did not create an object named sea_surface', {217expect_true(exists("sea_surface"))218})219test_that('sea_surface should be a data frame.', {220expect_true('data.frame' %in% class(sea_surface))221})222test_that('sea_surface does not contain the correct data.', {223expect_equal(dim(sea_surface), c(105, 13))224expect_equal(digest(int_round(sum(sea_surface$Year), 2)), 'f5530ffd8438e19b12730e111c4e2cc6') # we hid the answer to the test here so you can't see it, but we can still run the test225expect_equal(colnames(sea_surface), c("Year", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))226})227print("Success!")228}229230test_2.3.1 <- function(){231test_that('Solution is incorrect', {232expect_equal(digest(as.character(answer2.3.1)), '01a75cb73d67b0f895ff0e61449c7bf8') # we hid the answer to the test here so you can't see it, but we can still run the test233})234print("Success!")235}236237238test_2.4 <- function(){239test_that('Did not create an object named tidy_temp', {240expect_true(exists("tidy_temp"))241})242test_that('tidy_temp does not contain the correct number of rows and/or columns.', {243expect_equal(dim(tidy_temp), c(1260, 3))244})245test_that('Columns in tidy_temp contain incorrect values.', {246expect_equal(digest(int_round(sum(tidy_temp$Temperature, na.rm = TRUE), 2)), 'ffc4ca6bf87040a2a3f7f80c32c15def') # we hid the answer to the test here so you can't see it, but we can still run the test247})248print("Success!")249}250251test_2.5 <- function(){252test_that('Did not create a plot named nov_temp_plot', {253expect_true(exists("nov_temp_plot"))254})255properties <- c(nov_temp_plot$layers[[1]]$mapping, nov_temp_plot$mapping)256test_that('The Month column in nov_temp_plot should only contain November.', {257expect_equal(unique(nov_temp_plot$data$Month), "Nov")258})259test_that('Year should be on the x-axis.', {260expect_true("Year" == rlang::get_expr(properties$x))261})262test_that('Temperature should be on the y-axis.', {263expect_true("Temperature" == rlang::get_expr(properties$y))264})265test_that('nov_temp_plot should be a scatter plot.', {266expect_true("GeomPoint" %in% c(class(nov_temp_plot$layers[[1]]$geom)))267})268print("Success!")269}270271test_2.6 <- function(){272test_that('Did not create a plot named all_temp_plot', {273expect_true(exists("all_temp_plot"))274})275properties <- c(all_temp_plot$layers[[1]]$mapping, all_temp_plot$mapping)276test_that('Need to use tidy_temp as the data!', {277expect_true("Month" %in% colnames(all_temp_plot$data))278})279test_that('Should use facet_wrap to facet by month', {280expect_true('FacetWrap' %in% class(all_temp_plot$facet))281})282test_that('Year should be on the x-axis.', {283expect_true("Year" == rlang::get_expr(properties$x))284})285test_that('Temperature should be on the y-axis.', {286expect_true("Temperature" == rlang::get_expr(properties$y))287})288test_that('all_temp_plot should be a scatter plot.', {289expect_true("GeomPoint" %in% c(class(all_temp_plot$layers[[1]]$geom)))290})291print("Success!")292}293294test_3.1 <- function(){295test_that('Solution is incorrect', {296expect_equal(digest(as.character(answer3.1)), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test297})298print("Success!")299}300301test_3.2 <- function(){302test_that('Did not create an object named madrid', {303expect_true(exists("madrid"))304})305test_that('madrid should be a data frame.', {306expect_true('data.frame' %in% class(madrid))307})308test_that('madrid does not contain the correct data.', {309expect_equal(dim(madrid), c(51864, 17))310expect_equal(digest(int_round(sum(madrid$BEN, na.rm = TRUE), 2)), 'fc1f955cfd95bfb1528ba72b1d7dbf85') # we hid the answer to the test here so you can't see it, but we can still run the test311expect_equal(colnames(madrid), c("date", "BEN", "CO", "EBE", "MXY", "NMHC", "NO_2", "NOx", "OXY", "O_3", "PM10", "PXY", "SO_2", "TCH", "TOL", "year", "mnth"))312})313print("Success!")314}315316test_3.3 <- function(){317test_that('Did not create a plot named EBE_pollution', {318expect_true(exists("EBE_pollution"))319})320properties <- c(EBE_pollution$layers[[1]]$mapping, EBE_pollution$mapping)321test_that('date should be on the x-axis.', {322expect_true("date" == rlang::get_expr(properties$x))323})324test_that('EBE should be on the y-axis.', {325expect_true("EBE" == rlang::get_expr(properties$y))326})327test_that('EBE_pollution should be a scatter plot.', {328expect_true("GeomPoint" %in% c(class(EBE_pollution$layers[[1]]$geom)))329})330test_that('Labels on the axes should be descriptive and human readable.', {331expect_false(EBE_pollution$labels$y == 'EBE')332expect_false(EBE_pollution$labels$x == 'date')333})334print("Success!")335}336337test_3.4 <- function(){338test_that('Did not create an object named madrid_pollution', {339expect_true(exists("madrid_pollution"))340})341test_that('madrid_pollution does not contain the correct number of rows and/or columns.', {342expect_equal(dim(madrid_pollution), c(72, 3))343})344test_that('Columns in avocado_aggregate_2 contain incorrect values.', {345expect_equal(digest(int_round(sum(madrid_pollution$year), 2)), 'a23f8380b73a23a2f7ad10600a6a4829') # we hid the answer to the test here so you can't see it, but we can still run the test346})347print("Success!")348}349350test_3.5 <- function(){351test_that('Did not create a plot named madrid_plot', {352expect_true(exists("madrid_plot"))353})354properties <- c(madrid_plot$layers[[1]]$mapping, madrid_plot$mapping)355test_that('month should be on the x-axis.', {356expect_true("mnth" == rlang::get_expr(properties$x))357})358test_that('max_ebe should be on the y-axis.', {359expect_true("max_ebe" == rlang::get_expr(properties$y))360})361test_that('madrid_plot should be a scatter plot.', {362expect_true("GeomPoint" %in% c(class(madrid_plot$layers[[1]]$geom)))363})364test_that('Labels on the axes should be descriptive and human readable.', {365expect_false(madrid_plot$labels$y == 'max_ebe')366expect_false(madrid_plot$labels$x == 'mnth')367})368print("Success!")369}370371test_3.6 <- function(){372test_that('Did not create an object named pollution_2001', {373expect_true(exists("pollution_2001"))374})375test_that('pollution_2001 does not contain the correct number of rows and/or columns.', {376expect_equal(dim(pollution_2001), c(1, 14))377})378test_that('Columns in pollution_2001 contain incorrect values.', {379expect_equal(digest(int_round(pollution_2001$MXY, 2)), '9e56083b25f55a915ef285e242392a4a') # we hid the answer to the test here so you can't see it, but we can still run the test380expect_equal(digest(int_round(sum(pollution_2001), 2)), 'f12c2d5045ab522d0268bc7d14b17290') # we hid the answer to the test here so you can't see it, but we can still run the test381})382print("Success!")383}384385test_3.7 <- function(){386test_that('Did not create an object named pollution_2006', {387expect_true(exists("pollution_2006"))388})389test_that('pollution_2006 does not contain the correct number of rows and/or columns.', {390expect_equal(dim(pollution_2006), c(1, 14))391})392test_that('Columns in pollution_2006 contain incorrect values.', {393expect_equal(digest(int_round(pollution_2006$MXY, 2)), 'd1e716c1361c7b0e57cc9f66a87e1bfb') # we hid the answer to the test here so you can't see it, but we can still run the test394expect_equal(digest(int_round(sum(pollution_2006), 2)), 'a49327566693ae18684d9e8618ce48a1') # we hid the answer to the test here so you can't see it, but we can still run the test395})396print("Success!")397}398399test_3.8 <- function(){400test_that('Solution is incorrect', {401expect_equal(digest(as.character(answer3.8)), '1ce38a3fa8946d5768f4fc87b739ec31') # we hid the answer to the test here so you can't see it, but we can still run the test402})403print("Success!")404}405406test_3.9 <- function(){407test_that('Did not create an object named pollution_diff', {408expect_true(exists("pollution_diff"))409})410test_that('pollution_diff does not contain the correct number of rows and/or columns.', {411expect_equal(dim(pollution_diff), c(14, 2))412})413test_that('pollution_diff does not contain the correct columns: pollutant and value.', {414expect_equal(colnames(pollution_diff), c('pollutant', 'value'))415})416test_that('Columns in pollution_diff contain incorrect values.', {417expect_equal(digest(int_round(sum(pollution_diff$value), 2)), 'b6181c536435d6429d51eebb59563fb7') # we hid the answer to the test here so you can't see it, but we can still run the test418})419print("Success!")420}421422test_3.10 <- function(){423test_that('Did not create an object named max_pollution_diff', {424expect_true(exists("max_pollution_diff"))425})426test_that('max_pollution_diff does not contain the correct number of rows and/or columns.', {427expect_equal(dim(max_pollution_diff), c(1, 2))428})429test_that('max_pollution_diff does not contain the correct columns: pollutant and value.', {430expect_equal(colnames(max_pollution_diff), c('pollutant', 'value'))431})432test_that('Columns in pollution_diff contain incorrect values.', {433expect_equal(digest(int_round(sum(max_pollution_diff$value), 2)), '44ee23cae87e4fb16d02379e8b8536bc') # we hid the answer to the test here so you can't see it, but we can still run the test434})435print("Success!")436}437438439