Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2020-fall/materials/worksheet_09/tests_worksheet_09.R
2051 views
1
# +
2
library(testthat)
3
library(digest)
4
5
int_round <- function(x, digits){
6
x = x*10^digits
7
xint = as.integer(x)
8
xint1 = xint + 1
9
if (abs(xint - x) < abs(xint1 - x)){
10
return(xint)
11
}
12
else {
13
return(xint1)
14
}
15
}
16
# -
17
18
test_1.0 <- function() {
19
test_that('Did not create an object named answer1.0', {
20
expect_true(exists('answer1.0'))
21
})
22
test_that('Solution is incorrect', {
23
expect_equal(digest(answer1.0), '3a5505c06543876fe45598b5e5e5195d')
24
})
25
print("Success!")
26
}
27
28
test_1.1 <- function() {
29
test_that('Did not create an object named answer1.1', {
30
expect_true(exists('answer1.1'))
31
})
32
test_that('Solution is incorrect', {
33
expect_equal(digest(answer1.1), '475bf9280aab63a82af60791302736f6')
34
})
35
print("Success!")
36
}
37
38
test_1.2 <- function() {
39
test_that('Did not create an object named answer1.2', {
40
expect_true(exists('answer1.2'))
41
})
42
test_that('Solution is incorrect', {
43
expect_equal(digest(answer1.2), '75f1160e72554f4270c809f041c7a776')
44
})
45
print("Success!")
46
}
47
48
test_2.0 <- function() {
49
test_that('Did not create an object named answer2.0', {
50
expect_true(exists('answer2.0'))
51
})
52
test_that('Solution is incorrect', {
53
expect_equal(digest(int_round(answer2.0, 2)), '9a6564e67167bff7e7cf99a541a641f1')
54
})
55
print("Success!")
56
}
57
58
test_2.1 <- function() {
59
test_that('Did not create an object named answer2.1', {
60
expect_true(exists('answer2.1'))
61
})
62
test_that('Solution is incorrect', {
63
expect_equal(digest(int_round(answer2.1, 2)), '9a6564e67167bff7e7cf99a541a641f1')
64
})
65
print("Success!")
66
}
67
68
test_2.2 <- function() {
69
test_that('Did not create an object named answer2.2', {
70
expect_true(exists('answer2.2'))
71
})
72
test_that('Solution is incorrect', {
73
expect_equal(digest(int_round(answer2.2, 2)), 'd69e7827ff0b1272336c2136df42c3f0')
74
})
75
print("Success!")
76
}
77
78
test_2.3 <- function() {
79
test_that('Did not create an object named answer2.3', {
80
expect_true(exists('answer2.3'))
81
})
82
test_that('Solution is incorrect', {
83
expect_equal(digest(answer2.3), '475bf9280aab63a82af60791302736f6')
84
})
85
print("Success!")
86
}
87
88
test_3.0 <- function() {
89
test_that('Did not create an object named marathon', {
90
expect_true(exists("marathon"))
91
})
92
test_that('marathon should be a tibble.', {
93
expect_true('tbl' %in% class(marathon))
94
})
95
test_that('marathon does not contain the correct number of rows and/or columns.', {
96
expect_equal(dim(marathon), c(929, 13))
97
})
98
test_that('The marathon tibble does not contain the correct columns.', {
99
expect_true("time_hrs" %in% colnames(marathon))
100
expect_true("max" %in% colnames(marathon))
101
})
102
test_that('marathon contains the wrong data', {
103
expect_equal(digest(int_round(sum(marathon$max), 0)), 'b64d424699e3efa872a878b15e4615fc')
104
expect_equal(digest(int_round(sum(marathon$time_hrs), 0)), '0a386b4fbb992709ee886a69c311a49c')
105
})
106
print("Success!")
107
}
108
109
test_3.1 <- function() {
110
test_that('Did not create an object named marathon_split', {
111
expect_true(exists('marathon_split'))
112
})
113
test_that('marathon_split is not a split object (not a tibble)', {
114
expect_true('rsplit' %in% class(marathon_split))
115
})
116
test_that('marathon_split does not contain marathon data', {
117
expect_equal(dim(marathon_split$data), c(929, 13))
118
expect_equal(digest(int_round(sum(marathon_split$data$max), 0)), 'b64d424699e3efa872a878b15e4615fc')
119
expect_equal(digest(int_round(sum(marathon_split$data$time_hrs), 0)), '0a386b4fbb992709ee886a69c311a49c')
120
})
121
test_that('Did not create an object named marathon_training', {
122
expect_true(exists('marathon_training'))
123
})
124
test_that('marathon_training is not a tibble', {
125
expect_true('tbl' %in% class(marathon_training))
126
})
127
test_that('marathon_training does not contain 0.75 of the marathon data', {
128
expect_equal(dim(marathon_training), c(698, 13))
129
expect_equal(digest(int_round(sum(marathon_training$max), 0)), '6e85687c32809edf13dccf228f9f84e9')
130
expect_equal(digest(int_round(sum(marathon_training$time_hrs), 0)), '2213c3a0eb86305be22e0ca3b0a773c1')
131
})
132
test_that('Did not create an object named marathon_testing', {
133
expect_true(exists('marathon_testing'))
134
})
135
test_that('marathon_testing is not a tibble', {
136
expect_true('tbl' %in% class(marathon_testing))
137
})
138
test_that('marathon testing does not contain 0.25 of the marathon data', {
139
expect_equal(dim(marathon_testing), c(231, 13))
140
expect_equal(digest(int_round(sum(marathon_testing$max), 0)), 'e5032644f10cbf9a251aff4ed126d4af')
141
expect_equal(digest(int_round(sum(marathon_testing$time_hrs), 0)), 'ba825ab3d722243760b0700769f9371b')
142
})
143
print("Success!")
144
}
145
146
test_3.2 <- function() {
147
properties <- c(marathon_eda$layers[[1]]$mapping, marathon_eda$mapping)
148
labels <- marathon_eda$labels
149
layers <- marathon_eda$layers[[1]]
150
test_that('Did not create a plot named marathon_eda', {
151
expect_true(exists("marathon_eda"))
152
})
153
test_that('max should be on the x-axis.', {
154
expect_true("max" %in% c(rlang::get_expr(properties$x)))
155
})
156
test_that('time_hrs should be on the y-axis.', {
157
expect_true("time_hrs" %in% c(rlang::get_expr(properties$y)))
158
})
159
test_that('marathon_eda should be a scatter plot.', {
160
expect_equal(digest(class(rlang::get_expr(layers$geom))[1]), '911e5b9debfb523f25ad2ccc01a4b2dd')
161
})
162
test_that('Labels on the axes should be descriptive and human readable.', {
163
expect_false((labels$y) == 'time_hrs')
164
expect_false((labels$x) == 'max')
165
})
166
test_that('Only the training data set should be used to create the plot', {
167
expect_equal(int_round(nrow(marathon_eda$data), 0), 698)
168
})
169
print("Success!")
170
}
171
172
test_3.3 <- function() {
173
test_that('Did not create an object named lm_spec', {
174
expect_true(exists("lm_spec"))
175
})
176
test_that('lm_spec is not a linear regression model', {
177
expect_true('linear_reg' %in% class(lm_spec))
178
})
179
test_that('lm_spec does not contain the correct specifications', {
180
expect_equal(digest(as.character(lm_spec$mode)), 'b8bdd7015e0d1c6037512fd1396aef1a')
181
expect_equal(digest(as.character(lm_spec$engine)), '0995419f6f003f701c545d050292f42d')
182
})
183
print("Success!")
184
}
185
186
test_3.3.1 <- function() {
187
test_that('Did not create an object named lm_recipe', {
188
expect_true(exists('lm_recipe'))
189
})
190
test_that('lm_recipe is not a recipe', {
191
expect_true('recipe' %in% class(lm_recipe))
192
})
193
test_that('lm_recipe does not contain the correct variables', {
194
expect_equal(digest(int_round(sum(lm_recipe$template$max), 0)), '6e85687c32809edf13dccf228f9f84e9')
195
expect_equal(digest(int_round(sum(lm_recipe$template$time_hrs), 0)), '2213c3a0eb86305be22e0ca3b0a773c1')
196
})
197
test_that('Did not create an object named lm_fit', {
198
expect_true(exists('lm_fit'))
199
})
200
test_that('lm_fit is not a workflow', {
201
expect_true('workflow' %in% class(lm_fit))
202
})
203
test_that('lm_fit does not contain the correct data', {
204
expect_equal(digest(int_round(sum(lm_fit$pre$actions$recipe$recipe$template$max), 0)), '6e85687c32809edf13dccf228f9f84e9')
205
expect_equal(digest(int_round(sum(lm_fit$pre$actions$recipe$recipe$template$time_hrs), 0)), '2213c3a0eb86305be22e0ca3b0a773c1')
206
})
207
test_that('lm_fit coefficients are incorrect', {
208
expect_equal(digest(int_round(sum(lm_fit$fit$fit$fit$coefficients), 1)), '80b0ae73fe0e882b0a24973e4e2c8203')
209
})
210
print("Success!")
211
}
212
213
test_3.4 <- function() {
214
properties <- c(lm_predictions$layers[[1]]$mapping, lm_predictions$mapping)
215
labels <- lm_predictions$labels
216
layers <- c(lm_predictions$layers[[1]], lm_predictions$layers[[2]])
217
layers2 <- c(lm_predictions$layers[[2]], lm_predictions$layers[[1]])
218
test_that('Did not create a plot named lm_predictions', {
219
expect_true(exists("lm_predictions"))
220
})
221
test_that('max should be on the x-axis.', {
222
expect_true("max" %in% c(rlang::get_expr(properties$x)))
223
})
224
test_that('time_hrs should be on the y-axis.', {
225
expect_true("time_hrs" %in% c(rlang::get_expr(properties$y)))
226
})
227
test_that('lm_predictions should be a scatter plot.', {
228
expect_true('GeomPoint' %in% c(class(rlang::get_expr(lm_predictions$layers[[1]]$geom)),
229
class(rlang::get_expr(lm_predictions$layers[[2]]$geom))))
230
231
})
232
test_that('lm_predictions should have a best fit line using a linear regression model.', {
233
expect_true('GeomSmooth' %in% c(class(rlang::get_expr(lm_predictions$layers[[2]]$geom)),
234
class(rlang::get_expr(lm_predictions$layers[[1]]$geom))))
235
})
236
test_that('Labels on the axes should be descriptive and human readable.', {
237
expect_false((labels$y) == 'time_hrs')
238
expect_false((labels$x) == 'max')
239
})
240
print("Success!")
241
}
242
243
test_3.5 <- function() {
244
test_that('Did not create an object named lm_test_results', {
245
expect_true(exists('lm_test_results'))
246
})
247
test_that('lm_test_results is not a tibble', {
248
expect_true('tbl' %in% class(lm_test_results))
249
})
250
test_that('lm_test_results does not contain the correct data', {
251
expect_equal(dim(lm_test_results), c(3, 3))
252
expect_equal(digest(int_round(sum(lm_test_results$.estimate), 1)), 'a86d0670df7fb4f1da7b38943f5ee4e7')
253
})
254
test_that('Did not create an object named lm_rmspe', {
255
expect_true(exists('lm_rmspe'))
256
})
257
test_that('lm_rmspe is not a numeric', {
258
expect_true('numeric' %in% class(lm_rmspe))
259
})
260
test_that('lm_rmspe is not correct', {
261
expect_equal(digest(int_round(lm_rmspe, 1)), '25e6a154090e35101d7678d6f034353a')
262
})
263
print("Success!")
264
}
265
266
test_3.5.1 <- function() {
267
properties <- c(lm_predictions_test$mapping, lm_predictions_test$layers[[1]]$mapping)
268
labels <- lm_predictions_test$labels
269
layers <- lm_predictions_test$layers[[1]]
270
test_that('Did not create a plot named lm_predictions_test', {
271
expect_true(exists("lm_predictions_test"))
272
})
273
test_that('max should be on the x-axis.', {
274
expect_true("max" %in% c(rlang::get_expr(properties$x)))
275
})
276
test_that('time_hrs should be on the y-axis.', {
277
expect_true("time_hrs" %in% c(rlang::get_expr(properties$y)))
278
})
279
test_that('lm_predictions_test should be a scatter plot.', {
280
expect_equal(digest(class(rlang::get_expr(layers$geom))[1]), '911e5b9debfb523f25ad2ccc01a4b2dd')
281
})
282
test_that('Labels on the axes should be descriptive and human readable.', {
283
expect_false((labels$y) == 'time_hrs')
284
expect_false((labels$x) == 'max')
285
})
286
test_that('Only the testing data set should be used to create the plot', {
287
expect_equal(int_round(nrow(lm_predictions_test$data), 0), 231)
288
})
289
print("Success!")
290
}
291
292
test_3.6 <- function() {
293
test_that('Did not create an object named answer3.6', {
294
expect_true(exists('answer3.6'))
295
})
296
test_that('Solution is incorrect', {
297
expect_equal(digest(answer3.6), '75f1160e72554f4270c809f041c7a776')
298
})
299
print("Success!")
300
}
301
302
test_3.7 <- function() {
303
test_that('Did not create an object named answer3.7', {
304
expect_true(exists('answer3.7'))
305
})
306
test_that('Solution is incorrect', {
307
expect_equal(digest(answer3.7), '3a5505c06543876fe45598b5e5e5195d')
308
})
309
print("Success!")
310
}
311
312
test_3.8.1 <- function() {
313
test_that('Did not create an object named answer3.8.1', {
314
expect_true(exists('answer3.8.1'))
315
})
316
test_that('Solution is incorrect', {
317
expect_equal(digest(answer3.8.1), '75f1160e72554f4270c809f041c7a776')
318
})
319
print("Success!")
320
}
321
322