Path: blob/master/2021-spring/materials/tutorial_02/tests_tutorial_02.R
2051 views
# +1library(testthat)2library(digest)34#' Round double to precise integer5#'6#' `int_round` works to create an integer corresponding to a number that is7#' tested up to a particular decimal point of precision. This is useful when8#' there is a need to compare a numeric value using hashes.9#'10#' @param x Double vector of length one.11#' @param digits Double vector of length one to specify decimal point of precision. Negative numbers can be used to specifying significant digits > 0.1.12#'13#' @return Integer vector of length one corresponding to a particular decimal point of precision.14#'15#' @examples16#' # to get an integer up to two decimals of precision from 234.5678917#' int_round(234.56789, 2)18#'19#' to get an integer rounded to the hundred digit from 234.5678920#' int_round(234.56789, -2)21int_round <- function(x, digits){22x = x*10^digits23xint = as.integer(x)24xint1 = xint + 1L25if (abs(xint - x) < abs(xint1 - x)){26return(xint)27}28else {29return(xint1)30}31}32# -3334test_1.1 <- function() {35test_that("Solution is incorrect", {36expect_equal(digest(answer1.1), "475bf9280aab63a82af60791302736f6")37})38print("Success!")39}4041test_1.2 <- function() {42test_that("Solution is incorrect", {43expect_equal(digest(answer1.2), "c1f86f7430df7ddb256980ea6a3b57a4")44})45print("Success!")46}4748test_1.3.0 <- function() {49test_that("Did not create an object named happy_df_csv", {50expect_true(exists("happy_df_csv"))51})52test_that("happy_df_csv should be a data frame.", {53expect_true("data.frame" %in% class(happy_df_csv))54})55test_that("happy_df_csv does not contain the correct data.", {56expect_equal(dim(happy_df_csv), c(1562, 19))57expect_equal(digest(int_round(sum(happy_df_csv$year), 2)), 'b3c7645d3b175832c58747e47a9c7bff')58})59print("Success!")60}6162test_1.3.1 <- function() {63test_that("Did not create an object named happy_df", {64expect_true(exists("happy_df"))65})66test_that("happy_df should be a data frame.", {67expect_true("data.frame" %in% class(happy_df))68})69test_that("happy_df does not contain the correct data.", {70expect_equal(dim(happy_df), c(1562, 19))71expect_equal(digest(int_round(sum(happy_df$year), 2)), 'b3c7645d3b175832c58747e47a9c7bff')72})73print("Success!")74}7576test_1.3.2 <- function() {77test_that("Did not create an object named happy_step1", {78expect_true(exists("happy_step1"))79})80test_that("Did not create an object named happy_step2", {81expect_true(exists("happy_step2"))82})83test_that("Did not create an object named reduced_happy_df", {84expect_true(exists("reduced_happy_df"))85})86test_that("reduced_happy_df does not contain the correct number of rows and/or columns.", {87expect_setequal(dim(reduced_happy_df), c(141, 2))88})89test_that("The year column in reduced_happy_df should only contain 2017", {90expect_equal(int_round(unique(happy_step1$year), 0), 2017)91})92test_that("Columns in reduced_happy_df contain incorrect values.", {93expect_equal(digest(int_round(sum(reduced_happy_df$Positive.affect.scaled, na.rm = TRUE), 2)), '0a667e2dfc8faca592dde223c1ec3dec')94expect_equal(digest(int_round(sum(reduced_happy_df$Healthy.life.expectancy.at.birth, na.rm = TRUE), 2)), '90a8884408be62b74a0558ab7fb40db0')95})96print("Success!")97}9899test_1.4 <- function() {100test_that("Did not create a plot named happy_plot", {101expect_true(exists("happy_plot"))102})103properties <- c(happy_plot$layers[[1]]$mapping, happy_plot$mapping)104105test_that("Healthy.life.expectancy.at.birth should be on the x-axis.", {106expect_true("Healthy.life.expectancy.at.birth" == rlang::get_expr(properties$x))107})108test_that("Positive.affect.scaled should be on the y-axis.", {109expect_true("Positive.affect.scaled" == rlang::get_expr(properties$y))110})111test_that("happy_plot should be a scatter plot.", {112expect_true("GeomPoint" %in% c(class(happy_plot$layers[[1]]$geom)))113})114test_that("Labels on the axes should be descriptive and human readable.", {115expect_false(happy_plot$labels$y == "Positive.affect.scaled")116expect_false(happy_plot$labels$x == "Healthy.life.expectancy.at.birth")117})118print("Success!")119}120121test_2.1 <- function() {122test_that("Solution is incorrect", {123expect_equal(digest(as.character(answer2.1)), "475bf9280aab63a82af60791302736f6")124})125print("Success!")126}127128test_2.2.0 <- function() {129test_that("Did not create an object named whistler_2018", {130expect_true(exists("whistler_2018"))131})132test_that("whistler_2018 should be a data frame.", {133expect_true("data.frame" %in% class(whistler_2018))134})135test_that("whistler_2018 does not contain the correct number of rows and/or columns.", {136expect_equal(dim(whistler_2018), c(365, 27))137})138test_that("whistler_2018 does not contain the correct data.", {139expect_equal(digest(int_round(sum(whistler_2018$`Total Snow (cm)`, na.rm = TRUE), 2)), '2f5608a20784d5f19ac88237c04a1e10')140})141print("Success!")142}143144test_2.2.1 <- function() {145test_that("whistler_2018 does not contain the correct number of rows and/or columns.", {146expect_equal(dim(whistler_2018), c(365, 27))147})148test_that("whistler_2018 still contains white space in its column names.", {149expect_equal(int_round(length(grep(pattern = " ", x = colnames(whistler_2018))), 0), 0)150})151print("Success!")152}153154test_2.3 <- function() {155test_that("Did not create a plot named whistler_2018_plot", {156expect_true(exists("whistler_2018_plot"))157})158properties <- c(whistler_2018_plot$layers[[1]]$mapping, whistler_2018_plot$mapping)159160test_that("Date.Time should be on the x-axis.", {161expect_true("Date.Time" == rlang::get_expr(properties$x))162})163test_that("Total.Snow..cm. should be on the y-axis.", {164expect_true("Total.Snow..cm." == rlang::get_expr(properties$y))165})166test_that("whistler_2018_plot should be a line plot.", {167expect_true("GeomLine" %in% c(class(whistler_2018_plot$layers[[1]]$geom)))168})169test_that("Labels on the axes should be descriptive and human readable.", {170expect_false(whistler_2018_plot$labels$y == "Total.Snow..cm.")171expect_false(whistler_2018_plot$labels$x == "Date.Time")172})173print("Success!")174}175176test_3.0 <- function() {177test_that("Did not create a database connected called connection", {178expect_true(exists("connection"))179expect_equal(digest(class(connection)), "df66af2493a2405d1c7763f4bfbfebc0")180})181test_that("Did not create a table called project_data", {182expect_true(exists("project_data"))183expect_equal(digest(class(project_data))[[1]], "98f4e628b20def80372d0acca8b5edb0")184})185test_that("The project_data table has the wrong information in it", {186expect_equal(digest(colnames(project_data)[[13]]), "fd0b87dfaa67d4c90844080d5d9355ec")187expect_equal(digest(colnames(project_data)[[14]]), "2f2ec36ffb7792b1f8e274387e520edd")188expect_equal(digest(int_round(length(colnames(project_data)), 0)), "4d4c1ad2286f1a7670a024467dd10808")189expect_equal(digest(round(as_tibble(summarize(project_data, sum = sum(goal, na.rm = TRUE)))[[1]], 0)), '46e3118b0210dec665d9c8dacc68aacb') # cant apply int_round190})191print("Success!")192}193194test_3.1 <- function() {195test_that("Incorrect answer", {196expect_equal(digest(answer3.1), "3a5505c06543876fe45598b5e5e5195d")197})198print("Success!")199}200201202test_3.2 <- function() {203test_that("Did not create a plot named funding_over_time_plot", {204expect_true(exists("funding_over_time_plot"))205})206test_that("The axis labels are incorrect", {207expect_equal(digest(funding_over_time_plot$labels$x), "bd1055a7d6af03e74a4462edbc5b35c4")208expect_equal(digest(funding_over_time_plot$labels$y), "48c36ecb38a110dffe3f40add37ccdc1")209})210test_that("The x/y data are incorrect", {211expect_equal(digest(funding_over_time_plot$mapping$x), "c5a397ebe58129a88408811144869370")212expect_equal(digest(funding_over_time_plot$mapping$y), "5d75d853465e03f6828b25a1808b073b")213})214test_that("arrival_delay_plot should be a scatter plot", {215expect_equal(digest(class(funding_over_time_plot$layers[[1]]$geom)[[1]]), "911e5b9debfb523f25ad2ccc01a4b2dd")216})217print("Success!")218}219220221222test_3.4 <- function() {223test_that("Did not create project_data.csv in the data/ folder.", {224expect_true(file.exists("data/project_data.csv"))225})226test_that("the project_data.csv file does not contain the right data", { # read in data file as a string, check student's data = that string227expect_equal(digest(file.info("data/project_data.csv")$size), "5cb5f9f655e59768877bd995511acb2c")228})229test_that("the project_df object does not exist", {230expect_true(exists("project_df"))231})232test_that("the project_df object does not have the right number of rows & columns", {233expect_equal(digest(int_round(nrow(project_df), 0)), 'bef07e5e73d03523a0385aa9c06dadb4')234expect_equal(digest(int_round(ncol(project_df), 0)), '234a2a5581872457b9fe1187d1616b13')235})236test_that("the project_df object does not have the right data", {237expect_equal(digest(int_round(sum(project_df$goal), 0)), 'e5730c99d7f2abf710e5ebbdb4738bff')238})239print("Success!")240}241242243