Path: blob/master/2019-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), '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.4 <- function(){87test_that('Did not create an object named happiness_report_path', {88expect_true(exists("happiness_report_path"))89})90test_that('Did not create an object named happiness_report', {91expect_true(exists("happiness_report"))92})93test_that('happiness_report should be a data frame.', {94expect_true('data.frame' %in% class(happiness_report))95})96test_that('Did not use the shortest relative path', {97expect_equal(digest(happiness_report_path), 'f82fd45a6bec54581361a0dfe13e9ea1')98})99test_that('happiness_report does not contain the correct information.', {100expect_equal(dim(happiness_report), c(155, 5))101expect_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 test102expect_equal(colnames(happiness_report), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))103})104print("Success!")105}106107test_3.5 <- function(){108test_that('Solution is incorrect', {109expect_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 test110})111print("Success!")112}113114test_3.6.1 <- function(){115test_that('Did not create an object named happy_semi_df', {116expect_true(exists("happy_semi_df"))117})118test_that('happy_semi_df should be a data frame.', {119expect_true('data.frame' %in% class(happy_semi_df))120})121test_that('happy_semi_df does not contain the correct information.', {122expect_equal(dim(happy_semi_df), c(155, 5))123expect_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 test124expect_equal(colnames(happy_semi_df), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))125})126print("Success!")127}128129test_3.6.2 <- function(){130test_that('Did not create an object named happy_semi_df2', {131expect_true(exists("happy_semi_df2"))132})133test_that('happy_semi_df2 should be a data frame.', {134expect_true('data.frame' %in% class(happy_semi_df2))135})136test_that('happy_semi_df2 does not contain the correct information.', {137expect_equal(dim(happy_semi_df2), c(155, 5))138expect_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 test139expect_equal(colnames(happy_semi_df2), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))140})141print("Success!")142}143144test_3.6.3 <- function(){145test_that('Did not create an object named happy_tsv', {146expect_true(exists("happy_tsv"))147})148test_that('happy_tsv should be a data frame.', {149expect_true('data.frame' %in% class(happy_tsv))150})151test_that('happy_tsv does not contain the correct information.', {152expect_equal(dim(happy_tsv), c(155, 5))153expect_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 test154expect_equal(colnames(happy_tsv), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))155})156print("Success!")157}158159test_3.6.4 <- function(){160test_that('Did not create an object named happy_metadata', {161expect_true(exists("happy_metadata"))162})163test_that('happy_metadata should be a data frame.', {164expect_true('data.frame' %in% class(happy_metadata))165})166test_that('happy_metadata does not contain the correct information.', {167expect_equal(dim(happy_metadata), c(155, 5))168expect_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 test169expect_equal(colnames(happy_metadata), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))170})171print("Success!")172}173174test_3.6.5 <- function(){175test_that('Did not create an object named happy_header', {176expect_true(exists("happy_header"))177})178test_that('happy_header should be a data frame.', {179expect_true('data.frame' %in% class(happy_header))180})181test_that('happy_header does not contain the correct information.', {182expect_equal(dim(happy_header), c(155, 5))183expect_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 test184expect_equal(colnames(happy_header), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))185})186print("Success!")187}188189test_3.7 <- function(){190test_that('Did not create an object named happy_xlsx', {191expect_true(exists("happy_xlsx"))192})193test_that('happy_xlsx should be a data frame.', {194expect_true('data.frame' %in% class(happy_xlsx))195})196test_that('happy_xlsx does not contain the correct information.', {197expect_equal(dim(happy_xlsx), c(155, 5))198expect_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 test199expect_equal(colnames(happy_xlsx), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))200})201print("Success!")202}203204test_3.8 <- function(){205test_that('Solution is incorrect', {206expect_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 test207})208print("Success!")209}210211test_3.9 <- function(){212test_that('Did not create a plot named header_plot', {213expect_true(exists("header_plot"))214})215test_that('GDP_per_capita should be on the x-axis', {216expect_that("GDP_per_capita" %in% c(rlang::get_expr(header_plot$mapping$x), rlang::get_expr(header_plot$layers[[1]]$mapping$x)) , is_true())217})218test_that('life_expectancy should be on the y-axis', {219expect_that("life_expectancy" %in% c(rlang::get_expr(header_plot$mapping$y), rlang::get_expr(header_plot$layers[[1]]$mapping$y)) , is_true())220})221test_that('geom should be geom_point', {222expect_that("GeomPoint" %in% c(class(header_plot$layers[[1]]$geom)) , is_true())223})224print("Success!")225}226227228test_4.1 <- function(){229test_that('Solution is incorrect', {230expect_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 test231})232print("Success!")233}234235236test_4.2 <- function(){237test_that('Solution is incorrect', {238expect_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 test239})240print("Success!")241}242243test_4.3.1 <- function(){244test_that('Did not create the conn object', {245expect_true(exists("conn"))246})247test_that('conn is not an RSQLite database connection object', {248expect_equal(digest(class(conn)), 'db665ff04541c433e1f30318e7715ef4')249})250test_that('conn was loaded from the wrong file',{251expect_true(grepl('data/flights_filtered.db', format(conn)))252})253test_that('conn is the wrong database',{254expect_equal(digest(dbListTables(conn)), '51a67410d71bf270db1994ea905cda13')255})256print("Success!")257}258259test_4.3.2 <- function(){260test_that('table name is incorrect',{261expect_equal(digest(flights_table_name), '51a67410d71bf270db1994ea905cda13')262})263print('Success!')264}265266test_4.3.3 <- function(){267test_that('Did not create an object named flight_data', {268expect_true(exists("flight_data"))269})270test_that('flight_data should be a data frame.', {271expect_true('tbl_SQLiteConnection' %in% class(flight_data))272})273test_that('flight_data does not contain the correct information.', {274expect_equal(digest(as_tibble(tally(flight_data))[[1]]), 'fe365cf3cbd6f3a8164444879f87385e')275expect_equal(digest(dim(flight_data)[2]), '8ae1ac7bdf62dca7c19b427a9153445c')276expect_equal(digest( as_tibble(summarize(flight_data, sum=sum(ARRIVAL_DELAY)))[[1]] ),'7c262436b16a92bd887fedb5d946ca58')277expect_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'))278})279280print("Success!")281}282283test_4.4 <- function(){284test_that('Did not create an object named delay_data', {285expect_true(exists("delay_data"))286})287test_that('delay_data has the wrong dimensions', {288expect_equal(digest(as_tibble(tally(delay_data))[[1]]), 'ae6fbe0f998b93e033a6b3d7027d3500')289expect_equal(digest(dim(delay_data)[2]), 'c01f179e4b57ab8bd9de309e6d576c48')290})291test_that('delay data has the wrong variables', {292expect_equal(colnames(delay_data), c('DEPARTURE_DELAY', 'ARRIVAL_DELAY'))293})294print('Success!')295}296297test_4.5 <- function(){298test_that('Did not create a plot named arrival_delay_plot', {299expect_true(exists("arrival_delay_plot"))300})301test_that('The axis labels are incorrect', {302expect_equal(digest(arrival_delay_plot$labels$x), "721f8fe183a39e61a2140d9dd01b6042")303expect_equal(digest(arrival_delay_plot$labels$y), '9ae00562a72e7b3aa91a39269590839a')304})305test_that('The x/y data are incorrect', {306expect_equal(digest(arrival_delay_plot$mapping$x), '550b90233ab71397604016f5474f572f')307expect_equal(digest(arrival_delay_plot$mapping$y), 'f9e884084b84794d762a535f3facec85')308})309test_that('arrival_delay_plot should be a histogram', {310expect_equal(digest(class(arrival_delay_plot$layers[[1]]$geom)[[1]]), '00af9702c1de4491cd9d59e3c94080a8')311})312print("Success!")313}314315test_4.6 <- function(){316test_that('Did not create a plot named departure_delay_plot', {317expect_true(exists("departure_delay_plot"))318})319test_that('The axis labels are incorrect', {320expect_equal(digest(departure_delay_plot$labels$x), "721f8fe183a39e61a2140d9dd01b6042")321expect_equal(digest(departure_delay_plot$labels$y), '9ae00562a72e7b3aa91a39269590839a')322})323test_that('The x/y data are incorrect', {324expect_equal(digest(departure_delay_plot$mapping$x), 'c92e510b14ce856cce6eb14e740686b1')325expect_equal(digest(departure_delay_plot$mapping$y), 'f9e884084b84794d762a535f3facec85')326})327test_that('departure_delay_plot should be a histogram', {328expect_equal(digest(class(departure_delay_plot$layers[[1]]$geom)[[1]]), '00af9702c1de4491cd9d59e3c94080a8')329})330print("Success!")331}332333test_4.7 <- function(){334test_that('Answer incorrect', {335expect_equal(digest(answer4.7), '7d9e25abb82fe492a7007d36124cee47')336})337print('Success!')338}339340341test_4.8 <- function(){342test_that('Did not create the delay_dataframe object', {343expect_true(exists("delay_dataframe"))344})345test_that("The delay_dataframe doesn't have the right data in it", {346expect_equal(digest(sum(delay_dataframe)), '4bac82174d1816fd23b4af094ccac10c')347})348test_that('Did not create the delay_data.csv file', {349expect_true(file.exists('delay_data.csv'))350})351test_that('delay_data.csv does not have the right data in it', {352expect_equal(digest(sum(read_csv('delay_data.csv'))), '4bac82174d1816fd23b4af094ccac10c')353})354print('Success!')355}356357test_5.1.0 <- function(){358test_that('Solution is incorrect', {359expect_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 test360})361print("Success!")362}363364test_5.1.1 <- function(){365test_that('Solution is incorrect', {366expect_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 test367})368print("Success!")369}370371test_5.2 <- function(){372test_that('Did not create an object named gwp', {373expect_true(exists("gwp"))374})375test_that('gwp should not be a data frame.', {376expect_false('data.frame' %in% class(gwp))377})378test_that('gwp does not contain the correct information.', {379expect_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 test380expect_that('xml_document' %in% attributes(gwp)$class, is_true())381})382print("Success!")383}384385test_5.4 <- function(){386test_that('Did not create an object named gwp_value', {387expect_true(exists("gwp_value"))388})389test_that('gwp_value should be a vector containing characters.', {390expect_that(is.vector(gwp_value), is_true())391expect_equal(class(gwp_value), "character")392})393test_that('gwp_value should be 59 characters long.', {394expect_equal(length(gwp_value), 59)395})396print("Success!")397}398399test_5.5 <- function(){400test_that('Did not create an object named gwp', {401expect_true(exists("gwp"))402})403test_that('gwp should be a data frame.', {404expect_true('data.frame' %in% class(gwp))405})406test_that('gwp should contain the columns: year and gwp_value', {407expect_that("year" %in% colnames(gwp), is_true())408expect_that("gwp_value" %in% colnames(gwp), is_true())409})410test_that('gwp does not contain the correct information.', {411expect_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 test412expect_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 test413})414print("Success!")415}416417test_5.6 <- function(){418test_that('Did not create a plot named gwp_historical', {419expect_true(exists("gwp_historical"))420})421test_that('sqrt_year should be on the x-axis.', {422expect_that("sqrt_year" %in% c(rlang::get_expr(gwp_historical$mapping$x),rlang::get_expr(gwp_historical$layers[[1]]$mapping$x)), is_true())423})424test_that('gwp_value should be on the y-axis.', {425expect_that("gwp_value" %in% c(rlang::get_expr(gwp_historical$mapping$y), rlang::get_expr(gwp_historical$layers[[1]]$mapping$y)) , is_true())426})427test_that('gwp_historical should be a line plot.', {428expect_that("GeomLine" %in% c(class(gwp_historical$layers[[1]]$geom)) , is_true())429})430test_that('Labels on the axes should be descriptive and human readable.', {431expect_that((gwp_historical$labels$y) == 'gwp_value', is_false())432expect_that((gwp_historical$labels$x) == 'sqrt_year', is_false())433})434print("Success!")435}436437test_5.7 <- function(){438test_that('Solution is incorrect', {439expect_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 test440})441print("Success!")442}443444