Path: blob/master/2019-fall/materials/worksheet_11/tests_worksheet_11.R
2051 views
library(testthat)1library(digest)23test_1.0 <- function(){4test_that('Solution is incorrect', {5expect_equal(digest(answer1.0), '3a5505c06543876fe45598b5e5e5195d')6})7print("Success!")8}910test_1.1 <- function(){11test_that('variables should be assigned numeric values (do not include the definition in your answer, just the number associated with the definition)', {12values <- c(point_estimate, population, random_sampling, representative_sampling, population_quantity, sample, observation, sampling_distribution)13expect_is(values, 'numeric')14})1516test_that('At least one term-definition match is incorrect', {17expect_equal(digest(point_estimate), '0aee9b78301d7ec8998971363be87c03')18expect_equal(digest(population), '6717f2823d3202449301145073ab8719')19expect_equal(digest(random_sampling), 'db8e490a925a60e62212cefc7674ca02')20expect_equal(digest(representative_sampling), '90a7653d717dc1553ee564aa27b749b9')21expect_equal(digest(population_quantity), 'e5b57f323c7b3719bbaaf9f96b260d39')22expect_equal(digest(sample), '5e338704a8e069ebd8b38ca71991cf94')23expect_equal(digest(observation), '4a5d7d50676e6d0ea065f445d8a5539d')24expect_equal(digest(sampling_distribution), 'dbc09cba9fe2583fb01d63c70e1555a8')25})26print("Success!")27}2829test_1.2 <- function(){30properties <- c(pop_dist$layers[[1]]$mapping, pop_dist$mapping)31test_that('age should be on the x-axis.', {32expect_true("age" == rlang::get_expr(properties$x))33})34test_that('pop_dist should be a histogram.', {35expect_that("GeomBar" %in% class(pop_dist$layers[[1]]$geom), is_true())36})37test_that('can_seniors data should be used to create the histogram', {38expect_equal(nrow(pop_dist$data), 1027941)39expect_equal(round(as.numeric(sum(pop_dist$data))), 81516771)40})41test_that('Labels on the x axis should be descriptive and human readable.', {42expect_that((pop_dist$labels$x) == 'age', is_false())43})44print("Success!")45}4647test_1.3 <- function(){48test_that('pop_quantities has 3 columns and one row, with column names pop_mean, pop_med and pop_sd.', {49expect_equal(nrow(pop_quantities), 1)50expect_equal(ncol(pop_quantities), 3)51expect_equal(digest(paste(sort(colnames(pop_quantities)), collapse = "")), '723d282ea6dad216da6b1074ca7cf688')52})53print("Success!")54}5556test_1.4 <- function(){57test_that('sample_1 should have 2 columns and 40 rows', {58expect_equal(nrow(sample_1), 40)59expect_equal(ncol(sample_1), 2)60})61test_that('the column names of sample_1 should be replicate and age', {62expect_equal(digest(paste(sort(colnames(sample_1)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')63})64print("Success!")65}6667test_1.5 <- function(){68properties <- c(sample_1_dist$layers[[1]]$mapping, sample_1_dist$mapping)69test_that('age should be on the x-axis.', {70expect_true("age" == rlang::get_expr(properties$x))71})72test_that('sample_1_dist should be a histogram.', {73expect_that("GeomBar" %in% class(sample_1_dist$layers[[1]]$geom), is_true())74})75test_that('sample_1 data should be used to create the histogram', {76expect_equal(nrow(sample_1_dist$data), 40)77expect_equal(round(as.numeric(sum(sample_1_dist$data))), 3265)78})79test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {80expect_that((sample_1_dist$labels$x) == 'age', is_false())81expect_false(is.null(sample_1_dist$labels$title))82})83print("Success!")84}8586test_1.6 <- function(){87test_that('sample_1_estimates should have at least 3 columns, and 1 row', {88expect_equal(nrow(sample_1_estimates), 1)89expect_true(ncol(sample_1_estimates) >= 3)90})91test_that('sample_1_estimates has columns with correct names', {92expect_true("sample_1_mean" %in% colnames(sample_1_estimates))93expect_true("sample_1_med" %in% colnames(sample_1_estimates))94expect_true("sample_1_sd" %in% colnames(sample_1_estimates))95})96print("Success!")97}9899test_1.7 <- function(){100test_that('Solution is incorrect', {101expect_equal(digest(answer1.7), '475bf9280aab63a82af60791302736f6')102})103print("Success!")104}105106test_1.8.1 <- function(){107test_that('Solution is incorrect', {108expect_equal(digest(answer1.8.1), '475bf9280aab63a82af60791302736f6')109})110print("Success!")111}112113test_1.9 <- function(){114test_that('samples should have 60000 rows and 2 columns', {115expect_equal(ncol(samples), 2)116expect_equal(nrow(samples), 60000)117})118test_that('the column names of samples should be replicate and age', {119expect_equal(digest(paste(sort(colnames(samples)), collapse = "")), 'f4f0b2eff0a0eb0d22ac4df99afd13b7')120})121print("Success!")122}123124test_2.0 <- function(){125test_that('sample_estimates should have 1500 rows and 2 columns', {126expect_equal(ncol(sample_estimates), 2)127expect_equal(nrow(sample_estimates), 1500)128})129test_that('the column names of sample_estimates should be replicate and sample_mean', {130expect_equal(digest(paste(sort(colnames(sample_estimates)), collapse = "")), '7453089f8086e9a98a067f3eeac63363')131})132print("Success!")133}134135test_2.1 <- function(){136properties <- c(sampling_distribution$layers[[1]]$mapping, sampling_distribution$mapping)137test_that('sample_mean should be on the x-axis.', {138expect_true("sample_mean" == rlang::get_expr(properties$x))139})140test_that('sampling_distribution should be a histogram.', {141expect_that("GeomBar" %in% class(sampling_distribution$layers[[1]]$geom), is_true())142})143test_that('sampling_distribution data should be used to create the histogram', {144expect_equal(nrow(sampling_distribution$data), 1500)145expect_equal(round(as.numeric(sum(sampling_distribution$data))), 1244931)146})147test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {148expect_that((sampling_distribution$labels$x) == 'age', is_false())149expect_false(is.null(sampling_distribution$labels$title))150})151print("Success!")152}153154test_2.2 <- function(){155test_that('Solution is incorrect', {156expect_equal(digest(round(answer2.2)), '49df39a554b7960907ee669ebb6c2071')157})158print("Success!")159}160161test_2.3 <- function(){162test_that('Solution is incorrect', {163expect_equal(digest(answer2.3), '3a5505c06543876fe45598b5e5e5195d')164})165print("Success!")166}167168test_2.4 <- function(){169test_that('Solution is incorrect', {170expect_equal(digest(tolower(answer2.4)), '05ca18b596514af73f6880309a21b5dd')171})172print("Success!")173}174175test_2.5 <- function(){176properties <- c(sampling_distribution_20$layers[[1]]$mapping, sampling_distribution_20$mapping)177test_that('sample_mean should be on the x-axis.', {178expect_true("sample_mean" == rlang::get_expr(properties$x))179})180test_that('sampling_distribution should be a histogram.', {181expect_that("GeomBar" %in% class(sampling_distribution_20$layers[[1]]$geom), is_true())182})183test_that('sampling_distribution data should be used to create the histogram', {184expect_equal(nrow(sampling_distribution_20$data), 1500)185expect_equal(round(as.numeric(sum(sampling_distribution_20$data))), 1244837)186})187test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {188expect_that((sampling_distribution_20$labels$x) == 'age', is_false())189expect_equal(sampling_distribution_20$labels$title, "n = 20")190})191192print("Success!")193}194195test_2.6 <- function(){196properties <- c(sampling_distribution_100$layers[[1]]$mapping, sampling_distribution_100$mapping)197test_that('sample_mean should be on the x-axis.', {198expect_true("sample_mean" == rlang::get_expr(properties$x))199})200test_that('sampling_distribution should be a histogram.', {201expect_that("GeomBar" %in% class(sampling_distribution_100$layers[[1]]$geom), is_true())202})203test_that('sampling_distribution data should be used to create the histogram', {204expect_equal(nrow(sampling_distribution_100$data), 1500)205expect_equal(round(as.numeric(sum(sampling_distribution_100$data))), 1244833)206})207test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {208expect_that((sampling_distribution_100$labels$x) == 'age', is_false())209expect_equal(sampling_distribution_100$labels$title, "n = 100")210})211212print("Success!")213}214215test_2.8 <- function(){216test_that('Solution is incorrect', {217expect_equal(digest(answer2.8), 'c1f86f7430df7ddb256980ea6a3b57a4')218})219print("Success!")220}221222test_2.9 <- function(){223test_that('Solution is incorrect', {224expect_equal(digest(tolower(answer2.9)), 'd2a90307aac5ae8d0ef58e2fe730d38b')225})226print("Success!")227}228229