Path: blob/master/2022-spring/materials/tutorial_reading/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}31# -3233test_1.1 <- function() {34test_that("Solution is incorrect", {35expect_equal(digest(answer1.1), "475bf9280aab63a82af60791302736f6")36})37print("Success!")38}3940test_1.2 <- function() {41test_that("Solution is incorrect", {42expect_equal(digest(answer1.2), "c1f86f7430df7ddb256980ea6a3b57a4")43})44print("Success!")45}4647test_1.3.0 <- function() {48test_that("Did not create an object named happy_df_csv", {49expect_true(exists("happy_df_csv"))50})51test_that("happy_df_csv should be a data frame.", {52expect_true("data.frame" %in% class(happy_df_csv))53})54test_that("happy_df_csv does not contain the correct data.", {55expect_equal(dim(happy_df_csv), c(1562, 19))56expect_equal(digest(int_round(sum(happy_df_csv$year), 2)), 'b3c7645d3b175832c58747e47a9c7bff')57})58print("Success!")59}6061test_1.3.1 <- function() {62test_that("Did not create an object named happy_df", {63expect_true(exists("happy_df"))64})65test_that("happy_df should be a data frame.", {66expect_true("data.frame" %in% class(happy_df))67})68test_that("happy_df does not contain the correct data.", {69expect_equal(dim(happy_df), c(1562, 19))70expect_equal(digest(int_round(sum(happy_df$year), 2)), 'b3c7645d3b175832c58747e47a9c7bff')71})72print("Success!")73}7475test_1.3.2 <- function() {76test_that("Did not create an object named happy_step1", {77expect_true(exists("happy_step1"))78})79test_that("Did not create an object named happy_step2", {80expect_true(exists("happy_step2"))81})82test_that("Did not create an object named reduced_happy_df", {83expect_true(exists("reduced_happy_df"))84})85test_that("reduced_happy_df does not contain the correct number of rows and/or columns.", {86expect_setequal(dim(reduced_happy_df), c(141, 2))87})88test_that("The year column in reduced_happy_df should only contain 2017", {89expect_equal(int_round(unique(happy_step1$year), 0), 2017)90})91test_that("Columns in reduced_happy_df contain incorrect values.", {92expect_equal(digest(int_round(sum(reduced_happy_df$Positive.affect.scaled, na.rm = TRUE), 2)), '0a667e2dfc8faca592dde223c1ec3dec')93expect_equal(digest(int_round(sum(reduced_happy_df$Healthy.life.expectancy.at.birth, na.rm = TRUE), 2)), '90a8884408be62b74a0558ab7fb40db0')94})95print("Success!")96}9798test_1.4 <- function() {99test_that("Did not create a plot named happy_plot", {100expect_true(exists("happy_plot"))101})102properties <- c(happy_plot$layers[[1]]$mapping, happy_plot$mapping)103104test_that("Healthy.life.expectancy.at.birth should be on the x-axis.", {105expect_true("Healthy.life.expectancy.at.birth" == rlang::get_expr(properties$x))106})107test_that("Positive.affect.scaled should be on the y-axis.", {108expect_true("Positive.affect.scaled" == rlang::get_expr(properties$y))109})110test_that("happy_plot should be a scatter plot.", {111expect_true("GeomPoint" %in% c(class(happy_plot$layers[[1]]$geom)))112})113test_that("Labels on the axes should be descriptive and human readable.", {114expect_false(happy_plot$labels$y == "Positive.affect.scaled")115expect_false(happy_plot$labels$x == "Healthy.life.expectancy.at.birth")116})117print("Success!")118}119120test_2.1 <- function() {121test_that("Solution is incorrect", {122expect_equal(digest(as.character(answer2.1)), "475bf9280aab63a82af60791302736f6")123})124print("Success!")125}126127test_2.2.0 <- function() {128test_that("Did not create an object named whistler_2018", {129expect_true(exists("whistler_2018"))130})131test_that("whistler_2018 should be a data frame.", {132expect_true("data.frame" %in% class(whistler_2018))133})134test_that("whistler_2018 does not contain the correct number of rows and/or columns.", {135expect_equal(dim(whistler_2018), c(365, 27))136})137test_that("whistler_2018 does not contain the correct data.", {138expect_equal(digest(int_round(sum(whistler_2018$`Total Snow (cm)`, na.rm = TRUE), 2)), '2f5608a20784d5f19ac88237c04a1e10')139})140print("Success!")141}142143test_2.2.1 <- function() {144test_that("whistler_2018 does not contain the correct number of rows and/or columns.", {145expect_equal(dim(whistler_2018), c(365, 27))146})147test_that("whistler_2018 still contains white space in its column names.", {148expect_equal(int_round(length(grep(pattern = " ", x = colnames(whistler_2018))), 0), 0)149})150print("Success!")151}152153test_2.3 <- function() {154test_that("Did not create a plot named whistler_2018_plot", {155expect_true(exists("whistler_2018_plot"))156})157properties <- c(whistler_2018_plot$layers[[1]]$mapping, whistler_2018_plot$mapping)158159test_that("Date.Time should be on the x-axis.", {160expect_true("Date.Time" == rlang::get_expr(properties$x))161})162test_that("Total.Snow..cm. should be on the y-axis.", {163expect_true("Total.Snow..cm." == rlang::get_expr(properties$y))164})165test_that("whistler_2018_plot should be a line plot.", {166expect_true("GeomLine" %in% c(class(whistler_2018_plot$layers[[1]]$geom)))167})168test_that("Labels on the axes should be descriptive and human readable.", {169expect_false(whistler_2018_plot$labels$y == "Total.Snow..cm.")170expect_false(whistler_2018_plot$labels$x == "Date.Time")171})172print("Success!")173}174175test_3.0 <- function() {176test_that("Did not create a database connected called connection", {177expect_true(exists("connection"))178expect_equal(digest(class(connection)), "df66af2493a2405d1c7763f4bfbfebc0")179})180test_that("Did not create a table called project_data", {181expect_true(exists("project_data"))182expect_equal(digest(class(project_data))[[1]], "98f4e628b20def80372d0acca8b5edb0")183})184test_that("The project_data table has the wrong information in it", {185expect_equal(digest(colnames(project_data)[[13]]), "fd0b87dfaa67d4c90844080d5d9355ec")186expect_equal(digest(colnames(project_data)[[14]]), "2f2ec36ffb7792b1f8e274387e520edd")187expect_equal(digest(int_round(length(colnames(project_data)), 0)), "4d4c1ad2286f1a7670a024467dd10808")188expect_equal(digest(round(as_tibble(summarize(project_data, sum = sum(goal, na.rm = TRUE)))[[1]], 0)), '46e3118b0210dec665d9c8dacc68aacb') # cant apply int_round189})190print("Success!")191}192193test_3.1 <- function() {194test_that("Incorrect answer", {195expect_equal(digest(answer3.1), "3a5505c06543876fe45598b5e5e5195d")196})197print("Success!")198}199200201test_3.2 <- function() {202test_that("Did not create a plot named funding_over_time_plot", {203expect_true(exists("funding_over_time_plot"))204})205test_that("The axis labels are incorrect", {206expect_equal(digest(funding_over_time_plot$labels$x), "bd1055a7d6af03e74a4462edbc5b35c4")207expect_equal(digest(funding_over_time_plot$labels$y), "48c36ecb38a110dffe3f40add37ccdc1")208})209test_that("The x/y data are incorrect", {210expect_equal(digest(funding_over_time_plot$mapping$x), "c5a397ebe58129a88408811144869370")211expect_equal(digest(funding_over_time_plot$mapping$y), "5d75d853465e03f6828b25a1808b073b")212})213test_that("arrival_delay_plot should be a scatter plot", {214expect_equal(digest(class(funding_over_time_plot$layers[[1]]$geom)[[1]]), "911e5b9debfb523f25ad2ccc01a4b2dd")215})216print("Success!")217}218219220221test_3.4 <- function() {222test_that("Did not create project_data.csv in the data/ folder.", {223expect_true(file.exists("data/project_data.csv"))224})225test_that("the project_data.csv file does not contain the right data", { # read in data file as a string, check student's data = that string226expect_equal(digest(file.info("data/project_data.csv")$size), "5cb5f9f655e59768877bd995511acb2c")227})228test_that("the project_df object does not exist", {229expect_true(exists("project_df"))230})231test_that("the project_df object does not have the right number of rows & columns", {232expect_equal(digest(int_round(nrow(project_df), 0)), 'bef07e5e73d03523a0385aa9c06dadb4')233expect_equal(digest(int_round(ncol(project_df), 0)), '234a2a5581872457b9fe1187d1616b13')234})235test_that("the project_df object does not have the right data", {236expect_equal(digest(int_round(sum(project_df$goal), 0)), 'e5730c99d7f2abf710e5ebbdb4738bff')237})238print("Success!")239}240241242