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_06/tests_worksheet_06.R
2710 views
1
# +
2
library(testthat)
3
library(digest)
4
library(rlang)
5
6
int_round <- function(x, digits){
7
x = x*10^digits
8
xint = as.integer(x)
9
xint1 = xint + 1L
10
if (abs(xint - x) < abs(xint1 - x)){
11
return(xint)
12
}
13
else {
14
return(xint1)
15
}
16
}
17
# -
18
19
test_0.1 <- function(){
20
test_that('Solution is incorrect', {
21
expect_equal(digest(answer0.1), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test
22
})
23
print("Success!")
24
}
25
26
test_0.2 <- function(){
27
test_that('Solution is incorrect', {
28
expect_equal(digest(answer0.2), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test
29
})
30
print("Success!")
31
}
32
33
test_1.0 <- function(){
34
test_that('Did not create an object named cancer', {
35
expect_true(exists("cancer"))
36
})
37
test_that('cancer should be a data frame.', {
38
expect_true('data.frame' %in% class(cancer))
39
})
40
test_that('cancer does not contain the correct number of rows and/or columns.', {
41
expect_equal(dim(cancer), c(569, 12))
42
})
43
test_that('cancer does not contain the correct data.', {
44
expect_equal(digest(int_round(sum(cancer$Area), 2)), '1473d70e5646a26de3c52aa1abd85b1f')
45
expect_equal(colnames(cancer), c("ID", "Class", "Radius", "Texture", "Perimeter", "Area", "Smoothness", "Compactness", "Concavity", "Concave_points", "Symmetry", "Fractal_dimension"))
46
})
47
test_that('read.csv() instead of read_csv() function is used.', {
48
expect_true(class(cancer$Class) == 'character')
49
})
50
print("Success!")
51
}
52
53
test_1.1 <- function(){
54
test_that('Solution is incorrect', {
55
expect_equal(digest(answer1.1), 'd2a90307aac5ae8d0ef58e2fe730d38b') # we hid the answer to the test here so you can't see it, but we can still run the test
56
})
57
print("Success!")
58
}
59
60
test_1.2 <- function(){
61
properties <- c(cancer_plot$layers[[1]]$mapping, cancer_plot$mapping)
62
labels <- cancer_plot$labels
63
test_that('Did not create a plot named cancer_plot', {
64
expect_true(exists("cancer_plot"))
65
})
66
test_that('Symmetry should be on the x-axis.', {
67
expect_true("Symmetry" == rlang::get_expr(properties$x))
68
})
69
test_that('Radius should be on the y-axis.', {
70
expect_true("Radius" == rlang::get_expr(properties$y))
71
})
72
test_that('cancer_plot should be a scatter plot.', {
73
expect_true("GeomPoint" %in% class(cancer_plot$layers[[1]]$geom))
74
})
75
test_that('cancer_plot should map Class to colour.', {
76
expect_true(digest(rlang::get_expr(properties$colour)) %in% c('a4abb3d43fde633563dd1f5c3ea31f31', 'f9e884084b84794d762a535f3facec85'))
77
})
78
print("Success!")
79
}
80
81
test_1.3 <- function(){
82
test_that('Solution is incorrect', {
83
expect_equal(digest(answer1.3), '891e8a631267b478c03e25594808709d') # we hid the answer to the test here so you can't see it, but we can still run the test
84
})
85
print("Success!")
86
}
87
88
test_1.4 <- function(){
89
test_that('xa is incorrect.', {
90
expect_equal(digest(int_round(xa, 2)), 'c0048c4f8677b795155d8aa41e26a54d')
91
})
92
test_that('ya is incorrect.', {
93
expect_equal(digest(int_round(ya, 2)), 'a6e8462a7cace5673e544d1e8d238b52')
94
})
95
test_that('xb is incorrect.', {
96
expect_equal(digest(int_round(xb, 2)), '10aeddd8594c6ce210c731b8b94af435')
97
})
98
test_that('yb is incorrect.', {
99
expect_equal(digest(int_round(yb, 2)), '48139aad2994737e7e801156a24281ed')
100
})
101
print("Success!")
102
}
103
104
test_1.5 <- function(){
105
test_that('answer1.5 is incorrect', {
106
expect_equal(digest(int_round(answer1.5, 2)), 'a95ceee8390cb47bb05410a8d23c76cf') # we hid the answer to the test here so you can't see it, but we can still run the test
107
})
108
print("Success!")
109
}
110
111
test_1.6 <- function(){
112
test_that('zb is incorrect.', {
113
expect_equal(digest(int_round(zb,2)), 'b78a46ebc0bb9a4cc7f4f4b962f0b2ef')
114
})
115
test_that('za is incorrect.', {
116
expect_equal(digest(int_round(za,2)), 'b35d8adab2b7c839e5a8e2861080b03e')
117
})
118
print("Success!")
119
}
120
121
test_1.7 <- function(){
122
test_that('answer1.7 is incorrect', {
123
expect_equal(digest(int_round(answer1.7, 2)), 'c7fd80062a02f15d212704a20fae75fb') # we hid the answer to the test here so you can't see it, but we can still run the test
124
})
125
print("Success!")
126
}
127
128
test_1.8 <- function(){
129
test_that('point_a is incorrect.', {
130
expect_equal(digest(int_round(sum((point_a)),2)), '44014eaa19f1aef8e92b1020c47d662b')
131
})
132
test_that('point_b is incorrect.', {
133
expect_equal(digest(int_round(sum((point_b)),2)), 'e064b40c9ca28b04b874bcd8bdefa41e')
134
})
135
print("Success!")
136
}
137
138
test_1.09 <- function(){
139
test_that('dif_square is incorrect', {
140
expect_equal(digest(int_round(sum(dif_square),2)), 'e276884e43714ac361db1a1998bb6bc9') # we hid the answer to the test here so you can't see it, but we can still run the test
141
})
142
print("Success!")
143
}
144
145
146
test_1.09.1 <- function(){
147
test_that('dif_sum is incorrect', {
148
expect_equal(digest(int_round(dif_sum,2)), 'e276884e43714ac361db1a1998bb6bc9') # 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_1.09.2 <- function(){
154
test_that('root_dif_sum is incorrect', {
155
expect_equal(digest(int_round(root_dif_sum,2)), 'c7fd80062a02f15d212704a20fae75fb') # we hid the answer to the test here so you can't see it, but we can still run the test
156
})
157
print("Success!")
158
}
159
160
test_1.09.3 <- function(){
161
test_that('dist_cancer_two_rows is incorrect', {
162
expect_equal(digest(int_round(dist_cancer_two_rows,2)), 'c7fd80062a02f15d212704a20fae75fb') # we hid the answer to the test here so you can't see it, but we can still run the test
163
164
})
165
print("Success!")
166
}
167
168
169
test_1.09.4 <- function(){
170
test_that('Solution is incorrect', {
171
expect_equal(digest(answer1.09.4), '05ca18b596514af73f6880309a21b5dd') # we hid the answer to the test here so you can't see it, but we can still run the test
172
173
})
174
print("Success!")
175
}
176
177
test_2.0.0 <- function(){
178
test_that('Did not create an object named small_sample', {
179
expect_true(exists("small_sample"))
180
})
181
test_that('small_sample should be a data frame.', {
182
expect_true('data.frame' %in% class(small_sample))
183
})
184
test_that('small_sample does not contain the correct number of rows and/or columns.', {
185
expect_equal(dim(small_sample), c(5, 3))
186
})
187
test_that('small_sample does not contain the correct columns.', {
188
expect_true('Symmetry' %in% colnames(small_sample))
189
expect_true('Radius' %in% colnames(small_sample))
190
expect_true('Class' %in% colnames(small_sample))
191
})
192
print("Success!")
193
}
194
195
196
test_2.0.1 <- function(){
197
properties <- c(small_sample_plot$layers[[1]]$mapping, small_sample_plot$mapping)
198
labels <- small_sample_plot$labels
199
test_that('Did not create a plot named small_sample_plot', {
200
expect_true(exists("small_sample_plot"))
201
})
202
test_that('Did not use small_sample data to create small_sample_plot', {
203
expect_equal(digest(int_round(sum(small_sample_plot$data$Symmetry),2)), '9e13926505f026ba03c475b3a83f7a04')
204
})
205
test_that('Symmetry should be on the x-axis.', {
206
expect_true("Symmetry" == rlang::get_expr(properties$x))
207
})
208
test_that('Radius should be on the y-axis.', {
209
expect_true("Radius" == rlang::get_expr(properties$y))
210
})
211
test_that('small_sample_plot should be a scatter plot.', {
212
expect_true("GeomPoint" %in% c(class(small_sample_plot$layers[[1]]$geom)))
213
})
214
test_that('small_sample_plot should map Benign / Malignant to colour.', {
215
expect_true("Class" == rlang::get_expr(properties$colour))
216
})
217
print("Success!")
218
}
219
220
test_2.1 <- function(){
221
test_that('Did not create an object named newData', {
222
expect_true(exists("newData"))
223
})
224
test_that('newData should be a data frame.', {
225
expect_true('data.frame' %in% class(newData))
226
})
227
test_that('The last row of the Class column should be "unknown".', {
228
expect_equal((newData$Class[6]), 'unknown')
229
})
230
test_that('newData does not contain the correct number of rows and/or columns.', {
231
expect_equal(dim(newData), c(6, 3))
232
})
233
test_that('small_sample does not contain the correct data.', {
234
expect_equal(digest(int_round(sum(newData$Radius),2)), 'e65288ab08a1f50a7e0e9a972d0cdad3')
235
expect_equal(digest(int_round(sum(newData$Symmetry),2)), '9e13926505f026ba03c475b3a83f7a04')
236
})
237
print("Success!")
238
}
239
240
test_2.2 <- function(){
241
test_that('Did not create an object named dist_matrix', {
242
expect_true(exists("dist_matrix"))
243
})
244
test_that('dist_matrix should be a matrix.', {
245
expect_true('matrix' %in% class(dist_matrix))
246
})
247
test_that('dist_matrix does not contain the correct number of rows and/or columns.', {
248
expect_equal(dim(dist_matrix), c(6, 6))
249
})
250
test_that('dist_matrix does not contain the correct data.', {
251
expect_equal(digest(int_round(sum(dist_matrix[1, ]),2)), 'd34fef5547c4f837fe41124a8fca5527')
252
expect_equal(digest(int_round(sum(dist_matrix[2, ]),2)), 'cabe6c85a73de5051d70aea8c66c720f')
253
expect_equal(digest(int_round(sum(dist_matrix[5, ]),2)), '7145585a43115cfe7cc1e1038ef55cf0')
254
expect_equal(digest(int_round(sum(dist_matrix[6, ]),2)), 'e14b44a4689c4ded0d65a6b30d1bcd7f')
255
})
256
print("Success!")
257
}
258
259
test_2.3 <- function(){
260
test_that('Solution is incorrect', {
261
expect_equal(digest(answer2.3), '5b58e040ee35f3bcc6023fb7836c842e') # we hid the answer to the test here so you can't see it, but we can still run the test
262
})
263
print("Success!")
264
}
265
266
test_2.4 <- function(){
267
test_that('Solution is incorrect', {
268
expect_equal(digest(answer2.4), '9c8cb5538e7778bf0b1bd53e45fb78c9') # we hid the answer to the test here so you can't see it, but we can still run the test
269
})
270
print("Success!")
271
}
272
273
test_2.5 <- function(){
274
test_that('Solution is incorrect', {
275
expect_equal(digest(answer2.5), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test
276
})
277
print("Success!")
278
}
279
280
test_2.6 <- function(){
281
test_that('Solution is incorrect', {
282
expect_equal(digest(answer2.6), '9c8cb5538e7778bf0b1bd53e45fb78c9') # we hid the answer to the test here so you can't see it, but we can still run the test
283
284
})
285
print("Success!")
286
}
287
288
test_2.7 <- function(){
289
test_that('Solution is incorrect', {
290
expect_equal(digest(answer2.7), '0590b0427c1b19a6eb612d19888aa52f') # we hid the answer to the test here so you can't see it, but we can still run the test
291
292
})
293
print("Success!")
294
}
295
296
test_3.1 <- function(){
297
test_that('Did not create an object named knn_spec', {
298
expect_true(exists("knn_spec"))
299
})
300
test_that('k should be 7', {
301
expect_equal(as.numeric(get_expr(knn_spec$args$neighbors)), 7)
302
})
303
test_that('weight_func is incorrect', {
304
expect_equal(digest(as.character(get_expr(knn_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')
305
})
306
test_that('set_engine is incorrect', {
307
expect_equal(digest(as.character(knn_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')
308
})
309
test_that('mode is incorrect', {
310
expect_equal(digest(as.character(knn_spec$mode)), 'f361ba6f6b32d068e56f61f53d35e26a')
311
})
312
print("Success!")
313
}
314
315
test_3.2 <- function(){
316
test_that('Did not create an object named knn_fit', {
317
expect_true(exists("knn_fit"))
318
})
319
test_that('knn_fit should be a fit model.', {
320
expect_true('model_fit' %in% class(knn_fit))
321
})
322
test_that('knn_fit does not include cancer_train dataset', {
323
expect_equal(digest(as.character(knn_fit$fit$data$Class)), '93ecaae439b9f4e8e4297d3a851929f9')
324
expect_equal(digest(int_round(sum(knn_fit$fit$data$Symmetry),2)), '1473d70e5646a26de3c52aa1abd85b1f')
325
expect_equal(digest(int_round(sum(knn_fit$fit$data$Radius),2)), '1473d70e5646a26de3c52aa1abd85b1f')
326
})
327
test_that('knn_fit does not contain knn_spec', {
328
expect_equal(digest(int_round(get_expr(knn_fit$spec$args$neighbors),2)), '51465273097370367115dfe0228831f3')
329
expect_equal(digest(as.character(get_expr(knn_fit$spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')
330
expect_equal(digest(knn_fit$spec$mode), 'f361ba6f6b32d068e56f61f53d35e26a')
331
})
332
print("Success!")
333
}
334
335
test_3.3 <- function(){
336
test_that('Did not create an object named new_obs',{
337
expect_true(exists("new_obs"))
338
})
339
test_that('new_obs is not a tibble', {
340
expect_true('data.frame' %in% class(new_obs))
341
})
342
test_that('Wrong values for Symmetry and Radius', {
343
expect_equal(as.numeric(new_obs$Symmetry), 1)
344
expect_equal(as.numeric(new_obs$Radius), 0)
345
})
346
test_that('Did not create an object named class_prediction',{
347
expect_true(exists("class_prediction"))
348
})
349
test_that('Wrong class prediction', {
350
expect_equal(digest(as.character(class_prediction$.pred_class)), '5f0922939c45ef1054f852e83f91c660')
351
})
352
print("Success!")
353
}
354
355
test_3.4 <- function(){
356
test_that('k should be 7', {
357
expect_equal(int_round(get_expr(knn_spec$args$neighbors),0), 7)
358
})
359
test_that('weight_func is incorrect', {
360
expect_equal(digest(as.character(get_expr(knn_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')
361
})
362
test_that('set_engine is incorrect', {
363
expect_equal(digest(as.character(knn_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')
364
})
365
test_that('mode is incorrect', {
366
expect_equal(digest(as.character(knn_spec$mode)), 'f361ba6f6b32d068e56f61f53d35e26a')
367
})
368
test_that('Did not create an object named knn_fit_2', {
369
expect_true(exists("knn_fit_2"))
370
})
371
test_that('knn_fit_2 should be a fit model.', {
372
expect_true('model_fit' %in% class(knn_fit_2))
373
})
374
test_that('knn_fit_2 does not include knn_train_2 dataset', {
375
expect_equal(digest(as.character(knn_fit_2$fit$data$Class)), '93ecaae439b9f4e8e4297d3a851929f9')
376
expect_equal(digest(int_round(sum(knn_fit_2$fit$data$Symmetry),2)), '1473d70e5646a26de3c52aa1abd85b1f')
377
expect_equal(digest(int_round(sum(knn_fit_2$fit$data$Radius),2)), '1473d70e5646a26de3c52aa1abd85b1f')
378
expect_equal(digest(int_round(sum(knn_fit_2$fit$data$Concavity),2)), '1473d70e5646a26de3c52aa1abd85b1f')
379
})
380
test_that('knn_fit_2 does not contain knn_spec_2', {
381
expect_equal(digest(as.numeric(get_expr(knn_fit_2$spec$args$neighbors))), '90a7653d717dc1553ee564aa27b749b9')
382
expect_equal(digest(as.character(get_expr(knn_fit_2$spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')
383
expect_equal(digest(knn_fit_2$spec$mode), 'f361ba6f6b32d068e56f61f53d35e26a')
384
})
385
test_that('Did not create an object named new_obs_2',{
386
expect_true(exists("new_obs_2"))
387
})
388
test_that('new_obs_2 is not a tibble', {
389
expect_true('data.frame' %in% class(new_obs_2))
390
})
391
test_that('Wrong values for Symmetry, Radius, and Concavity', {
392
expect_equal(int_round(new_obs_2$Symmetry, 0), 1)
393
expect_equal(int_round(new_obs_2$Radius, 0), 0)
394
expect_equal(int_round(new_obs_2$Concavity, 0), 1)
395
})
396
test_that('Did not create an object named class_prediction_2',{
397
expect_true(exists("class_prediction_2"))
398
})
399
test_that('Wrong class prediction', {
400
expect_equal(digest(as.character(class_prediction_2$.pred_class)), '5f0922939c45ef1054f852e83f91c660')
401
})
402
print("Success!")
403
}
404
405
test_3.5 <- function(){
406
test_that('Did not create a object named knn_recipe', {
407
expect_true(exists("knn_recipe"))
408
})
409
test_that('knn_recipe is not a recipe object', {
410
expect_equal(digest(class(knn_recipe)), '4b3ed1334bff94d43e32a36a1f16a2f2')
411
})
412
print("Success!")
413
}
414
415
test_3.6 <- function(){
416
test_that('Did not create an object named knn_workflow', {
417
expect_true(exists("knn_workflow"))
418
})
419
test_that('knn_workflow is not a workflow', {
420
expect_true('workflow' %in% class(knn_workflow))
421
})
422
test_that('knn_workflow does not contain the right model specification', {
423
expect_equal(int_round(get_expr(knn_workflow$fit$actions$model$spec$args$neighbors),0), 7)
424
})
425
test_that('Did not add knn_recipe', {
426
expect_true('recipe' %in% class(knn_workflow$pre$actions$recipe$recipe))
427
})
428
test_that('knn_recipe does not contain the cancer dataset', {
429
expect_equal(digest(int_round(sum(knn_workflow$pre$actions$recipe$recipe$template$Radius),2)), '1473d70e5646a26de3c52aa1abd85b1f')
430
expect_equal(digest(int_round(sum(knn_workflow$pre$actions$recipe$recipe$template$Area),2)), '1473d70e5646a26de3c52aa1abd85b1f')
431
})
432
print("Success!")
433
}
434
435
test_3.7 <- function(){
436
test_that('Did not create an object named class_prediction_all',{
437
expect_true(exists("class_prediction_all"))
438
})
439
test_that('Wrong class prediction', {
440
expect_equal(digest(as.character(class_prediction_all$.pred_class)), '3a5505c06543876fe45598b5e5e5195d')
441
})
442
print("Success!")
443
}
444
445
test_4.0 <- function(){
446
test_that('Solution is incorrect', {
447
expect_equal(digest(as.character(answer4.0)), '75f1160e72554f4270c809f041c7a776')
448
})
449
print("Success!")
450
}
451
452
test_4.1 <- function(){
453
test_that('Solution is incorrect', {
454
expect_equal(digest(as.character(answer4.1)), '475bf9280aab63a82af60791302736f6')
455
})
456
print("Success!")
457
}
458
459