Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2019-fall/materials/worksheet_06/tests_worksheet_06.R
2051 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 <- function(){
179
properties <- c(small_sample_plot$layers[[1]]$mapping, small_sample_plot$mapping)
180
labels <- small_sample_plot$labels
181
test_that('Did not create an object named small_sample', {
182
expect_true(exists("small_sample"))
183
})
184
test_that('small_sample should be a data frame.', {
185
expect_true('data.frame' %in% class(small_sample))
186
})
187
test_that('small_sample does not contain the correct number of rows and/or columns.', {
188
expect_equal(dim(small_sample), c(5, 3))
189
})
190
test_that('small_sample does not contain the correct data.', {
191
expect_equal(digest(as.numeric(sum(small_sample$Symmetry))), '9335520ab7f7f606573cad5c82a24542')
192
})
193
test_that('Did not create a plot named small_sample_plot', {
194
expect_true(exists("small_sample_plot"))
195
})
196
test_that('Did not use small_sample data to create small_sample_plot', {
197
expect_equal(digest(small_sample_plot$data$Symmetry), 'f4c490a7955304fbdeb905214ea0f3d3')
198
})
199
test_that('Symmetry should be on the x-axis.', {
200
expect_that("Symmetry" == rlang::get_expr(properties$x), is_true())
201
})
202
test_that('Radius should be on the y-axis.', {
203
expect_that("Radius" == rlang::get_expr(properties$y) , is_true())
204
})
205
test_that('small_sample_plot should be a scatter plot.', {
206
expect_that("GeomPoint" %in% c(class(small_sample_plot$layers[[1]]$geom)) , is_true())
207
})
208
test_that('small_sample_plot should map Benign / Malignant to colour.', {
209
expect_that("Class" == rlang::get_expr(properties$colour) , is_true())
210
})
211
print("Success!")
212
}
213
214
test_2.1 <- function(){
215
test_that('Did not create an object named newData', {
216
expect_true(exists("newData"))
217
})
218
test_that('newData should be a data frame.', {
219
expect_true('data.frame' %in% class(newData))
220
})
221
test_that('The last row of the Class column should be unknown.', {
222
expect_equal(newData$Class[6], 'unknown')
223
})
224
test_that('newData does not contain the correct number of rows and/or columns.', {
225
expect_equal(dim(newData), c(6, 3))
226
})
227
test_that('small_sample does not contain the correct data.', {
228
expect_equal(digest(sum(as.numeric(newData$Radius))), '291661ebdc869b7f43db4135b6dff842')
229
expect_equal(digest(sum(as.numeric(newData$Symmetry))), '9335520ab7f7f606573cad5c82a24542')
230
})
231
print("Success!")
232
}
233
234
test_2.2 <- function(){
235
test_that('Did not create an object named dist_matrix', {
236
expect_true(exists("dist_matrix"))
237
})
238
test_that('dist_matrix should be a matrix.', {
239
expect_true('matrix' %in% class(dist_matrix))
240
})
241
test_that('dist_matrix does not contain the correct number of rows and/or columns.', {
242
expect_equal(dim(dist_matrix), c(6, 6))
243
})
244
test_that('dist_matrix does not contain the correct data.', {
245
expect_equal(digest(as.numeric(sum(dist_matrix[1, ]))), digest(sum(dist_matrix[, 1])))
246
expect_equal(digest(as.numeric(sum(dist_matrix[2, ]))), digest(sum(dist_matrix[, 2])))
247
expect_equal(digest(as.numeric(sum(dist_matrix[5, ]))), digest(sum(dist_matrix[, 5])))
248
expect_equal(digest(as.numeric(sum(dist_matrix[6, ]))), digest(sum(dist_matrix[, 6])))
249
})
250
print("Success!")
251
}
252
253
test_2.3 <- function(){
254
test_that('Solution is incorrect', {
255
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
256
257
})
258
print("Success!")
259
}
260
261
test_2.4 <- function(){
262
test_that('Solution is incorrect', {
263
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
264
265
})
266
print("Success!")
267
}
268
269
test_2.5 <- function(){
270
test_that('Solution is incorrect', {
271
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
272
273
})
274
print("Success!")
275
}
276
277
test_2.6 <- function(){
278
test_that('Solution is incorrect', {
279
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
280
281
})
282
print("Success!")
283
}
284
285
test_2.7 <- function(){
286
test_that('Solution is incorrect', {
287
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
288
289
})
290
print("Success!")
291
}
292
293
294
295
test_3.0 <- function(){
296
test_that('Did not create an object named X_train', {
297
expect_true(exists("X_train"))
298
})
299
test_that('X_train should be a data frame.', {
300
expect_true('data.frame' %in% class(X_train))
301
})
302
test_that('X_train does not contain the correct number of rows and/or columns.', {
303
expect_equal(dim(X_train), c(569, 2))
304
})
305
test_that('X_train does not contain the column(s) Symmetry and/or Radius.', {
306
expect_true('Symmetry' %in% colnames(X_train))
307
expect_true('Radius' %in% colnames(X_train))
308
})
309
test_that('Did not create an object named Y_train', {
310
expect_true(exists("Y_train"))
311
})
312
test_that('Y_train should be a character.', {
313
expect_true('character' %in% class(Y_train))
314
})
315
test_that('Y_train is not the correct length.', {
316
expect_equal(length(Y_train), 569)
317
})
318
print("Success!")
319
}
320
321
test_3.1 <- function(){
322
test_that('method should be knn', {
323
expect_equal(as.character(model_knn$method), 'knn')
324
})
325
test_that('k should be 7', {
326
expect_equal(as.numeric(model_knn$results$k), 7)
327
})
328
test_that('model_knn contains incorrect information.', {
329
expect_equal(digest(as.numeric(sum(model_knn$trainingData$Symmetry))), '47d0e881a9a1b19e57f9c068c08765fa')
330
expect_equal(digest(as.numeric(sum(model_knn$trainingData$Radius))), '5818709a65b4a5df9cb392b9cc66e32b')
331
expect_equal(as.numeric(summary(model_knn$trainingData$.outcome)[1]), 357)
332
})
333
print("Success!")
334
}
335
336
test_3.2 <- function(){
337
test_that('Did not create an object named new_obs', {
338
expect_true(exists("new_obs"))
339
})
340
test_that('new_obs should be a data frame.', {
341
expect_true('data.frame' %in% class(new_obs))
342
})
343
test_that('Did not create an object named predicted_knn_7', {
344
expect_true(exists("predicted_knn_7"))
345
})
346
test_that('predicted_knn_7 should be a factor.', {
347
expect_true('factor' %in% class(predicted_knn_7))
348
})
349
test_that('predicted_knn_7 prediction is incorrect.', {
350
expect_equal(digest(as.character(predicted_knn_7)), '5f0922939c45ef1054f852e83f91c660')
351
})
352
print("Success!")
353
}
354
355
test_3.3 <- function(){
356
test_that('Did not create an object named X_train_3', {
357
expect_true(exists("X_train_3"))
358
})
359
test_that('X_train_3 should be a data frame.', {
360
expect_true('data.frame' %in% class(X_train_3))
361
})
362
test_that('X_train_3 does not contain the correct number of rows and/or columns.', {
363
expect_equal(dim(X_train_3), c(569, 3))
364
})
365
test_that('Did not create an object named Y_train_3', {
366
expect_true(exists("Y_train_3"))
367
})
368
test_that('Y_train_3 should be a character.', {
369
expect_true('character' %in% class(Y_train_3))
370
})
371
test_that('Y_train_3 is not the correct length.', {
372
expect_equal(length(Y_train_3), 569)
373
})
374
test_that('Did not create an object named new_obs_3', {
375
expect_true(exists("new_obs_3"))
376
})
377
test_that('new_obs_3 should be a data frame.', {
378
expect_true('data.frame' %in% class(new_obs_3))
379
})
380
test_that('Did not create an object named predicted_3_knn_7', {
381
expect_true(exists("predicted_3_knn_7"))
382
})
383
test_that('predicted_3_knn_7 should be a factor.', {
384
expect_true('factor' %in% class(predicted_3_knn_7))
385
})
386
test_that('predicted_3_knn_7 prediction is incorrect.', {
387
expect_equal(digest(as.character(predicted_3_knn_7)), '5f0922939c45ef1054f852e83f91c660')
388
})
389
print("Success!")
390
}
391
392
test_3.4 <- function(){
393
test_that('Did not create an object named X_train_all', {
394
expect_true(exists("X_train_all"))
395
})
396
test_that('X_train_all should be a data frame.', {
397
expect_true('data.frame' %in% class(X_train_all))
398
})
399
test_that('X_train_all does not contain the correct number of rows and/or columns.', {
400
expect_equal(dim(X_train_all), c(569, 10))
401
})
402
test_that('Did not create an object named Y_train_all', {
403
expect_true(exists("Y_train_all"))
404
})
405
test_that('Y_train_all should be a character.', {
406
expect_true('character' %in% class(Y_train_all))
407
})
408
test_that('Y_train_all is not the correct length.', {
409
expect_equal(length(Y_train_all), 569)
410
})
411
test_that('Did not create an object named new_obs_all', {
412
expect_true(exists("new_obs_all"))
413
})
414
test_that('new_obs_all should be a data frame.', {
415
expect_true('data.frame' %in% class(new_obs_all))
416
})
417
test_that('Did not create an object named predicted_all_knn_7', {
418
expect_true(exists("predicted_all_knn_7"))
419
})
420
test_that('predicted_all_knn_7 should be a factor.', {
421
expect_true('factor' %in% class(predicted_all_knn_7))
422
})
423
test_that('predicted_all_knn_7 prediction is incorrect.', {
424
expect_equal(digest(as.character(predicted_all_knn_7)), '3a5505c06543876fe45598b5e5e5195d')
425
})
426
print("Success!")
427
}
428
429
test_4.0 <- function(){
430
test_that('Solution is incorrect', {
431
expect_equal(digest(as.character(answer4.0)), '75f1160e72554f4270c809f041c7a776')
432
})
433
print("Success!")
434
}
435
436
test_4.1 <- function(){
437
test_that('Solution is incorrect', {
438
expect_equal(digest(as.character(answer4.1)), '475bf9280aab63a82af60791302736f6')
439
})
440
print("Success!")
441
}
442