Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2020-spring/materials/worksheet_06/tests_worksheet_06.R
2715 views
1
library(testthat)
2
library(digest)
3
4
test_0.1 <- function(){
5
test_that('Solution is incorrect', {
6
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
7
8
})
9
print("Success!")
10
}
11
12
test_0.2 <- function(){
13
test_that('Solution is incorrect', {
14
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
15
16
})
17
print("Success!")
18
}
19
20
21
test_1.0 <- function(){
22
test_that('Did not create an object named cancer', {
23
expect_true(exists("cancer"))
24
})
25
test_that('cancer should be a data frame.', {
26
expect_true('data.frame' %in% class(cancer))
27
})
28
test_that('cancer does not contain the correct number of rows and/or columns.', {
29
expect_equal(dim(cancer), c(569, 12))
30
})
31
test_that('cancer does not contain the correct data.', {
32
expect_equal(digest(as.numeric(sum(cancer$Area))), 'a2c1855f3fa92423aa169c350fc95232')
33
expect_equal(colnames(cancer), c("ID", "Class", "Radius", "Texture", "Perimeter", "Area", "Smoothness", "Compactness", "Concavity", "Concave_points", "Symmetry", "Fractal_dimension"))
34
})
35
print("Success!")
36
}
37
38
test_1.1 <- function(){
39
test_that('Solution is incorrect', {
40
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
41
42
})
43
print("Success!")
44
}
45
46
test_1.2 <- function(){
47
properties <- c(cancer_plot$layers[[1]]$mapping, cancer_plot$mapping)
48
labels <- cancer_plot$labels
49
test_that('Did not create a plot named cancer_plot', {
50
expect_true(exists("cancer_plot"))
51
})
52
test_that('Symmetry should be on the x-axis.', {
53
expect_that("Symmetry" == rlang::get_expr(properties$x), is_true())
54
})
55
test_that('Radius should be on the y-axis.', {
56
expect_that("Radius" == rlang::get_expr(properties$y) , is_true())
57
})
58
test_that('cancer_plot should be a scatter plot.', {
59
expect_that("GeomPoint" %in% class(cancer_plot$layers[[1]]$geom) , is_true())
60
})
61
test_that('cancer_plot should map Class to colour.', {
62
expect_true(digest(rlang::get_expr(properties$colour)) %in% c('a4abb3d43fde633563dd1f5c3ea31f31', 'f9e884084b84794d762a535f3facec85'))
63
})
64
print("Success!")
65
}
66
67
test_1.3 <- function(){
68
test_that('Solution is incorrect', {
69
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
70
71
})
72
print("Success!")
73
}
74
75
test_1.4 <- function(){
76
test_that('xa is incorrect.', {
77
expect_equal(digest(as.numeric(xa)), '218983ef51880f784c62ff2aedc196f3')
78
})
79
test_that('ya is incorrect.', {
80
expect_equal(digest(as.numeric(ya)), 'a1914c10445a398934c0e0015b9b18ae')
81
})
82
test_that('xb is incorrect.', {
83
expect_equal(digest(as.numeric(xb)), '5b34d8796880663f75ea423ccb4ea8cd')
84
})
85
test_that('yb is incorrect.', {
86
expect_equal(digest(as.numeric(yb)), '4490c7a115f39cede8cd353713230e95')
87
})
88
print("Success!")
89
}
90
91
test_1.5 <- function(){
92
test_that('answer1.5 is incorrect', {
93
expect_equal(digest(as.numeric(answer1.5)), 'ab39ff487bddaa92a62eadbbe3e46da6') # we hid the answer to the test here so you can't see it, but we can still run the test
94
95
})
96
print("Success!")
97
}
98
99
test_1.6 <- function(){
100
test_that('zb is incorrect.', {
101
expect_equal(digest(as.numeric(zb)), 'b62bcabaf783e3e2d0745ca4a41219da')
102
})
103
test_that('za is incorrect.', {
104
expect_equal(digest(as.numeric(za)), '8f22ef4a815b2e1bd4f7ec511bbc30f2')
105
})
106
print("Success!")
107
}
108
109
test_1.7 <- function(){
110
test_that('answer1.7 is incorrect', {
111
expect_equal(digest(as.numeric(answer1.7)), '97c5e6129bc96a23ed7298d78bf7f8b2') # we hid the answer to the test here so you can't see it, but we can still run the test
112
113
})
114
print("Success!")
115
}
116
117
118
119
test_1.8 <- function(){
120
test_that('point_a is incorrect.', {
121
expect_equal(digest(as.numeric(sum(point_a))), '309d3b37c196b24341299aabdac15644')
122
})
123
test_that('point_b is incorrect.', {
124
expect_equal(digest(as.numeric(sum(point_b))), '00bb41bc0f538b06f627ffbd9874a6a8')
125
})
126
print("Success!")
127
}
128
129
test_1.9 <- function(){
130
test_that('difference is incorrect', {
131
expect_equal(digest(as.numeric(sum(difference))), 'ef1fc2c1e06df149b42dcfb47596319f') # we hid the answer to the test here so you can't see it, but we can still run the test
132
133
})
134
print("Success!")
135
}
136
137
test_1.10 <- function(){
138
test_that('dif_square is incorrect', {
139
expect_equal(digest(as.numeric(sum(dif_square))), '0299530505a02b47c2a30af0ecd6026b') # we hid the answer to the test here so you can't see it, but we can still run the test
140
141
})
142
print("Success!")
143
}
144
145
test_1.10.1 <- function(){
146
test_that('dif_sum is incorrect', {
147
expect_equal(digest(as.numeric(dif_sum)), '0299530505a02b47c2a30af0ecd6026b') # we hid the answer to the test here so you can't see it, but we can still run the test
148
149
})
150
print("Success!")
151
}
152
153
test_1.10.2 <- function(){
154
test_that('root_dif_sum is incorrect', {
155
expect_equal(digest(as.numeric(root_dif_sum)), '97c5e6129bc96a23ed7298d78bf7f8b2') # we hid the answer to the test here so you can't see it, but we can still run the test
156
157
})
158
print("Success!")
159
}
160
161
test_1.10.3 <- function(){
162
test_that('dist_cancer_two_rows is incorrect', {
163
expect_equal(digest(as.numeric(dist_cancer_two_rows)), '97c5e6129bc96a23ed7298d78bf7f8b2') # we hid the answer to the test here so you can't see it, but we can still run the test
164
165
})
166
print("Success!")
167
}
168
169
170
test_1.10.4 <- function(){
171
test_that('Solution is incorrect', {
172
expect_equal(digest(answer1.10.4), '05ca18b596514af73f6880309a21b5dd') # we hid the answer to the test here so you can't see it, but we can still run the test
173
174
})
175
print("Success!")
176
}
177
178
test_2.0.0 <- function(){
179
test_that('Did not create an object named small_sample', {
180
expect_true(exists("small_sample"))
181
})
182
test_that('small_sample should be a data frame.', {
183
expect_true('data.frame' %in% class(small_sample))
184
})
185
test_that('small_sample does not contain the correct number of rows and/or columns.', {
186
expect_equal(dim(small_sample), c(5, 3))
187
})
188
test_that('small_sample does not contain the correct data.', {
189
expect_equal(digest(as.numeric(sum(small_sample$Symmetry))), '9335520ab7f7f606573cad5c82a24542')
190
})
191
print("Success!")
192
}
193
194
195
test_2.0.1 <- function(){
196
properties <- c(small_sample_plot$layers[[1]]$mapping, small_sample_plot$mapping)
197
labels <- small_sample_plot$labels
198
test_that('Did not create a plot named small_sample_plot', {
199
expect_true(exists("small_sample_plot"))
200
})
201
test_that('Did not use small_sample data to create small_sample_plot', {
202
expect_equal(digest(small_sample_plot$data$Symmetry), 'f4c490a7955304fbdeb905214ea0f3d3')
203
})
204
test_that('Symmetry should be on the x-axis.', {
205
expect_that("Symmetry" == rlang::get_expr(properties$x), is_true())
206
})
207
test_that('Radius should be on the y-axis.', {
208
expect_that("Radius" == rlang::get_expr(properties$y) , is_true())
209
})
210
test_that('small_sample_plot should be a scatter plot.', {
211
expect_that("GeomPoint" %in% c(class(small_sample_plot$layers[[1]]$geom)) , is_true())
212
})
213
test_that('small_sample_plot should map Benign / Malignant to colour.', {
214
expect_that("Class" == rlang::get_expr(properties$colour) , is_true())
215
})
216
print("Success!")
217
}
218
219
test_2.1 <- function(){
220
test_that('Did not create an object named newData', {
221
expect_true(exists("newData"))
222
})
223
test_that('newData should be a data frame.', {
224
expect_true('data.frame' %in% class(newData))
225
})
226
test_that('The last row of the Class column should be unknown.', {
227
expect_equal(newData$Class[6], 'unknown')
228
})
229
test_that('newData does not contain the correct number of rows and/or columns.', {
230
expect_equal(dim(newData), c(6, 3))
231
})
232
test_that('small_sample does not contain the correct data.', {
233
expect_equal(digest(sum(as.numeric(newData$Radius))), '291661ebdc869b7f43db4135b6dff842')
234
expect_equal(digest(sum(as.numeric(newData$Symmetry))), '9335520ab7f7f606573cad5c82a24542')
235
})
236
print("Success!")
237
}
238
239
test_2.2 <- function(){
240
test_that('Did not create an object named dist_matrix', {
241
expect_true(exists("dist_matrix"))
242
})
243
test_that('dist_matrix should be a matrix.', {
244
expect_true('matrix' %in% class(dist_matrix))
245
})
246
test_that('dist_matrix does not contain the correct number of rows and/or columns.', {
247
expect_equal(dim(dist_matrix), c(6, 6))
248
})
249
test_that('dist_matrix does not contain the correct data.', {
250
expect_equal(digest(as.numeric(sum(dist_matrix[1, ]))), digest(sum(dist_matrix[, 1])))
251
expect_equal(digest(as.numeric(sum(dist_matrix[2, ]))), digest(sum(dist_matrix[, 2])))
252
expect_equal(digest(as.numeric(sum(dist_matrix[5, ]))), digest(sum(dist_matrix[, 5])))
253
expect_equal(digest(as.numeric(sum(dist_matrix[6, ]))), digest(sum(dist_matrix[, 6])))
254
})
255
print("Success!")
256
}
257
258
test_2.3 <- function(){
259
test_that('Solution is incorrect', {
260
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
261
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
})
271
print("Success!")
272
}
273
274
test_2.5 <- function(){
275
test_that('Solution is incorrect', {
276
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
277
278
})
279
print("Success!")
280
}
281
282
test_2.6 <- function(){
283
test_that('Solution is incorrect', {
284
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
285
286
})
287
print("Success!")
288
}
289
290
test_2.7 <- function(){
291
test_that('Solution is incorrect', {
292
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
293
294
})
295
print("Success!")
296
}
297
298
299
300
test_3.0 <- function(){
301
test_that('Did not create an object named X_train', {
302
expect_true(exists("X_train"))
303
})
304
test_that('X_train should be a data frame.', {
305
expect_true('data.frame' %in% class(X_train))
306
})
307
test_that('X_train does not contain the correct number of rows and/or columns.', {
308
expect_equal(dim(X_train), c(569, 2))
309
})
310
test_that('X_train does not contain the column(s) Symmetry and/or Radius.', {
311
expect_true('Symmetry' %in% colnames(X_train))
312
expect_true('Radius' %in% colnames(X_train))
313
})
314
test_that('Did not create an object named Y_train', {
315
expect_true(exists("Y_train"))
316
})
317
test_that('Y_train should be a character.', {
318
expect_true('character' %in% class(Y_train))
319
})
320
test_that('Y_train is not the correct length.', {
321
expect_equal(length(Y_train), 569)
322
})
323
print("Success!")
324
}
325
326
test_3.1 <- function(){
327
test_that('method should be knn', {
328
expect_equal(as.character(model_knn$method), 'knn')
329
})
330
test_that('k should be 7', {
331
expect_equal(as.numeric(model_knn$results$k), 7)
332
})
333
test_that('model_knn contains incorrect information.', {
334
expect_equal(digest(as.numeric(sum(model_knn$trainingData$Symmetry))), '47d0e881a9a1b19e57f9c068c08765fa')
335
expect_equal(digest(as.numeric(sum(model_knn$trainingData$Radius))), '5818709a65b4a5df9cb392b9cc66e32b')
336
expect_equal(as.numeric(summary(model_knn$trainingData$.outcome)[1]), 357)
337
})
338
print("Success!")
339
}
340
341
test_3.2 <- function(){
342
test_that('Did not create an object named new_obs', {
343
expect_true(exists("new_obs"))
344
})
345
test_that('new_obs should be a data frame.', {
346
expect_true('data.frame' %in% class(new_obs))
347
})
348
test_that('Did not create an object named predicted_knn_7', {
349
expect_true(exists("predicted_knn_7"))
350
})
351
test_that('predicted_knn_7 should be a factor.', {
352
expect_true('factor' %in% class(predicted_knn_7))
353
})
354
test_that('predicted_knn_7 prediction is incorrect.', {
355
expect_equal(digest(as.character(predicted_knn_7)), '5f0922939c45ef1054f852e83f91c660')
356
})
357
print("Success!")
358
}
359
360
test_3.3 <- function(){
361
test_that('Did not create an object named X_train_3', {
362
expect_true(exists("X_train_3"))
363
})
364
test_that('X_train_3 should be a data frame.', {
365
expect_true('data.frame' %in% class(X_train_3))
366
})
367
test_that('X_train_3 does not contain the correct number of rows and/or columns.', {
368
expect_equal(dim(X_train_3), c(569, 3))
369
})
370
test_that('Did not create an object named Y_train_3', {
371
expect_true(exists("Y_train_3"))
372
})
373
test_that('Y_train_3 should be a character.', {
374
expect_true('character' %in% class(Y_train_3))
375
})
376
test_that('Y_train_3 is not the correct length.', {
377
expect_equal(length(Y_train_3), 569)
378
})
379
test_that('Did not create an object named new_obs_3', {
380
expect_true(exists("new_obs_3"))
381
})
382
test_that('new_obs_3 should be a data frame.', {
383
expect_true('data.frame' %in% class(new_obs_3))
384
})
385
test_that('Did not create an object named predicted_3_knn_7', {
386
expect_true(exists("predicted_3_knn_7"))
387
})
388
test_that('predicted_3_knn_7 should be a factor.', {
389
expect_true('factor' %in% class(predicted_3_knn_7))
390
})
391
test_that('predicted_3_knn_7 prediction is incorrect.', {
392
expect_equal(digest(as.character(predicted_3_knn_7)), '5f0922939c45ef1054f852e83f91c660')
393
})
394
print("Success!")
395
}
396
397
test_3.4 <- function(){
398
test_that('Did not create an object named X_train_all', {
399
expect_true(exists("X_train_all"))
400
})
401
test_that('X_train_all should be a data frame.', {
402
expect_true('data.frame' %in% class(X_train_all))
403
})
404
test_that('X_train_all does not contain the correct number of rows and/or columns.', {
405
expect_equal(dim(X_train_all), c(569, 10))
406
})
407
test_that('Did not create an object named Y_train_all', {
408
expect_true(exists("Y_train_all"))
409
})
410
test_that('Y_train_all should be a character.', {
411
expect_true('character' %in% class(Y_train_all))
412
})
413
test_that('Y_train_all is not the correct length.', {
414
expect_equal(length(Y_train_all), 569)
415
})
416
test_that('Did not create an object named new_obs_all', {
417
expect_true(exists("new_obs_all"))
418
})
419
test_that('new_obs_all should be a data frame.', {
420
expect_true('data.frame' %in% class(new_obs_all))
421
})
422
test_that('Did not create an object named predicted_all_knn_7', {
423
expect_true(exists("predicted_all_knn_7"))
424
})
425
test_that('predicted_all_knn_7 should be a factor.', {
426
expect_true('factor' %in% class(predicted_all_knn_7))
427
})
428
test_that('predicted_all_knn_7 prediction is incorrect.', {
429
expect_equal(digest(as.character(predicted_all_knn_7)), '3a5505c06543876fe45598b5e5e5195d')
430
})
431
print("Success!")
432
}
433
434
test_4.0 <- function(){
435
test_that('Solution is incorrect', {
436
expect_equal(digest(as.character(answer4.0)), '75f1160e72554f4270c809f041c7a776')
437
})
438
print("Success!")
439
}
440
441
test_4.1 <- function(){
442
test_that('Solution is incorrect', {
443
expect_equal(digest(as.character(answer4.1)), '475bf9280aab63a82af60791302736f6')
444
})
445
print("Success!")
446
}
447