Path: blob/master/2021-summer/materials/worksheet_02/tests_worksheet_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), 'c1f86f7430df7ddb256980ea6a3b57a4') # we hid the answer to the test here so you can't see it, but we can still run the test37})38print("Success!")39}4041test_1.2 <- function(){42test_that('Solution is incorrect', {43expect_equal(digest(answer1.2), '05ca18b596514af73f6880309a21b5dd') # we hid the answer to the test here so you can't see it, but we can still run the test44})45print("Success!")46}4748test_1.3 <- function(){49test_that('Solution is incorrect', {50expect_equal(digest(A), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test51expect_equal(digest(B), 'e5b57f323c7b3719bbaaf9f96b260d39') # we hid the answer to the test here so you can't see it, but we can still run the test52expect_equal(digest(C), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test53expect_equal(digest(D), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test54expect_equal(digest(E), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test55expect_equal(digest(F), 'e5b57f323c7b3719bbaaf9f96b260d39') # we hid the answer to the test here so you can't see it, but we can still run the test56})57print("Success!")58}5960test_1.4 <- function(){61test_that('Solution is incorrect', {62expect_equal(digest(answer1.4), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test63})64print("Success!")65}6667test_1.5 <- function(){68test_that('Solution is incorrect', {69expect_equal(digest(A), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test70expect_equal(digest(B), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test71expect_equal(digest(C), '5e338704a8e069ebd8b38ca71991cf94') # we hid the answer to the test here so you can't see it, but we can still run the test72expect_equal(digest(D), 'dbc09cba9fe2583fb01d63c70e1555a8') # we hid the answer to the test here so you can't see it, but we can still run the test73expect_equal(digest(E), 'e5b57f323c7b3719bbaaf9f96b260d39') # 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_2.1 <- function(){79test_that('Solution is incorrect', {80expect_equal(digest(G), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test81expect_equal(digest(H), 'e5b57f323c7b3719bbaaf9f96b260d39') # we hid the answer to the test here so you can't see it, but we can still run the test82expect_equal(digest(I), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test83expect_equal(digest(J), 'dbc09cba9fe2583fb01d63c70e1555a8') # we hid the answer to the test here so you can't see it, but we can still run the test84})85print("Success!")86}8788test_2.2 <- function(){89test_that('Solution is incorrect', {90expect_equal(digest(answer2.2), '05ca18b596514af73f6880309a21b5dd') # we hid the answer to the test here so you can't see it, but we can still run the test91})92print("Success!")93}9495test_2.3 <- function(){96test_that('Solution is incorrect', {97expect_that(exists('answer2.3'), is_true())98expect_equal(digest(answer2.3), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test99})100print("Success!")101}102103test_3.1 <- function(){104test_that('Solution is incorrect', {105expect_equal(digest(answer3.1), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test106})107print("Success!")108}109110test_3.2 <- function(){111test_that('Solution is incorrect', {112expect_equal(digest(answer3.2), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test113})114print("Success!")115}116117test_3.3 <- function(){118test_that('Solution is incorrect', {119expect_equal(digest(A), '0590b0427c1b19a6eb612d19888aa52f') # we hid the answer to the test here so you can't see it, but we can still run the test120expect_equal(digest(B), 'e6d21e822242a5c1c9f58806024e77ba') # we hid the answer to the test here so you can't see it, but we can still run the test121expect_equal(digest(C), 'f2375c071669f9b6399f28e06a598e57') # we hid the answer to the test here so you can't see it, but we can still run the test122expect_equal(digest(D), '0590b0427c1b19a6eb612d19888aa52f') # we hid the answer to the test here so you can't see it, but we can still run the test123expect_equal(digest(E), '863dfc36ab2bfe97404cc8fc074a5241') # we hid the answer to the test here so you can't see it, but we can still run the test124expect_equal(digest(F), '3697e3a900218295975a04d2205e3518') # we hid the answer to the test here so you can't see it, but we can still run the test125})126print("Success!")127}128129130test_3.4 <- function(){131test_that('Did not create an object named happiness_report_path', {132expect_true(exists("happiness_report_path"))133})134test_that('Did not create an object named happiness_report', {135expect_true(exists("happiness_report"))136})137test_that('happiness_report should be a data frame.', {138expect_true('data.frame' %in% class(happiness_report))139})140test_that('Did not use the shortest relative path', {141expect_equal(digest(happiness_report_path), 'f82fd45a6bec54581361a0dfe13e9ea1')142})143test_that('happiness_report does not contain the correct information.', {144expect_equal(dim(happiness_report), c(155, 5))145expect_equal(digest(int_round(sum(happiness_report$freedom), 2)), 'fb345985a119a2b0f99a7896efa4d2c7') # we hid the answer to the test here so you can't see it, but we can still run the test146expect_equal(colnames(happiness_report), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))147})148print("Success!")149}150151test_3.5 <- function(){152test_that('Solution is incorrect', {153expect_equal(digest(answer3.5), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test154})155print("Success!")156}157158test_3.6.1 <- function(){159test_that('Did not create an object named happy_semi_df', {160expect_true(exists("happy_semi_df"))161})162test_that('happy_semi_df should be a data frame.', {163expect_true('data.frame' %in% class(happy_semi_df))164})165test_that('happy_semi_df does not contain the correct information.', {166expect_equal(dim(happy_semi_df), c(155, 5))167expect_equal(digest(int_round(sum(as.integer(sub(",", ".", happy_semi_df$freedom, fixed = TRUE))), 2)), '1473d70e5646a26de3c52aa1abd85b1f') # we hid the answer to the test here so you can't see it, but we can still run the test168expect_equal(colnames(happy_semi_df), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))169})170print("Success!")171}172173test_3.6.2 <- function(){174test_that('Did not create an object named happy_semi_df2', {175expect_true(exists("happy_semi_df2"))176})177test_that('happy_semi_df2 should be a data frame.', {178expect_true('data.frame' %in% class(happy_semi_df2))179})180test_that('happy_semi_df2 does not contain the correct information.', {181expect_equal(dim(happy_semi_df2), c(155, 5))182expect_equal(digest(int_round(sum(happy_semi_df2$happiness_score), 2)), '5c379e06a33669ccfd7208cb56366ca2') # we hid the answer to the test here so you can't see it, but we can still run the test183expect_equal(colnames(happy_semi_df2), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))184})185print("Success!")186}187188test_3.6.3 <- function(){189test_that('Did not create an object named happy_tsv', {190expect_true(exists("happy_tsv"))191})192test_that('happy_tsv should be a data frame.', {193expect_true('data.frame' %in% class(happy_tsv))194})195test_that('happy_tsv does not contain the correct information.', {196expect_equal(dim(happy_tsv), c(155, 5))197expect_equal(digest(int_round(sum(happy_tsv$freedom), 2)), 'fb345985a119a2b0f99a7896efa4d2c7') # we hid the answer to the test here so you can't see it, but we can still run the test198expect_equal(colnames(happy_tsv), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))199})200print("Success!")201}202203test_3.6.4 <- function(){204test_that('Did not create an object named happy_metadata', {205expect_true(exists("happy_metadata"))206})207test_that('happy_metadata should be a data frame.', {208expect_true('data.frame' %in% class(happy_metadata))209})210test_that('happy_metadata does not contain the correct information.', {211expect_equal(dim(happy_metadata), c(155, 5))212expect_equal(digest(int_round(sum(happy_metadata$freedom), 2)), 'fb345985a119a2b0f99a7896efa4d2c7') # we hid the answer to the test here so you can't see it, but we can still run the test213expect_equal(colnames(happy_metadata), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))214})215print("Success!")216}217218test_3.6.5 <- function(){219test_that('Did not create an object named happy_header', {220expect_true(exists("happy_header"))221})222test_that('happy_header should be a data frame.', {223expect_true('data.frame' %in% class(happy_header))224})225test_that('happy_header does not contain the correct information.', {226expect_equal(dim(happy_header), c(155, 5))227expect_equal(digest(int_round(sum(happy_header$freedom), 2)), 'fb345985a119a2b0f99a7896efa4d2c7') # we hid the answer to the test here so you can't see it, but we can still run the test228expect_equal(colnames(happy_header), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))229})230print("Success!")231}232233test_3.7 <- function(){234test_that('Did not create an object named happy_xlsx', {235expect_true(exists("happy_xlsx"))236})237test_that('happy_xlsx should be a data frame.', {238expect_true('data.frame' %in% class(happy_xlsx))239})240test_that('happy_xlsx does not contain the correct information.', {241expect_equal(dim(happy_xlsx), c(155, 5))242expect_equal(digest(int_round(sum(happy_xlsx$freedom), 2)), 'fb345985a119a2b0f99a7896efa4d2c7') # we hid the answer to the test here so you can't see it, but we can still run the test243expect_equal(colnames(happy_xlsx), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))244})245print("Success!")246}247248test_3.8 <- function(){249test_that('Solution is incorrect', {250expect_equal(digest(answer3.8), '0590b0427c1b19a6eb612d19888aa52f') # we hid the answer to the test here so you can't see it, but we can still run the test251})252print("Success!")253}254255test_3.9 <- function(){256test_that('Did not create a plot named header_plot', {257expect_true(exists("header_plot"))258})259260properties <- c(header_plot$layers[[1]]$mapping, header_plot$mapping)261labels <- header_plot$labels262263test_that('GDP_per_capita should be on the x-axis.', {264expect_true("GDP_per_capita" == rlang::get_expr(properties$x))265})266test_that('life_expectancy should be on the y-axis.', {267expect_true("life_expectancy" == rlang::get_expr(properties$y))268})269test_that('header_plot should be a scatter plot.', {270expect_that("GeomPoint" %in% c(class(header_plot$layers[[1]]$geom)) , is_true())271})272test_that('Labels on the axes should be descriptive and human readable.', {273expect_that((header_plot$labels$x) == 'GDP_per_capita', is_false())274expect_that((header_plot$labels$y) == 'life_expectancy', is_false())275})276print("Success!")277}278279280test_4.1 <- function(){281test_that('Solution is incorrect', {282expect_equal(digest(answer4.1), 'd2a90307aac5ae8d0ef58e2fe730d38b') # we hid the answer to the test here so you can't see it, but we can still run the test283})284print("Success!")285}286287288test_4.2 <- function(){289test_that('Solution is incorrect', {290expect_equal(digest(answer4.2), 'c1f86f7430df7ddb256980ea6a3b57a4') # we hid the answer to the test here so you can't see it, but we can still run the test291})292print("Success!")293}294295test_4.3.1 <- function(){296test_that('Did not create the conn object', {297expect_true(exists("conn"))298})299test_that('conn is not an RSQLite database connection object', {300expect_equal(digest(class(conn)), 'db665ff04541c433e1f30318e7715ef4')301})302test_that('conn was loaded from the wrong file',{303expect_true(grepl('data/flights_filtered.db', format(conn)))304})305test_that('conn is the wrong database',{306expect_equal(digest(dbListTables(conn)), '51a67410d71bf270db1994ea905cda13')307})308print("Success!")309}310311test_4.3.2 <- function(){312test_that('table name is incorrect',{313expect_equal(digest(flights_table_name), '51a67410d71bf270db1994ea905cda13')314})315print('Success!')316}317318test_4.3.3 <- function(){319test_that('Did not create an object named flight_data', {320expect_true(exists("flight_data"))321})322test_that('flight_data should be a data frame.', {323expect_true('tbl_SQLiteConnection' %in% class(flight_data))324})325test_that('flight_data does not contain the correct information.', {326expect_equal(digest(int_round(as_tibble(tally(flight_data))[[1]], 2)), '6bf4cc4788963306e4991f74f4edfd06')327expect_equal(digest(int_round(dim(flight_data)[2], 2)), '306a937dfa0335e74514e4c6044755f6')328expect_equal(digest(int_round(as_tibble(summarize(flight_data, sum=sum(ARRIVAL_DELAY)))[[1]], 2)), '18935ad0b2d8d671ed44f695a1324b62')329expect_equal(colnames(flight_data), c('YEAR', 'MONTH', 'DAY', 'DAY_OF_WEEK', 'ORIGIN_AIRPORT', 'DESTINATION_AIRPORT', 'DISTANCE', 'SCHEDULED_DEPARTURE', 'DEPARTURE_DELAY', 'SCHEDULED_ARRIVAL', 'ARRIVAL_DELAY', 'DIVERTED', 'CANCELLED'))330})331332print("Success!")333}334335test_4.4 <- function(){336test_that('Did not create an object named delay_data', {337expect_true(exists("delay_data"))338})339test_that('delay_data has the wrong dimensions', {340expect_equal(digest(int_round(as_tibble(tally(delay_data))[[1]], 2)), 'c73696e1f5ebc74021271a447e6a027d')341expect_equal(digest(int_round(dim(delay_data)[2], 2)), '2567f3d5adc004a73dc268884026f3bd')342})343test_that('delay data has the wrong variables', {344expect_setequal(colnames(delay_data), c('DEPARTURE_DELAY', 'ARRIVAL_DELAY'))345})346print('Success!')347}348349test_4.5 <- function(){350test_that('Did not create a plot named arrival_delay_plot', {351expect_true(exists("arrival_delay_plot"))352})353test_that('The axis labels are incorrect', {354expect_equal(digest(arrival_delay_plot$labels$x), "721f8fe183a39e61a2140d9dd01b6042")355expect_equal(digest(arrival_delay_plot$labels$y), '9ae00562a72e7b3aa91a39269590839a')356})357test_that('The x/y data are incorrect', {358expect_equal(digest(arrival_delay_plot$mapping$x), '550b90233ab71397604016f5474f572f')359expect_equal(digest(arrival_delay_plot$mapping$y), 'f9e884084b84794d762a535f3facec85')360})361test_that('arrival_delay_plot should be a histogram', {362expect_equal(digest(class(arrival_delay_plot$layers[[1]]$geom)[[1]]), '00af9702c1de4491cd9d59e3c94080a8')363})364print("Success!")365}366367test_4.6 <- function(){368test_that('Did not create a plot named departure_delay_plot', {369expect_true(exists("departure_delay_plot"))370})371test_that('The axis labels are incorrect', {372expect_equal(digest(departure_delay_plot$labels$x), "721f8fe183a39e61a2140d9dd01b6042")373expect_equal(digest(departure_delay_plot$labels$y), '9ae00562a72e7b3aa91a39269590839a')374})375test_that('The x/y data are incorrect', {376expect_equal(digest(departure_delay_plot$mapping$x), 'c92e510b14ce856cce6eb14e740686b1')377expect_equal(digest(departure_delay_plot$mapping$y), 'f9e884084b84794d762a535f3facec85')378})379test_that('departure_delay_plot should be a histogram', {380expect_equal(digest(class(departure_delay_plot$layers[[1]]$geom)[[1]]), '00af9702c1de4491cd9d59e3c94080a8')381})382print("Success!")383}384385test_4.7 <- function(){386test_that('Answer incorrect', {387expect_equal(digest(answer4.7), '7d9e25abb82fe492a7007d36124cee47')388})389print('Success!')390}391392test_4.8.1 <- function(){393test_that('Answer incorrect', {394expect_equal(digest(answer4.8.1), '75f1160e72554f4270c809f041c7a776')395})396print('Success!')397}398399400test_4.8.2 <- function(){401test_that('Did not create the delay_dataframe object', {402expect_true(exists("delay_dataframe"))403})404test_that("The delay_dataframe doesn't have the right data in it", {405expect_equal(digest(int_round(sum(delay_dataframe), 2)), 'd142b116cb76f4e5c3fab6e96bd89a22')406})407test_that('Did not create the delay_data.csv file', {408expect_true(file.exists('data/delay_data.csv'))409})410test_that('delay_data.csv does not have the right data in it', {411expect_equal(digest(int_round(sum(read_csv('data/delay_data.csv')), 2)), 'd142b116cb76f4e5c3fab6e96bd89a22')412})413print('Success!')414}415416test_5.1.0 <- function(){417test_that('Solution is incorrect', {418expect_equal(digest(as.character(answer5.1.0)), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test419})420print("Success!")421}422423test_5.1.1 <- function(){424test_that('Solution is incorrect', {425expect_equal(digest(as.character(answer5.1.1)), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test426})427print("Success!")428}429430test_5.2 <- function(){431test_that('Did not create an object named gwp', {432expect_true(exists("gwp"))433})434test_that('gwp should not be a data frame.', {435expect_false('data.frame' %in% class(gwp))436})437test_that('gwp does not contain the correct information.', {438expect_equal(digest(int_round(length(gwp), 2)), '2567f3d5adc004a73dc268884026f3bd') # we hid the answer to the test here so you can't see it, but we can still run the test439expect_true('xml_document' %in% attributes(gwp)$class)440})441print("Success!")442}443444test_5.4 <- function(){445test_that('Did not create an object named gwp_value', {446expect_true(exists("gwp_value"))447})448test_that('gwp_value should be a vector containing characters.', {449expect_that(is.vector(gwp_value), is_true())450expect_equal(class(gwp_value), "character")451})452print("Success!")453}454455test_5.5 <- function(){456test_that('Did not create an object named gwp', {457expect_true(exists("gwp"))458})459test_that('gwp should be a data frame.', {460expect_true('data.frame' %in% class(gwp))461})462test_that('gwp should contain the columns: year and gwp_value', {463expect_that("year" %in% colnames(gwp), is_true())464expect_that("gwp_value" %in% colnames(gwp), is_true())465})466print("Success!")467}468469test_5.6 <- function(){470test_that('Did not create a plot named gwp_historical', {471expect_true(exists("gwp_historical"))472})473474properties <- c(gwp_historical$layers[[1]]$mapping, gwp_historical$mapping)475labels <- gwp_historical$labels476477test_that('sqrt_year should be on the x-axis.', {478expect_true("sqrt_year" == rlang::get_expr(properties$x))479})480test_that('gwp_value should be on the y-axis.', {481expect_true("gwp_value" == rlang::get_expr(properties$y))482})483test_that('gwp_historical should be a line plot.', {484expect_that("GeomLine" %in% c(class(gwp_historical$layers[[1]]$geom)) , is_true())485})486test_that('Labels on the axes should be descriptive and human readable.', {487expect_that((gwp_historical$labels$y) == 'gwp_value', is_false())488expect_that((gwp_historical$labels$x) == 'sqrt_year', is_false())489})490print("Success!")491}492493test_5.7 <- function(){494test_that('Solution is incorrect', {495expect_equal(digest(as.character(answer5.7)), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test496})497print("Success!")498}499500501