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