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_04/tests_worksheet_04.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_A), '05ca18b596514af73f6880309a21b5dd')
7
expect_equal(digest(answer0.1_B), 'd2a90307aac5ae8d0ef58e2fe730d38b')
8
expect_equal(digest(answer0.1_C), '05ca18b596514af73f6880309a21b5dd')
9
expect_equal(digest(answer0.1_D), '05ca18b596514af73f6880309a21b5dd')
10
expect_equal(digest(answer0.1_E), 'd2a90307aac5ae8d0ef58e2fe730d38b')# we hid the answer to the test here so you can't see it, but we can still run the test
11
})
12
print("Success!")
13
}
14
15
test_0.2 <- function(){
16
test_that('Solution is incorrect', {
17
expect_equal(digest(answer0.2), '75f1160e72554f4270c809f041c7a776') # we hid the answer to the test here so you can't see it, but we can still run the test
18
})
19
print("Success!")
20
}
21
22
test_0.3 <- function(){
23
test_that('Solution is incorrect', {
24
expect_equal(digest(answer0.3), 'c1f86f7430df7ddb256980ea6a3b57a4') # we hid the answer to the test here so you can't see it, but we can still run the test
25
})
26
print("Success!")
27
}
28
29
test_0.4 <- function(){
30
test_that('Solution is incorrect', {
31
expect_equal(digest(answer0.4), 'c1f86f7430df7ddb256980ea6a3b57a4') # we hid the answer to the test here so you can't see it, but we can still run the test
32
})
33
print("Success!")
34
}
35
36
test_0.5 <- function(){
37
test_that('Solution is incorrect', {
38
expect_equal(digest(answer0.5_1), '75f1160e72554f4270c809f041c7a776')
39
expect_equal(digest(answer0.5_2), '01a75cb73d67b0f895ff0e61449c7bf8')
40
expect_equal(digest(answer0.5_3), '3a5505c06543876fe45598b5e5e5195d')
41
expect_equal(digest(answer0.5_4), 'f76b651ab8fcb8d470f79550bf2af53a')
42
expect_equal(digest(answer0.5_5), 'c1f86f7430df7ddb256980ea6a3b57a4')
43
expect_equal(digest(answer0.5_6), '475bf9280aab63a82af60791302736f6')# we hid the answer to the test here so you can't see it, but we can still run the test
44
})
45
print("Success!")
46
}
47
48
test_1.0 <- function(){
49
test_that('Solution is incorrect', {
50
expect_equal(digest(answer1.0), '75f1160e72554f4270c809f041c7a776') # we hid the answer to the test here so you can't see it, but we can still run the test
51
})
52
print("Success!")
53
}
54
55
test_1.1 <- function(){
56
test_that('Did not create an object named world_vaccination', {
57
expect_true(exists("world_vaccination"))
58
})
59
test_that('world_vaccination should be a data frame.', {
60
expect_true('data.frame' %in% class(world_vaccination))
61
})
62
test_that('Did not remove NA values from the pct_vaccinated column.', {
63
expect_equal(any(is.na(world_vaccination$pct_vaccinated)), FALSE)
64
})
65
test_that('world_vaccination does not contain the correct number of rows and/or columns.', {
66
expect_equal(dim(world_vaccination), c(385, 4))
67
})
68
test_that('Columns in world_vaccination contain incorrect values.', {
69
expect_equal(digest(as.numeric(sum(world_vaccination$year))), 'ed828cd9c4dbc736fb12a1a8643aaeec')
70
})
71
print("Success!")
72
}
73
74
test_1.2 <- function(){
75
test_that('Did not create a plot named world_vacc_plot', {
76
expect_true(exists("world_vacc_plot"))
77
})
78
test_that('year should be on the x-axis.', {
79
expect_that("year" %in% c(rlang::get_expr(world_vacc_plot$mapping$x),rlang::get_expr(world_vacc_plot$layers[[1]]$mapping$x)), is_true())
80
})
81
test_that('pct_vaccinated should be on the y-axis.', {
82
expect_that("pct_vaccinated" %in% c(rlang::get_expr(world_vacc_plot$mapping$y), rlang::get_expr(world_vacc_plot$layers[[1]]$mapping$y)) , is_true())
83
})
84
test_that('world_vacc_plot should be a scatter plot.', {
85
expect_that("GeomPoint" %in% c(class(world_vacc_plot$layers[[1]]$geom)) , is_true())
86
})
87
test_that('Labels on the axes should be descriptive and human readable.', {
88
expect_that((world_vacc_plot$labels$y) == 'pct_vaccinated', is_false())
89
expect_that((world_vacc_plot$labels$x) == 'year', is_false())
90
})
91
print("Success!")
92
}
93
94
test_1.3 <- function(){
95
test_that('Solution is incorrect', {
96
expect_equal(digest(answer1.3), 'c1f86f7430df7ddb256980ea6a3b57a4') # we hid the answer to the test here so you can't see it, but we can still run the test
97
})
98
print("Success!")
99
}
100
101
test_1.4 <- function(){
102
properties <- c(compare_vacc_plot$layers[[1]]$mapping, compare_vacc_plot$mapping)
103
labels <- compare_vacc_plot$labels
104
test_that('Did not create a plot named compare_vacc_plot', {
105
expect_true(exists("compare_vacc_plot"))
106
})
107
test_that('year should be on the x-axis.', {
108
expect_true("year" == rlang::get_expr(properties$x))
109
})
110
test_that('pct_vaccinated should be on the y-axis.', {
111
expect_true("pct_vaccinated" == rlang::get_expr(properties$y))
112
})
113
test_that('vaccine should map to colour and shape.', {
114
expect_true("vaccine" == rlang::get_expr(properties$colour))
115
})
116
test_that('vaccine should map to shape and colour.', {
117
expect_true("vaccine" == rlang::get_expr(properties$shape))
118
})
119
test_that('compare_vacc_plot should be a scatter plot.', {
120
expect_that("GeomPoint" %in% c(class(compare_vacc_plot$layers[[1]]$geom)) , is_true())
121
})
122
test_that('Labels on the axes and legend need to be changed to be descriptive, nicely formatted, and human readable.', {
123
expect_that((labels$y) == 'pct_vaccinated', is_false())
124
expect_that((labels$x) == 'year', is_false())
125
expect_that((labels$colour) == 'vaccine', is_false())
126
expect_that((labels$shape) == 'vaccine', is_false())
127
})
128
print("Success!")
129
}
130
131
test_1.5 <- function(){
132
test_that('Did not create an object named polio', {
133
expect_true(exists("polio"))
134
})
135
test_that('The vaccine column in polio should only contain the polio vaccine.', {
136
expect_equal(unique(polio$vaccine), "polio")
137
})
138
test_that('polio does not contain the correct number of rows and/or columns.', {
139
expect_equal(dim(polio), c(228, 4))
140
})
141
test_that('Columns in polio contain incorrect values.', {
142
expect_equal(digest(as.numeric(sum(polio$pct_vaccinated))), 'cfd7ed9e50ed446d50289ff89ef338a4') # we hid the answer to the test here so you can't see it, but we can still run the test
143
})
144
print("Success!")
145
}
146
147
test_1.6 <- function(){
148
properties <- c(polio_regions$layers[[1]]$mapping, polio_regions$mapping)
149
labels <- polio_regions$labels
150
test_that('Did not create a plot named polio_regions', {
151
expect_true(exists("polio_regions"))
152
})
153
test_that('year should be on the x-axis.', {
154
expect_true("year" == rlang::get_expr(properties$x))
155
})
156
test_that('pct_vaccinated should be on the y-axis.', {
157
expect_true("pct_vaccinated" == rlang::get_expr(properties$y))
158
})
159
test_that('who_region should map to colour and shape.', {
160
expect_true("who_region" == rlang::get_expr(properties$colour))
161
})
162
test_that('who_region should map to shape and colour.', {
163
expect_true("who_region" == rlang::get_expr(properties$shape))
164
})
165
test_that('polio_regions should be a scatter plot.', {
166
expect_that("GeomPoint" %in% c(class(polio_regions$layers[[1]]$geom)) , is_true())
167
})
168
test_that('Labels on the axes and legend should be descriptive and human readable.', {
169
expect_that((labels$y) == 'pct_vaccinated', is_false())
170
expect_that((labels$x) == 'year', is_false())
171
})
172
print("Success!")
173
}
174
175
test_1.7.1 <- function(){
176
properties <- c(polio_regions_line$layers[[1]]$mapping, polio_regions_line$mapping)
177
labels <- polio_regions_line$labels
178
test_that('Did not create a plot named polio_regions_line', {
179
expect_true(exists("polio_regions_line"))
180
})
181
test_that('year should be on the x-axis.', {
182
expect_true("year" == rlang::get_expr(properties$x))
183
})
184
test_that('pct_vaccinated should be on the y-axis.', {
185
expect_true("pct_vaccinated" == rlang::get_expr(properties$y))
186
})
187
test_that('who_region should map to colour.', {
188
expect_true("who_region" == rlang::get_expr(properties$colour))
189
})
190
test_that('polio_regions_line should be a line plot.', {
191
expect_that("GeomLine" %in% c(class(polio_regions_line$layers[[1]]$geom)) , is_true())
192
})
193
test_that('Labels on the axes should be descriptive and human readable.', {
194
expect_that((labels$y) == 'pct_vaccinated', is_false())
195
expect_that((labels$x) == 'year', is_false())
196
})
197
print("Success!")
198
}
199
200
test_1.7.2 <- function(){
201
properties <- c(polio_regions_line$layers[[1]]$mapping, polio_regions_line$mapping)
202
labels <- polio_regions_line$labels
203
test_that('Did not create a plot named polio_regions_line', {
204
expect_true(exists("polio_regions_line"))
205
})
206
test_that('year should be on the x-axis.', {
207
expect_true("year" == rlang::get_expr(properties$x))
208
})
209
test_that('pct_vaccinated should be on the y-axis.', {
210
expect_true("pct_vaccinated" == rlang::get_expr(properties$y))
211
})
212
test_that('who_region should map to colour.', {
213
expect_true("who_region" == rlang::get_expr(properties$colour))
214
})
215
test_that('polio_regions_line should be a line plot.', {
216
expect_that("GeomLine" %in% c(class(polio_regions_line$layers[[1]]$geom)) , is_true())
217
})
218
test_that('Labels on the axes and legend should be descriptive and human readable.', {
219
expect_that((labels$y) == 'pct_vaccinated', is_false())
220
expect_that((labels$x) == 'year', is_false())
221
expect_that((labels$colour) == 'who_region', is_false())
222
})
223
print("Success!")
224
}
225
226
test_1.8 <- function(){
227
properties <- c(side_by_side_world$layers[[1]]$mapping, side_by_side_world$mapping)
228
labels <- side_by_side_world$labels
229
test_that('Did not create a plot named side_by_side_world', {
230
expect_true(exists("side_by_side_world"))
231
})
232
test_that('year should be on the x-axis.', {
233
expect_true("year" == rlang::get_expr(properties$x))
234
})
235
test_that('pct_vaccinated should be on the y-axis.', {
236
expect_true("pct_vaccinated" == rlang::get_expr(properties$y))
237
})
238
test_that('who_region should map to colour.', {
239
expect_true("who_region" == rlang::get_expr(properties$colour))
240
})
241
test_that('side_by_side_world should be faceted by the vaccine column.', {
242
expect_that('FacetGrid' %in% class(rlang::get_expr(side_by_side_world$facet)), is_true())
243
})
244
test_that('side_by_side_world should be a line plot.', {
245
expect_that("GeomLine" %in% c(class(side_by_side_world$layers[[1]]$geom)) , is_true())
246
})
247
test_that('Labels on the axes and legend should be descriptive and human readable.', {
248
expect_that((labels$y) == 'pct_vaccinated', is_false())
249
expect_that((labels$x) == 'year', is_false())
250
expect_that((labels$colour) == 'who_region', is_false())
251
})
252
print("Success!")
253
}
254
255
test_1.9.1 <- function(){
256
properties <- c(vertical_world$layers[[1]]$mapping, vertical_world$mapping)
257
labels <- vertical_world$labels
258
test_that('Did not create a plot named vertical_world', {
259
expect_true(exists("vertical_world"))
260
})
261
test_that('year should be on the x-axis.', {
262
expect_true("year" == rlang::get_expr(properties$x))
263
})
264
test_that('pct_vaccinated should be on the y-axis.', {
265
expect_true("pct_vaccinated" == rlang::get_expr(properties$y))
266
})
267
test_that('who_region should map to colour.', {
268
expect_true("who_region" == rlang::get_expr(properties$colour))
269
})
270
test_that('vertical_world should be faceted by the vaccine column.', {
271
expect_that('FacetGrid' %in% class(rlang::get_expr(vertical_world$facet)), is_true())
272
})
273
test_that('vertical_world should be a line plot.', {
274
expect_that("GeomLine" %in% c(class(vertical_world$layers[[1]]$geom)) , is_true())
275
})
276
test_that('Labels on the axes and legend should be descriptive and human readable.', {
277
expect_that((labels$y) == 'pct_vaccinated', is_false())
278
expect_that((labels$x) == 'year', is_false())
279
expect_that((labels$colour) == 'who_region', is_false())
280
})
281
print("Success!")
282
}
283
284
test_1.9.2 <- function(){
285
test_that('Solution is incorrect', {
286
expect_equal(digest(answer1.9.2), 'c1f86f7430df7ddb256980ea6a3b57a4') # we hid the answer to the test here so you can't see it, but we can still run the test
287
})
288
print("Success!")
289
}
290
291
test_2.1 <- function(){
292
test_that('Solution is incorrect', {
293
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
294
})
295
print("Success!")
296
}
297
298
test_2.2 <- function(){
299
test_that('Did not create an object named fast_food', {
300
expect_true(exists("fast_food"))
301
})
302
test_that('fast_food does not contain the correct number of rows and/or columns.', {
303
expect_equal(dim(fast_food), c(10000, 2))
304
})
305
test_that('Columns in fast_food contain incorrect values.', {
306
expect_equal(digest(as.character(fast_food[[3,1]])), '2e716500dfeb89b1b087089a5b1355f8') # we hid the answer to the test here so you can't see it, but we can still run the test
307
expect_equal(digest(as.character(fast_food[[4,2]])), 'd599245d7d7e3f56863ba3a6112ca71b')
308
})
309
print("Success!")
310
}
311
312
test_2.3 <- function(){
313
test_that('Did not create an object named top_restaurants', {
314
expect_true(exists("top_restaurants"))
315
})
316
test_that('top_restaurants does not contain the correct number of rows and/or columns.', {
317
expect_equal(dim(top_restaurants), c(9, 2))
318
})
319
test_that('Columns in fast_food contain incorrect values.', {
320
expect_equal(digest(sum(as.numeric(top_restaurants$n, na.rm = TRUE))), '4745bd90664c8cb9935cfb1a4cf51d77')
321
})
322
print("Success!")
323
}
324
325
test_2.4 <- function(){
326
properties <- c(count_bar_chart$layers[[1]]$mapping, count_bar_chart$mapping)
327
labels <- count_bar_chart$labels
328
test_that('Did not create a plot named count_bar_chart', {
329
expect_true(exists("count_bar_chart"))
330
})
331
test_that('name should be on the x-axis.', {
332
expect_true("name" == rlang::get_expr(properties$x))
333
})
334
test_that('n should be on the y-axis.', {
335
expect_true("n" == rlang::get_expr(properties$y))
336
})
337
test_that('vertical_world should be a bar plot.', {
338
expect_that("GeomBar" %in% c(class(count_bar_chart$layers[[1]]$geom)) , is_true())
339
})
340
test_that('Labels on the axes and legend should be descriptive and human readable.', {
341
expect_that((labels$y) == 'n', is_false())
342
expect_that((labels$x) == 'name', is_false())
343
})
344
print("Success!")
345
}
346
347
test_2.5_A <- function(){
348
properties <- c(count_bar_chart_A$layers[[1]]$mapping, count_bar_chart_A$mapping)
349
labels <- count_bar_chart_A$labels
350
test_that('Did not create a plot named count_bar_chart_A', {
351
expect_true(exists("count_bar_chart_A"))
352
})
353
test_that('name should be on the x-axis.', {
354
expect_true("name" == rlang::get_expr(properties$x))
355
})
356
test_that('x-axis (bar) labels should be at an angle between 20 and 90 degrees.', {
357
expect_that(count_bar_chart_A$theme$axis.text.x$angle <= 90 & count_bar_chart_A$theme$axis.text.x$angle >= 20, is_true())
358
})
359
test_that('hjust should equal 1', {
360
expect_equal(digest(as.numeric(count_bar_chart_A$theme$axis.text.x$hjust)), '6717f2823d3202449301145073ab8719')
361
})
362
test_that('n should be on the y-axis.', {
363
expect_true("n" == rlang::get_expr(properties$y))
364
})
365
test_that('vertical_world should be a bar plot.', {
366
expect_that("GeomBar" %in% c(class(count_bar_chart_A$layers[[1]]$geom)) , is_true())
367
})
368
test_that('Labels on the axes and legend should be descriptive and human readable.', {
369
expect_that((labels$y) == 'n', is_false())
370
expect_that((labels$x) == 'name', is_false())
371
})
372
print("Success!")
373
}
374
375
test_2.5_B <- function(){
376
properties <- c(count_bar_chart_B$layers[[1]]$mapping, count_bar_chart_B$mapping)
377
labels <- count_bar_chart_B$labels
378
test_that('Did not create a plot named count_bar_chart_B', {
379
expect_true(exists("count_bar_chart_B"))
380
})
381
test_that('name should be on the x-axis.', {
382
expect_true("name" == rlang::get_expr(properties$x))
383
})
384
test_that('The coordinate axes should be flipped', {
385
expect_true("CoordFlip" %in% class(ggplot_build(count_bar_chart_B)$layout$coord))
386
})
387
test_that('n should be on the y-axis.', {
388
expect_true("n" == rlang::get_expr(properties$y))
389
})
390
test_that('vertical_world should be a bar plot.', {
391
expect_that("GeomBar" %in% c(class(count_bar_chart_B$layers[[1]]$geom)) , is_true())
392
})
393
test_that('Labels on the axes and legend should be descriptive and human readable.', {
394
expect_that((labels$y) == 'n', is_false())
395
expect_that((labels$x) == 'name', is_false())
396
})
397
print("Success!")
398
}
399
400
test_2.6 <- function(){
401
test_that('Solution is incorrect', {
402
expect_equal(digest(answer2.6), '948a9b527842ee791d4f18fb5594fbf7')
403
})
404
print("Success!")
405
}
406
407
test_2.7 <- function(){
408
test_that('Did not create an object named state_counts', {
409
expect_true(exists("state_counts"))
410
})
411
test_that('The state column in state_counts should only contain CA, OR, and WA', {
412
expect_equal((state_counts$state), c("CA", "OR", "WA"))
413
})
414
test_that('state_counts does not contain the correct number of rows and/or columns.', {
415
expect_equal(dim(state_counts), c(3, 2))
416
})
417
test_that('Columns in state_counts contain incorrect values.', {
418
expect_equal(digest(sum(as.numeric(state_counts$n, na.rm = TRUE))), '4745bd90664c8cb9935cfb1a4cf51d77')
419
})
420
print("Success!")
421
}
422
423
test_2.8 <- function(){
424
properties <- c(state_counts_plot$layers[[1]]$mapping, state_counts_plot$mapping)
425
labels <- state_counts_plot$labels
426
test_that('Did not create a plot named state_counts_plot', {
427
expect_true(exists("state_counts_plot"))
428
})
429
test_that('state should be on the x-axis.', {
430
expect_true("state" == rlang::get_expr(properties$x))
431
})
432
test_that('n should be on the y-axis.', {
433
expect_true("n" == rlang::get_expr(properties$y))
434
})
435
test_that('state_counts_plot should be a bar plot.', {
436
expect_that("GeomBar" %in% c(class(state_counts_plot$layers[[1]]$geom)) , is_true())
437
})
438
test_that('Labels on the axes and legend should be descriptive and human readable.', {
439
expect_that((labels$y) == 'n', is_false())
440
expect_that((labels$x) == 'state', is_false())
441
})
442
print("Success!")
443
}
444
445
test_2.9.1 <- function(){
446
test_that('Solution is incorrect', {
447
expect_equal(digest(answer2.9.1), '2bedd54d48692762c119b27f5ec7a320')
448
})
449
print("Success!")
450
}
451
452
test_2.9.2 <- function(){
453
test_that('Did not create an object named top_n_state', {
454
expect_true(exists("top_n_state"))
455
})
456
test_that('The state column in top_n_state should only contain CA, OR, and WA', {
457
expect_equal(digest(top_n_state$state), '8e6ddcfd9ebaf85379a6d46f7949bce0')
458
})
459
test_that('top_n_state does not contain the correct number of rows and/or columns.', {
460
expect_equal(dim(top_n_state), c(27, 3))
461
})
462
test_that('Columns in top_n_state contain incorrect values.', {
463
expect_equal(digest(sum(as.numeric(top_n_state$n, na.rm = TRUE))), '4745bd90664c8cb9935cfb1a4cf51d77')
464
})
465
print("Success!")
466
}
467
468
test_2.9.3 <- function(){
469
properties <- c(top_n_state_plot$layers[[1]]$mapping, top_n_state_plot$mapping)
470
labels <- top_n_state_plot$labels
471
test_that('Did not create a plot named top_n_state_plot', {
472
expect_true(exists("top_n_state_plot"))
473
})
474
test_that('state should be on the x-axis.', {
475
expect_true("state" == rlang::get_expr(properties$x))
476
})
477
test_that('n should be on the y-axis.', {
478
expect_true("n" == rlang::get_expr(properties$y))
479
})
480
test_that('name should be used to determine bar fill colour.', {
481
expect_true("name" == rlang::get_expr(properties$fill))
482
})
483
test_that('top_n_state_plot should be a bar plot.', {
484
expect_that("GeomBar" %in% c(class(top_n_state_plot$layers[[1]]$geom)) , is_true())
485
})
486
test_that('Labels on the axes and legend should be descriptive and human readable.', {
487
expect_that((labels$y) == 'n', is_false())
488
expect_that((labels$x) == 'state', is_false())
489
expect_that((labels$fill) == 'name', is_false())
490
})
491
print("Success!")
492
}
493
494
test_2.9.4 <- function(){
495
properties <- c(top_n_state_plot$layers[[1]]$mapping, top_n_state_plot$mapping)
496
labels <- top_n_state_plot$labels
497
test_that('Did not create a plot named top_n_state_plot', {
498
expect_true(exists("top_n_state_plot"))
499
})
500
test_that('state should be on the x-axis.', {
501
expect_true("state" == rlang::get_expr(properties$x))
502
})
503
test_that('n should be on the y-axis.', {
504
expect_true("n" == rlang::get_expr(properties$y))
505
})
506
test_that('name should be used to determine bar fill colour.', {
507
expect_true("name" == rlang::get_expr(properties$fill))
508
})
509
test_that('top_n_state_plot should be a bar plot.', {
510
expect_that("GeomBar" %in% c(class(top_n_state_plot$layers[[1]]$geom)) , is_true())
511
})
512
test_that('Labels on the axes and legend should be descriptive and human readable.', {
513
expect_that((labels$y) == 'n', is_false())
514
expect_that((labels$x) == 'state', is_false())
515
expect_that((labels$x) == 'name', is_false())
516
})
517
print("Success!")
518
}
519
520
test_2.9.5 <- function(){
521
test_that('Solution is incorrect', {
522
expect_equal(digest(answer2.9.5), '0590b0427c1b19a6eb612d19888aa52f') # we hid the answer to the test here so you can't see it, but we can still run the test
523
})
524
print("Success!")
525
}
526