Path: blob/master/2020-spring/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), '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(answer2), '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(answer4), '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(sum(as.numeric(happy_semi_df$freedom))), '9c9393e1464352cd4fbea94dfadfa02a') # 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})228test_that('GDP_per_capita should be on the x-axis', {229expect_that("GDP_per_capita" %in% c(rlang::get_expr(header_plot$mapping$x), rlang::get_expr(header_plot$layers[[1]]$mapping$x)) , is_true())230})231test_that('life_expectancy should be on the y-axis', {232expect_that("life_expectancy" %in% c(rlang::get_expr(header_plot$mapping$y), rlang::get_expr(header_plot$layers[[1]]$mapping$y)) , is_true())233})234test_that('geom should be geom_point', {235expect_that("GeomPoint" %in% c(class(header_plot$layers[[1]]$geom)) , is_true())236})237print("Success!")238}239240241test_4.1 <- function(){242test_that('Solution is incorrect', {243expect_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 test244})245print("Success!")246}247248249test_4.2 <- function(){250test_that('Solution is incorrect', {251expect_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 test252})253print("Success!")254}255256test_4.3.1 <- function(){257test_that('Did not create the conn object', {258expect_true(exists("conn"))259})260test_that('conn is not an RSQLite database connection object', {261expect_equal(digest(class(conn)), 'db665ff04541c433e1f30318e7715ef4')262})263test_that('conn was loaded from the wrong file',{264expect_true(grepl('data/flights_filtered.db', format(conn)))265})266test_that('conn is the wrong database',{267expect_equal(digest(dbListTables(conn)), '51a67410d71bf270db1994ea905cda13')268})269print("Success!")270}271272test_4.3.2 <- function(){273test_that('table name is incorrect',{274expect_equal(digest(flights_table_name), '51a67410d71bf270db1994ea905cda13')275})276print('Success!')277}278279test_4.3.3 <- function(){280test_that('Did not create an object named flight_data', {281expect_true(exists("flight_data"))282})283test_that('flight_data should be a data frame.', {284expect_true('tbl_SQLiteConnection' %in% class(flight_data))285})286test_that('flight_data does not contain the correct information.', {287expect_equal(digest(as_tibble(tally(flight_data))[[1]]), 'fe365cf3cbd6f3a8164444879f87385e')288expect_equal(digest(dim(flight_data)[2]), '8ae1ac7bdf62dca7c19b427a9153445c')289expect_equal(digest( as_tibble(summarize(flight_data, sum=sum(ARRIVAL_DELAY)))[[1]] ),'7c262436b16a92bd887fedb5d946ca58')290expect_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'))291})292293print("Success!")294}295296test_4.4 <- function(){297test_that('Did not create an object named delay_data', {298expect_true(exists("delay_data"))299})300test_that('delay_data has the wrong dimensions', {301expect_equal(digest(as_tibble(tally(delay_data))[[1]]), 'ae6fbe0f998b93e033a6b3d7027d3500')302expect_equal(digest(dim(delay_data)[2]), 'c01f179e4b57ab8bd9de309e6d576c48')303})304test_that('delay data has the wrong variables', {305expect_setequal(colnames(delay_data), c('DEPARTURE_DELAY', 'ARRIVAL_DELAY'))306})307print('Success!')308}309310test_4.5 <- function(){311test_that('Did not create a plot named arrival_delay_plot', {312expect_true(exists("arrival_delay_plot"))313})314test_that('The axis labels are incorrect', {315expect_equal(digest(arrival_delay_plot$labels$x), "721f8fe183a39e61a2140d9dd01b6042")316expect_equal(digest(arrival_delay_plot$labels$y), '9ae00562a72e7b3aa91a39269590839a')317})318test_that('The x/y data are incorrect', {319expect_equal(digest(arrival_delay_plot$mapping$x), '550b90233ab71397604016f5474f572f')320expect_equal(digest(arrival_delay_plot$mapping$y), 'f9e884084b84794d762a535f3facec85')321})322test_that('arrival_delay_plot should be a histogram', {323expect_equal(digest(class(arrival_delay_plot$layers[[1]]$geom)[[1]]), '00af9702c1de4491cd9d59e3c94080a8')324})325print("Success!")326}327328test_4.6 <- function(){329test_that('Did not create a plot named departure_delay_plot', {330expect_true(exists("departure_delay_plot"))331})332test_that('The axis labels are incorrect', {333expect_equal(digest(departure_delay_plot$labels$x), "721f8fe183a39e61a2140d9dd01b6042")334expect_equal(digest(departure_delay_plot$labels$y), '9ae00562a72e7b3aa91a39269590839a')335})336test_that('The x/y data are incorrect', {337expect_equal(digest(departure_delay_plot$mapping$x), 'c92e510b14ce856cce6eb14e740686b1')338expect_equal(digest(departure_delay_plot$mapping$y), 'f9e884084b84794d762a535f3facec85')339})340test_that('departure_delay_plot should be a histogram', {341expect_equal(digest(class(departure_delay_plot$layers[[1]]$geom)[[1]]), '00af9702c1de4491cd9d59e3c94080a8')342})343print("Success!")344}345346test_4.7 <- function(){347test_that('Answer incorrect', {348expect_equal(digest(answer4.7), '7d9e25abb82fe492a7007d36124cee47')349})350print('Success!')351}352353test_4.8.1 <- function(){354test_that('Answer incorrect', {355expect_equal(digest(answer4.8.1), '75f1160e72554f4270c809f041c7a776')356})357print('Success!')358}359360361test_4.8.2 <- function(){362test_that('Did not create the delay_dataframe object', {363expect_true(exists("delay_dataframe"))364})365test_that("The delay_dataframe doesn't have the right data in it", {366expect_equal(digest(sum(delay_dataframe)), '4bac82174d1816fd23b4af094ccac10c')367})368test_that('Did not create the delay_data.csv file', {369expect_true(file.exists('data/delay_data.csv'))370})371test_that('delay_data.csv does not have the right data in it', {372expect_equal(digest(sum(read_csv('data/delay_data.csv'))), '4bac82174d1816fd23b4af094ccac10c')373})374print('Success!')375}376377test_5.1.0 <- function(){378test_that('Solution is incorrect', {379expect_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 test380})381print("Success!")382}383384test_5.1.1 <- function(){385test_that('Solution is incorrect', {386expect_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 test387})388print("Success!")389}390391test_5.2 <- function(){392test_that('Did not create an object named gwp', {393expect_true(exists("gwp"))394})395test_that('gwp should not be a data frame.', {396expect_false('data.frame' %in% class(gwp))397})398test_that('gwp does not contain the correct information.', {399expect_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 test400expect_that('xml_document' %in% attributes(gwp)$class, is_true())401})402print("Success!")403}404405test_5.4 <- function(){406test_that('Did not create an object named gwp_value', {407expect_true(exists("gwp_value"))408})409test_that('gwp_value should be a vector containing characters.', {410expect_that(is.vector(gwp_value), is_true())411expect_equal(class(gwp_value), "character")412})413test_that('gwp_value should be 59 characters long.', {414expect_equal(length(gwp_value), 59)415})416print("Success!")417}418419test_5.5 <- function(){420test_that('Did not create an object named gwp', {421expect_true(exists("gwp"))422})423test_that('gwp should be a data frame.', {424expect_true('data.frame' %in% class(gwp))425})426test_that('gwp should contain the columns: year and gwp_value', {427expect_that("year" %in% colnames(gwp), is_true())428expect_that("gwp_value" %in% colnames(gwp), is_true())429})430test_that('gwp does not contain the correct information.', {431expect_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 test432expect_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 test433})434print("Success!")435}436437test_5.6 <- function(){438test_that('Did not create a plot named gwp_historical', {439expect_true(exists("gwp_historical"))440})441test_that('sqrt_year should be on the x-axis.', {442expect_that("sqrt_year" %in% c(rlang::get_expr(gwp_historical$mapping$x),rlang::get_expr(gwp_historical$layers[[1]]$mapping$x)), is_true())443})444test_that('gwp_value should be on the y-axis.', {445expect_that("gwp_value" %in% c(rlang::get_expr(gwp_historical$mapping$y), rlang::get_expr(gwp_historical$layers[[1]]$mapping$y)) , is_true())446})447test_that('gwp_historical should be a line plot.', {448expect_that("GeomLine" %in% c(class(gwp_historical$layers[[1]]$geom)) , is_true())449})450test_that('Labels on the axes should be descriptive and human readable.', {451expect_that((gwp_historical$labels$y) == 'gwp_value', is_false())452expect_that((gwp_historical$labels$x) == 'sqrt_year', is_false())453})454print("Success!")455}456457test_5.7 <- function(){458test_that('Solution is incorrect', {459expect_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 test460})461print("Success!")462}463464