Path: blob/master/2022-spring/materials/worksheet_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}3132test_1.1 <- function(){33test_that('Solution is incorrect', {34expect_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 test35})36print("Success!")37}3839test_1.2 <- function(){40test_that('Solution is incorrect', {41expect_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 test42})43print("Success!")44}4546test_1.3 <- function(){47test_that('Solution is incorrect', {48expect_equal(digest(A), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test49expect_equal(digest(B), 'e5b57f323c7b3719bbaaf9f96b260d39') # we hid the answer to the test here so you can't see it, but we can still run the test50expect_equal(digest(C), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test51expect_equal(digest(D), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test52expect_equal(digest(E), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test53expect_equal(digest(F), 'e5b57f323c7b3719bbaaf9f96b260d39') # we hid the answer to the test here so you can't see it, but we can still run the test54})55print("Success!")56}5758test_1.4 <- function(){59test_that('Solution is incorrect', {60expect_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 test61})62print("Success!")63}6465test_1.5 <- function(){66test_that('Solution is incorrect', {67expect_equal(digest(A), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test68expect_equal(digest(B), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test69expect_equal(digest(C), '5e338704a8e069ebd8b38ca71991cf94') # we hid the answer to the test here so you can't see it, but we can still run the test70expect_equal(digest(D), 'dbc09cba9fe2583fb01d63c70e1555a8') # we hid the answer to the test here so you can't see it, but we can still run the test71expect_equal(digest(E), 'e5b57f323c7b3719bbaaf9f96b260d39') # we hid the answer to the test here so you can't see it, but we can still run the test72})73print("Success!")74}7576test_2.1 <- function(){77test_that('Solution is incorrect', {78expect_equal(digest(G), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test79expect_equal(digest(H), 'e5b57f323c7b3719bbaaf9f96b260d39') # we hid the answer to the test here so you can't see it, but we can still run the test80expect_equal(digest(I), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test81expect_equal(digest(J), 'dbc09cba9fe2583fb01d63c70e1555a8') # we hid the answer to the test here so you can't see it, but we can still run the test82})83print("Success!")84}8586test_2.2 <- function(){87test_that('Solution is incorrect', {88expect_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 test89})90print("Success!")91}9293test_2.3 <- function(){94test_that('Solution is incorrect', {95expect_that(exists('answer2.3'), is_true())96expect_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 test97})98print("Success!")99}100101test_3.1 <- function(){102test_that('Solution is incorrect', {103expect_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 test104})105print("Success!")106}107108test_3.2 <- function(){109test_that('Solution is incorrect', {110expect_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 test111})112print("Success!")113}114115test_3.3 <- function(){116test_that('Solution is incorrect', {117expect_equal(digest(A), '0590b0427c1b19a6eb612d19888aa52f') # we hid the answer to the test here so you can't see it, but we can still run the test118expect_equal(digest(B), 'e6d21e822242a5c1c9f58806024e77ba') # we hid the answer to the test here so you can't see it, but we can still run the test119expect_equal(digest(C), 'f2375c071669f9b6399f28e06a598e57') # we hid the answer to the test here so you can't see it, but we can still run the test120expect_equal(digest(D), '0590b0427c1b19a6eb612d19888aa52f') # we hid the answer to the test here so you can't see it, but we can still run the test121expect_equal(digest(E), '863dfc36ab2bfe97404cc8fc074a5241') # we hid the answer to the test here so you can't see it, but we can still run the test122expect_equal(digest(F), '3697e3a900218295975a04d2205e3518') # we hid the answer to the test here so you can't see it, but we can still run the test123})124print("Success!")125}126127128test_3.4 <- function(){129test_that('Did not create an object named happiness_report_path', {130expect_true(exists("happiness_report_path"))131})132test_that('Did not create an object named happiness_report', {133expect_true(exists("happiness_report"))134})135test_that('happiness_report should be a data frame.', {136expect_true('data.frame' %in% class(happiness_report))137})138test_that('Did not use the shortest relative path', {139expect_equal(digest(happiness_report_path), 'f82fd45a6bec54581361a0dfe13e9ea1')140})141test_that('happiness_report does not contain the correct information.', {142expect_equal(dim(happiness_report), c(155, 5))143expect_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 test144expect_equal(colnames(happiness_report), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))145})146print("Success!")147}148149test_3.5 <- function(){150test_that('Solution is incorrect', {151expect_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 test152})153print("Success!")154}155156test_3.6.1 <- function(){157test_that('Did not create an object named happy_semi_df', {158expect_true(exists("happy_semi_df"))159})160test_that('happy_semi_df should be a data frame.', {161expect_true('data.frame' %in% class(happy_semi_df))162})163test_that('happy_semi_df does not contain the correct information.', {164expect_equal(dim(happy_semi_df), c(155, 5))165expect_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 test166expect_equal(colnames(happy_semi_df), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))167})168print("Success!")169}170171test_3.6.2 <- function(){172test_that('Did not create an object named happy_semi_df2', {173expect_true(exists("happy_semi_df2"))174})175test_that('happy_semi_df2 should be a data frame.', {176expect_true('data.frame' %in% class(happy_semi_df2))177})178test_that('happy_semi_df2 does not contain the correct information.', {179expect_equal(dim(happy_semi_df2), c(155, 5))180expect_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 test181expect_equal(colnames(happy_semi_df2), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))182})183print("Success!")184}185186test_3.6.3 <- function(){187test_that('Did not create an object named happy_tsv', {188expect_true(exists("happy_tsv"))189})190test_that('happy_tsv should be a data frame.', {191expect_true('data.frame' %in% class(happy_tsv))192})193test_that('happy_tsv does not contain the correct information.', {194expect_equal(dim(happy_tsv), c(155, 5))195expect_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 test196expect_equal(colnames(happy_tsv), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))197})198print("Success!")199}200201test_3.6.4 <- function(){202test_that('Did not create an object named happy_metadata', {203expect_true(exists("happy_metadata"))204})205test_that('happy_metadata should be a data frame.', {206expect_true('data.frame' %in% class(happy_metadata))207})208test_that('happy_metadata does not contain the correct information.', {209expect_equal(dim(happy_metadata), c(155, 5))210expect_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 test211expect_equal(colnames(happy_metadata), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))212})213print("Success!")214}215216test_3.6.5 <- function(){217test_that('Did not create an object named happy_header', {218expect_true(exists("happy_header"))219})220test_that('happy_header should be a data frame.', {221expect_true('data.frame' %in% class(happy_header))222})223test_that('happy_header does not contain the correct information.', {224expect_equal(dim(happy_header), c(155, 5))225expect_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 test226expect_equal(colnames(happy_header), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))227})228print("Success!")229}230231test_3.7 <- function(){232test_that('Did not create an object named happy_xlsx', {233expect_true(exists("happy_xlsx"))234})235test_that('happy_xlsx should be a data frame.', {236expect_true('data.frame' %in% class(happy_xlsx))237})238test_that('happy_xlsx does not contain the correct information.', {239expect_equal(dim(happy_xlsx), c(155, 5))240expect_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 test241expect_equal(colnames(happy_xlsx), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))242})243print("Success!")244}245246test_3.8 <- function(){247test_that('Solution is incorrect', {248expect_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 test249})250print("Success!")251}252253test_3.9 <- function(){254test_that('Did not create a plot named header_plot', {255expect_true(exists("header_plot"))256})257258properties <- c(header_plot$layers[[1]]$mapping, header_plot$mapping)259labels <- header_plot$labels260261test_that('GDP_per_capita should be on the x-axis.', {262expect_true("GDP_per_capita" == rlang::get_expr(properties$x))263})264test_that('life_expectancy should be on the y-axis.', {265expect_true("life_expectancy" == rlang::get_expr(properties$y))266})267test_that('header_plot should be a scatter plot.', {268expect_that("GeomPoint" %in% c(class(header_plot$layers[[1]]$geom)) , is_true())269})270test_that('Labels on the axes should be descriptive and human readable.', {271expect_that((header_plot$labels$x) == 'GDP_per_capita', is_false())272expect_that((header_plot$labels$y) == 'life_expectancy', is_false())273})274print("Success!")275}276277278test_4.1 <- function(){279test_that('Solution is incorrect', {280expect_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 test281})282print("Success!")283}284285286test_4.2 <- function(){287test_that('Solution is incorrect', {288expect_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 test289})290print("Success!")291}292293test_4.3.1 <- function(){294test_that('Did not create the conn object', {295expect_true(exists("conn"))296})297test_that('conn is not an RSQLite database connection object', {298expect_equal(digest(class(conn)), 'db665ff04541c433e1f30318e7715ef4')299})300test_that('conn was loaded from the wrong file',{301expect_true(grepl('data/flights_filtered.db', format(conn)))302})303test_that('conn is the wrong database',{304expect_equal(digest(dbListTables(conn)), '51a67410d71bf270db1994ea905cda13')305})306print("Success!")307}308309test_4.3.2 <- function(){310test_that('table name is incorrect',{311expect_equal(digest(flights_table_name), '51a67410d71bf270db1994ea905cda13')312})313print('Success!')314}315316test_4.3.3 <- function(){317test_that('Did not create an object named flight_data', {318expect_true(exists("flight_data"))319})320test_that('flight_data should be a data frame.', {321expect_true('tbl_SQLiteConnection' %in% class(flight_data))322})323test_that('flight_data does not contain the correct information.', {324expect_equal(digest(int_round(as_tibble(tally(flight_data))[[1]], 2)), '6bf4cc4788963306e4991f74f4edfd06')325expect_equal(digest(int_round(dim(flight_data)[2], 2)), '306a937dfa0335e74514e4c6044755f6')326expect_equal(digest(int_round(as_tibble(summarize(flight_data, sum=sum(ARRIVAL_DELAY)))[[1]], 2)), '18935ad0b2d8d671ed44f695a1324b62')327expect_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'))328})329330print("Success!")331}332333test_4.4 <- function(){334test_that('Did not create an object named delay_data', {335expect_true(exists("delay_data"))336})337test_that('delay_data has the wrong dimensions', {338expect_equal(digest(int_round(as_tibble(tally(delay_data))[[1]], 2)), 'c73696e1f5ebc74021271a447e6a027d')339expect_equal(digest(int_round(dim(delay_data)[2], 2)), '2567f3d5adc004a73dc268884026f3bd')340})341test_that('delay data has the wrong variables', {342expect_setequal(colnames(delay_data), c('DEPARTURE_DELAY', 'ARRIVAL_DELAY'))343})344print('Success!')345}346347test_4.5 <- function(){348test_that('Did not create a plot named arrival_delay_plot', {349expect_true(exists("arrival_delay_plot"))350})351test_that('The axis labels are incorrect', {352expect_equal(digest(arrival_delay_plot$labels$x), "721f8fe183a39e61a2140d9dd01b6042")353expect_equal(digest(arrival_delay_plot$labels$y), '9ae00562a72e7b3aa91a39269590839a')354})355test_that('The x/y data are incorrect', {356expect_equal(digest(arrival_delay_plot$mapping$x), '550b90233ab71397604016f5474f572f')357expect_equal(digest(arrival_delay_plot$mapping$y), 'f9e884084b84794d762a535f3facec85')358})359test_that('arrival_delay_plot should be a histogram', {360expect_equal(digest(class(arrival_delay_plot$layers[[1]]$geom)[[1]]), '00af9702c1de4491cd9d59e3c94080a8')361})362print("Success!")363}364365test_4.6 <- function(){366test_that('Did not create a plot named departure_delay_plot', {367expect_true(exists("departure_delay_plot"))368})369test_that('The axis labels are incorrect', {370expect_equal(digest(departure_delay_plot$labels$x), "721f8fe183a39e61a2140d9dd01b6042")371expect_equal(digest(departure_delay_plot$labels$y), '9ae00562a72e7b3aa91a39269590839a')372})373test_that('The x/y data are incorrect', {374expect_equal(digest(departure_delay_plot$mapping$x), 'c92e510b14ce856cce6eb14e740686b1')375expect_equal(digest(departure_delay_plot$mapping$y), 'f9e884084b84794d762a535f3facec85')376})377test_that('departure_delay_plot should be a histogram', {378expect_equal(digest(class(departure_delay_plot$layers[[1]]$geom)[[1]]), '00af9702c1de4491cd9d59e3c94080a8')379})380print("Success!")381}382383test_4.7 <- function(){384test_that('Answer incorrect', {385expect_equal(digest(answer4.7), '7d9e25abb82fe492a7007d36124cee47')386})387print('Success!')388}389390test_4.8.1 <- function(){391test_that('Answer incorrect', {392expect_equal(digest(answer4.8.1), '75f1160e72554f4270c809f041c7a776')393})394print('Success!')395}396397398test_4.8.2 <- function(){399test_that('Did not create the delay_dataframe object', {400expect_true(exists("delay_dataframe"))401})402test_that("The delay_dataframe doesn't have the right data in it", {403expect_equal(digest(int_round(sum(delay_dataframe), 2)), 'd142b116cb76f4e5c3fab6e96bd89a22')404})405test_that('Did not create the delay_data.csv file', {406expect_true(file.exists('data/delay_data.csv'))407})408test_that('delay_data.csv does not have the right data in it', {409expect_equal(digest(int_round(sum(read_csv('data/delay_data.csv')), 2)), 'd142b116cb76f4e5c3fab6e96bd89a22')410})411print('Success!')412}413414test_5.1.0 <- function(){415test_that('Solution is incorrect', {416expect_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 test417})418print("Success!")419}420421test_5.1.1 <- function(){422test_that('Solution is incorrect', {423expect_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 test424})425print("Success!")426}427428test_5.2 <- function(){429test_that('Did not create an object named gwp', {430expect_true(exists("gwp"))431})432test_that('gwp should not be a data frame.', {433expect_false('data.frame' %in% class(gwp))434})435test_that('gwp does not contain the correct information.', {436expect_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 test437expect_true('xml_document' %in% attributes(gwp)$class)438})439print("Success!")440}441442test_5.4 <- function(){443test_that('Did not create an object named gwp_value', {444expect_true(exists("gwp_value"))445})446test_that('gwp_value should be a vector containing characters.', {447expect_that(is.vector(gwp_value), is_true())448expect_equal(class(gwp_value), "character")449})450print("Success!")451}452453test_5.5 <- function(){454test_that('Did not create an object named gwp', {455expect_true(exists("gwp"))456})457test_that('gwp should be a data frame.', {458expect_true('data.frame' %in% class(gwp))459})460test_that('gwp should contain the columns: year and gwp_value', {461expect_that("year" %in% colnames(gwp), is_true())462expect_that("gwp_value" %in% colnames(gwp), is_true())463})464print("Success!")465}466467test_5.6 <- function(){468test_that('Did not create a plot named gwp_historical', {469expect_true(exists("gwp_historical"))470})471472properties <- c(gwp_historical$layers[[1]]$mapping, gwp_historical$mapping)473labels <- gwp_historical$labels474475test_that('sqrt_year should be on the x-axis.', {476expect_true("sqrt_year" == rlang::get_expr(properties$x))477})478test_that('gwp_value should be on the y-axis.', {479expect_true("gwp_value" == rlang::get_expr(properties$y))480})481test_that('gwp_historical should be a line plot.', {482expect_that("GeomLine" %in% c(class(gwp_historical$layers[[1]]$geom)) , is_true())483})484test_that('Labels on the axes should be descriptive and human readable.', {485expect_that((gwp_historical$labels$y) == 'gwp_value', is_false())486expect_that((gwp_historical$labels$x) == 'sqrt_year', is_false())487})488print("Success!")489}490491test_5.7 <- function(){492test_that('Solution is incorrect', {493expect_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 test494})495print("Success!")496}497498499