Path: blob/master/2020-fall/materials/tutorial_02/tests_tutorial_02.R
2051 views
library(testthat)1library(digest)23test_1.1 <- function() {4test_that("Solution is incorrect", {5expect_equal(digest(answer1.1), "475bf9280aab63a82af60791302736f6")6})7print("Success!")8}910test_1.2 <- function() {11test_that("Solution is incorrect", {12expect_equal(digest(answer1.2), "c1f86f7430df7ddb256980ea6a3b57a4")13})14print("Success!")15}1617test_1.3.0 <- function() {18test_that("Did not create an object named happy_df_csv", {19expect_true(exists("happy_df_csv"))20})21test_that("happy_df_csv should be a data frame.", {22expect_true("data.frame" %in% class(happy_df_csv))23})24test_that("happy_df_csv does not contain the correct data.", {25expect_equal(dim(happy_df_csv), c(1562, 19))26expect_equal(digest(as.numeric(sum(happy_df_csv$year))), "17201cb95c3eb622e848455bd480ef23")27})28print("Success!")29}3031test_1.3.1 <- function() {32test_that("Did not create an object named happy_df", {33expect_true(exists("happy_df"))34})35test_that("happy_df should be a data frame.", {36expect_true("data.frame" %in% class(happy_df))37})38test_that("happy_df does not contain the correct data.", {39expect_equal(dim(happy_df), c(1562, 19))40expect_equal(digest(as.numeric(sum(happy_df$year))), "17201cb95c3eb622e848455bd480ef23")41})42print("Success!")43}4445test_1.3.2 <- function() {46test_that("Did not create an object named happy_step1", {47expect_true(exists("happy_step1"))48})49test_that("Did not create an object named happy_step2", {50expect_true(exists("happy_step2"))51})52test_that("Did not create an object named reduced_happy_df", {53expect_true(exists("reduced_happy_df"))54})55test_that("reduced_happy_df does not contain the correct number of rows and/or columns.", {56expect_setequal(dim(reduced_happy_df), c(141, 2))57})58test_that("The year column in reduced_happy_df should only contain 2017", {59expect_equal(unique(happy_step1$year), 2017)60})61test_that("Columns in reduced_happy_df contain incorrect values.", {62expect_equal(digest(round(as.numeric(sum(reduced_happy_df$Positive.affect.scaled, na.rm = TRUE)))), "1c760b1821e90b04ede5ecc5e6d3a0fc")63expect_equal(digest(round(as.numeric(sum(reduced_happy_df$Healthy.life.expectancy.at.birth, na.rm = TRUE)))), "a2ef97bc5692bdc875485fa1683a243b")64})65print("Success!")66}6768test_1.4 <- function() {69test_that("Did not create a plot named happy_plot", {70expect_true(exists("happy_plot"))71})72properties <- c(happy_plot$layers[[1]]$mapping, happy_plot$mapping)7374test_that("Healthy.life.expectancy.at.birth should be on the x-axis.", {75expect_true("Healthy.life.expectancy.at.birth" == rlang::get_expr(properties$x))76})77test_that("Positive.affect.scaled should be on the y-axis.", {78expect_true("Positive.affect.scaled" == rlang::get_expr(properties$y))79})80test_that("happy_plot should be a scatter plot.", {81expect_true("GeomPoint" %in% c(class(happy_plot$layers[[1]]$geom)))82})83test_that("Labels on the axes should be descriptive and human readable.", {84expect_false(happy_plot$labels$y == "Positive.affect.scaled")85expect_false(happy_plot$labels$x == "Healthy.life.expectancy.at.birth")86})87print("Success!")88}8990test_2.1 <- function() {91test_that("Solution is incorrect", {92expect_equal(digest(as.character(answer2.1)), "475bf9280aab63a82af60791302736f6")93})94print("Success!")95}9697test_2.2.0 <- function() {98test_that("Did not create an object named whistler_2018", {99expect_true(exists("whistler_2018"))100})101test_that("whistler_2018 should be a data frame.", {102expect_true("data.frame" %in% class(whistler_2018))103})104test_that("whistler_2018 does not contain the correct number of rows and/or columns.", {105expect_equal(dim(whistler_2018), c(365, 27))106})107test_that("whistler_2018 does not contain the correct data.", {108expect_equal(digest(round(as.numeric(sum(whistler_2018$`Total Snow (cm)`, na.rm = TRUE)))), "58b219918cd2f36aeaf9b9b3dc071c76")109})110print("Success!")111}112113test_2.2.1 <- function() {114test_that("whistler_2018 does not contain the correct number of rows and/or columns.", {115expect_equal(dim(whistler_2018), c(365, 27))116})117test_that("whistler_2018 still contains white space in its column names.", {118expect_equal(length(grep(pattern = " ", x = colnames(whistler_2018))), 0)119})120print("Success!")121}122123test_2.3 <- function() {124test_that("Did not create a plot named whistler_2018_plot", {125expect_true(exists("whistler_2018_plot"))126})127properties <- c(whistler_2018_plot$layers[[1]]$mapping, whistler_2018_plot$mapping)128129test_that("Date.Time should be on the x-axis.", {130expect_true("Date.Time" == rlang::get_expr(properties$x))131})132test_that("Total.Snow..cm. should be on the y-axis.", {133expect_true("Total.Snow..cm." == rlang::get_expr(properties$y))134})135test_that("whistler_2018_plot should be a line plot.", {136expect_true("GeomLine" %in% c(class(whistler_2018_plot$layers[[1]]$geom)))137})138test_that("Labels on the axes should be descriptive and human readable.", {139expect_false(whistler_2018_plot$labels$y == "Total.Snow..cm.")140expect_false(whistler_2018_plot$labels$x == "Date.Time")141})142print("Success!")143}144145test_3.0 <- function() {146test_that("Did not create a database connected called connection", {147expect_true(exists("connection"))148expect_equal(digest(class(connection)), "df66af2493a2405d1c7763f4bfbfebc0")149})150test_that("Did not create a table called project_data", {151expect_true(exists("project_data"))152expect_equal(digest(class(project_data))[[1]], "98f4e628b20def80372d0acca8b5edb0")153})154test_that("The project_data table has the wrong information in it", {155expect_equal(digest(colnames(project_data)[[13]]), "fd0b87dfaa67d4c90844080d5d9355ec")156expect_equal(digest(colnames(project_data)[[14]]), "2f2ec36ffb7792b1f8e274387e520edd")157expect_equal(digest(length(colnames(project_data))), "4d4c1ad2286f1a7670a024467dd10808")158expect_equal(digest(as_tibble(summarize(project_data, sum = sum(goal, na.rm = TRUE)))[[1]]), "f8d1fabeacced2ef4c82748ca212b724")159})160print("Success!")161}162163test_3.1 <- function() {164test_that("Incorrect answer", {165expect_equal(digest(answer3.1), "3a5505c06543876fe45598b5e5e5195d")166})167print("Success!")168}169170171test_3.2 <- function() {172test_that("Did not create a plot named funding_over_time_plot", {173expect_true(exists("funding_over_time_plot"))174})175test_that("The axis labels are incorrect", {176expect_equal(digest(funding_over_time_plot$labels$x), "bd1055a7d6af03e74a4462edbc5b35c4")177expect_equal(digest(funding_over_time_plot$labels$y), "48c36ecb38a110dffe3f40add37ccdc1")178})179test_that("The x/y data are incorrect", {180expect_equal(digest(funding_over_time_plot$mapping$x), "c5a397ebe58129a88408811144869370")181expect_equal(digest(funding_over_time_plot$mapping$y), "5d75d853465e03f6828b25a1808b073b")182})183test_that("arrival_delay_plot should be a scatter plot", {184expect_equal(digest(class(funding_over_time_plot$layers[[1]]$geom)[[1]]), "911e5b9debfb523f25ad2ccc01a4b2dd")185})186print("Success!")187}188189190191test_3.4 <- function() {192test_that("Did not create project_data.csv in the data/ folder.", {193expect_true(file.exists("data/project_data.csv"))194})195test_that("the project_data.csv file does not contain the right data", { # read in data file as a string, check student's data = that string196expect_equal(digest(file.info("data/project_data.csv")$size), "8e5009fb2a1bed6bf7ae2a76deef7fea")197})198test_that("the project_df object does not exist", {199expect_true(exists("project_df"))200})201test_that("the project_df object does not have the right number of rows & columns", {202expect_equal(digest(nrow(project_df)), "40202635f105eddd66654976af5d9e1f")203expect_equal(digest(ncol(project_df)), "11946e7a3ed5e1776e81c0f0ecd383d0")204})205test_that("the project_df object does not have the right data", {206expect_equal(digest(sum(project_df)), "daca6d59f60da17a8beacb0b8caa51b9")207})208print("Success!")209}210211212