Path: blob/master/2020-fall/materials/tutorial_01/tests_tutorial_01.R
2051 views
library(testthat)1library(digest)23test_revision <- function(){4test_that('Solution is incorrect', {5expect_equal(digest(A), 'dbc09cba9fe2583fb01d63c70e1555a8') # we hid the answer to the test here so you can't see it, but we can still run the test6expect_equal(digest(B), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test)7expect_equal(digest(C), '0aee9b78301d7ec8998971363be87c03') # we hid the answer to the test here so you can't see it, but we can still run the test8expect_equal(digest(D), '5e338704a8e069ebd8b38ca71991cf94') # we hid the answer to the test here so you can't see it, but we can still run the test9expect_equal(digest(E), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test10expect_equal(digest(F), 'e5b57f323c7b3719bbaaf9f96b260d39') # 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('Solution is incorrect', {17expect_equal(digest(answer1.1), '75f1160e72554f4270c809f041c7a776') # we hid the answer to the test here so you can't see it, but we can still run the test18})19print("Success!")20}2122test_1.2 <- function(){23test_that('Solution is incorrect', {24expect_equal(digest(answer1.2), 'd2a90307aac5ae8d0ef58e2fe730d38b') # we hid the answer to the test here so you can't see it, but we can still run the test25})26print("Success!")27}2829test_1.3 <- function(){30test_that('Solution is incorrect', {31expect_equal(digest(answer1.3), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test32})33print("Success!")34}3536test_1.4 <- function(){37test_that('The tidyverse package needs to be loaded.', {38expect_that("package:tidyverse" %in% search() , is_true())39})40print("Success!")41}4243test_1.5 <- function(){44test_that('Did not create an object named marathon_small.', {45expect_true(exists("marathon_small"))46})47test_that('read.csv() used instead of read_csv()', {48expect_true(class(marathon_small$sex) == "character")49})50test_that('marathon_small should be a data frame.', {51expect_true('data.frame' %in% class(marathon_small))52})53test_that('marathon_small does not contain the correct data.', {54expect_equal(dim(marathon_small), c(1833, 5))55expect_equal(digest(as.integer(sum(marathon_small$age))), 'd3eeb781803aac1dd87dd166bfddf422')56expect_equal(colnames(marathon_small), c("age", "bmi", "km5_time_seconds", "km10_time_seconds", "sex"))57})58print("Success!")59}6061test_1.6 <- function(){62test_that('Did not create an object named marathon_age.', {63expect_true(exists("marathon_age"))64})65test_that('Did not create an object named marathon_select.', {66expect_true(exists("marathon_select"))67})68test_that('marathon_age does not contain the correct number of rows and/or columns.', {69expect_equal(dim(marathon_age), c(922, 5))70})71test_that('marathon_select does not contain the correct number of rows and/or columns.', {72expect_equal(dim(marathon_select), c(922, 2))73})74test_that('Columns in marathon_select contain incorrect values.', {75expect_equal(digest(as.integer(sum(marathon_select$bmi))), '26b0ef5c0ff9f596d71bec8beb2fd961') # we hid the answer to the test here so you can't see it, but we can still run the test76expect_equal(digest(as.integer(sum(marathon_select$km5_time_seconds, na.rm = TRUE))), '34a5cc5cac5c7e94d58a8de9c1d25d2a') # we hid the answer to the test here so you can't see it, but we can still run the test77})78print("Success!")79}8081test_1.7 <- function(){82test_that('Did not create an object named marathon_mutate.', {83expect_true(exists("marathon_mutate"))84})85test_that('Did not create an object named marathon_exact.', {86expect_true(exists("marathon_exact"))87})88test_that('marathon_mutate does not contain the correct number of rows and/or columns.', {89expect_equal(dim(marathon_mutate), c(922, 3))90})91test_that('marathon_exact does not contain the correct number of rows and/or columns.', {92expect_equal(dim(marathon_exact), c(922, 2))93})94test_that('Columns in marathon_exact contain incorrect values.', {95expect_equal(digest(as.integer(sum(marathon_exact$bmi))), '26b0ef5c0ff9f596d71bec8beb2fd961') # we hid the answer to the test here so you can't see it, but we can still run the test96expect_equal(digest(as.integer(sum(marathon_exact$km5_time_minutes, na.rm = TRUE))), '2d07e1d5bee213ac567286a85e4e74af') # we hid the answer to the test here so you can't see it, but we can still run the test97})98print("Success!")99}100101test_1.8 <- function(){102test_that('Did not create a plot named marathon_plot', {103expect_true(exists("marathon_plot"))104})105106properties <- c(marathon_plot$layers[[1]]$mapping, marathon_plot$mapping)107test_that('bmi should be on the x-axis.', {108expect_true("bmi" == rlang::get_expr(properties$x))109})110test_that('km5_time_minutes should be on the y-axis.', {111expect_true("km5_time_minutes" == rlang::get_expr(properties$y))112})113test_that('marathon_plot should be a scatter plot.', {114expect_true("GeomPoint" %in% c(class(marathon_plot$layers[[1]]$geom)))115})116test_that('Labels on the axes should be descriptive and human readable.', {117expect_false(marathon_plot$labels$y == 'km5_time_minutes')118expect_false(marathon_plot$labels$x == 'bmi')119})120print("Success!")121}122123test_1.10 <- function(){124test_that('Did not create a plot named age_vs_time', {125expect_true(exists("age_vs_time"))126})127test_that('Did not create a data frame named marathon_small_mins', {128expect_true(exists("marathon_small_mins"))129})130131properties <- c(age_vs_time$layers[[1]]$mapping, age_vs_time$mapping)132test_that('age should be on the x-axis.', {133expect_true("age" == rlang::get_expr(properties$x))134})135test_that('km5_time_minutes should be on the y-axis.', {136expect_true("km5_time_minutes" == rlang::get_expr(properties$y))137})138test_that('age_vs_time should be a scatter plot.', {139expect_true("GeomPoint" %in% c(class(age_vs_time$layers[[1]]$geom)))140})141test_that('Labels on the axes should be descriptive and human readable.', {142expect_false(age_vs_time$labels$y == 'km5_time_minutes')143# expect_false(age_vs_time$labels$x == 'age') # removed since 'age' is human-readable144})145print("Success!")146}147148test_2.1 <- function(){149test_that('Solution is incorrect', {150expect_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 test151})152print("Success!")153}154155test_2.2 <- function(){156test_that('Solution is incorrect', {157expect_equal(digest(answer2.2), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test158})159print("Success!")160}161162test_2.3 <- function(){163test_that('Did not create an object named bike_data.', {164expect_true(exists("bike_data"))165})166test_that('bike_data should be a data frame.', {167expect_true('data.frame' %in% class(bike_data))168})169test_that('bike_data does not contain the correct information.', {170expect_equal(dim(bike_data), c(731, 4))171expect_equal(digest(as.integer(sum(bike_data$casual_users))), "037ce752369c998dc005b8f088a0cb35")172expect_equal(colnames(bike_data), c("temperature", "casual_users", "registered_users", "season"))173})174test_that('read.csv() used instead of read_csv()', {175expect_true(class(bike_data$season) == "character")176})177print("Success!")178}179180test_2.4 <- function(){181test_that('Did not create an object named bike_mutate.', {182expect_true(exists("bike_mutate"))183})184test_that('bike_mutate does not contain the correct number of rows and/or columns.', {185expect_equal(dim(bike_mutate), c(731, 5))186})187test_that('Columns in bike_mutate contain incorrect values.', {188expect_equal(digest(as.integer(sum(bike_mutate$total_users))), 'ca696c077151dc0a05b3e3862ab38f52') # we hid the answer to the test here so you can't see it, but we can still run the test189expect_equal(digest(as.integer(sum(bike_mutate$temperature, na.rm = TRUE))), '01a2384f7d878a7355399ff2517925cc') # we hid the answer to the test here so you can't see it, but we can still run the test190})191print("Success!")192}193194test_2.5 <- function(){195test_that('Did not create an object named bike_filter.', {196expect_true(exists("bike_filter"))197})198test_that('The season column in bike_filter should only contain Spring.', {199expect_equal(unique(bike_filter$season), "Spring")200})201test_that('bike_filter does not contain the correct number of rows and/or columns.', {202expect_equal(dim(bike_filter), c(181, 5))203})204test_that('Columns in bike_filter contain incorrect values.', {205expect_equal(digest(as.integer(sum(bike_filter$total_users))), '051a1e8b9293438bbc0cb8ed6fa4e959') # we hid the answer to the test here so you can't see it, but we can still run the test206expect_equal(digest(as.integer(sum(bike_filter$temperature, na.rm = TRUE))), '15865a3cff7594d62b7897f23fb27fa8') # we hid the answer to the test here so you can't see it, but we can still run the test207})208print("Success!")209}210211test_3.1 <- function(){212test_that('Solution is incorrect', {213expect_equal(digest(answer3.1), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test214})215print("Success!")216}217218