Path: blob/master/2020-fall/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(round(as.numeric(sum(avocado$small_hass_volume)))), 'd462f87e11e563c8fb50ea6c6ac51815') # 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", "yr", "region", "wk"))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})64properties <- c(avocado_plot$layers[[1]]$mapping, avocado_plot$mapping)65test_that('Date should be on the x-axis.', {66expect_true("Date" == rlang::get_expr(properties$x))67})68test_that('average_price should be on the y-axis.', {69expect_true("average_price" == rlang::get_expr(properties$y))70})71test_that('avocado_plot should be a scatter plot.', {72expect_true("GeomPoint" %in% c(class(avocado_plot$layers[[1]]$geom)))73})74test_that('Labels on the axes should be descriptive and human readable.', {75expect_false(avocado_plot$labels$y == 'average_price')76})77print("Success!")78}7980test_1.6 <- function(){81test_that('Did not create an object named avocado_aggregate', {82expect_true(exists("avocado_aggregate"))83})84test_that('avocado_aggregate does not contain the correct number of rows and/or columns.', {85expect_equal(dim(avocado_aggregate), c(53, 2))86})87test_that('Columns in avocado_aggregate contain incorrect values.', {88expect_equal(digest(as.numeric(sum(avocado_aggregate$wk))), 'd27e825e408c446c586593f719b5545e') # we hid the answer to the test here so you can't see it, but we can still run the test89expect_equal(digest(round(as.numeric(sum(avocado_aggregate$average_price)))), 'f67fbc496dfabdb88e8a3761809759ab') # we hid the answer to the test here so you can't see it, but we can still run the test90})91print("Success!")92}9394test_1.7 <- function(){95test_that('Did not create a plot named avocado_aggregate_plot', {96expect_true(exists("avocado_aggregate_plot"))97})98properties <- c(avocado_aggregate_plot$layers[[1]]$mapping, avocado_aggregate_plot$mapping)99test_that('week should be on the x-axis.', {100expect_true("wk" == rlang::get_expr(properties$x))101})102test_that('average_price should be on the y-axis.', {103expect_true("average_price" == rlang::get_expr(properties$y))104})105test_that('avocado_aggregate_plot should be a scatter plot.', {106expect_true("GeomPoint" %in% c(class(avocado_aggregate_plot$layers[[1]]$geom)))107})108test_that('Labels on the axes should be descriptive and human readable.', {109expect_false(avocado_aggregate_plot$labels$y == 'average_price')110expect_false(avocado_aggregate_plot$labels$x == 'wk')111})112print("Success!")113}114115test_1.8 <- function(){116test_that('Did not create an object named avocado', {117expect_true(exists("avocado"))118})119test_that('avocado does not contain the correct number of rows and/or columns.', {120expect_equal(dim(avocado), c(17911, 10))121})122test_that('Columns in avocado contain incorrect values.', {123expect_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 test124expect_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 test125})126print("Success!")127}128129test_1.9 <- function(){130test_that('Did not create an object named avocado_aggregate_2', {131expect_true(exists("avocado_aggregate_2"))132})133test_that('avocado_aggregate_2 does not contain the correct number of rows and/or columns.', {134expect_equal(dim(avocado_aggregate_2), c(53, 2))135})136test_that('Columns in avocado_aggregate_2 contain incorrect values.', {137expect_equal(digest(round(as.numeric(sum(avocado_aggregate_2$total_volume)))), 'cf6644bdab5e986758065d29039b4ab1') # we hid the answer to the test here so you can't see it, but we can still run the test138expect_equal(digest(as.numeric(sum(avocado_aggregate_2$wk))), 'd27e825e408c446c586593f719b5545e') # we hid the answer to the test here so you can't see it, but we can still run the test139})140print("Success!")141}142143test_1.10 <- function(){144test_that('Did not create a plot named avocado_aggregate_plot_2', {145expect_true(exists("avocado_aggregate_plot_2"))146})147properties <- c(avocado_aggregate_plot_2$layers[[1]]$mapping, avocado_aggregate_plot_2$mapping)148test_that('week should be on the x-axis.', {149expect_true("wk" == rlang::get_expr(properties$x))150})151test_that('total_volume should be on the y-axis.', {152expect_true("total_volume" == rlang::get_expr(properties$y))153})154test_that('avocado_aggregate_plot_2 should be a scatter plot.', {155expect_true("GeomPoint" %in% c(class(avocado_aggregate_plot_2$layers[[1]]$geom)))156})157test_that('Labels on the axes should be descriptive and human readable.', {158expect_false(avocado_aggregate_plot_2$labels$y == 'total_volume')159expect_false(avocado_aggregate_plot_2$labels$x == 'wk')160})161print("Success!")162}163164test_2.1 <- function(){165test_that('Solution is incorrect', {166expect_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 test167})168print("Success!")169}170171test_2.2 <- function(){172test_that('Solution is incorrect', {173expect_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 test174})175print("Success!")176}177178test_2.3 <- function(){179test_that('Did not create an object named sea_surface', {180expect_true(exists("sea_surface"))181})182test_that('sea_surface should be a data frame.', {183expect_true('data.frame' %in% class(sea_surface))184})185test_that('sea_surface does not contain the correct data.', {186expect_equal(dim(sea_surface), c(105, 13))187expect_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 test188expect_equal(colnames(sea_surface), c("Year", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))189})190print("Success!")191}192193test_2.3.1 <- function(){194test_that('Solution is incorrect', {195expect_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 test196})197print("Success!")198}199200201test_2.4 <- function(){202test_that('Did not create an object named tidy_temp', {203expect_true(exists("tidy_temp"))204})205test_that('tidy_temp does not contain the correct number of rows and/or columns.', {206expect_equal(dim(tidy_temp), c(1260, 3))207})208test_that('Columns in tidy_temp contain incorrect values.', {209expect_equal(digest(round(as.numeric(sum(tidy_temp$Temperature, na.rm = TRUE)))), 'af04dd98a55b36f9b8344dc9146adec7') # we hid the answer to the test here so you can't see it, but we can still run the test210})211print("Success!")212}213214test_2.5 <- function(){215test_that('Did not create a plot named nov_temp_plot', {216expect_true(exists("nov_temp_plot"))217})218properties <- c(nov_temp_plot$layers[[1]]$mapping, nov_temp_plot$mapping)219test_that('The Month column in nov_temp_plot should only contain November.', {220expect_equal(unique(nov_temp_plot$data$Month), "Nov")221})222test_that('Year should be on the x-axis.', {223expect_true("Year" == rlang::get_expr(properties$x))224})225test_that('Temperature should be on the y-axis.', {226expect_true("Temperature" == rlang::get_expr(properties$y))227})228test_that('nov_temp_plot should be a scatter plot.', {229expect_true("GeomPoint" %in% c(class(nov_temp_plot$layers[[1]]$geom)))230})231print("Success!")232}233234test_2.6 <- function(){235test_that('Did not create a plot named all_temp_plot', {236expect_true(exists("all_temp_plot"))237})238properties <- c(all_temp_plot$layers[[1]]$mapping, all_temp_plot$mapping)239test_that('Need to use tidy_temp as the data!', {240expect_true("Month" %in% colnames(all_temp_plot$data))241})242test_that('Should use facet_wrap to facet by month', {243expect_true('FacetWrap' %in% class(all_temp_plot$facet))244})245test_that('Year should be on the x-axis.', {246expect_true("Year" == rlang::get_expr(properties$x))247})248test_that('Temperature should be on the y-axis.', {249expect_true("Temperature" == rlang::get_expr(properties$y))250})251test_that('all_temp_plot should be a scatter plot.', {252expect_true("GeomPoint" %in% c(class(all_temp_plot$layers[[1]]$geom)))253})254print("Success!")255}256257test_3.1 <- function(){258test_that('Solution is incorrect', {259expect_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 test260})261print("Success!")262}263264test_3.2 <- function(){265test_that('Did not create an object named madrid', {266expect_true(exists("madrid"))267})268test_that('madrid should be a data frame.', {269expect_true('data.frame' %in% class(madrid))270})271test_that('madrid does not contain the correct data.', {272expect_equal(dim(madrid), c(51864, 17))273expect_equal(digest(round(sum(madrid$BEN, na.rm = TRUE))), '2cd026ed377da614f376230e021330a0') # we hid the answer to the test here so you can't see it, but we can still run the test274expect_equal(colnames(madrid), c("date", "BEN", "CO", "EBE", "MXY", "NMHC", "NO_2", "NOx", "OXY", "O_3", "PM10", "PXY", "SO_2", "TCH", "TOL", "year", "mnth"))275})276print("Success!")277}278279test_3.3 <- function(){280test_that('Did not create a plot named EBE_pollution', {281expect_true(exists("EBE_pollution"))282})283properties <- c(EBE_pollution$layers[[1]]$mapping, EBE_pollution$mapping)284test_that('date should be on the x-axis.', {285expect_true("date" == rlang::get_expr(properties$x))286})287test_that('EBE should be on the y-axis.', {288expect_true("EBE" == rlang::get_expr(properties$y))289})290test_that('EBE_pollution should be a scatter plot.', {291expect_true("GeomPoint" %in% c(class(EBE_pollution$layers[[1]]$geom)))292})293test_that('Labels on the axes should be descriptive and human readable.', {294expect_false(EBE_pollution$labels$y == 'EBE')295expect_false(EBE_pollution$labels$x == 'date')296})297print("Success!")298}299300test_3.4 <- function(){301test_that('Did not create an object named madrid_pollution', {302expect_true(exists("madrid_pollution"))303})304test_that('madrid_pollution does not contain the correct number of rows and/or columns.', {305expect_equal(dim(madrid_pollution), c(72, 3))306})307test_that('Columns in avocado_aggregate_2 contain incorrect values.', {308expect_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 test309})310print("Success!")311}312313test_3.5 <- function(){314test_that('Did not create a plot named madrid_plot', {315expect_true(exists("madrid_plot"))316})317properties <- c(madrid_plot$layers[[1]]$mapping, madrid_plot$mapping)318test_that('month should be on the x-axis.', {319expect_true("mnth" == rlang::get_expr(properties$x))320})321test_that('max_ebe should be on the y-axis.', {322expect_true("max_ebe" == rlang::get_expr(properties$y))323})324test_that('madrid_plot should be a scatter plot.', {325expect_true("GeomPoint" %in% c(class(madrid_plot$layers[[1]]$geom)))326})327test_that('Labels on the axes should be descriptive and human readable.', {328expect_false(madrid_plot$labels$y == 'max_ebe')329expect_false(madrid_plot$labels$x == 'mnth')330})331print("Success!")332}333334test_3.6 <- function(){335test_that('Did not create an object named pollution_2001', {336expect_true(exists("pollution_2001"))337})338test_that('pollution_2001 does not contain the correct number of rows and/or columns.', {339expect_equal(dim(pollution_2001), c(1, 14))340})341test_that('Columns in pollution_2001 contain incorrect values.', {342expect_equal(digest(round(as.numeric(pollution_2001$MXY))), '60b03cfa348171e330bfce8c904e05e1') # we hid the answer to the test here so you can't see it, but we can still run the test343expect_equal(digest(round(as.numeric(sum(pollution_2001)))), '0b3b179a82413c4c7e8c77ced0af64bc') # we hid the answer to the test here so you can't see it, but we can still run the test344})345print("Success!")346}347348test_3.7 <- function(){349test_that('Did not create an object named pollution_2006', {350expect_true(exists("pollution_2006"))351})352test_that('pollution_2006 does not contain the correct number of rows and/or columns.', {353expect_equal(dim(pollution_2006), c(1, 14))354})355test_that('Columns in pollution_2006 contain incorrect values.', {356expect_equal(digest(round(as.numeric(pollution_2006$MXY))), 'ab6d52266d2102fdb86c7ebecd3a881e') # we hid the answer to the test here so you can't see it, but we can still run the test357expect_equal(digest(round(as.numeric(sum(pollution_2006)))), '43861b3e08cca196ae86cc7682d51cbc') # 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.8 <- function(){363test_that('Solution is incorrect', {364expect_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 test365})366print("Success!")367}368369test_3.9 <- function(){370test_that('Did not create an object named pollution_diff', {371expect_true(exists("pollution_diff"))372})373test_that('pollution_diff does not contain the correct number of rows and/or columns.', {374expect_equal(dim(pollution_diff), c(14, 2))375})376test_that('pollution_diff does not contain the correct columns: pollutant and value.', {377expect_equal(colnames(pollution_diff), c('pollutant', 'value'))378})379test_that('Columns in pollution_diff contain incorrect values.', {380expect_equal(digest(round(as.numeric(sum(pollution_diff$value)))), 'e2e3d4936bf34e5a6d239da763a0e19a') # 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.10 <- function(){386test_that('Did not create an object named max_pollution_diff', {387expect_true(exists("max_pollution_diff"))388})389test_that('max_pollution_diff does not contain the correct number of rows and/or columns.', {390expect_equal(dim(max_pollution_diff), c(1, 2))391})392test_that('max_pollution_diff does not contain the correct columns: pollutant and value.', {393expect_equal(colnames(max_pollution_diff), c('pollutant', 'value'))394})395test_that('Columns in pollution_diff contain incorrect values.', {396expect_equal(digest(round(as.numeric(sum(max_pollution_diff$value)))), '2ee915b9eb2a629faf934dc8dd9cdc46') # we hid the answer to the test here so you can't see it, but we can still run the test397})398print("Success!")399}400401