Path: blob/master/2020-fall/materials/worksheet_02/tests_worksheet_02.R
2707 views
library(testthat)1library(digest)23test_1.1 <- function(){4test_that('Solution is incorrect', {5expect_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 test6})7print("Success!")8}910test_1.2 <- function(){11test_that('Solution is incorrect', {12expect_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 test13})14print("Success!")15}1617test_1.3 <- function(){18test_that('Solution is incorrect', {19expect_equal(digest(A), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test20expect_equal(digest(B), 'e5b57f323c7b3719bbaaf9f96b260d39') # we hid the answer to the test here so you can't see it, but we can still run the test21expect_equal(digest(C), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test22expect_equal(digest(D), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test23expect_equal(digest(E), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test24expect_equal(digest(F), 'e5b57f323c7b3719bbaaf9f96b260d39') # we hid the answer to the test here so you can't see it, but we can still run the test25})26print("Success!")27}2829test_1.4 <- function(){30test_that('Solution is incorrect', {31expect_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 test32})33print("Success!")34}3536test_1.5 <- function(){37test_that('Solution is incorrect', {38expect_equal(digest(A), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test39expect_equal(digest(B), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test40expect_equal(digest(C), '5e338704a8e069ebd8b38ca71991cf94') # we hid the answer to the test here so you can't see it, but we can still run the test41expect_equal(digest(D), 'dbc09cba9fe2583fb01d63c70e1555a8') # we hid the answer to the test here so you can't see it, but we can still run the test42expect_equal(digest(E), 'e5b57f323c7b3719bbaaf9f96b260d39') # we hid the answer to the test here so you can't see it, but we can still run the test43})44print("Success!")45}4647test_2.1 <- function(){48test_that('Solution is incorrect', {49expect_equal(digest(G), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test50expect_equal(digest(H), 'e5b57f323c7b3719bbaaf9f96b260d39') # we hid the answer to the test here so you can't see it, but we can still run the test51expect_equal(digest(I), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test52expect_equal(digest(J), 'dbc09cba9fe2583fb01d63c70e1555a8') # we hid the answer to the test here so you can't see it, but we can still run the test53})54print("Success!")55}5657test_2.2 <- function(){58test_that('Solution is incorrect', {59expect_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 test60})61print("Success!")62}6364test_2.3 <- function(){65test_that('Solution is incorrect', {66expect_that(exists('answer2.3'), is_true())67expect_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 test68})69print("Success!")70}7172test_3.1 <- function(){73test_that('Solution is incorrect', {74expect_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 test75})76print("Success!")77}7879test_3.2 <- function(){80test_that('Solution is incorrect', {81expect_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 test82})83print("Success!")84}8586test_3.3 <- function(){87test_that('Solution is incorrect', {88expect_equal(digest(A), '0590b0427c1b19a6eb612d19888aa52f') # we hid the answer to the test here so you can't see it, but we can still run the test89expect_equal(digest(B), 'e6d21e822242a5c1c9f58806024e77ba') # we hid the answer to the test here so you can't see it, but we can still run the test90expect_equal(digest(C), 'f2375c071669f9b6399f28e06a598e57') # we hid the answer to the test here so you can't see it, but we can still run the test91expect_equal(digest(D), '0590b0427c1b19a6eb612d19888aa52f') # we hid the answer to the test here so you can't see it, but we can still run the test92expect_equal(digest(E), '863dfc36ab2bfe97404cc8fc074a5241') # we hid the answer to the test here so you can't see it, but we can still run the test93expect_equal(digest(F), '3697e3a900218295975a04d2205e3518') # we hid the answer to the test here so you can't see it, but we can still run the test94})95print("Success!")96}979899test_3.4 <- function(){100test_that('Did not create an object named happiness_report_path', {101expect_true(exists("happiness_report_path"))102})103test_that('Did not create an object named happiness_report', {104expect_true(exists("happiness_report"))105})106test_that('happiness_report should be a data frame.', {107expect_true('data.frame' %in% class(happiness_report))108})109test_that('Did not use the shortest relative path', {110expect_equal(digest(happiness_report_path), 'f82fd45a6bec54581361a0dfe13e9ea1')111})112test_that('happiness_report does not contain the correct information.', {113expect_equal(dim(happiness_report), c(155, 5))114expect_equal(digest(as.integer(sum(happiness_report$freedom))), '5dfdba537fcd9880bb235099a0b2d71b') # we hid the answer to the test here so you can't see it, but we can still run the test115expect_equal(colnames(happiness_report), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))116})117print("Success!")118}119120test_3.5 <- function(){121test_that('Solution is incorrect', {122expect_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 test123})124print("Success!")125}126127test_3.6.1 <- function(){128test_that('Did not create an object named happy_semi_df', {129expect_true(exists("happy_semi_df"))130})131test_that('happy_semi_df should be a data frame.', {132expect_true('data.frame' %in% class(happy_semi_df))133})134test_that('happy_semi_df does not contain the correct information.', {135expect_equal(dim(happy_semi_df), c(155, 5))136expect_equal(digest(round(sum(as.numeric(sub(",", ".", happy_semi_df$freedom, fixed = TRUE))))), '1cb54c4e3d3eb5af32ce10cfb9e6a19a') # we hid the answer to the test here so you can't see it, but we can still run the test137expect_equal(colnames(happy_semi_df), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))138})139print("Success!")140}141142test_3.6.2 <- function(){143test_that('Did not create an object named happy_semi_df2', {144expect_true(exists("happy_semi_df2"))145})146test_that('happy_semi_df2 should be a data frame.', {147expect_true('data.frame' %in% class(happy_semi_df2))148})149test_that('happy_semi_df2 does not contain the correct information.', {150expect_equal(dim(happy_semi_df2), c(155, 5))151expect_equal(digest(as.integer(sum(happy_semi_df2$happiness_score))), 'c3ad708acb2b90a9e40e48f729083e69') # we hid the answer to the test here so you can't see it, but we can still run the test152expect_equal(colnames(happy_semi_df2), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))153})154print("Success!")155}156157test_3.6.3 <- function(){158test_that('Did not create an object named happy_tsv', {159expect_true(exists("happy_tsv"))160})161test_that('happy_tsv should be a data frame.', {162expect_true('data.frame' %in% class(happy_tsv))163})164test_that('happy_tsv does not contain the correct information.', {165expect_equal(dim(happy_tsv), c(155, 5))166expect_equal(digest(as.integer(sum(happy_tsv$freedom))), '5dfdba537fcd9880bb235099a0b2d71b') # we hid the answer to the test here so you can't see it, but we can still run the test167expect_equal(colnames(happy_tsv), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))168})169print("Success!")170}171172test_3.6.4 <- function(){173test_that('Did not create an object named happy_metadata', {174expect_true(exists("happy_metadata"))175})176test_that('happy_metadata should be a data frame.', {177expect_true('data.frame' %in% class(happy_metadata))178})179test_that('happy_metadata does not contain the correct information.', {180expect_equal(dim(happy_metadata), c(155, 5))181expect_equal(digest(as.integer(sum(happy_metadata$freedom))), '5dfdba537fcd9880bb235099a0b2d71b') # we hid the answer to the test here so you can't see it, but we can still run the test182expect_equal(colnames(happy_metadata), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))183})184print("Success!")185}186187test_3.6.5 <- function(){188test_that('Did not create an object named happy_header', {189expect_true(exists("happy_header"))190})191test_that('happy_header should be a data frame.', {192expect_true('data.frame' %in% class(happy_header))193})194test_that('happy_header does not contain the correct information.', {195expect_equal(dim(happy_header), c(155, 5))196expect_equal(digest(as.integer(sum(happy_header$freedom))), '5dfdba537fcd9880bb235099a0b2d71b') # we hid the answer to the test here so you can't see it, but we can still run the test197expect_equal(colnames(happy_header), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))198})199print("Success!")200}201202test_3.7 <- function(){203test_that('Did not create an object named happy_xlsx', {204expect_true(exists("happy_xlsx"))205})206test_that('happy_xlsx should be a data frame.', {207expect_true('data.frame' %in% class(happy_xlsx))208})209test_that('happy_xlsx does not contain the correct information.', {210expect_equal(dim(happy_xlsx), c(155, 5))211expect_equal(digest(as.integer(sum(happy_xlsx$freedom))), '5dfdba537fcd9880bb235099a0b2d71b') # we hid the answer to the test here so you can't see it, but we can still run the test212expect_equal(colnames(happy_xlsx), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))213})214print("Success!")215}216217test_3.8 <- function(){218test_that('Solution is incorrect', {219expect_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 test220})221print("Success!")222}223224test_3.9 <- function(){225test_that('Did not create a plot named header_plot', {226expect_true(exists("header_plot"))227})228229properties <- c(header_plot$layers[[1]]$mapping, header_plot$mapping)230labels <- header_plot$labels231232test_that('GDP_per_capita should be on the x-axis.', {233expect_true("GDP_per_capita" == rlang::get_expr(properties$x))234})235test_that('life_expectancy should be on the y-axis.', {236expect_true("life_expectancy" == rlang::get_expr(properties$y))237})238test_that('header_plot should be a scatter plot.', {239expect_that("GeomPoint" %in% c(class(header_plot$layers[[1]]$geom)) , is_true())240})241test_that('Labels on the axes should be descriptive and human readable.', {242expect_that((header_plot$labels$x) == 'GDP_per_capita', is_false())243expect_that((header_plot$labels$y) == 'life_expectancy', is_false())244})245print("Success!")246}247248249test_4.1 <- function(){250test_that('Solution is incorrect', {251expect_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 test252})253print("Success!")254}255256257test_4.2 <- function(){258test_that('Solution is incorrect', {259expect_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 test260})261print("Success!")262}263264test_4.3.1 <- function(){265test_that('Did not create the conn object', {266expect_true(exists("conn"))267})268test_that('conn is not an RSQLite database connection object', {269expect_equal(digest(class(conn)), 'db665ff04541c433e1f30318e7715ef4')270})271test_that('conn was loaded from the wrong file',{272expect_true(grepl('data/flights_filtered.db', format(conn)))273})274test_that('conn is the wrong database',{275expect_equal(digest(dbListTables(conn)), '51a67410d71bf270db1994ea905cda13')276})277print("Success!")278}279280test_4.3.2 <- function(){281test_that('table name is incorrect',{282expect_equal(digest(flights_table_name), '51a67410d71bf270db1994ea905cda13')283})284print('Success!')285}286287test_4.3.3 <- function(){288test_that('Did not create an object named flight_data', {289expect_true(exists("flight_data"))290})291test_that('flight_data should be a data frame.', {292expect_true('tbl_SQLiteConnection' %in% class(flight_data))293})294test_that('flight_data does not contain the correct information.', {295expect_equal(digest(as_tibble(tally(flight_data))[[1]]), 'fe365cf3cbd6f3a8164444879f87385e')296expect_equal(digest(dim(flight_data)[2]), '8ae1ac7bdf62dca7c19b427a9153445c')297expect_equal(digest( as_tibble(summarize(flight_data, sum=sum(ARRIVAL_DELAY)))[[1]] ),'7c262436b16a92bd887fedb5d946ca58')298expect_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'))299})300301print("Success!")302}303304test_4.4 <- function(){305test_that('Did not create an object named delay_data', {306expect_true(exists("delay_data"))307})308test_that('delay_data has the wrong dimensions', {309expect_equal(digest(as_tibble(tally(delay_data))[[1]]), 'ae6fbe0f998b93e033a6b3d7027d3500')310expect_equal(digest(dim(delay_data)[2]), 'c01f179e4b57ab8bd9de309e6d576c48')311})312test_that('delay data has the wrong variables', {313expect_setequal(colnames(delay_data), c('DEPARTURE_DELAY', 'ARRIVAL_DELAY'))314})315print('Success!')316}317318test_4.5 <- function(){319test_that('Did not create a plot named arrival_delay_plot', {320expect_true(exists("arrival_delay_plot"))321})322test_that('The axis labels are incorrect', {323expect_equal(digest(arrival_delay_plot$labels$x), "721f8fe183a39e61a2140d9dd01b6042")324expect_equal(digest(arrival_delay_plot$labels$y), '9ae00562a72e7b3aa91a39269590839a')325})326test_that('The x/y data are incorrect', {327expect_equal(digest(arrival_delay_plot$mapping$x), '550b90233ab71397604016f5474f572f')328expect_equal(digest(arrival_delay_plot$mapping$y), 'f9e884084b84794d762a535f3facec85')329})330test_that('arrival_delay_plot should be a histogram', {331expect_equal(digest(class(arrival_delay_plot$layers[[1]]$geom)[[1]]), '00af9702c1de4491cd9d59e3c94080a8')332})333print("Success!")334}335336test_4.6 <- function(){337test_that('Did not create a plot named departure_delay_plot', {338expect_true(exists("departure_delay_plot"))339})340test_that('The axis labels are incorrect', {341expect_equal(digest(departure_delay_plot$labels$x), "721f8fe183a39e61a2140d9dd01b6042")342expect_equal(digest(departure_delay_plot$labels$y), '9ae00562a72e7b3aa91a39269590839a')343})344test_that('The x/y data are incorrect', {345expect_equal(digest(departure_delay_plot$mapping$x), 'c92e510b14ce856cce6eb14e740686b1')346expect_equal(digest(departure_delay_plot$mapping$y), 'f9e884084b84794d762a535f3facec85')347})348test_that('departure_delay_plot should be a histogram', {349expect_equal(digest(class(departure_delay_plot$layers[[1]]$geom)[[1]]), '00af9702c1de4491cd9d59e3c94080a8')350})351print("Success!")352}353354test_4.7 <- function(){355test_that('Answer incorrect', {356expect_equal(digest(answer4.7), '7d9e25abb82fe492a7007d36124cee47')357})358print('Success!')359}360361test_4.8.1 <- function(){362test_that('Answer incorrect', {363expect_equal(digest(answer4.8.1), '75f1160e72554f4270c809f041c7a776')364})365print('Success!')366}367368369test_4.8.2 <- function(){370test_that('Did not create the delay_dataframe object', {371expect_true(exists("delay_dataframe"))372})373test_that("The delay_dataframe doesn't have the right data in it", {374expect_equal(digest(sum(delay_dataframe)), '4bac82174d1816fd23b4af094ccac10c')375})376test_that('Did not create the delay_data.csv file', {377expect_true(file.exists('data/delay_data.csv'))378})379test_that('delay_data.csv does not have the right data in it', {380expect_equal(digest(sum(read_csv('data/delay_data.csv'))), '4bac82174d1816fd23b4af094ccac10c')381})382print('Success!')383}384385test_5.1.0 <- function(){386test_that('Solution is incorrect', {387expect_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 test388})389print("Success!")390}391392test_5.1.1 <- function(){393test_that('Solution is incorrect', {394expect_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 test395})396print("Success!")397}398399test_5.2 <- function(){400test_that('Did not create an object named gwp', {401expect_true(exists("gwp"))402})403test_that('gwp should not be a data frame.', {404expect_false('data.frame' %in% class(gwp))405})406test_that('gwp does not contain the correct information.', {407expect_equal(digest(as.numeric(length(gwp))), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test408expect_that('xml_document' %in% attributes(gwp)$class, is_true())409})410print("Success!")411}412413test_5.4 <- function(){414test_that('Did not create an object named gwp_value', {415expect_true(exists("gwp_value"))416})417test_that('gwp_value should be a vector containing characters.', {418expect_that(is.vector(gwp_value), is_true())419expect_equal(class(gwp_value), "character")420})421test_that('gwp_value should be 59 characters long.', {422expect_equal(length(gwp_value), 59)423})424print("Success!")425}426427test_5.5 <- function(){428test_that('Did not create an object named gwp', {429expect_true(exists("gwp"))430})431test_that('gwp should be a data frame.', {432expect_true('data.frame' %in% class(gwp))433})434test_that('gwp should contain the columns: year and gwp_value', {435expect_that("year" %in% colnames(gwp), is_true())436expect_that("gwp_value" %in% colnames(gwp), is_true())437})438test_that('gwp does not contain the correct information.', {439expect_equal(digest(as.numeric(sum(gwp$year))), 'a531a2d37eb64bf5b3ffb7ee17dbaedc') # we hid the answer to the test here so you can't see it, but we can still run the test440expect_equal(digest(as.numeric(sum(gwp$gwp_value))), 'f555139e88b37cd50d8c5d170ea575bd') # we hid the answer to the test here so you can't see it, but we can still run the test441})442print("Success!")443}444445test_5.6 <- function(){446test_that('Did not create a plot named gwp_historical', {447expect_true(exists("gwp_historical"))448})449450properties <- c(gwp_historical$layers[[1]]$mapping, gwp_historical$mapping)451labels <- gwp_historical$labels452453test_that('sqrt_year should be on the x-axis.', {454expect_true("sqrt_year" == rlang::get_expr(properties$x))455})456test_that('gwp_value should be on the y-axis.', {457expect_true("gwp_value" == rlang::get_expr(properties$y))458})459test_that('gwp_historical should be a line plot.', {460expect_that("GeomLine" %in% c(class(gwp_historical$layers[[1]]$geom)) , is_true())461})462test_that('Labels on the axes should be descriptive and human readable.', {463expect_that((gwp_historical$labels$y) == 'gwp_value', is_false())464expect_that((gwp_historical$labels$x) == 'sqrt_year', is_false())465})466print("Success!")467}468469test_5.7 <- function(){470test_that('Solution is incorrect', {471expect_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 test472})473print("Success!")474}475476