Path: blob/master/2022-spring/materials/tutorial_wrangling/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(A), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test35expect_equal(digest(B), 'e5b57f323c7b3719bbaaf9f96b260d39') # we hid the answer to the test here so you can't see it, but we can still run the test36expect_equal(digest(C), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test37expect_equal(digest(D), 'dbc09cba9fe2583fb01d63c70e1555a8') # we hid the answer to the test here so you can't see it, but we can still run the test38expect_equal(digest(E), '0aee9b78301d7ec8998971363be87c03') # we hid the answer to the test here so you can't see it, but we can still run the test39expect_equal(digest(F), '5e338704a8e069ebd8b38ca71991cf94') # we hid the answer to the test here so you can't see it, but we can still run the test40})41print("Success!")42}4344test_1.1 <- function(){45test_that('Did not create an object named avocado', {46expect_true(exists("avocado"))47})48test_that('avocado should be a data frame.', {49expect_true('data.frame' %in% class(avocado))50})51test_that('avocado does not contain the correct number of rows and/or columns.', {52expect_equal(dim(avocado), c(17911, 9))53})54test_that('avocado does not contain the correct data.', {55expect_equal(digest(int_round(sum(avocado$average_price), 2)), '925e36908ff4830300330ada3666458c')56expect_equal(colnames(avocado), c("Date", "average_price", "small_hass_volume", "large_hass_volume", "extra_l_hass_volume", "type", "yr", "region", "wk"))57})58print("Success!")59}6061test_1.2 <- function(){62test_that('Did not create an object named cheapest', {63expect_true(exists("cheapest"))64})65test_that('cheapest should be a data frame.', {66expect_true('data.frame' %in% class(cheapest))67})68test_that('avocado does not contain the correct number of rows and/or columns.', {69expect_equal(dim(cheapest), c(1, 2))70})71test_that('cheapest does not contain the correct data.', {72expect_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 test73expect_equal(digest(int_round(unlist(select(cheapest, -region)), 2)), 'bd15e8575c605a44f323d2c67cbfd7f1') # 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.3 <- function(){79test_that('Did not create a plot named avocado_plot', {80expect_true(exists("avocado_plot"))81})82properties <- c(avocado_plot$layers[[1]]$mapping, avocado_plot$mapping)83test_that('total_volume should be on the x-axis.', {84expect_true("total_volume" == rlang::get_expr(properties$x))85})86test_that('average_price should be on the y-axis.', {87expect_true("average_price" == rlang::get_expr(properties$y))88})89test_that('region should be Houston.', {90expect_true(unique(avocado_plot$data$region) == "Houston")91})92test_that('avocado_plot should be a scatter plot.', {93expect_true("GeomPoint" %in% c(class(avocado_plot$layers[[1]]$geom)))94})95test_that('Labels on the axes should be descriptive and human readable.', {96expect_false(avocado_plot$labels$y == 'average_price')97expect_false(avocado_plot$labels$x == 'total_volume')98})99print("Success!")100}101102test_3.1 <- function(){103test_that('Did not create an object named sea_surface', {104expect_true(exists("sea_surface"))105})106test_that('sea_surface should be a data frame.', {107expect_true('data.frame' %in% class(sea_surface))108})109test_that('sea_surface does not contain the correct number of rows and/or columns.', {110expect_equal(dim(sea_surface), c(105, 13))111})112test_that('sea_surface does not contain the correct data.', {113expect_equal(digest(int_round(sum(sea_surface$Dec, na.rm = TRUE), 2)), '15045e9db8607a868e0cc475a3f7b9b8')114expect_equal(colnames(sea_surface), c("Year", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))115})116print("Success!")117}118119120