Path: blob/master/2021-summer/materials/worksheet_04/tests_worksheet_04.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)2021int_round <- function(x, digits){22x = x*10^digits23xint = as.integer(x)24xint1 = xint + 1L25if (abs(xint - x) < abs(xint1 - x)){26return(xint)27}28else {29return(xint1)30}31}32# -3334test_0.1 <- function(){35test_that('Solution is incorrect', {36expect_equal(digest(answer0.1_A), '05ca18b596514af73f6880309a21b5dd')37expect_equal(digest(answer0.1_B), 'd2a90307aac5ae8d0ef58e2fe730d38b')38expect_equal(digest(answer0.1_C), '05ca18b596514af73f6880309a21b5dd')39expect_equal(digest(answer0.1_D), '05ca18b596514af73f6880309a21b5dd')40expect_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 test41})42print("Success!")43}4445test_0.2 <- function(){46test_that('Solution is incorrect', {47expect_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 test48})49print("Success!")50}5152test_0.3 <- function(){53test_that('Solution is incorrect', {54expect_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 test55})56print("Success!")57}5859test_0.4 <- function(){60test_that('Solution is incorrect', {61expect_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 test62})63print("Success!")64}6566test_0.5 <- function(){67test_that('Solution is incorrect', {68expect_equal(digest(answer0.5_1), '75f1160e72554f4270c809f041c7a776')69expect_equal(digest(answer0.5_2), '01a75cb73d67b0f895ff0e61449c7bf8')70expect_equal(digest(answer0.5_3), '3a5505c06543876fe45598b5e5e5195d')71expect_equal(digest(answer0.5_4), 'f76b651ab8fcb8d470f79550bf2af53a')72expect_equal(digest(answer0.5_5), 'c1f86f7430df7ddb256980ea6a3b57a4')73expect_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 test74})75print("Success!")76}7778test_1.0 <- function(){79test_that('Solution is incorrect', {80expect_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 test81})82print("Success!")83}8485test_1.1 <- function(){86test_that('Did not create an object named world_vaccination', {87expect_true(exists("world_vaccination"))88})89test_that('world_vaccination should be a data frame.', {90expect_true('data.frame' %in% class(world_vaccination))91})92test_that('Did not remove NA values from the pct_vaccinated column.', {93expect_equal(any(is.na(world_vaccination$pct_vaccinated)), FALSE)94})95test_that('world_vaccination does not contain the correct number of rows and/or columns.', {96expect_equal(dim(world_vaccination), c(385, 4))97})98test_that('Columns in world_vaccination contain incorrect values.', {99expect_equal(digest(int_round(sum(world_vaccination$yr), 2)), 'c33bf4bae928bb4b1cd449c386b9e444')100})101print("Success!")102}103104test_1.2 <- function(){105test_that('Did not create a plot named world_vacc_plot', {106expect_true(exists("world_vacc_plot"))107})108test_that('year should be on the x-axis.', {109expect_that("yr" %in% c(rlang::get_expr(world_vacc_plot$mapping$x),rlang::get_expr(world_vacc_plot$layers[[1]]$mapping$x)), is_true())110})111test_that('pct_vaccinated should be on the y-axis.', {112expect_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())113})114test_that('world_vacc_plot should be a scatter plot.', {115expect_true("GeomPoint" %in% c(class(world_vacc_plot$layers[[1]]$geom)))116})117test_that('Labels on the axes should be descriptive and human readable.', {118expect_false((world_vacc_plot$labels$y) == 'pct_vaccinated')119expect_false((world_vacc_plot$labels$x) == 'yr')120})121print("Success!")122}123124test_1.3 <- function(){125test_that('Solution is incorrect', {126expect_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 test127})128print("Success!")129}130131test_1.4 <- function(){132properties <- c(compare_vacc_plot$layers[[1]]$mapping, compare_vacc_plot$mapping)133labels <- compare_vacc_plot$labels134test_that('Did not create a plot named compare_vacc_plot', {135expect_true(exists("compare_vacc_plot"))136})137test_that('year should be on the x-axis.', {138expect_true("yr" == rlang::get_expr(properties$x))139})140test_that('pct_vaccinated should be on the y-axis.', {141expect_true("pct_vaccinated" == rlang::get_expr(properties$y))142})143test_that('vaccine should map to colour and shape.', {144expect_true("vaccine" == rlang::get_expr(properties$colour))145})146test_that('vaccine should map to shape and colour.', {147expect_true("vaccine" == rlang::get_expr(properties$shape))148})149test_that('vaccine should map to shape and colour.', {150expect_false("character" %in% class(compare_vacc_plot$layers[[1]]$mapping$shape))151})152test_that('vaccine should map to shape and colour.', {153expect_false("character" %in% class(compare_vacc_plot$layers[[1]]$mapping$colour))154})155test_that('compare_vacc_plot should be a scatter plot.', {156expect_true("GeomPoint" %in% c(class(compare_vacc_plot$layers[[1]]$geom)))157})158test_that('Labels on the axes and legend need to be changed to be descriptive, nicely formatted, and human readable.', {159expect_false((labels$y) == 'pct_vaccinated')160expect_false((labels$x) == 'yr')161})162print("Success!")163}164165test_1.5 <- function(){166test_that('Did not create an object named polio', {167expect_true(exists("polio"))168})169test_that('The vaccine column in polio should only contain the polio vaccine.', {170expect_equal(unique(polio$vaccine), "polio")171})172test_that('polio does not contain the correct number of rows and/or columns.', {173expect_equal(dim(polio), c(228, 4))174})175test_that('Columns in polio contain incorrect values.', {176expect_equal(digest(int_round(sum(polio$pct_vaccinated), 2)), '58fb69cd5c4b217284c6d49e313ab0e8') # we hid the answer to the test here so you can't see it, but we can still run the test177})178print("Success!")179}180181test_1.6 <- function(){182properties <- c(polio_regions$layers[[1]]$mapping, polio_regions$mapping)183labels <- polio_regions$labels184test_that('Did not create a plot named polio_regions', {185expect_true(exists("polio_regions"))186})187test_that('year should be on the x-axis.', {188expect_true("yr" == rlang::get_expr(properties$x))189})190test_that('pct_vaccinated should be on the y-axis.', {191expect_true("pct_vaccinated" == rlang::get_expr(properties$y))192})193test_that('who_region should map to colour and shape.', {194expect_true("who_region" == rlang::get_expr(properties$colour))195})196test_that('who_region should map to shape and colour.', {197expect_true("who_region" == rlang::get_expr(properties$shape))198})199test_that('polio_regions should be a scatter plot.', {200expect_true("GeomPoint" %in% c(class(polio_regions$layers[[1]]$geom)))201})202test_that('Labels on the axes and legend should be descriptive and human readable.', {203expect_false((labels$y) == 'pct_vaccinated')204expect_false((labels$x) == 'yr')205})206print("Success!")207}208209210test_1.7.1 <- function(){211212properties <- c(polio_regions_line$layers[[1]]$mapping, polio_regions_line$mapping)213labels <- polio_regions_line$labels214215test_that('Did not create a plot named polio_regions_line', {216expect_true(exists("polio_regions_line"))217})218test_that('year should be on the x-axis.', {219expect_true("yr" == rlang::get_expr(properties$x))220})221test_that('pct_vaccinated should be on the y-axis.', {222expect_true("pct_vaccinated" == rlang::get_expr(properties$y))223})224test_that('who_region should map to colour.', {225expect_true("who_region" == rlang::get_expr(properties$colour))226})227228if (length(polio_regions_line$layers) == 2) {229test_that('who_region should map to colour.', {230expect_true("who_region" == rlang::get_expr(c(polio_regions_line$layers[[1]]$mapping)$colour) &231"who_region" == rlang::get_expr(c(polio_regions_line$layers[[2]]$mapping)$colour) |232"who_region" == rlang::get_expr(c(polio_regions_line$layers[[2]]$mapping)$colour) &233"who_region" == rlang::get_expr(c(polio_regions_line$layers[[1]]$mapping)$colour))234} )} else {235test_that('who_region should map to colour.', {236expect_true("who_region" == rlang::get_expr(properties$colour))237})238}239240if (length(polio_regions_line$layers) == 2) { # if there are 2 layers then check if the two layers are points and lines241test_that('polio_regions_line should be a line plot.', {242expect_true("GeomPoint" %in% c(class(polio_regions_line$layers[[1]]$geom)) &243"GeomLine" %in% c(class(polio_regions_line$layers[[2]]$geom)) |244"GeomPoint" %in% c(class(polio_regions_line$layers[[2]]$geom)) &245"GeomLine" %in% c(class(polio_regions_line$layers[[1]]$geom)))246} )} else {247test_that('polio_regions_line should be a line plot.', {248expect_true("GeomLine" %in% c(class(polio_regions_line$layers[[1]]$geom)))249})250}251test_that('Labels on the axes should be descriptive and human readable.', {252expect_false((labels$y) == 'pct_vaccinated')253expect_false((labels$x) == 'yr')254})255print("Success!")256}257258259test_1.7.2 <- function(){260properties <- c(polio_regions_line$layers[[1]]$mapping, polio_regions_line$mapping)261labels <- polio_regions_line$labels262test_that('Did not create a plot named polio_regions_line', {263expect_true(exists("polio_regions_line"))264})265test_that('year should be on the x-axis.', {266expect_true("yr" == rlang::get_expr(properties$x))267})268test_that('pct_vaccinated should be on the y-axis.', {269expect_true("pct_vaccinated" == rlang::get_expr(properties$y))270})271test_that('who_region should map to colour.', {272expect_true("who_region" == rlang::get_expr(properties$colour))273})274275if (length(polio_regions_line$layers) == 2) {276test_that('who_region should map to colour.', {277expect_true("who_region" == rlang::get_expr(c(polio_regions_line$layers[[1]]$mapping)$colour) &278"who_region" == rlang::get_expr(c(polio_regions_line$layers[[2]]$mapping)$colour) |279"who_region" == rlang::get_expr(c(polio_regions_line$layers[[2]]$mapping)$colour) &280"who_region" == rlang::get_expr(c(polio_regions_line$layers[[1]]$mapping)$colour))281} )} else {282test_that('who_region should map to colour.', {283expect_true("who_region" == rlang::get_expr(properties$colour))284})285}286287if (length(polio_regions_line$layers) == 2) { # if there are 2 layers then check if the two layers are points and lines288test_that('polio_regions_line should be a line plot.', {289expect_true("GeomPoint" %in% c(class(polio_regions_line$layers[[1]]$geom)) &290"GeomLine" %in% c(class(polio_regions_line$layers[[2]]$geom)) |291"GeomPoint" %in% c(class(polio_regions_line$layers[[2]]$geom)) &292"GeomLine" %in% c(class(polio_regions_line$layers[[1]]$geom)))293} )} else {294test_that('polio_regions_line should be a line plot.', {295expect_true("GeomLine" %in% c(class(polio_regions_line$layers[[1]]$geom)))296})297}298test_that('Labels on the axes and legend should be descriptive and human readable.', {299expect_false((labels$y) == 'pct_vaccinated')300expect_false((labels$x) == 'yr')301expect_false((labels$colour) == 'who_region')302})303print("Success!")304}305306307test_1.8 <- function(){308properties <- c(side_by_side_world$layers[[1]]$mapping, side_by_side_world$mapping)309labels <- side_by_side_world$labels310test_that('Did not create a plot named side_by_side_world', {311expect_true(exists("side_by_side_world"))312})313test_that('year should be on the x-axis.', {314expect_true("yr" == rlang::get_expr(properties$x))315})316test_that('pct_vaccinated should be on the y-axis.', {317expect_true("pct_vaccinated" == rlang::get_expr(properties$y))318})319test_that('side_by_side_world should be faceted by the vaccine column.', {320expect_true('FacetGrid' %in% class(rlang::get_expr(side_by_side_world$facet)))321})322test_that("side_by_side_world should be split horizontally", {323expect_true("vaccine" %in% names(rlang::get_expr(side_by_side_world$facet$params$cols)))324})325if (length(side_by_side_world$layers) == 2) {326test_that('who_region should map to colour.', {327expect_true("who_region" == rlang::get_expr(c(side_by_side_world$layers[[1]]$mapping)$colour) &328"who_region" == rlang::get_expr(c(side_by_side_world$layers[[2]]$mapping)$colour) |329"who_region" == rlang::get_expr(c(side_by_side_world$layers[[2]]$mapping)$colour) &330"who_region" == rlang::get_expr(c(side_by_side_world$layers[[1]]$mapping)$colour))331} )} else {332test_that('who_region should map to colour.', {333expect_true("who_region" == rlang::get_expr(properties$colour))334})335}336337if (length(side_by_side_world$layers) == 2) { # if there are 2 layers then check if the two layers are points and lines338test_that('side_by_side_world should be a line plot.', {339expect_true("GeomPoint" %in% c(class(side_by_side_world$layers[[1]]$geom)) &340"GeomLine" %in% c(class(side_by_side_world$layers[[2]]$geom)) |341"GeomPoint" %in% c(class(side_by_side_world$layers[[2]]$geom)) &342"GeomLine" %in% c(class(side_by_side_world$layers[[1]]$geom)))343} )} else {344test_that('side_by_side_world should be a line plot.', {345expect_true("GeomLine" %in% c(class(side_by_side_world$layers[[1]]$geom)))346})347}348test_that('Labels on the axes and legend should be descriptive and human readable.', {349expect_false((labels$y) == 'pct_vaccinated')350expect_false((labels$x) == 'yr')351expect_false((labels$colour) == 'who_region')352})353print("Success!")354}355356test_1.9.1 <- function(){357properties <- c(vertical_world$layers[[1]]$mapping, vertical_world$mapping)358labels <- vertical_world$labels359test_that('Did not create a plot named vertical_world', {360expect_true(exists("vertical_world"))361})362test_that('year should be on the x-axis.', {363expect_true("yr" == rlang::get_expr(properties$x))364})365test_that('pct_vaccinated should be on the y-axis.', {366expect_true("pct_vaccinated" == rlang::get_expr(properties$y))367})368test_that('vertical_world should be faceted by the vaccine column.', {369expect_true('FacetGrid' %in% class(rlang::get_expr(vertical_world$facet)))370})371test_that("vertical_world should be split vertically", {372expect_true("vaccine" %in% names(rlang::get_expr(vertical_world$facet$params$rows)))373})374if (length(vertical_world$layers) == 2) {375test_that('who_region should map to colour.', {376expect_true("who_region" == rlang::get_expr(c(vertical_world$layers[[1]]$mapping)$colour) &377"who_region" == rlang::get_expr(c(vertical_world$layers[[2]]$mapping)$colour) |378"who_region" == rlang::get_expr(c(vertical_world$layers[[2]]$mapping)$colour) &379"who_region" == rlang::get_expr(c(vertical_world$layers[[1]]$mapping)$colour))380} )} else {381test_that('who_region should map to colour.', {382expect_true("who_region" == rlang::get_expr(properties$colour))383})384}385386if (length(vertical_world$layers) == 2) { # if there are 2 layers then check if the two layers are points and lines387test_that('vertical_world should be a line plot.', {388expect_true("GeomPoint" %in% c(class(vertical_world$layers[[1]]$geom)) &389"GeomLine" %in% c(class(vertical_world$layers[[2]]$geom)) |390"GeomPoint" %in% c(class(vertical_world$layers[[2]]$geom)) &391"GeomLine" %in% c(class(vertical_world$layers[[1]]$geom)))392} )} else {393test_that('vertical_world should be a line plot.', {394expect_true("GeomLine" %in% c(class(vertical_world$layers[[1]]$geom)))395})396}397test_that('Labels on the axes and legend should be descriptive and human readable.', {398expect_false((labels$y) == 'pct_vaccinated')399expect_false((labels$x) == 'yr')400expect_false((labels$colour) == 'who_region')401})402print("Success!")403}404405test_1.9.2 <- function(){406test_that('Solution is incorrect', {407expect_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 test408})409print("Success!")410}411412test_2.1 <- function(){413test_that('Solution is incorrect', {414expect_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 test415})416print("Success!")417}418419test_2.2.1 <- function(){420test_that('Solution is incorrect', {421expect_equal(digest(answer2.2.1), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test422})423print("Success!")424}425426test_2.2 <- function(){427test_that('Did not create an object named fast_food', {428expect_true(exists("fast_food"))429})430test_that('fast_food does not contain the correct number of rows and/or columns.', {431expect_equal(dim(fast_food), c(10000, 2))432})433test_that('Columns in fast_food contain incorrect values.', {434expect_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 test435expect_equal(digest(as.character(fast_food[[4,2]])), 'd599245d7d7e3f56863ba3a6112ca71b')436})437print("Success!")438}439440test_2.3 <- function(){441test_that('Did not create an object named top_restaurants', {442expect_true(exists("top_restaurants"))443})444test_that('top_restaurants does not contain the correct number of rows and/or columns.', {445expect_equal(dim(top_restaurants), c(9, 2))446})447test_that('Columns in fast_food contain incorrect values.', {448expect_equal(digest(int_round(sum(as.numeric(top_restaurants$n, na.rm = TRUE)), 2)), 'aa0419a07d43532e90a1f1c2d0c2b665')449})450print("Success!")451}452453test_2.4 <- function(){454properties <- c(count_bar_chart$layers[[1]]$mapping, count_bar_chart$mapping)455labels <- count_bar_chart$labels456test_that('Did not create a plot named count_bar_chart', {457expect_true(exists("count_bar_chart"))458})459test_that('name should be on the x-axis.', {460expect_true("name" == rlang::get_expr(properties$x))461})462test_that('n should be on the y-axis.', {463expect_true("n" == rlang::get_expr(properties$y))464})465test_that('vertical_world should be a bar plot.', {466expect_true("GeomBar" %in% c(class(count_bar_chart$layers[[1]]$geom)))467})468test_that('Labels on the axes and legend should be descriptive and human readable.', {469expect_false((labels$y) == 'n')470expect_false((labels$x) == 'name')471})472print("Success!")473}474475test_2.5_A <- function(){476properties <- c(count_bar_chart_A$layers[[1]]$mapping, count_bar_chart_A$mapping)477labels <- count_bar_chart_A$labels478test_that('Did not create a plot named count_bar_chart_A', {479expect_true(exists("count_bar_chart_A"))480})481test_that('name should be on the x-axis.', {482expect_true("name" == rlang::get_expr(properties$x))483})484test_that('x-axis (bar) labels should be at an angle between 20 and 90 degrees.', {485expect_true(count_bar_chart_A$theme$axis.text.x$angle <= 90 & count_bar_chart_A$theme$axis.text.x$angle >= 20)486})487test_that('hjust should equal 1', {488expect_equal(digest(int_round(count_bar_chart_A$theme$axis.text.x$hjust, 2)), '5d6e7fe43b3b73e5fd2961d5162486fa')489})490test_that('n should be on the y-axis.', {491expect_true("n" == rlang::get_expr(properties$y))492})493test_that('vertical_world should be a bar plot.', {494expect_true("GeomBar" %in% c(class(count_bar_chart_A$layers[[1]]$geom)))495})496test_that('Labels on the axes and legend should be descriptive and human readable.', {497expect_false((labels$y) == 'n')498expect_false((labels$x) == 'name')499})500print("Success!")501}502503test_2.5_B <- function(){504properties <- c(count_bar_chart_B$layers[[1]]$mapping, count_bar_chart_B$mapping)505labels <- count_bar_chart_B$labels506test_that('Did not create a plot named count_bar_chart_B', {507expect_true(exists("count_bar_chart_B"))508})509test_that('name should be on the x-axis.', {510expect_true("name" == rlang::get_expr(properties$x))511})512test_that('The coordinate axes should be flipped', {513expect_true("CoordFlip" %in% class(ggplot_build(count_bar_chart_B)$layout$coord))514})515test_that('n should be on the y-axis.', {516expect_true("n" == rlang::get_expr(properties$y))517})518test_that('vertical_world should be a bar plot.', {519expect_true("GeomBar" %in% c(class(count_bar_chart_B$layers[[1]]$geom)))520})521test_that('Labels on the axes and legend should be descriptive and human readable.', {522expect_false((labels$y) == 'n')523expect_false((labels$x) == 'name')524})525print("Success!")526}527528test_2.6 <- function(){529test_that('Solution is incorrect', {530expect_equal(digest(answer2.6), '948a9b527842ee791d4f18fb5594fbf7')531})532print("Success!")533}534535test_2.7 <- function(){536test_that('Did not create an object named state_counts', {537expect_true(exists("state_counts"))538})539test_that('The state column in state_counts should only contain CA, OR, and WA', {540expect_equal((state_counts$st), c("CA", "OR", "WA"))541})542test_that('state_counts does not contain the correct number of rows and/or columns.', {543expect_equal(dim(state_counts), c(3, 2))544})545test_that('Columns in state_counts contain incorrect values.', {546expect_equal(digest(int_round(sum(as.numeric(state_counts$n, na.rm = TRUE)), 2)), 'aa0419a07d43532e90a1f1c2d0c2b665')547})548print("Success!")549}550551test_2.8 <- function(){552properties <- c(state_counts_plot$layers[[1]]$mapping, state_counts_plot$mapping)553labels <- state_counts_plot$labels554test_that('Did not create a plot named state_counts_plot', {555expect_true(exists("state_counts_plot"))556})557test_that('state should be on the x-axis.', {558expect_true("st" == rlang::get_expr(properties$x))559})560test_that('n should be on the y-axis.', {561expect_true("n" == rlang::get_expr(properties$y))562})563test_that('state_counts_plot should be a bar plot.', {564expect_true("GeomBar" %in% c(class(state_counts_plot$layers[[1]]$geom)))565})566test_that('state_counts_plot should not be filled.', {567expect_false("PositionFill" %in% class(state_counts_plot$layers[[1]]$position))568})569test_that('Labels on the axes and legend should be descriptive and human readable.', {570expect_false((labels$y) == 'n')571expect_false((labels$x) == 'st')572})573print("Success!")574}575576test_2.9.0 <- function(){577test_that('Solution is incorrect', {578expect_equal(digest(answer2.9.0), '2bedd54d48692762c119b27f5ec7a320')579})580print("Success!")581}582583test_2.9.1 <- function(){584test_that('Solution is incorrect', {585expect_equal(digest(answer2.9.1), '75f1160e72554f4270c809f041c7a776') # we hid the answer to the test here so you can't see it, but we can still run the test586})587print("Success!")588}589590test_2.9.2 <- function(){591test_that('Did not create an object named top_n_state', {592expect_true(exists("top_n_state"))593})594test_that('The state column in top_n_state should only contain CA, OR, and WA', {595expect_equal(digest(top_n_state$st), '8e6ddcfd9ebaf85379a6d46f7949bce0')596})597test_that('top_n_state does not contain the correct number of rows and/or columns.', {598expect_equal(dim(top_n_state), c(27, 3))599})600test_that('Columns in top_n_state contain incorrect values.', {601expect_equal(digest(int_round(sum(top_n_state$n, na.rm = TRUE), 2)), 'aa0419a07d43532e90a1f1c2d0c2b665')602})603print("Success!")604}605606test_2.9.3 <- function(){607properties <- c(top_n_state_plot$layers[[1]]$mapping, top_n_state_plot$mapping)608labels <- top_n_state_plot$labels609test_that('Did not create a plot named top_n_state_plot', {610expect_true(exists("top_n_state_plot"))611})612test_that('state should be on the x-axis.', {613expect_true("st" == rlang::get_expr(properties$x))614})615test_that('n should be on the y-axis.', {616expect_true("n" == rlang::get_expr(properties$y))617})618test_that('name should be used to determine bar fill colour.', {619expect_true("name" == rlang::get_expr(properties$fill))620})621test_that('top_n_state_plot should be a bar plot.', {622expect_true("GeomBar" %in% c(class(top_n_state_plot$layers[[1]]$geom)))623})624test_that('top_n_state_plot position should be dodge.', {625expect_true("PositionDodge" %in% class(top_n_state_plot$layers[[1]]$position))626})627test_that('Labels on the axes and legend should be descriptive and human readable.', {628expect_false((labels$y) == 'n')629expect_false((labels$x) == 'st')630expect_false((labels$fill) == 'name')631})632print("Success!")633}634635test_2.9.4 <- function(){636properties <- c(top_n_state_plot$layers[[1]]$mapping, top_n_state_plot$mapping)637labels <- top_n_state_plot$labels638test_that('Did not create a plot named top_n_state_plot', {639expect_true(exists("top_n_state_plot"))640})641test_that('state should be on the x-axis.', {642expect_true("st" == rlang::get_expr(properties$x))643})644test_that('n should be on the y-axis.', {645expect_true("n" == rlang::get_expr(properties$y))646})647test_that('name should be used to determine bar fill colour.', {648expect_true("name" == rlang::get_expr(properties$fill))649})650test_that('top_n_state_plot should be a bar plot.', {651expect_true("GeomBar" %in% c(class(top_n_state_plot$layers[[1]]$geom)))652})653test_that('top_n_state_plot position should be fill', {654expect_true("PositionFill" %in% class(top_n_state_plot$layers[[1]]$position))655})656test_that('Labels on the axes and legend should be descriptive and human readable.', {657expect_false((labels$y) == 'n')658expect_false((labels$x) == 'st')659expect_false((labels$x) == 'name')660})661print("Success!")662}663664test_2.9.5 <- function(){665test_that('Solution is incorrect', {666expect_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 test667})668print("Success!")669}670671672