Path: blob/master/2020-spring/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(sum(avocado$average_price)), 'b987e782cc203bbe06ef8fde8da42991')27expect_equal(colnames(avocado), c("Date", "average_price", "small_hass_volume", "large_hass_volume", "extra_l_hass_volume", "type", "year", "region", "week"))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})53test_that('total_volume should be on the x-axis.', {54expect_that("total_volume" %in% c(rlang::get_expr(avocado_plot$mapping$x),rlang::get_expr(avocado_plot$layers[[1]]$mapping$x)), is_true())55})56test_that('average_price should be on the y-axis.', {57expect_that("average_price" %in% c(rlang::get_expr(avocado_plot$mapping$y), rlang::get_expr(avocado_plot$layers[[1]]$mapping$y)) , is_true())58})59test_that('avocado_plot should be a scatter plot.', {60expect_that("GeomPoint" %in% c(class(avocado_plot$layers[[1]]$geom)) , is_true())61})62test_that('Labels on the axes should be descriptive and human readable.', {63expect_that((avocado_plot$labels$y) == 'average_price', is_false())64expect_that((avocado_plot$labels$x) == 'total_volume', is_false())65})66print("Success!")67}6869test_3.1 <- function(){70test_that('Did not create an object named sea_surface', {71expect_true(exists("sea_surface"))72})73test_that('sea_surface should be a data frame.', {74expect_true('data.frame' %in% class(sea_surface))75})76test_that('sea_surface does not contain the correct number of rows and/or columns.', {77expect_equal(dim(sea_surface), c(105, 13))78})79test_that('sea_surface does not contain the correct data.', {80expect_equal(digest(sum(sea_surface$Dec)), '9c9393e1464352cd4fbea94dfadfa02a')81expect_equal(colnames(sea_surface), c("Year", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))82})83print("Success!")84}8586