Path: blob/master/2019-fall/materials/worksheet_04/tests_worksheet_04.R
2715 views
library(testthat)1library(digest)23test_0.1 <- function(){4test_that('Solution is incorrect', {5expect_equal(digest(answer0.1_A), '05ca18b596514af73f6880309a21b5dd')6expect_equal(digest(answer0.1_B), 'd2a90307aac5ae8d0ef58e2fe730d38b')7expect_equal(digest(answer0.1_C), '05ca18b596514af73f6880309a21b5dd')8expect_equal(digest(answer0.1_D), '05ca18b596514af73f6880309a21b5dd')9expect_equal(digest(answer0.1_E), 'd2a90307aac5ae8d0ef58e2fe730d38b')# we hid the answer to the test here so you can't see it, but we can still run the test10})11print("Success!")12}1314test_0.2 <- function(){15test_that('Solution is incorrect', {16expect_equal(digest(answer0.2), '75f1160e72554f4270c809f041c7a776') # we hid the answer to the test here so you can't see it, but we can still run the test17})18print("Success!")19}2021test_0.3 <- function(){22test_that('Solution is incorrect', {23expect_equal(digest(answer0.3), 'c1f86f7430df7ddb256980ea6a3b57a4') # we hid the answer to the test here so you can't see it, but we can still run the test24})25print("Success!")26}2728test_0.4 <- function(){29test_that('Solution is incorrect', {30expect_equal(digest(answer0.4), 'c1f86f7430df7ddb256980ea6a3b57a4') # we hid the answer to the test here so you can't see it, but we can still run the test31})32print("Success!")33}3435test_0.5 <- function(){36test_that('Solution is incorrect', {37expect_equal(digest(answer0.5_1), '75f1160e72554f4270c809f041c7a776')38expect_equal(digest(answer0.5_2), '01a75cb73d67b0f895ff0e61449c7bf8')39expect_equal(digest(answer0.5_3), '3a5505c06543876fe45598b5e5e5195d')40expect_equal(digest(answer0.5_4), 'f76b651ab8fcb8d470f79550bf2af53a')41expect_equal(digest(answer0.5_5), 'c1f86f7430df7ddb256980ea6a3b57a4')42expect_equal(digest(answer0.5_6), '475bf9280aab63a82af60791302736f6')# 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_1.0 <- function(){48test_that('Solution is incorrect', {49expect_equal(digest(answer1.0), '75f1160e72554f4270c809f041c7a776') # 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_1.1 <- function(){55test_that('Did not create an object named world_vaccination', {56expect_true(exists("world_vaccination"))57})58test_that('world_vaccination should be a data frame.', {59expect_true('data.frame' %in% class(world_vaccination))60})61test_that('Did not remove NA values from the pct_vaccinated column.', {62expect_equal(any(is.na(world_vaccination$pct_vaccinated)), FALSE)63})64test_that('world_vaccination does not contain the correct number of rows and/or columns.', {65expect_equal(dim(world_vaccination), c(385, 4))66})67test_that('Columns in world_vaccination contain incorrect values.', {68expect_equal(digest(as.numeric(sum(world_vaccination$year))), 'ed828cd9c4dbc736fb12a1a8643aaeec')69})70print("Success!")71}7273test_1.2 <- function(){74test_that('Did not create a plot named world_vacc_plot', {75expect_true(exists("world_vacc_plot"))76})77test_that('year should be on the x-axis.', {78expect_that("year" %in% c(rlang::get_expr(world_vacc_plot$mapping$x),rlang::get_expr(world_vacc_plot$layers[[1]]$mapping$x)), is_true())79})80test_that('pct_vaccinated should be on the y-axis.', {81expect_that("pct_vaccinated" %in% c(rlang::get_expr(world_vacc_plot$mapping$y), rlang::get_expr(world_vacc_plot$layers[[1]]$mapping$y)) , is_true())82})83test_that('world_vacc_plot should be a scatter plot.', {84expect_that("GeomPoint" %in% c(class(world_vacc_plot$layers[[1]]$geom)) , is_true())85})86test_that('Labels on the axes should be descriptive and human readable.', {87expect_that((world_vacc_plot$labels$y) == 'pct_vaccinated', is_false())88expect_that((world_vacc_plot$labels$x) == 'year', is_false())89})90print("Success!")91}9293test_1.3 <- function(){94test_that('Solution is incorrect', {95expect_equal(digest(answer1.3), 'c1f86f7430df7ddb256980ea6a3b57a4') # we hid the answer to the test here so you can't see it, but we can still run the test96})97print("Success!")98}99100test_1.4 <- function(){101properties <- c(compare_vacc_plot$layers[[1]]$mapping, compare_vacc_plot$mapping)102labels <- compare_vacc_plot$labels103test_that('Did not create a plot named compare_vacc_plot', {104expect_true(exists("compare_vacc_plot"))105})106test_that('year should be on the x-axis.', {107expect_true("year" == rlang::get_expr(properties$x))108})109test_that('pct_vaccinated should be on the y-axis.', {110expect_true("pct_vaccinated" == rlang::get_expr(properties$y))111})112test_that('vaccine should map to colour and shape.', {113expect_true("vaccine" == rlang::get_expr(properties$colour))114})115test_that('vaccine should map to shape and colour.', {116expect_true("vaccine" == rlang::get_expr(properties$shape))117})118test_that('compare_vacc_plot should be a scatter plot.', {119expect_that("GeomPoint" %in% c(class(compare_vacc_plot$layers[[1]]$geom)) , is_true())120})121test_that('Labels on the axes and legend need to be changed to be descriptive, nicely formatted, and human readable.', {122expect_that((labels$y) == 'pct_vaccinated', is_false())123expect_that((labels$x) == 'year', is_false())124expect_that((labels$colour) == 'vaccine', is_false())125expect_that((labels$shape) == 'vaccine', is_false())126})127print("Success!")128}129130test_1.5 <- function(){131test_that('Did not create an object named polio', {132expect_true(exists("polio"))133})134test_that('The vaccine column in polio should only contain the polio vaccine.', {135expect_equal(unique(polio$vaccine), "polio")136})137test_that('polio does not contain the correct number of rows and/or columns.', {138expect_equal(dim(polio), c(228, 4))139})140test_that('Columns in polio contain incorrect values.', {141expect_equal(digest(as.numeric(sum(polio$pct_vaccinated))), 'cfd7ed9e50ed446d50289ff89ef338a4') # we hid the answer to the test here so you can't see it, but we can still run the test142})143print("Success!")144}145146test_1.6 <- function(){147properties <- c(polio_regions$layers[[1]]$mapping, polio_regions$mapping)148labels <- polio_regions$labels149test_that('Did not create a plot named polio_regions', {150expect_true(exists("polio_regions"))151})152test_that('year should be on the x-axis.', {153expect_true("year" == rlang::get_expr(properties$x))154})155test_that('pct_vaccinated should be on the y-axis.', {156expect_true("pct_vaccinated" == rlang::get_expr(properties$y))157})158test_that('who_region should map to colour and shape.', {159expect_true("who_region" == rlang::get_expr(properties$colour))160})161test_that('who_region should map to shape and colour.', {162expect_true("who_region" == rlang::get_expr(properties$shape))163})164test_that('polio_regions should be a scatter plot.', {165expect_that("GeomPoint" %in% c(class(polio_regions$layers[[1]]$geom)) , is_true())166})167test_that('Labels on the axes and legend should be descriptive and human readable.', {168expect_that((labels$y) == 'pct_vaccinated', is_false())169expect_that((labels$x) == 'year', is_false())170})171print("Success!")172}173174test_1.7.1 <- function(){175properties <- c(polio_regions_line$layers[[1]]$mapping, polio_regions_line$mapping)176labels <- polio_regions_line$labels177test_that('Did not create a plot named polio_regions_line', {178expect_true(exists("polio_regions_line"))179})180test_that('year should be on the x-axis.', {181expect_true("year" == rlang::get_expr(properties$x))182})183test_that('pct_vaccinated should be on the y-axis.', {184expect_true("pct_vaccinated" == rlang::get_expr(properties$y))185})186test_that('who_region should map to colour.', {187expect_true("who_region" == rlang::get_expr(properties$colour))188})189test_that('polio_regions_line should be a line plot.', {190expect_that("GeomLine" %in% c(class(polio_regions_line$layers[[1]]$geom)) , is_true())191})192test_that('Labels on the axes should be descriptive and human readable.', {193expect_that((labels$y) == 'pct_vaccinated', is_false())194expect_that((labels$x) == 'year', is_false())195})196print("Success!")197}198199test_1.7.2 <- function(){200properties <- c(polio_regions_line$layers[[1]]$mapping, polio_regions_line$mapping)201labels <- polio_regions_line$labels202test_that('Did not create a plot named polio_regions_line', {203expect_true(exists("polio_regions_line"))204})205test_that('year should be on the x-axis.', {206expect_true("year" == rlang::get_expr(properties$x))207})208test_that('pct_vaccinated should be on the y-axis.', {209expect_true("pct_vaccinated" == rlang::get_expr(properties$y))210})211test_that('who_region should map to colour.', {212expect_true("who_region" == rlang::get_expr(properties$colour))213})214test_that('polio_regions_line should be a line plot.', {215expect_that("GeomLine" %in% c(class(polio_regions_line$layers[[1]]$geom)) , is_true())216})217test_that('Labels on the axes and legend should be descriptive and human readable.', {218expect_that((labels$y) == 'pct_vaccinated', is_false())219expect_that((labels$x) == 'year', is_false())220expect_that((labels$colour) == 'who_region', is_false())221})222print("Success!")223}224225test_1.8 <- function(){226properties <- c(side_by_side_world$layers[[1]]$mapping, side_by_side_world$mapping)227labels <- side_by_side_world$labels228test_that('Did not create a plot named side_by_side_world', {229expect_true(exists("side_by_side_world"))230})231test_that('year should be on the x-axis.', {232expect_true("year" == rlang::get_expr(properties$x))233})234test_that('pct_vaccinated should be on the y-axis.', {235expect_true("pct_vaccinated" == rlang::get_expr(properties$y))236})237test_that('who_region should map to colour.', {238expect_true("who_region" == rlang::get_expr(properties$colour))239})240test_that('side_by_side_world should be faceted by the vaccine column.', {241expect_that('FacetGrid' %in% class(rlang::get_expr(side_by_side_world$facet)), is_true())242})243test_that('side_by_side_world should be a line plot.', {244expect_that("GeomLine" %in% c(class(side_by_side_world$layers[[1]]$geom)) , is_true())245})246test_that('Labels on the axes and legend should be descriptive and human readable.', {247expect_that((labels$y) == 'pct_vaccinated', is_false())248expect_that((labels$x) == 'year', is_false())249expect_that((labels$colour) == 'who_region', is_false())250})251print("Success!")252}253254test_1.9.1 <- function(){255properties <- c(vertical_world$layers[[1]]$mapping, vertical_world$mapping)256labels <- vertical_world$labels257test_that('Did not create a plot named vertical_world', {258expect_true(exists("vertical_world"))259})260test_that('year should be on the x-axis.', {261expect_true("year" == rlang::get_expr(properties$x))262})263test_that('pct_vaccinated should be on the y-axis.', {264expect_true("pct_vaccinated" == rlang::get_expr(properties$y))265})266test_that('who_region should map to colour.', {267expect_true("who_region" == rlang::get_expr(properties$colour))268})269test_that('vertical_world should be faceted by the vaccine column.', {270expect_that('FacetGrid' %in% class(rlang::get_expr(vertical_world$facet)), is_true())271})272test_that('vertical_world should be a line plot.', {273expect_that("GeomLine" %in% c(class(vertical_world$layers[[1]]$geom)) , is_true())274})275test_that('Labels on the axes and legend should be descriptive and human readable.', {276expect_that((labels$y) == 'pct_vaccinated', is_false())277expect_that((labels$x) == 'year', is_false())278expect_that((labels$colour) == 'who_region', is_false())279})280print("Success!")281}282283test_1.9.2 <- function(){284test_that('Solution is incorrect', {285expect_equal(digest(answer1.9.2), 'c1f86f7430df7ddb256980ea6a3b57a4') # we hid the answer to the test here so you can't see it, but we can still run the test286})287print("Success!")288}289290test_2.1 <- function(){291test_that('Solution is incorrect', {292expect_equal(digest(answer2.1), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test293})294print("Success!")295}296297test_2.2 <- function(){298test_that('Did not create an object named fast_food', {299expect_true(exists("fast_food"))300})301test_that('fast_food does not contain the correct number of rows and/or columns.', {302expect_equal(dim(fast_food), c(10000, 2))303})304test_that('Columns in fast_food contain incorrect values.', {305expect_equal(digest(as.character(fast_food[[3,1]])), '2e716500dfeb89b1b087089a5b1355f8') # we hid the answer to the test here so you can't see it, but we can still run the test306expect_equal(digest(as.character(fast_food[[4,2]])), 'd599245d7d7e3f56863ba3a6112ca71b')307})308print("Success!")309}310311test_2.3 <- function(){312test_that('Did not create an object named top_restaurants', {313expect_true(exists("top_restaurants"))314})315test_that('top_restaurants does not contain the correct number of rows and/or columns.', {316expect_equal(dim(top_restaurants), c(9, 2))317})318test_that('Columns in fast_food contain incorrect values.', {319expect_equal(digest(sum(as.numeric(top_restaurants$n, na.rm = TRUE))), '4745bd90664c8cb9935cfb1a4cf51d77')320})321print("Success!")322}323324test_2.4 <- function(){325properties <- c(count_bar_chart$layers[[1]]$mapping, count_bar_chart$mapping)326labels <- count_bar_chart$labels327test_that('Did not create a plot named count_bar_chart', {328expect_true(exists("count_bar_chart"))329})330test_that('name should be on the x-axis.', {331expect_true("name" == rlang::get_expr(properties$x))332})333test_that('n should be on the y-axis.', {334expect_true("n" == rlang::get_expr(properties$y))335})336test_that('vertical_world should be a bar plot.', {337expect_that("GeomBar" %in% c(class(count_bar_chart$layers[[1]]$geom)) , is_true())338})339test_that('Labels on the axes and legend should be descriptive and human readable.', {340expect_that((labels$y) == 'n', is_false())341expect_that((labels$x) == 'name', is_false())342})343print("Success!")344}345346test_2.5_A <- function(){347properties <- c(count_bar_chart_A$layers[[1]]$mapping, count_bar_chart_A$mapping)348labels <- count_bar_chart_A$labels349test_that('Did not create a plot named count_bar_chart_A', {350expect_true(exists("count_bar_chart_A"))351})352test_that('name should be on the x-axis.', {353expect_true("name" == rlang::get_expr(properties$x))354})355test_that('x-axis (bar) labels should be at an angle between 20 and 90 degrees.', {356expect_that(count_bar_chart_A$theme$axis.text.x$angle <= 90 & count_bar_chart_A$theme$axis.text.x$angle >= 20, is_true())357})358test_that('hjust should equal 1', {359expect_equal(digest(as.numeric(count_bar_chart_A$theme$axis.text.x$hjust)), '6717f2823d3202449301145073ab8719')360})361test_that('n should be on the y-axis.', {362expect_true("n" == rlang::get_expr(properties$y))363})364test_that('vertical_world should be a bar plot.', {365expect_that("GeomBar" %in% c(class(count_bar_chart_A$layers[[1]]$geom)) , is_true())366})367test_that('Labels on the axes and legend should be descriptive and human readable.', {368expect_that((labels$y) == 'n', is_false())369expect_that((labels$x) == 'name', is_false())370})371print("Success!")372}373374test_2.5_B <- function(){375properties <- c(count_bar_chart_B$layers[[1]]$mapping, count_bar_chart_B$mapping)376labels <- count_bar_chart_B$labels377test_that('Did not create a plot named count_bar_chart_B', {378expect_true(exists("count_bar_chart_B"))379})380test_that('name should be on the x-axis.', {381expect_true("name" == rlang::get_expr(properties$x))382})383test_that('The coordinate axes should be flipped', {384expect_true("CoordFlip" %in% class(ggplot_build(count_bar_chart_B)$layout$coord))385})386test_that('n should be on the y-axis.', {387expect_true("n" == rlang::get_expr(properties$y))388})389test_that('vertical_world should be a bar plot.', {390expect_that("GeomBar" %in% c(class(count_bar_chart_B$layers[[1]]$geom)) , is_true())391})392test_that('Labels on the axes and legend should be descriptive and human readable.', {393expect_that((labels$y) == 'n', is_false())394expect_that((labels$x) == 'name', is_false())395})396print("Success!")397}398399test_2.6 <- function(){400test_that('Solution is incorrect', {401expect_equal(digest(answer2.6), '948a9b527842ee791d4f18fb5594fbf7')402})403print("Success!")404}405406test_2.7 <- function(){407test_that('Did not create an object named state_counts', {408expect_true(exists("state_counts"))409})410test_that('The state column in state_counts should only contain CA, OR, and WA', {411expect_equal((state_counts$state), c("CA", "OR", "WA"))412})413test_that('state_counts does not contain the correct number of rows and/or columns.', {414expect_equal(dim(state_counts), c(3, 2))415})416test_that('Columns in state_counts contain incorrect values.', {417expect_equal(digest(sum(as.numeric(state_counts$n, na.rm = TRUE))), '4745bd90664c8cb9935cfb1a4cf51d77')418})419print("Success!")420}421422test_2.8 <- function(){423properties <- c(state_counts_plot$layers[[1]]$mapping, state_counts_plot$mapping)424labels <- state_counts_plot$labels425test_that('Did not create a plot named state_counts_plot', {426expect_true(exists("state_counts_plot"))427})428test_that('state should be on the x-axis.', {429expect_true("state" == rlang::get_expr(properties$x))430})431test_that('n should be on the y-axis.', {432expect_true("n" == rlang::get_expr(properties$y))433})434test_that('state_counts_plot should be a bar plot.', {435expect_that("GeomBar" %in% c(class(state_counts_plot$layers[[1]]$geom)) , is_true())436})437test_that('Labels on the axes and legend should be descriptive and human readable.', {438expect_that((labels$y) == 'n', is_false())439expect_that((labels$x) == 'state', is_false())440})441print("Success!")442}443444test_2.9.1 <- function(){445test_that('Solution is incorrect', {446expect_equal(digest(answer2.9.1), '2bedd54d48692762c119b27f5ec7a320')447})448print("Success!")449}450451test_2.9.2 <- function(){452test_that('Did not create an object named top_n_state', {453expect_true(exists("top_n_state"))454})455test_that('The state column in top_n_state should only contain CA, OR, and WA', {456expect_equal(digest(top_n_state$state), '8e6ddcfd9ebaf85379a6d46f7949bce0')457})458test_that('top_n_state does not contain the correct number of rows and/or columns.', {459expect_equal(dim(top_n_state), c(27, 3))460})461test_that('Columns in top_n_state contain incorrect values.', {462expect_equal(digest(sum(as.numeric(top_n_state$n, na.rm = TRUE))), '4745bd90664c8cb9935cfb1a4cf51d77')463})464print("Success!")465}466467test_2.9.3 <- function(){468properties <- c(top_n_state_plot$layers[[1]]$mapping, top_n_state_plot$mapping)469labels <- top_n_state_plot$labels470test_that('Did not create a plot named top_n_state_plot', {471expect_true(exists("top_n_state_plot"))472})473test_that('state should be on the x-axis.', {474expect_true("state" == rlang::get_expr(properties$x))475})476test_that('n should be on the y-axis.', {477expect_true("n" == rlang::get_expr(properties$y))478})479test_that('name should be used to determine bar fill colour.', {480expect_true("name" == rlang::get_expr(properties$fill))481})482test_that('top_n_state_plot should be a bar plot.', {483expect_that("GeomBar" %in% c(class(top_n_state_plot$layers[[1]]$geom)) , is_true())484})485test_that('Labels on the axes and legend should be descriptive and human readable.', {486expect_that((labels$y) == 'n', is_false())487expect_that((labels$x) == 'state', is_false())488expect_that((labels$fill) == 'name', is_false())489})490print("Success!")491}492493test_2.9.4 <- function(){494properties <- c(top_n_state_plot$layers[[1]]$mapping, top_n_state_plot$mapping)495labels <- top_n_state_plot$labels496test_that('Did not create a plot named top_n_state_plot', {497expect_true(exists("top_n_state_plot"))498})499test_that('state should be on the x-axis.', {500expect_true("state" == rlang::get_expr(properties$x))501})502test_that('n should be on the y-axis.', {503expect_true("n" == rlang::get_expr(properties$y))504})505test_that('name should be used to determine bar fill colour.', {506expect_true("name" == rlang::get_expr(properties$fill))507})508test_that('top_n_state_plot should be a bar plot.', {509expect_that("GeomBar" %in% c(class(top_n_state_plot$layers[[1]]$geom)) , is_true())510})511test_that('Labels on the axes and legend should be descriptive and human readable.', {512expect_that((labels$y) == 'n', is_false())513expect_that((labels$x) == 'state', is_false())514expect_that((labels$x) == 'name', is_false())515})516print("Success!")517}518519test_2.9.5 <- function(){520test_that('Solution is incorrect', {521expect_equal(digest(answer2.9.5), '0590b0427c1b19a6eb612d19888aa52f') # we hid the answer to the test here so you can't see it, but we can still run the test522})523print("Success!")524}525526