Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2020-fall/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.1), '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(answer1.2), '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(answer1.3), '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('read.csv() used instead of read_csv()', {
49
expect_true(class(marathon_small$sex) == "character")
50
})
51
test_that('marathon_small should be a data frame.', {
52
expect_true('data.frame' %in% class(marathon_small))
53
})
54
test_that('marathon_small does not contain the correct data.', {
55
expect_equal(dim(marathon_small), c(1833, 5))
56
expect_equal(digest(as.integer(sum(marathon_small$age))), 'd3eeb781803aac1dd87dd166bfddf422')
57
expect_equal(colnames(marathon_small), c("age", "bmi", "km5_time_seconds", "km10_time_seconds", "sex"))
58
})
59
print("Success!")
60
}
61
62
test_1.6 <- function(){
63
test_that('Did not create an object named marathon_age.', {
64
expect_true(exists("marathon_age"))
65
})
66
test_that('Did not create an object named marathon_select.', {
67
expect_true(exists("marathon_select"))
68
})
69
test_that('marathon_age does not contain the correct number of rows and/or columns.', {
70
expect_equal(dim(marathon_age), c(922, 5))
71
})
72
test_that('marathon_select does not contain the correct number of rows and/or columns.', {
73
expect_equal(dim(marathon_select), c(922, 2))
74
})
75
test_that('Columns in marathon_select contain incorrect values.', {
76
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
77
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
78
})
79
print("Success!")
80
}
81
82
test_1.7 <- function(){
83
test_that('Did not create an object named marathon_mutate.', {
84
expect_true(exists("marathon_mutate"))
85
})
86
test_that('Did not create an object named marathon_exact.', {
87
expect_true(exists("marathon_exact"))
88
})
89
test_that('marathon_mutate does not contain the correct number of rows and/or columns.', {
90
expect_equal(dim(marathon_mutate), c(922, 3))
91
})
92
test_that('marathon_exact does not contain the correct number of rows and/or columns.', {
93
expect_equal(dim(marathon_exact), c(922, 2))
94
})
95
test_that('Columns in marathon_exact contain incorrect values.', {
96
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
97
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
98
})
99
print("Success!")
100
}
101
102
test_1.8 <- function(){
103
test_that('Did not create a plot named marathon_plot', {
104
expect_true(exists("marathon_plot"))
105
})
106
107
properties <- c(marathon_plot$layers[[1]]$mapping, marathon_plot$mapping)
108
test_that('bmi should be on the x-axis.', {
109
expect_true("bmi" == rlang::get_expr(properties$x))
110
})
111
test_that('km5_time_minutes should be on the y-axis.', {
112
expect_true("km5_time_minutes" == rlang::get_expr(properties$y))
113
})
114
test_that('marathon_plot should be a scatter plot.', {
115
expect_true("GeomPoint" %in% c(class(marathon_plot$layers[[1]]$geom)))
116
})
117
test_that('Labels on the axes should be descriptive and human readable.', {
118
expect_false(marathon_plot$labels$y == 'km5_time_minutes')
119
expect_false(marathon_plot$labels$x == 'bmi')
120
})
121
print("Success!")
122
}
123
124
test_1.10 <- function(){
125
test_that('Did not create a plot named age_vs_time', {
126
expect_true(exists("age_vs_time"))
127
})
128
test_that('Did not create a data frame named marathon_small_mins', {
129
expect_true(exists("marathon_small_mins"))
130
})
131
132
properties <- c(age_vs_time$layers[[1]]$mapping, age_vs_time$mapping)
133
test_that('age should be on the x-axis.', {
134
expect_true("age" == rlang::get_expr(properties$x))
135
})
136
test_that('km5_time_minutes should be on the y-axis.', {
137
expect_true("km5_time_minutes" == rlang::get_expr(properties$y))
138
})
139
test_that('age_vs_time should be a scatter plot.', {
140
expect_true("GeomPoint" %in% c(class(age_vs_time$layers[[1]]$geom)))
141
})
142
test_that('Labels on the axes should be descriptive and human readable.', {
143
expect_false(age_vs_time$labels$y == 'km5_time_minutes')
144
# expect_false(age_vs_time$labels$x == 'age') # removed since 'age' is human-readable
145
})
146
print("Success!")
147
}
148
149
test_2.1 <- function(){
150
test_that('Solution is incorrect', {
151
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
152
})
153
print("Success!")
154
}
155
156
test_2.2 <- function(){
157
test_that('Solution is incorrect', {
158
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
159
})
160
print("Success!")
161
}
162
163
test_2.3 <- function(){
164
test_that('Did not create an object named bike_data.', {
165
expect_true(exists("bike_data"))
166
})
167
test_that('bike_data should be a data frame.', {
168
expect_true('data.frame' %in% class(bike_data))
169
})
170
test_that('bike_data does not contain the correct information.', {
171
expect_equal(dim(bike_data), c(731, 4))
172
expect_equal(digest(as.integer(sum(bike_data$casual_users))), "037ce752369c998dc005b8f088a0cb35")
173
expect_equal(colnames(bike_data), c("temperature", "casual_users", "registered_users", "season"))
174
})
175
test_that('read.csv() used instead of read_csv()', {
176
expect_true(class(bike_data$season) == "character")
177
})
178
print("Success!")
179
}
180
181
test_2.4 <- function(){
182
test_that('Did not create an object named bike_mutate.', {
183
expect_true(exists("bike_mutate"))
184
})
185
test_that('bike_mutate does not contain the correct number of rows and/or columns.', {
186
expect_equal(dim(bike_mutate), c(731, 5))
187
})
188
test_that('Columns in bike_mutate contain incorrect values.', {
189
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
190
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
191
})
192
print("Success!")
193
}
194
195
test_2.5 <- function(){
196
test_that('Did not create an object named bike_filter.', {
197
expect_true(exists("bike_filter"))
198
})
199
test_that('The season column in bike_filter should only contain Spring.', {
200
expect_equal(unique(bike_filter$season), "Spring")
201
})
202
test_that('bike_filter does not contain the correct number of rows and/or columns.', {
203
expect_equal(dim(bike_filter), c(181, 5))
204
})
205
test_that('Columns in bike_filter contain incorrect values.', {
206
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
207
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
208
})
209
print("Success!")
210
}
211
212
test_3.1 <- function(){
213
test_that('Solution is incorrect', {
214
expect_equal(digest(answer3.1), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test
215
})
216
print("Success!")
217
}
218