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_01/tests_tutorial_01.R
2051 views
1
library(testthat)
2
library(digest)
3
4
test_revision <- function(){
5
test_that('Solution is incorrect', {
6
expect_equal(digest(A), 'dbc09cba9fe2583fb01d63c70e1555a8') # we hid the answer to the test here so you can't see it, but we can still run the test
7
expect_equal(digest(B), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test)
8
expect_equal(digest(C), '0aee9b78301d7ec8998971363be87c03') # we hid the answer to the test here so you can't see it, but we can still run the test
9
expect_equal(digest(D), '5e338704a8e069ebd8b38ca71991cf94') # we hid the answer to the test here so you can't see it, but we can still run the test
10
expect_equal(digest(E), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test
11
expect_equal(digest(F), 'e5b57f323c7b3719bbaaf9f96b260d39') # we hid the answer to the test here so you can't see it, but we can still run the test
12
})
13
print("Success!")
14
}
15
16
test_1.1 <- function(){
17
test_that('Solution is incorrect', {
18
expect_equal(digest(answer1), '75f1160e72554f4270c809f041c7a776') # we hid the answer to the test here so you can't see it, but we can still run the test
19
})
20
print("Success!")
21
}
22
23
test_1.2 <- function(){
24
test_that('Solution is incorrect', {
25
expect_equal(digest(answer2), 'd2a90307aac5ae8d0ef58e2fe730d38b') # we hid the answer to the test here so you can't see it, but we can still run the test
26
})
27
print("Success!")
28
}
29
30
test_1.3 <- function(){
31
test_that('Solution is incorrect', {
32
expect_equal(digest(answer3), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test
33
})
34
print("Success!")
35
}
36
37
test_1.4 <- function(){
38
test_that('The tidyverse package needs to be loaded.', {
39
expect_that("package:tidyverse" %in% search() , is_true())
40
})
41
print("Success!")
42
}
43
44
test_1.5 <- function(){
45
test_that('Did not create an object named marathon_small.', {
46
expect_true(exists("marathon_small"))
47
})
48
test_that('marathon_small should be a data frame.', {
49
expect_true('data.frame' %in% class(marathon_small))
50
})
51
test_that('marathon_small does not contain the correct data.', {
52
expect_equal(dim(marathon_small), c(1833, 5))
53
expect_equal(sum(marathon_small$age), 66455.5)
54
expect_equal(colnames(marathon_small), c("age", "bmi", "km5_time_seconds", "km10_time_seconds", "sex"))
55
})
56
print("Success!")
57
}
58
59
test_1.6 <- function(){
60
test_that('Did not create an object named marathon_age.', {
61
expect_true(exists("marathon_age"))
62
})
63
test_that('Did not create an object named marathon_select.', {
64
expect_true(exists("marathon_select"))
65
})
66
test_that('marathon_age does not contain the correct number of rows and/or columns.', {
67
expect_equal(dim(marathon_age), c(922, 5))
68
})
69
test_that('marathon_select does not contain the correct number of rows and/or columns.', {
70
expect_equal(dim(marathon_select), c(922, 2))
71
})
72
test_that('Columns in marathon_select contain incorrect values.', {
73
expect_equal(digest(as.integer(sum(marathon_select$bmi))), '26b0ef5c0ff9f596d71bec8beb2fd961') # we hid the answer to the test here so you can't see it, but we can still run the test
74
expect_equal(digest(as.integer(sum(marathon_select$km5_time_seconds, na.rm = TRUE))), '34a5cc5cac5c7e94d58a8de9c1d25d2a') # we hid the answer to the test here so you can't see it, but we can still run the test
75
})
76
print("Success!")
77
}
78
79
test_1.7 <- function(){
80
test_that('Did not create an object named marathon_mutate.', {
81
expect_true(exists("marathon_mutate"))
82
})
83
test_that('Did not create an object named marathon_exact.', {
84
expect_true(exists("marathon_exact"))
85
})
86
test_that('marathon_mutate does not contain the correct number of rows and/or columns.', {
87
expect_equal(dim(marathon_mutate), c(922, 3))
88
})
89
test_that('marathon_exact does not contain the correct number of rows and/or columns.', {
90
expect_equal(dim(marathon_exact), c(922, 2))
91
})
92
test_that('Columns in marathon_exact contain incorrect values.', {
93
expect_equal(digest(as.integer(sum(marathon_exact$bmi))), '26b0ef5c0ff9f596d71bec8beb2fd961') # we hid the answer to the test here so you can't see it, but we can still run the test
94
expect_equal(digest(as.integer(sum(marathon_exact$km5_time_minutes, na.rm = TRUE))), '2d07e1d5bee213ac567286a85e4e74af') # we hid the answer to the test here so you can't see it, but we can still run the test
95
})
96
print("Success!")
97
}
98
99
test_1.8 <- function(){
100
test_that('Did not create a plot named marathon_plot', {
101
expect_true(exists("marathon_plot"))
102
})
103
test_that('bmi should be on the x-axis.', {
104
expect_that("bmi" %in% c(rlang::get_expr(marathon_plot$mapping$x),rlang::get_expr(marathon_plot$layers[[1]]$mapping$x)), is_true())
105
})
106
test_that('km5_time_minutes should be on the y-axis.', {
107
expect_that("km5_time_minutes" %in% c(rlang::get_expr(marathon_plot$mapping$y), rlang::get_expr(marathon_plot$layers[[1]]$mapping$y)) , is_true())
108
})
109
test_that('marathon_plot should be a scatter plot.', {
110
expect_that("GeomPoint" %in% c(class(marathon_plot$layers[[1]]$geom)) , is_true())
111
})
112
test_that('Labels on the axes should be descriptive and human readable.', {
113
expect_that((marathon_plot$labels$y) == 'km5_time_minutes', is_false())
114
expect_that((marathon_plot$labels$x) == 'bmi', is_false())
115
})
116
print("Success!")
117
}
118
119
test_1.10 <- function(){
120
test_that('Did not create a plot named age_vs_time', {
121
expect_true(exists("age_vs_time"))
122
})
123
test_that('age should be on the x-axis.', {
124
expect_that("age" %in% c(rlang::get_expr(age_vs_time$mapping$x),rlang::get_expr(age_vs_time$layers[[1]]$mapping$x)), is_true())
125
})
126
test_that('km5_time_minutes should be on the y-axis.', {
127
expect_that("km5_time_minutes" %in% c(rlang::get_expr(age_vs_time$mapping$y), rlang::get_expr(age_vs_time$layers[[1]]$mapping$y)) , is_true())
128
})
129
test_that('age_vs_time should be a scatter plot.', {
130
expect_that("GeomPoint" %in% c(class(age_vs_time$layers[[1]]$geom)) , is_true())
131
})
132
test_that('Labels on the axes should be descriptive and human readable.', {
133
expect_that((age_vs_time$labels$y) == 'km5_time_minutes', is_false())
134
expect_that((age_vs_time$labels$x) == 'age', is_false())
135
})
136
print("Success!")
137
}
138
139
test_2.1 <- function(){
140
test_that('Solution is incorrect', {
141
expect_equal(digest(answer2.1), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test
142
})
143
print("Success!")
144
}
145
146
test_2.2 <- function(){
147
test_that('Solution is incorrect', {
148
expect_equal(digest(answer2.2), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test
149
})
150
print("Success!")
151
}
152
153
test_2.3 <- function(){
154
test_that('Did not create an object named bike_data.', {
155
expect_true(exists("bike_data"))
156
})
157
test_that('bike_data should be a data frame.', {
158
expect_true('data.frame' %in% class(bike_data))
159
})
160
test_that('bike_data does not contain the correct information.', {
161
expect_equal(dim(bike_data), c(731, 4))
162
expect_equal(sum(bike_data$casual_users), 620017)
163
expect_equal(colnames(bike_data), c("temperature", "casual_users", "registered_users", "season"))
164
})
165
print("Success!")
166
}
167
168
test_2.4 <- function(){
169
test_that('Did not create an object named bike_mutate.', {
170
expect_true(exists("bike_mutate"))
171
})
172
test_that('bike_mutate does not contain the correct number of rows and/or columns.', {
173
expect_equal(dim(bike_mutate), c(731, 5))
174
})
175
test_that('Columns in bike_mutate contain incorrect values.', {
176
expect_equal(digest(as.integer(sum(bike_mutate$total_users))), 'ca696c077151dc0a05b3e3862ab38f52') # we hid the answer to the test here so you can't see it, but we can still run the test
177
expect_equal(digest(as.integer(sum(bike_mutate$temperature, na.rm = TRUE))), '01a2384f7d878a7355399ff2517925cc') # we hid the answer to the test here so you can't see it, but we can still run the test
178
})
179
print("Success!")
180
}
181
182
test_2.5 <- function(){
183
test_that('Did not create an object named bike_filter.', {
184
expect_true(exists("bike_filter"))
185
})
186
test_that('The season column in bike_filter should only contain Spring.', {
187
expect_equal(unique(bike_filter$season), "Spring")
188
})
189
test_that('bike_filter does not contain the correct number of rows and/or columns.', {
190
expect_equal(dim(bike_filter), c(181, 5))
191
})
192
test_that('Columns in bike_filter contain incorrect values.', {
193
expect_equal(digest(as.integer(sum(bike_filter$total_users))), '051a1e8b9293438bbc0cb8ed6fa4e959') # we hid the answer to the test here so you can't see it, but we can still run the test
194
expect_equal(digest(as.integer(sum(bike_filter$temperature, na.rm = TRUE))), '15865a3cff7594d62b7897f23fb27fa8') # we hid the answer to the test here so you can't see it, but we can still run the test
195
})
196
print("Success!")
197
}
198
199
test_3.1 <- function(){
200
test_that('Solution is incorrect', {
201
expect_equal(digest(answer_filter), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test
202
})
203
print("Success!")
204
}
205