Path: blob/master/2019-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), '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(answer2), '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(answer3), '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('marathon_small should be a data frame.', {48expect_true('data.frame' %in% class(marathon_small))49})50test_that('marathon_small does not contain the correct data.', {51expect_equal(dim(marathon_small), c(1833, 5))52expect_equal(sum(marathon_small$age), 66455.5)53expect_equal(colnames(marathon_small), c("age", "bmi", "km5_time_seconds", "km10_time_seconds", "sex"))54})55print("Success!")56}5758test_1.6 <- function(){59test_that('Did not create an object named marathon_age.', {60expect_true(exists("marathon_age"))61})62test_that('Did not create an object named marathon_select.', {63expect_true(exists("marathon_select"))64})65test_that('marathon_age does not contain the correct number of rows and/or columns.', {66expect_equal(dim(marathon_age), c(922, 5))67})68test_that('marathon_select does not contain the correct number of rows and/or columns.', {69expect_equal(dim(marathon_select), c(922, 2))70})71test_that('Columns in marathon_select contain incorrect values.', {72expect_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 test73expect_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 test74})75print("Success!")76}7778test_1.7 <- function(){79test_that('Did not create an object named marathon_mutate.', {80expect_true(exists("marathon_mutate"))81})82test_that('Did not create an object named marathon_exact.', {83expect_true(exists("marathon_exact"))84})85test_that('marathon_mutate does not contain the correct number of rows and/or columns.', {86expect_equal(dim(marathon_mutate), c(922, 3))87})88test_that('marathon_exact does not contain the correct number of rows and/or columns.', {89expect_equal(dim(marathon_exact), c(922, 2))90})91test_that('Columns in marathon_exact contain incorrect values.', {92expect_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 test93expect_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 test94})95print("Success!")96}9798test_1.8 <- function(){99test_that('Did not create a plot named marathon_plot', {100expect_true(exists("marathon_plot"))101})102test_that('bmi should be on the x-axis.', {103expect_that("bmi" %in% c(rlang::get_expr(marathon_plot$mapping$x),rlang::get_expr(marathon_plot$layers[[1]]$mapping$x)), is_true())104})105test_that('km5_time_minutes should be on the y-axis.', {106expect_that("km5_time_minutes" %in% c(rlang::get_expr(marathon_plot$mapping$y), rlang::get_expr(marathon_plot$layers[[1]]$mapping$y)) , is_true())107})108test_that('marathon_plot should be a scatter plot.', {109expect_that("GeomPoint" %in% c(class(marathon_plot$layers[[1]]$geom)) , is_true())110})111test_that('Labels on the axes should be descriptive and human readable.', {112expect_that((marathon_plot$labels$y) == 'km5_time_minutes', is_false())113expect_that((marathon_plot$labels$x) == 'bmi', is_false())114})115print("Success!")116}117118test_1.10 <- function(){119test_that('Did not create a plot named age_vs_time', {120expect_true(exists("age_vs_time"))121})122test_that('age should be on the x-axis.', {123expect_that("age" %in% c(rlang::get_expr(age_vs_time$mapping$x),rlang::get_expr(age_vs_time$layers[[1]]$mapping$x)), is_true())124})125test_that('km5_time_minutes should be on the y-axis.', {126expect_that("km5_time_minutes" %in% c(rlang::get_expr(age_vs_time$mapping$y), rlang::get_expr(age_vs_time$layers[[1]]$mapping$y)) , is_true())127})128test_that('age_vs_time should be a scatter plot.', {129expect_that("GeomPoint" %in% c(class(age_vs_time$layers[[1]]$geom)) , is_true())130})131test_that('Labels on the axes should be descriptive and human readable.', {132expect_that((age_vs_time$labels$y) == 'km5_time_minutes', is_false())133expect_that((age_vs_time$labels$x) == 'age', is_false())134})135print("Success!")136}137138test_2.1 <- function(){139test_that('Solution is incorrect', {140expect_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 test141})142print("Success!")143}144145test_2.2 <- function(){146test_that('Solution is incorrect', {147expect_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 test148})149print("Success!")150}151152test_2.3 <- function(){153test_that('Did not create an object named bike_data.', {154expect_true(exists("bike_data"))155})156test_that('bike_data should be a data frame.', {157expect_true('data.frame' %in% class(bike_data))158})159test_that('bike_data does not contain the correct information.', {160expect_equal(dim(bike_data), c(731, 4))161expect_equal(sum(bike_data$casual_users), 620017)162expect_equal(colnames(bike_data), c("temperature", "casual_users", "registered_users", "season"))163})164print("Success!")165}166167test_2.4 <- function(){168test_that('Did not create an object named bike_mutate.', {169expect_true(exists("bike_mutate"))170})171test_that('bike_mutate does not contain the correct number of rows and/or columns.', {172expect_equal(dim(bike_mutate), c(731, 5))173})174test_that('Columns in bike_mutate contain incorrect values.', {175expect_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 test176expect_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 test177})178print("Success!")179}180181test_2.5 <- function(){182test_that('Did not create an object named bike_filter.', {183expect_true(exists("bike_filter"))184})185test_that('The season column in bike_filter should only contain Spring.', {186expect_equal(unique(bike_filter$season), "Spring")187})188test_that('bike_filter does not contain the correct number of rows and/or columns.', {189expect_equal(dim(bike_filter), c(181, 5))190})191test_that('Columns in bike_filter contain incorrect values.', {192expect_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 test193expect_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 test194})195print("Success!")196}197198test_3.1 <- function(){199test_that('Solution is incorrect', {200expect_equal(digest(answer_filter), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test201})202print("Success!")203}204205