Path: blob/master/2020-fall/materials/tutorial_03/tests_tutorial_03.R
2051 views
library(testthat)1library(digest)23test_0.1 <- function(){4test_that('Solution is incorrect', {5expect_equal(digest(A), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test6expect_equal(digest(B), 'e5b57f323c7b3719bbaaf9f96b260d39') # we hid the answer to the test here so you can't see it, but we can still run the test7expect_equal(digest(C), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test8expect_equal(digest(D), 'dbc09cba9fe2583fb01d63c70e1555a8') # we hid the answer to the test here so you can't see it, but we can still run the test9expect_equal(digest(E), '0aee9b78301d7ec8998971363be87c03') # we hid the answer to the test here so you can't see it, but we can still run the test10expect_equal(digest(F), '5e338704a8e069ebd8b38ca71991cf94') # we hid the answer to the test here so you can't see it, but we can still run the test11})12print("Success!")13}1415test_1.1 <- function(){16test_that('Did not create an object named avocado', {17expect_true(exists("avocado"))18})19test_that('avocado should be a data frame.', {20expect_true('data.frame' %in% class(avocado))21})22test_that('avocado does not contain the correct number of rows and/or columns.', {23expect_equal(dim(avocado), c(17911, 9))24})25test_that('avocado does not contain the correct data.', {26expect_equal(digest(round(sum(avocado$average_price))), 'd478458402bf9895986c7e8a50ad4b61')27expect_equal(colnames(avocado), c("Date", "average_price", "small_hass_volume", "large_hass_volume", "extra_l_hass_volume", "type", "yr", "region", "wk"))28})29print("Success!")30}3132test_1.2 <- function(){33test_that('Did not create an object named cheapest', {34expect_true(exists("cheapest"))35})36test_that('cheapest should be a data frame.', {37expect_true('data.frame' %in% class(cheapest))38})39test_that('avocado does not contain the correct number of rows and/or columns.', {40expect_equal(dim(cheapest), c(1, 2))41})42test_that('cheapest does not contain the correct data.', {43expect_equal(digest(cheapest$region[0]), '5152ac13bdd09110d9ee9c169a3d9237') # we hid the answer to the test here so you can't see it, but we can still run the test44expect_equal(digest(as.numeric(unlist(select(cheapest, -region)))), '481ea83b5704f345de5f42f139ee11c7') # we hid the answer to the test here so you can't see it, but we can still run the test45})46print("Success!")47}4849test_1.3 <- function(){50test_that('Did not create a plot named avocado_plot', {51expect_true(exists("avocado_plot"))52})53properties <- c(avocado_plot$layers[[1]]$mapping, avocado_plot$mapping)54test_that('total_volume should be on the x-axis.', {55expect_true("total_volume" == rlang::get_expr(properties$x))56})57test_that('average_price should be on the y-axis.', {58expect_true("average_price" == rlang::get_expr(properties$y))59})60test_that('region should be Houston.', {61expect_true(unique(avocado_plot$data$region) == "Houston")62})63test_that('avocado_plot should be a scatter plot.', {64expect_true("GeomPoint" %in% c(class(avocado_plot$layers[[1]]$geom)))65})66test_that('Labels on the axes should be descriptive and human readable.', {67expect_false(avocado_plot$labels$y == 'average_price')68expect_false(avocado_plot$labels$x == 'total_volume')69})70print("Success!")71}7273test_3.1 <- function(){74test_that('Did not create an object named sea_surface', {75expect_true(exists("sea_surface"))76})77test_that('sea_surface should be a data frame.', {78expect_true('data.frame' %in% class(sea_surface))79})80test_that('sea_surface does not contain the correct number of rows and/or columns.', {81expect_equal(dim(sea_surface), c(105, 13))82})83test_that('sea_surface does not contain the correct data.', {84expect_equal(digest(round(sum(sea_surface$Dec, na.rm = TRUE))), 'b077728cf0193c3d5dbe79a925096b26')85expect_equal(colnames(sea_surface), c("Year", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))86})87print("Success!")88}8990