Path: blob/master/2020-spring/materials/tutorial_11/tests_tutorial_11.R
2051 views
library(testthat)1library(digest)23test_1.0 <- function(){4properties <- c(pop_dist$layers[[1]]$mapping, pop_dist$mapping)5test_that('grade should be on the x-axis.', {6expect_true("grade" == rlang::get_expr(properties$x))7})8test_that('pop_dist should be a histogram.', {9expect_that("GeomBar" %in% class(pop_dist$layers[[1]]$geom), is_true())10})11test_that('students_pop data should be used to create the histogram', {12expect_equal(nrow(pop_dist$data), 10000)13expect_equal(round(as.numeric(sum(pop_dist$data))), 700329)14})15test_that('Labels on the x axis should be descriptive and human readable.', {16expect_that((pop_dist$labels$x) == 'grade', is_false())17})18print("Success!")19}2021test_1.2 <- function(){22test_that('pop_parameters has 3 columns and one row, with column names pop_mean, pop_med and pop_sd.', {23expect_equal(nrow(pop_parameters), 1)24expect_equal(ncol(pop_parameters), 3)25expect_equal(digest(round(pop_parameters$pop_mean,1)), '2a560fd9a59ef8af4b1f1b40af6ab40d')26expect_equal(digest(round(pop_parameters$pop_sd,1)), '03fd1d869a1a7fb2c286fa4568b080fe')27})28print("Success!")29}3031test_1.3 <- function(){32test_that('samples should have 7500 rows and 2 columns', {33expect_equal(ncol(samples), 2)34expect_equal(nrow(samples), 7500)35})36test_that('the column names of samples should be replicate and grade', {37expect_equal(digest(paste(sort(colnames(samples)), collapse = "")), '0454d7f37ea4f0b0109a37b637be0481')38})39print("Success!")40}4142test_1.4 <- function(){43test_that('sample_estimates should have 1500 rows and 2 columns', {44expect_equal(ncol(sample_estimates), 2)45expect_equal(nrow(sample_estimates), 1500)46})47test_that('the column names of sample_estimates should be replicate and sample_mean', {48expect_equal(digest(paste(sort(colnames(sample_estimates)), collapse = "")), '7453089f8086e9a98a067f3eeac63363')49})50print("Success!")51}5253test_1.5 <- function(){54properties <- c(sampling_distribution_5$layers[[1]]$mapping, sampling_distribution_5$mapping)55test_that('sample_mean should be on the x-axis.', {56expect_true("sample_mean" == rlang::get_expr(properties$x))57})58test_that('sampling_distribution_5 should be a histogram.', {59expect_that("GeomBar" %in% class(sampling_distribution_5$layers[[1]]$geom), is_true())60})61test_that('sampling_distribution data should be used to create the histogram', {62expect_equal(nrow(sampling_distribution_5$data), 1500)63expect_equal(round(as.numeric(sum(sampling_distribution_5$data))), 1230616)64})65test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {66expect_that((sampling_distribution_5$labels$x) == 'age', is_false())67expect_false(is.null(sampling_distribution_5$labels$title))68})69print("Success!")70}7172test_1.8 <- function(){73properties <- c(sampling_distribution_5$layers[[1]]$mapping, sampling_distribution_5$mapping)74test_that('sample_mean should be on the x-axis.', {75expect_true("sample_mean" == rlang::get_expr(properties$x))76})77test_that('sampling_distribution_5 should be a histogram.', {78expect_that("GeomBar" %in% class(sampling_distribution_5$layers[[1]]$geom), is_true())79})80test_that('sampling_distribution data should be used to create the histogram', {81expect_equal(nrow(sampling_distribution_5$data), 1500)82expect_equal(round(as.numeric(sum(sampling_distribution_5$data))), 1129548)83})84test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {85expect_that((sampling_distribution_5$labels$x) == 'cups', is_false())86expect_false(is.null(sampling_distribution_5$labels$title))87})88print("Success!")89}9091test_2.0 <- function(){92properties <- c(sampling_distribution_30$layers[[1]]$mapping, sampling_distribution_30$mapping)93test_that('sample_mean should be on the x-axis.', {94expect_true("sample_mean" == rlang::get_expr(properties$x))95})96test_that('sampling_distribution_30 should be a histogram.', {97expect_that("GeomBar" %in% class(sampling_distribution_30$layers[[1]]$geom), is_true())98})99test_that('sampling_distribution_30 data should be used to create the histogram', {100expect_equal(nrow(sampling_distribution_30$data), 1500)101expect_equal(round(as.numeric(sum(sampling_distribution_30$data))), 1129466)102})103test_that('Labels on the x axis should be descriptive. The plot should have a descriptive title.', {104expect_that((sampling_distribution_30$labels$x) == 'age', is_false())105expect_false(is.null(sampling_distribution_30$labels$title))106})107print("Success!")108}109110