Path: blob/master/2022-spring/materials/worksheet_viz/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.1 <- function(){33test_that('Solution is incorrect', {34expect_equal(digest(answer0.1_A), '05ca18b596514af73f6880309a21b5dd')35expect_equal(digest(answer0.1_B), 'd2a90307aac5ae8d0ef58e2fe730d38b')36expect_equal(digest(answer0.1_C), '05ca18b596514af73f6880309a21b5dd')37expect_equal(digest(answer0.1_D), '05ca18b596514af73f6880309a21b5dd')38expect_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 test39})40print("Success!")41}4243test_0.2 <- function(){44test_that('Solution is incorrect', {45expect_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 test46})47print("Success!")48}4950test_0.3 <- function(){51test_that('Solution is incorrect', {52expect_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 test53})54print("Success!")55}5657test_0.4 <- function(){58test_that('Solution is incorrect', {59expect_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 test60})61print("Success!")62}6364test_0.5 <- function(){65test_that('Solution is incorrect', {66expect_equal(digest(answer0.5_1), '75f1160e72554f4270c809f041c7a776')67expect_equal(digest(answer0.5_2), '01a75cb73d67b0f895ff0e61449c7bf8')68expect_equal(digest(answer0.5_3), '3a5505c06543876fe45598b5e5e5195d')69expect_equal(digest(answer0.5_4), 'f76b651ab8fcb8d470f79550bf2af53a')70expect_equal(digest(answer0.5_5), 'c1f86f7430df7ddb256980ea6a3b57a4')71expect_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 test72})73print("Success!")74}7576test_1.0 <- function(){77test_that('Solution is incorrect', {78expect_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 test79})80print("Success!")81}8283test_1.1 <- function(){84test_that('Did not create an object named world_vaccination', {85expect_true(exists("world_vaccination"))86})87test_that('world_vaccination should be a data frame.', {88expect_true('data.frame' %in% class(world_vaccination))89})90test_that('Did not remove NA values from the pct_vaccinated column.', {91expect_equal(any(is.na(world_vaccination$pct_vaccinated)), FALSE)92})93test_that('world_vaccination does not contain the correct number of rows and/or columns.', {94expect_equal(dim(world_vaccination), c(385, 4))95})96test_that('Columns in world_vaccination contain incorrect values.', {97expect_equal(digest(int_round(sum(world_vaccination$yr), 2)), 'c33bf4bae928bb4b1cd449c386b9e444')98})99print("Success!")100}101102test_1.2 <- function(){103test_that('Did not create a plot named world_vacc_plot', {104expect_true(exists("world_vacc_plot"))105})106test_that('year should be on the x-axis.', {107expect_that("yr" %in% c(rlang::get_expr(world_vacc_plot$mapping$x),rlang::get_expr(world_vacc_plot$layers[[1]]$mapping$x)), is_true())108})109test_that('pct_vaccinated should be on the y-axis.', {110expect_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())111})112test_that('world_vacc_plot should be a scatter plot.', {113expect_true("GeomPoint" %in% c(class(world_vacc_plot$layers[[1]]$geom)))114})115test_that('Labels on the axes should be descriptive and human readable.', {116expect_false((world_vacc_plot$labels$y) == 'pct_vaccinated')117expect_false((world_vacc_plot$labels$x) == 'yr')118})119print("Success!")120}121122test_1.3 <- function(){123test_that('Solution is incorrect', {124expect_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 test125})126print("Success!")127}128129test_1.4 <- function(){130properties <- c(compare_vacc_plot$layers[[1]]$mapping, compare_vacc_plot$mapping)131labels <- compare_vacc_plot$labels132test_that('Did not create a plot named compare_vacc_plot', {133expect_true(exists("compare_vacc_plot"))134})135test_that('year should be on the x-axis.', {136expect_true("yr" == rlang::get_expr(properties$x))137})138test_that('pct_vaccinated should be on the y-axis.', {139expect_true("pct_vaccinated" == rlang::get_expr(properties$y))140})141test_that('vaccine should map to colour and shape.', {142expect_true("vaccine" == rlang::get_expr(properties$colour))143})144test_that('vaccine should map to shape and colour.', {145expect_true("vaccine" == rlang::get_expr(properties$shape))146})147test_that('vaccine should map to shape and colour.', {148expect_false("character" %in% class(compare_vacc_plot$layers[[1]]$mapping$shape))149})150test_that('vaccine should map to shape and colour.', {151expect_false("character" %in% class(compare_vacc_plot$layers[[1]]$mapping$colour))152})153test_that('compare_vacc_plot should be a scatter plot.', {154expect_true("GeomPoint" %in% c(class(compare_vacc_plot$layers[[1]]$geom)))155})156test_that('Labels on the axes and legend need to be changed to be descriptive, nicely formatted, and human readable.', {157expect_false((labels$y) == 'pct_vaccinated')158expect_false((labels$x) == 'yr')159})160print("Success!")161}162163test_1.5 <- function(){164test_that('Did not create an object named polio', {165expect_true(exists("polio"))166})167test_that('The vaccine column in polio should only contain the polio vaccine.', {168expect_equal(unique(polio$vaccine), "polio")169})170test_that('polio does not contain the correct number of rows and/or columns.', {171expect_equal(dim(polio), c(228, 4))172})173test_that('Columns in polio contain incorrect values.', {174expect_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 test175})176print("Success!")177}178179test_1.6 <- function(){180properties <- c(polio_regions$layers[[1]]$mapping, polio_regions$mapping)181labels <- polio_regions$labels182test_that('Did not create a plot named polio_regions', {183expect_true(exists("polio_regions"))184})185test_that('year should be on the x-axis.', {186expect_true("yr" == rlang::get_expr(properties$x))187})188test_that('pct_vaccinated should be on the y-axis.', {189expect_true("pct_vaccinated" == rlang::get_expr(properties$y))190})191test_that('who_region should map to colour and shape.', {192expect_true("who_region" == rlang::get_expr(properties$colour))193})194test_that('who_region should map to shape and colour.', {195expect_true("who_region" == rlang::get_expr(properties$shape))196})197test_that('polio_regions should be a scatter plot.', {198expect_true("GeomPoint" %in% c(class(polio_regions$layers[[1]]$geom)))199})200test_that('Labels on the axes and legend should be descriptive and human readable.', {201expect_false((labels$y) == 'pct_vaccinated')202expect_false((labels$x) == 'yr')203})204print("Success!")205}206207208test_1.7.1 <- function(){209210properties <- c(polio_regions_line$layers[[1]]$mapping, polio_regions_line$mapping)211labels <- polio_regions_line$labels212213test_that('Did not create a plot named polio_regions_line', {214expect_true(exists("polio_regions_line"))215})216test_that('year should be on the x-axis.', {217expect_true("yr" == rlang::get_expr(properties$x))218})219test_that('pct_vaccinated should be on the y-axis.', {220expect_true("pct_vaccinated" == rlang::get_expr(properties$y))221})222test_that('who_region should map to colour.', {223expect_true("who_region" == rlang::get_expr(properties$colour))224})225226if (length(polio_regions_line$layers) == 2) {227test_that('who_region should map to colour.', {228expect_true("who_region" == rlang::get_expr(c(polio_regions_line$layers[[1]]$mapping)$colour) &229"who_region" == rlang::get_expr(c(polio_regions_line$layers[[2]]$mapping)$colour) |230"who_region" == rlang::get_expr(c(polio_regions_line$layers[[2]]$mapping)$colour) &231"who_region" == rlang::get_expr(c(polio_regions_line$layers[[1]]$mapping)$colour))232} )} else {233test_that('who_region should map to colour.', {234expect_true("who_region" == rlang::get_expr(properties$colour))235})236}237238if (length(polio_regions_line$layers) == 2) { # if there are 2 layers then check if the two layers are points and lines239test_that('polio_regions_line should be a line plot.', {240expect_true("GeomPoint" %in% c(class(polio_regions_line$layers[[1]]$geom)) &241"GeomLine" %in% c(class(polio_regions_line$layers[[2]]$geom)) |242"GeomPoint" %in% c(class(polio_regions_line$layers[[2]]$geom)) &243"GeomLine" %in% c(class(polio_regions_line$layers[[1]]$geom)))244} )} else {245test_that('polio_regions_line should be a line plot.', {246expect_true("GeomLine" %in% c(class(polio_regions_line$layers[[1]]$geom)))247})248}249test_that('Labels on the axes should be descriptive and human readable.', {250expect_false((labels$y) == 'pct_vaccinated')251expect_false((labels$x) == 'yr')252})253print("Success!")254}255256257test_1.7.2 <- function(){258properties <- c(polio_regions_line$layers[[1]]$mapping, polio_regions_line$mapping)259labels <- polio_regions_line$labels260test_that('Did not create a plot named polio_regions_line', {261expect_true(exists("polio_regions_line"))262})263test_that('year should be on the x-axis.', {264expect_true("yr" == rlang::get_expr(properties$x))265})266test_that('pct_vaccinated should be on the y-axis.', {267expect_true("pct_vaccinated" == rlang::get_expr(properties$y))268})269test_that('who_region should map to colour.', {270expect_true("who_region" == rlang::get_expr(properties$colour))271})272273if (length(polio_regions_line$layers) == 2) {274test_that('who_region should map to colour.', {275expect_true("who_region" == rlang::get_expr(c(polio_regions_line$layers[[1]]$mapping)$colour) &276"who_region" == rlang::get_expr(c(polio_regions_line$layers[[2]]$mapping)$colour) |277"who_region" == rlang::get_expr(c(polio_regions_line$layers[[2]]$mapping)$colour) &278"who_region" == rlang::get_expr(c(polio_regions_line$layers[[1]]$mapping)$colour))279} )} else {280test_that('who_region should map to colour.', {281expect_true("who_region" == rlang::get_expr(properties$colour))282})283}284285if (length(polio_regions_line$layers) == 2) { # if there are 2 layers then check if the two layers are points and lines286test_that('polio_regions_line should be a line plot.', {287expect_true("GeomPoint" %in% c(class(polio_regions_line$layers[[1]]$geom)) &288"GeomLine" %in% c(class(polio_regions_line$layers[[2]]$geom)) |289"GeomPoint" %in% c(class(polio_regions_line$layers[[2]]$geom)) &290"GeomLine" %in% c(class(polio_regions_line$layers[[1]]$geom)))291} )} else {292test_that('polio_regions_line should be a line plot.', {293expect_true("GeomLine" %in% c(class(polio_regions_line$layers[[1]]$geom)))294})295}296test_that('Labels on the axes and legend should be descriptive and human readable.', {297expect_false((labels$y) == 'pct_vaccinated')298expect_false((labels$x) == 'yr')299expect_false((labels$colour) == 'who_region')300})301print("Success!")302}303304305test_1.8 <- function(){306properties <- c(side_by_side_world$layers[[1]]$mapping, side_by_side_world$mapping)307labels <- side_by_side_world$labels308test_that('Did not create a plot named side_by_side_world', {309expect_true(exists("side_by_side_world"))310})311test_that('year should be on the x-axis.', {312expect_true("yr" == rlang::get_expr(properties$x))313})314test_that('pct_vaccinated should be on the y-axis.', {315expect_true("pct_vaccinated" == rlang::get_expr(properties$y))316})317test_that('side_by_side_world should be faceted by the vaccine column.', {318expect_true('FacetGrid' %in% class(rlang::get_expr(side_by_side_world$facet)))319})320test_that("side_by_side_world should be split horizontally", {321expect_true("vaccine" %in% names(rlang::get_expr(side_by_side_world$facet$params$cols)))322})323if (length(side_by_side_world$layers) == 2) {324test_that('who_region should map to colour.', {325expect_true("who_region" == rlang::get_expr(c(side_by_side_world$layers[[1]]$mapping)$colour) &326"who_region" == rlang::get_expr(c(side_by_side_world$layers[[2]]$mapping)$colour) |327"who_region" == rlang::get_expr(c(side_by_side_world$layers[[2]]$mapping)$colour) &328"who_region" == rlang::get_expr(c(side_by_side_world$layers[[1]]$mapping)$colour))329} )} else {330test_that('who_region should map to colour.', {331expect_true("who_region" == rlang::get_expr(properties$colour))332})333}334335if (length(side_by_side_world$layers) == 2) { # if there are 2 layers then check if the two layers are points and lines336test_that('side_by_side_world should be a line plot.', {337expect_true("GeomPoint" %in% c(class(side_by_side_world$layers[[1]]$geom)) &338"GeomLine" %in% c(class(side_by_side_world$layers[[2]]$geom)) |339"GeomPoint" %in% c(class(side_by_side_world$layers[[2]]$geom)) &340"GeomLine" %in% c(class(side_by_side_world$layers[[1]]$geom)))341} )} else {342test_that('side_by_side_world should be a line plot.', {343expect_true("GeomLine" %in% c(class(side_by_side_world$layers[[1]]$geom)))344})345}346test_that('Labels on the axes and legend should be descriptive and human readable.', {347expect_false((labels$y) == 'pct_vaccinated')348expect_false((labels$x) == 'yr')349expect_false((labels$colour) == 'who_region')350})351print("Success!")352}353354test_1.9.1 <- function(){355properties <- c(vertical_world$layers[[1]]$mapping, vertical_world$mapping)356labels <- vertical_world$labels357test_that('Did not create a plot named vertical_world', {358expect_true(exists("vertical_world"))359})360test_that('year should be on the x-axis.', {361expect_true("yr" == rlang::get_expr(properties$x))362})363test_that('pct_vaccinated should be on the y-axis.', {364expect_true("pct_vaccinated" == rlang::get_expr(properties$y))365})366test_that('vertical_world should be faceted by the vaccine column.', {367expect_true('FacetGrid' %in% class(rlang::get_expr(vertical_world$facet)))368})369test_that("vertical_world should be split vertically", {370expect_true("vaccine" %in% names(rlang::get_expr(vertical_world$facet$params$rows)))371})372if (length(vertical_world$layers) == 2) {373test_that('who_region should map to colour.', {374expect_true("who_region" == rlang::get_expr(c(vertical_world$layers[[1]]$mapping)$colour) &375"who_region" == rlang::get_expr(c(vertical_world$layers[[2]]$mapping)$colour) |376"who_region" == rlang::get_expr(c(vertical_world$layers[[2]]$mapping)$colour) &377"who_region" == rlang::get_expr(c(vertical_world$layers[[1]]$mapping)$colour))378} )} else {379test_that('who_region should map to colour.', {380expect_true("who_region" == rlang::get_expr(properties$colour))381})382}383384if (length(vertical_world$layers) == 2) { # if there are 2 layers then check if the two layers are points and lines385test_that('vertical_world should be a line plot.', {386expect_true("GeomPoint" %in% c(class(vertical_world$layers[[1]]$geom)) &387"GeomLine" %in% c(class(vertical_world$layers[[2]]$geom)) |388"GeomPoint" %in% c(class(vertical_world$layers[[2]]$geom)) &389"GeomLine" %in% c(class(vertical_world$layers[[1]]$geom)))390} )} else {391test_that('vertical_world should be a line plot.', {392expect_true("GeomLine" %in% c(class(vertical_world$layers[[1]]$geom)))393})394}395test_that('Labels on the axes and legend should be descriptive and human readable.', {396expect_false((labels$y) == 'pct_vaccinated')397expect_false((labels$x) == 'yr')398expect_false((labels$colour) == 'who_region')399})400print("Success!")401}402403test_1.9.2 <- function(){404test_that('Solution is incorrect', {405expect_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 test406})407print("Success!")408}409410test_2.1 <- function(){411test_that('Solution is incorrect', {412expect_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 test413})414print("Success!")415}416417test_2.2.1 <- function(){418test_that('Solution is incorrect', {419expect_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 test420})421print("Success!")422}423424test_2.2 <- function(){425test_that('Did not create an object named fast_food', {426expect_true(exists("fast_food"))427})428test_that('fast_food does not contain the correct number of rows and/or columns.', {429expect_equal(dim(fast_food), c(10000, 2))430})431test_that('Columns in fast_food contain incorrect values.', {432expect_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 test433expect_equal(digest(as.character(fast_food[[4,2]])), 'd599245d7d7e3f56863ba3a6112ca71b')434})435print("Success!")436}437438test_2.3 <- function(){439test_that('Did not create an object named top_restaurants', {440expect_true(exists("top_restaurants"))441})442test_that('top_restaurants does not contain the correct number of rows and/or columns.', {443expect_equal(dim(top_restaurants), c(9, 2))444})445test_that('Columns in fast_food contain incorrect values.', {446expect_equal(digest(int_round(sum(as.numeric(top_restaurants$n, na.rm = TRUE)), 2)), 'aa0419a07d43532e90a1f1c2d0c2b665')447})448print("Success!")449}450451test_2.4 <- function(){452properties <- c(count_bar_chart$layers[[1]]$mapping, count_bar_chart$mapping)453labels <- count_bar_chart$labels454test_that('Did not create a plot named count_bar_chart', {455expect_true(exists("count_bar_chart"))456})457test_that('name should be on the x-axis.', {458expect_true("name" == rlang::get_expr(properties$x))459})460test_that('n should be on the y-axis.', {461expect_true("n" == rlang::get_expr(properties$y))462})463test_that('vertical_world should be a bar plot.', {464expect_true("GeomBar" %in% c(class(count_bar_chart$layers[[1]]$geom)))465})466test_that('Labels on the axes and legend should be descriptive and human readable.', {467expect_false((labels$y) == 'n')468expect_false((labels$x) == 'name')469})470print("Success!")471}472473test_2.5_A <- function(){474properties <- c(count_bar_chart_A$layers[[1]]$mapping, count_bar_chart_A$mapping)475labels <- count_bar_chart_A$labels476test_that('Did not create a plot named count_bar_chart_A', {477expect_true(exists("count_bar_chart_A"))478})479test_that('name should be on the x-axis.', {480expect_true("name" == rlang::get_expr(properties$x))481})482test_that('x-axis (bar) labels should be at an angle between 20 and 90 degrees.', {483expect_true(count_bar_chart_A$theme$axis.text.x$angle <= 90 & count_bar_chart_A$theme$axis.text.x$angle >= 20)484})485test_that('hjust should equal 1', {486expect_equal(digest(int_round(count_bar_chart_A$theme$axis.text.x$hjust, 2)), '5d6e7fe43b3b73e5fd2961d5162486fa')487})488test_that('n should be on the y-axis.', {489expect_true("n" == rlang::get_expr(properties$y))490})491test_that('vertical_world should be a bar plot.', {492expect_true("GeomBar" %in% c(class(count_bar_chart_A$layers[[1]]$geom)))493})494test_that('Labels on the axes and legend should be descriptive and human readable.', {495expect_false((labels$y) == 'n')496expect_false((labels$x) == 'name')497})498print("Success!")499}500501test_2.5_B <- function(){502properties <- c(count_bar_chart_B$layers[[1]]$mapping, count_bar_chart_B$mapping)503labels <- count_bar_chart_B$labels504test_that('Did not create a plot named count_bar_chart_B', {505expect_true(exists("count_bar_chart_B"))506})507test_that('name should be on the x-axis.', {508expect_true("name" == rlang::get_expr(properties$x))509})510test_that('The coordinate axes should be flipped', {511expect_true("CoordFlip" %in% class(ggplot_build(count_bar_chart_B)$layout$coord))512})513test_that('n should be on the y-axis.', {514expect_true("n" == rlang::get_expr(properties$y))515})516test_that('vertical_world should be a bar plot.', {517expect_true("GeomBar" %in% c(class(count_bar_chart_B$layers[[1]]$geom)))518})519test_that('Labels on the axes and legend should be descriptive and human readable.', {520expect_false((labels$y) == 'n')521expect_false((labels$x) == 'name')522})523print("Success!")524}525526test_2.6 <- function(){527test_that('Solution is incorrect', {528expect_equal(digest(answer2.6), '948a9b527842ee791d4f18fb5594fbf7')529})530print("Success!")531}532533test_2.7 <- function(){534test_that('Did not create an object named state_counts', {535expect_true(exists("state_counts"))536})537test_that('The state column in state_counts should only contain CA, OR, and WA', {538expect_equal((state_counts$st), c("CA", "OR", "WA"))539})540test_that('state_counts does not contain the correct number of rows and/or columns.', {541expect_equal(dim(state_counts), c(3, 2))542})543test_that('Columns in state_counts contain incorrect values.', {544expect_equal(digest(int_round(sum(as.numeric(state_counts$n, na.rm = TRUE)), 2)), 'aa0419a07d43532e90a1f1c2d0c2b665')545})546print("Success!")547}548549test_2.8 <- function(){550properties <- c(state_counts_plot$layers[[1]]$mapping, state_counts_plot$mapping)551labels <- state_counts_plot$labels552test_that('Did not create a plot named state_counts_plot', {553expect_true(exists("state_counts_plot"))554})555test_that('state should be on the x-axis.', {556expect_true("st" == rlang::get_expr(properties$x))557})558test_that('n should be on the y-axis.', {559expect_true("n" == rlang::get_expr(properties$y))560})561test_that('state_counts_plot should be a bar plot.', {562expect_true("GeomBar" %in% c(class(state_counts_plot$layers[[1]]$geom)))563})564test_that('state_counts_plot should not be filled.', {565expect_false("PositionFill" %in% class(state_counts_plot$layers[[1]]$position))566})567test_that('Labels on the axes and legend should be descriptive and human readable.', {568expect_false((labels$y) == 'n')569expect_false((labels$x) == 'st')570})571print("Success!")572}573574test_2.9.0 <- function(){575test_that('Solution is incorrect', {576expect_equal(digest(answer2.9.0), '2bedd54d48692762c119b27f5ec7a320')577})578print("Success!")579}580581test_2.9.1 <- function(){582test_that('Solution is incorrect', {583expect_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 test584})585print("Success!")586}587588test_2.9.2 <- function(){589test_that('Did not create an object named top_n_state', {590expect_true(exists("top_n_state"))591})592test_that('The state column in top_n_state should only contain CA, OR, and WA', {593expect_equal(digest(top_n_state$st), '8e6ddcfd9ebaf85379a6d46f7949bce0')594})595test_that('top_n_state does not contain the correct number of rows and/or columns.', {596expect_equal(dim(top_n_state), c(27, 3))597})598test_that('Columns in top_n_state contain incorrect values.', {599expect_equal(digest(int_round(sum(top_n_state$n, na.rm = TRUE), 2)), 'aa0419a07d43532e90a1f1c2d0c2b665')600})601print("Success!")602}603604test_2.9.3 <- function(){605properties <- c(top_n_state_plot$layers[[1]]$mapping, top_n_state_plot$mapping)606labels <- top_n_state_plot$labels607test_that('Did not create a plot named top_n_state_plot', {608expect_true(exists("top_n_state_plot"))609})610test_that('state should be on the x-axis.', {611expect_true("st" == rlang::get_expr(properties$x))612})613test_that('n should be on the y-axis.', {614expect_true("n" == rlang::get_expr(properties$y))615})616test_that('name should be used to determine bar fill colour.', {617expect_true("name" == rlang::get_expr(properties$fill))618})619test_that('top_n_state_plot should be a bar plot.', {620expect_true("GeomBar" %in% c(class(top_n_state_plot$layers[[1]]$geom)))621})622test_that('top_n_state_plot position should be dodge.', {623expect_true("PositionDodge" %in% class(top_n_state_plot$layers[[1]]$position))624})625test_that('Labels on the axes and legend should be descriptive and human readable.', {626expect_false((labels$y) == 'n')627expect_false((labels$x) == 'st')628expect_false((labels$fill) == 'name')629})630print("Success!")631}632633test_2.9.4 <- function(){634properties <- c(top_n_state_plot$layers[[1]]$mapping, top_n_state_plot$mapping)635labels <- top_n_state_plot$labels636test_that('Did not create a plot named top_n_state_plot', {637expect_true(exists("top_n_state_plot"))638})639test_that('state should be on the x-axis.', {640expect_true("st" == rlang::get_expr(properties$x))641})642test_that('n should be on the y-axis.', {643expect_true("n" == rlang::get_expr(properties$y))644})645test_that('name should be used to determine bar fill colour.', {646expect_true("name" == rlang::get_expr(properties$fill))647})648test_that('top_n_state_plot should be a bar plot.', {649expect_true("GeomBar" %in% c(class(top_n_state_plot$layers[[1]]$geom)))650})651test_that('top_n_state_plot position should be fill', {652expect_true("PositionFill" %in% class(top_n_state_plot$layers[[1]]$position))653})654test_that('Labels on the axes and legend should be descriptive and human readable.', {655expect_false((labels$y) == 'n')656expect_false((labels$x) == 'st')657expect_false((labels$x) == 'name')658})659print("Success!")660}661662test_2.9.5 <- function(){663test_that('Solution is incorrect', {664expect_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 test665})666print("Success!")667}668669670