Path: blob/master/2020-spring/materials/worksheet_03/tests_worksheet_03.R
2051 views
library(testthat)1library(digest)23test_0.1 <- function(){4test_that('Solution is incorrect', {5expect_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 test6})7print("Success!")8}910test_0.2 <- function(){11test_that('Solution is incorrect', {12expect_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 test13})14print("Success!")15}1617test_0.3 <- function(){18test_that('Solution is incorrect', {19expect_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 test20})21print("Success!")22}2324test_1.1 <- function(){25test_that('Solution is incorrect', {26expect_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 test27})28print("Success!")29}3031test_1.2 <- function(){32test_that('Solution is incorrect', {33expect_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 test34})35print("Success!")36}3738test_1.3 <- function(){39test_that('Did not create an object named avocado', {40expect_true(exists("avocado"))41})42test_that('avocado should be a data frame.', {43expect_true('data.frame' %in% class(avocado))44})45test_that('avocado does not contain the correct data.', {46expect_equal(dim(avocado), c(17911, 9))47expect_equal(digest(as.numeric(sum(avocado$small_hass_volume))), '5c68093945e78d372cac50a8eacec8fa') # we hid the answer to the test here so you can't see it, but we can still run the test48expect_equal(colnames(avocado), c("Date", "average_price", "small_hass_volume", "large_hass_volume", "extra_l_hass_volume", "type", "year", "region", "week"))49})50print("Success!")51}5253test_1.4 <- function(){54test_that('Solution is incorrect', {55expect_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 test56})57print("Success!")58}5960test_1.5 <- function(){61test_that('Did not create a plot named avocado_plot', {62expect_true(exists("avocado_plot"))63})64test_that('Date should be on the x-axis.', {65expect_that("Date" %in% c(rlang::get_expr(avocado_plot$mapping$x),rlang::get_expr(avocado_plot$layers[[1]]$mapping$x)), is_true())66})67test_that('average_price should be on the y-axis.', {68expect_that("average_price" %in% c(rlang::get_expr(avocado_plot$mapping$y), rlang::get_expr(avocado_plot$layers[[1]]$mapping$y)) , is_true())69})70test_that('avocado_plot should be a scatter plot.', {71expect_that("GeomPoint" %in% c(class(avocado_plot$layers[[1]]$geom)) , is_true())72})73test_that('Labels on the axes should be descriptive and human readable.', {74expect_that((avocado_plot$labels$y) == 'average_price', is_false())75})76print("Success!")77}7879test_1.6 <- function(){80test_that('Did not create an object named avocado_aggregate', {81expect_true(exists("avocado_aggregate"))82})83test_that('avocado_aggregate does not contain the correct number of rows and/or columns.', {84expect_equal(dim(avocado_aggregate), c(53, 2))85})86test_that('Columns in avocado_aggregate contain incorrect values.', {87expect_equal(digest(as.numeric(sum(avocado_aggregate$week))), 'd27e825e408c446c586593f719b5545e') # we hid the answer to the test here so you can't see it, but we can still run the test88expect_equal(digest(as.numeric(sum(avocado_aggregate$average_price))), '124d515821b05dcb2a472f2999ca770e') # we hid the answer to the test here so you can't see it, but we can still run the test89})90print("Success!")91}9293test_1.7 <- function(){94test_that('Did not create a plot named avocado_aggregate_plot', {95expect_true(exists("avocado_aggregate_plot"))96})97test_that('week should be on the x-axis.', {98expect_that("week" %in% c(rlang::get_expr(avocado_aggregate_plot$mapping$x),rlang::get_expr(avocado_aggregate_plot$layers[[1]]$mapping$x)), is_true())99})100test_that('average_price should be on the y-axis.', {101expect_that("average_price" %in% c(rlang::get_expr(avocado_aggregate_plot$mapping$y), rlang::get_expr(avocado_aggregate_plot$layers[[1]]$mapping$y)) , is_true())102})103test_that('avocado_aggregate_plot should be a scatter plot.', {104expect_that("GeomPoint" %in% c(class(avocado_aggregate_plot$layers[[1]]$geom)) , is_true())105})106test_that('Labels on the axes should be descriptive and human readable.', {107expect_that((avocado_aggregate_plot$labels$y) == 'average_price', is_false())108expect_that((avocado_aggregate_plot$labels$x) == 'week', is_false())109})110print("Success!")111}112113test_1.8 <- function(){114test_that('Did not create an object named avocado', {115expect_true(exists("avocado"))116})117test_that('avocado does not contain the correct number of rows and/or columns.', {118expect_equal(dim(avocado), c(17911, 10))119})120test_that('Columns in avocado contain incorrect values.', {121expect_equal(digest(round(sum(avocado$total_volume))), 'aa2d30b2344bef18ef305fb4541b78b6') # we hid the answer to the test here so you can't see it, but we can still run the test122expect_equal(digest(round(sum(avocado$average_price, na.rm = TRUE))), 'd478458402bf9895986c7e8a50ad4b61') # we hid the answer to the test here so you can't see it, but we can still run the test123})124print("Success!")125}126127test_1.9 <- function(){128test_that('Did not create an object named avocado_aggregate_2', {129expect_true(exists("avocado_aggregate_2"))130})131test_that('avocado_aggregate_2 does not contain the correct number of rows and/or columns.', {132expect_equal(dim(avocado_aggregate_2), c(53, 2))133})134test_that('Columns in avocado_aggregate_2 contain incorrect values.', {135expect_equal(digest(as.numeric(sum(avocado_aggregate_2$total_volume))), '6c1676dd13517d0eed2be5e246dc8ef1') # we hid the answer to the test here so you can't see it, but we can still run the test136expect_equal(digest(as.numeric(sum(avocado_aggregate_2$week))), 'd27e825e408c446c586593f719b5545e') # we hid the answer to the test here so you can't see it, but we can still run the test137})138print("Success!")139}140141test_1.10 <- function(){142test_that('Did not create a plot named avocado_aggregate_plot_2', {143expect_true(exists("avocado_aggregate_plot_2"))144})145test_that('week should be on the x-axis.', {146expect_that("week" %in% c(rlang::get_expr(avocado_aggregate_plot_2$mapping$x),rlang::get_expr(avocado_aggregate_plot_2$layers[[1]]$mapping$x)), is_true())147})148test_that('total_volume should be on the y-axis.', {149expect_that("total_volume" %in% c(rlang::get_expr(avocado_aggregate_plot_2$mapping$y), rlang::get_expr(avocado_aggregate_plot_2$layers[[1]]$mapping$y)) , is_true())150})151test_that('avocado_aggregate_plot_2 should be a scatter plot.', {152expect_that("GeomPoint" %in% c(class(avocado_aggregate_plot_2$layers[[1]]$geom)) , is_true())153})154test_that('Labels on the axes should be descriptive and human readable.', {155expect_that((avocado_aggregate_plot_2$labels$y) == 'total_volume', is_false())156expect_that((avocado_aggregate_plot_2$labels$x) == 'week', is_false())157})158print("Success!")159}160161test_2.1 <- function(){162test_that('Solution is incorrect', {163expect_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 test164})165print("Success!")166}167168test_2.2 <- function(){169test_that('Solution is incorrect', {170expect_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 test171})172print("Success!")173}174175test_2.3 <- function(){176test_that('Did not create an object named sea_surface', {177expect_true(exists("sea_surface"))178})179test_that('sea_surface should be a data frame.', {180expect_true('data.frame' %in% class(sea_surface))181})182test_that('sea_surface does not contain the correct data.', {183expect_equal(dim(sea_surface), c(105, 13))184expect_equal(digest(as.numeric(sum(sea_surface$Year))), 'f56bfa7ed2b5549995da2c88407099a4') # we hid the answer to the test here so you can't see it, but we can still run the test185expect_equal(colnames(sea_surface), c("Year", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))186})187print("Success!")188}189190test_2.3.1 <- function(){191test_that('Solution is incorrect', {192expect_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 test193})194print("Success!")195}196197198test_2.4 <- function(){199test_that('Did not create an object named tidy_temp', {200expect_true(exists("tidy_temp"))201})202test_that('tidy_temp does not contain the correct number of rows and/or columns.', {203expect_equal(dim(tidy_temp), c(1260, 3))204})205test_that('Columns in tidy_temp contain incorrect values.', {206expect_equal(digest(as.numeric(sum(tidy_temp$Temperature, na.rm = TRUE))), '378da034470b139e7a84f82e2fcdcb08') # we hid the answer to the test here so you can't see it, but we can still run the test207})208print("Success!")209}210211test_2.5 <- function(){212test_that('Did not create a plot named nov_temp_plot', {213expect_true(exists("nov_temp_plot"))214})215test_that('The Month column in nov_temp_plot should only contain November.', {216expect_equal(unique(nov_temp_plot$data$Month), "Nov")217})218test_that('Year should be on the x-axis.', {219expect_that("Year" %in% c(rlang::get_expr(nov_temp_plot$mapping$x),rlang::get_expr(nov_temp_plot$layers[[1]]$mapping$x)), is_true())220})221test_that('Temperature should be on the y-axis.', {222expect_that("Temperature" %in% c(rlang::get_expr(nov_temp_plot$mapping$y), rlang::get_expr(nov_temp_plot$layers[[1]]$mapping$y)) , is_true())223})224test_that('nov_temp_plot should be a scatter plot.', {225expect_that("GeomPoint" %in% c(class(nov_temp_plot$layers[[1]]$geom)) , is_true())226})227print("Success!")228}229230test_2.6 <- function(){231test_that('Did not create a plot named all_temp_plot', {232expect_true(exists("all_temp_plot"))233})234test_that('Need to use tidy_temp as the data!', {235expect_true("Month" %in% colnames(all_temp_plot$data))236})237test_that('Should use facet_wrap to facet by month', {238expect_true('FacetWrap' %in% class(all_temp_plot$facet))239})240test_that('Year should be on the x-axis.', {241expect_that("Year" %in% c(rlang::get_expr(all_temp_plot$mapping$x),rlang::get_expr(all_temp_plot$layers[[1]]$mapping$x)), is_true())242})243test_that('Temperature should be on the y-axis.', {244expect_that("Temperature" %in% c(rlang::get_expr(all_temp_plot$mapping$y), rlang::get_expr(all_temp_plot$layers[[1]]$mapping$y)) , is_true())245})246test_that('all_temp_plot should be a scatter plot.', {247expect_that("GeomPoint" %in% c(class(all_temp_plot$layers[[1]]$geom)) , is_true())248})249print("Success!")250}251252test_3.1 <- function(){253test_that('Solution is incorrect', {254expect_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 test255})256print("Success!")257}258259test_3.2 <- function(){260test_that('Did not create an object named madrid', {261expect_true(exists("madrid"))262})263test_that('madrid should be a data frame.', {264expect_true('data.frame' %in% class(madrid))265})266test_that('madrid does not contain the correct data.', {267expect_equal(dim(madrid), c(51864, 17))268expect_equal(digest(sum(madrid$BEN)), '9c9393e1464352cd4fbea94dfadfa02a') # we hid the answer to the test here so you can't see it, but we can still run the test269expect_equal(colnames(madrid), c("date", "BEN", "CO", "EBE", "MXY", "NMHC", "NO_2", "NOx", "OXY", "O_3", "PM10", "PXY", "SO_2", "TCH", "TOL", "year", "month"))270})271print("Success!")272}273274test_3.3 <- function(){275test_that('Did not create a plot named EBE_pollution', {276expect_true(exists("EBE_pollution"))277})278test_that('date should be on the x-axis.', {279expect_that("date" %in% c(rlang::get_expr(EBE_pollution$mapping$x),rlang::get_expr(EBE_pollution$layers[[1]]$mapping$x)), is_true())280})281test_that('EBE should be on the y-axis.', {282expect_that("EBE" %in% c(rlang::get_expr(EBE_pollution$mapping$y), rlang::get_expr(EBE_pollution$layers[[1]]$mapping$y)) , is_true())283})284test_that('EBE_pollution should be a scatter plot.', {285expect_that("GeomPoint" %in% c(class(EBE_pollution$layers[[1]]$geom)) , is_true())286})287test_that('Labels on the axes should be descriptive and human readable.', {288expect_that((EBE_pollution$labels$y) == 'EBE', is_false())289expect_that((EBE_pollution$labels$x) == 'date', is_false())290})291print("Success!")292}293294test_3.4 <- function(){295test_that('Did not create an object named madrid_pollution', {296expect_true(exists("madrid_pollution"))297})298test_that('madrid_pollution does not contain the correct number of rows and/or columns.', {299expect_equal(dim(madrid_pollution), c(72, 3))300})301test_that('Columns in avocado_aggregate_2 contain incorrect values.', {302expect_equal(digest(as.numeric(sum(madrid_pollution$year))), '625a52cbf322663507323f452293484f') # we hid the answer to the test here so you can't see it, but we can still run the test303})304print("Success!")305}306307test_3.5 <- function(){308test_that('Did not create a plot named madrid_plot', {309expect_true(exists("madrid_plot"))310})311test_that('month should be on the x-axis.', {312expect_that("month" %in% c(rlang::get_expr(madrid_plot$mapping$x),rlang::get_expr(madrid_plot$layers[[1]]$mapping$x)), is_true())313})314test_that('max_ebe should be on the y-axis.', {315expect_that("max_ebe" %in% c(rlang::get_expr(madrid_plot$mapping$y), rlang::get_expr(madrid_plot$layers[[1]]$mapping$y)) , is_true())316})317test_that('madrid_plot should be a scatter plot.', {318expect_that("GeomPoint" %in% c(class(madrid_plot$layers[[1]]$geom)) , is_true())319})320test_that('Labels on the axes should be descriptive and human readable.', {321expect_that((madrid_plot$labels$y) == 'max_ebe', is_false())322expect_that((madrid_plot$labels$x) == 'month', is_false())323})324print("Success!")325}326327test_3.6 <- function(){328test_that('Did not create an object named pollution_2001', {329expect_true(exists("pollution_2001"))330})331test_that('pollution_2001 does not contain the correct number of rows and/or columns.', {332expect_equal(dim(pollution_2001), c(1, 14))333})334test_that('Columns in pollution_2001 contain incorrect values.', {335expect_equal(digest(pollution_2001$MXY), '04d5d8b3f2a28a07e24c30fe05b4d105') # we hid the answer to the test here so you can't see it, but we can still run the test336expect_equal(digest(as.numeric(sum(pollution_2001))), '7641b9c141801f7dfd1e36295531cd81') # we hid the answer to the test here so you can't see it, but we can still run the test337})338print("Success!")339}340341test_3.7 <- function(){342test_that('Did not create an object named pollution_2006', {343expect_true(exists("pollution_2006"))344})345test_that('pollution_2006 does not contain the correct number of rows and/or columns.', {346expect_equal(dim(pollution_2006), c(1, 14))347})348test_that('Columns in pollution_2006 contain incorrect values.', {349expect_equal(digest(pollution_2006$MXY), '64d199ddc067d7977719ecede6a11333') # we hid the answer to the test here so you can't see it, but we can still run the test350expect_equal(digest(as.numeric(sum(pollution_2006))), 'd5563a7b0d7d1a9eb53269ad69ed4e0b') # we hid the answer to the test here so you can't see it, but we can still run the test351})352print("Success!")353}354355test_3.8 <- function(){356test_that('Solution is incorrect', {357expect_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 test358})359print("Success!")360}361362test_3.9 <- function(){363test_that('Did not create an object named pollution_diff', {364expect_true(exists("pollution_diff"))365})366test_that('pollution_diff does not contain the correct number of rows and/or columns.', {367expect_equal(dim(pollution_diff), c(14, 2))368})369test_that('pollution_diff does not contain the correct columns: pollutant and value.', {370expect_equal(colnames(pollution_diff), c('pollutant', 'value'))371})372test_that('Columns in pollution_diff contain incorrect values.', {373expect_equal(digest(as.numeric(sum(pollution_diff$value))), 'd4f4caf121e7237dc6328782e399d193') # we hid the answer to the test here so you can't see it, but we can still run the test374})375print("Success!")376}377378test_3.10 <- function(){379test_that('Did not create an object named max_pollution_diff', {380expect_true(exists("max_pollution_diff"))381})382test_that('max_pollution_diff does not contain the correct number of rows and/or columns.', {383expect_equal(dim(max_pollution_diff), c(1, 2))384})385test_that('max_pollution_diff does not contain the correct columns: pollutant and value.', {386expect_equal(colnames(max_pollution_diff), c('pollutant', 'value'))387})388test_that('Columns in pollution_diff contain incorrect values.', {389expect_equal(digest(as.numeric(sum(max_pollution_diff$value))), '6372781239f4b691087e7d411c9ba64f') # we hid the answer to the test here so you can't see it, but we can still run the test390})391print("Success!")392}393394