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_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$yr))), '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("yr" %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) == 'yr', 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("yr" == 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('vaccine should map to shape and colour.', {
120
expect_that("character" %in% class(compare_vacc_plot$layers[[1]]$mapping$shape), is_false())
121
})
122
test_that('vaccine should map to shape and colour.', {
123
expect_that("character" %in% class(compare_vacc_plot$layers[[1]]$mapping$colour), is_false())
124
})
125
test_that('compare_vacc_plot should be a scatter plot.', {
126
expect_that("GeomPoint" %in% c(class(compare_vacc_plot$layers[[1]]$geom)) , is_true())
127
})
128
test_that('Labels on the axes and legend need to be changed to be descriptive, nicely formatted, and human readable.', {
129
expect_that((labels$y) == 'pct_vaccinated', is_false())
130
expect_that((labels$x) == 'yr', is_false())
131
expect_that((labels$colour) == 'vaccine', is_false())
132
expect_that((labels$shape) == 'vaccine', is_false())
133
})
134
print("Success!")
135
}
136
137
test_1.5 <- function(){
138
test_that('Did not create an object named polio', {
139
expect_true(exists("polio"))
140
})
141
test_that('The vaccine column in polio should only contain the polio vaccine.', {
142
expect_equal(unique(polio$vaccine), "polio")
143
})
144
test_that('polio does not contain the correct number of rows and/or columns.', {
145
expect_equal(dim(polio), c(228, 4))
146
})
147
test_that('Columns in polio contain incorrect values.', {
148
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
149
})
150
print("Success!")
151
}
152
153
test_1.6 <- function(){
154
properties <- c(polio_regions$layers[[1]]$mapping, polio_regions$mapping)
155
labels <- polio_regions$labels
156
test_that('Did not create a plot named polio_regions', {
157
expect_true(exists("polio_regions"))
158
})
159
test_that('year should be on the x-axis.', {
160
expect_true("yr" == rlang::get_expr(properties$x))
161
})
162
test_that('pct_vaccinated should be on the y-axis.', {
163
expect_true("pct_vaccinated" == rlang::get_expr(properties$y))
164
})
165
test_that('who_region should map to colour and shape.', {
166
expect_true("who_region" == rlang::get_expr(properties$colour))
167
})
168
test_that('who_region should map to shape and colour.', {
169
expect_true("who_region" == rlang::get_expr(properties$shape))
170
})
171
test_that('polio_regions should be a scatter plot.', {
172
expect_that("GeomPoint" %in% c(class(polio_regions$layers[[1]]$geom)) , is_true())
173
})
174
test_that('Labels on the axes and legend should be descriptive and human readable.', {
175
expect_that((labels$y) == 'pct_vaccinated', is_false())
176
expect_that((labels$x) == 'yr', is_false())
177
})
178
print("Success!")
179
}
180
181
182
test_1.7.1 <- function(){
183
184
properties <- c(polio_regions_line$layers[[1]]$mapping, polio_regions_line$mapping)
185
labels <- polio_regions_line$labels
186
187
test_that('Did not create a plot named polio_regions_line', {
188
expect_true(exists("polio_regions_line"))
189
})
190
test_that('year should be on the x-axis.', {
191
expect_true("yr" == rlang::get_expr(properties$x))
192
})
193
test_that('pct_vaccinated should be on the y-axis.', {
194
expect_true("pct_vaccinated" == rlang::get_expr(properties$y))
195
})
196
test_that('who_region should map to colour.', {
197
expect_true("who_region" == rlang::get_expr(properties$colour))
198
})
199
200
if (length(polio_regions_line$layers) == 2) {
201
test_that('who_region should map to colour.', {
202
expect_that("who_region" == rlang::get_expr(c(polio_regions_line$layers[[1]]$mapping)$colour) &
203
"who_region" == rlang::get_expr(c(polio_regions_line$layers[[2]]$mapping)$colour) |
204
"who_region" == rlang::get_expr(c(polio_regions_line$layers[[2]]$mapping)$colour) &
205
"who_region" == rlang::get_expr(c(polio_regions_line$layers[[1]]$mapping)$colour), is_true())
206
} )} else {
207
test_that('who_region should map to colour.', {
208
expect_true("who_region" == rlang::get_expr(properties$colour))
209
})
210
}
211
212
if (length(polio_regions_line$layers) == 2) { # if there are 2 layers then check if the two layers are points and lines
213
test_that('polio_regions_line should be a line plot.', {
214
expect_that("GeomPoint" %in% c(class(polio_regions_line$layers[[1]]$geom)) &
215
"GeomLine" %in% c(class(polio_regions_line$layers[[2]]$geom)) |
216
"GeomPoint" %in% c(class(polio_regions_line$layers[[2]]$geom)) &
217
"GeomLine" %in% c(class(polio_regions_line$layers[[1]]$geom)), is_true())
218
} )} else {
219
test_that('polio_regions_line should be a line plot.', {
220
expect_that("GeomLine" %in% c(class(polio_regions_line$layers[[1]]$geom)) , is_true())
221
})
222
}
223
test_that('Labels on the axes should be descriptive and human readable.', {
224
expect_that((labels$y) == 'pct_vaccinated', is_false())
225
expect_that((labels$x) == 'yr', is_false())
226
})
227
print("Success!")
228
}
229
230
231
test_1.7.2 <- function(){
232
properties <- c(polio_regions_line$layers[[1]]$mapping, polio_regions_line$mapping)
233
labels <- polio_regions_line$labels
234
test_that('Did not create a plot named polio_regions_line', {
235
expect_true(exists("polio_regions_line"))
236
})
237
test_that('year should be on the x-axis.', {
238
expect_true("yr" == rlang::get_expr(properties$x))
239
})
240
test_that('pct_vaccinated should be on the y-axis.', {
241
expect_true("pct_vaccinated" == rlang::get_expr(properties$y))
242
})
243
test_that('who_region should map to colour.', {
244
expect_true("who_region" == rlang::get_expr(properties$colour))
245
})
246
247
if (length(polio_regions_line$layers) == 2) {
248
test_that('who_region should map to colour.', {
249
expect_that("who_region" == rlang::get_expr(c(polio_regions_line$layers[[1]]$mapping)$colour) &
250
"who_region" == rlang::get_expr(c(polio_regions_line$layers[[2]]$mapping)$colour) |
251
"who_region" == rlang::get_expr(c(polio_regions_line$layers[[2]]$mapping)$colour) &
252
"who_region" == rlang::get_expr(c(polio_regions_line$layers[[1]]$mapping)$colour), is_true())
253
} )} else {
254
test_that('who_region should map to colour.', {
255
expect_true("who_region" == rlang::get_expr(properties$colour))
256
})
257
}
258
259
if (length(polio_regions_line$layers) == 2) { # if there are 2 layers then check if the two layers are points and lines
260
test_that('polio_regions_line should be a line plot.', {
261
expect_that("GeomPoint" %in% c(class(polio_regions_line$layers[[1]]$geom)) &
262
"GeomLine" %in% c(class(polio_regions_line$layers[[2]]$geom)) |
263
"GeomPoint" %in% c(class(polio_regions_line$layers[[2]]$geom)) &
264
"GeomLine" %in% c(class(polio_regions_line$layers[[1]]$geom)), is_true())
265
} )} else {
266
test_that('polio_regions_line should be a line plot.', {
267
expect_that("GeomLine" %in% c(class(polio_regions_line$layers[[1]]$geom)) , is_true())
268
})
269
}
270
test_that('Labels on the axes and legend should be descriptive and human readable.', {
271
expect_that((labels$y) == 'pct_vaccinated', is_false())
272
expect_that((labels$x) == 'yr', is_false())
273
expect_that((labels$colour) == 'who_region', is_false())
274
})
275
print("Success!")
276
}
277
278
279
test_1.8 <- function(){
280
properties <- c(side_by_side_world$layers[[1]]$mapping, side_by_side_world$mapping)
281
labels <- side_by_side_world$labels
282
test_that('Did not create a plot named side_by_side_world', {
283
expect_true(exists("side_by_side_world"))
284
})
285
test_that('year should be on the x-axis.', {
286
expect_true("yr" == rlang::get_expr(properties$x))
287
})
288
test_that('pct_vaccinated should be on the y-axis.', {
289
expect_true("pct_vaccinated" == rlang::get_expr(properties$y))
290
})
291
test_that('side_by_side_world should be faceted by the vaccine column.', {
292
expect_that('FacetGrid' %in% class(rlang::get_expr(side_by_side_world$facet)), is_true())
293
})
294
test_that("side_by_side_world should be split horizontally", {
295
expect_that("vaccine" %in% names(rlang::get_expr(side_by_side_world$facet$params$cols)), is_true())
296
})
297
if (length(side_by_side_world$layers) == 2) {
298
test_that('who_region should map to colour.', {
299
expect_that("who_region" == rlang::get_expr(c(side_by_side_world$layers[[1]]$mapping)$colour) &
300
"who_region" == rlang::get_expr(c(side_by_side_world$layers[[2]]$mapping)$colour) |
301
"who_region" == rlang::get_expr(c(side_by_side_world$layers[[2]]$mapping)$colour) &
302
"who_region" == rlang::get_expr(c(side_by_side_world$layers[[1]]$mapping)$colour), is_true())
303
} )} else {
304
test_that('who_region should map to colour.', {
305
expect_true("who_region" == rlang::get_expr(properties$colour))
306
})
307
}
308
309
if (length(side_by_side_world$layers) == 2) { # if there are 2 layers then check if the two layers are points and lines
310
test_that('side_by_side_world should be a line plot.', {
311
expect_that("GeomPoint" %in% c(class(side_by_side_world$layers[[1]]$geom)) &
312
"GeomLine" %in% c(class(side_by_side_world$layers[[2]]$geom)) |
313
"GeomPoint" %in% c(class(side_by_side_world$layers[[2]]$geom)) &
314
"GeomLine" %in% c(class(side_by_side_world$layers[[1]]$geom)), is_true())
315
} )} else {
316
test_that('side_by_side_world should be a line plot.', {
317
expect_that("GeomLine" %in% c(class(side_by_side_world$layers[[1]]$geom)) , is_true())
318
})
319
}
320
test_that('Labels on the axes and legend should be descriptive and human readable.', {
321
expect_that((labels$y) == 'pct_vaccinated', is_false())
322
expect_that((labels$x) == 'yr', is_false())
323
expect_that((labels$colour) == 'who_region', is_false())
324
})
325
print("Success!")
326
}
327
328
test_1.9.1 <- function(){
329
properties <- c(vertical_world$layers[[1]]$mapping, vertical_world$mapping)
330
labels <- vertical_world$labels
331
test_that('Did not create a plot named vertical_world', {
332
expect_true(exists("vertical_world"))
333
})
334
test_that('year should be on the x-axis.', {
335
expect_true("yr" == rlang::get_expr(properties$x))
336
})
337
test_that('pct_vaccinated should be on the y-axis.', {
338
expect_true("pct_vaccinated" == rlang::get_expr(properties$y))
339
})
340
test_that('vertical_world should be faceted by the vaccine column.', {
341
expect_that('FacetGrid' %in% class(rlang::get_expr(vertical_world$facet)), is_true())
342
})
343
test_that("vertical_world should be split vertically", {
344
expect_that("vaccine" %in% names(rlang::get_expr(vertical_world$facet$params$rows)), is_true())
345
})
346
if (length(vertical_world$layers) == 2) {
347
test_that('who_region should map to colour.', {
348
expect_that("who_region" == rlang::get_expr(c(vertical_world$layers[[1]]$mapping)$colour) &
349
"who_region" == rlang::get_expr(c(vertical_world$layers[[2]]$mapping)$colour) |
350
"who_region" == rlang::get_expr(c(vertical_world$layers[[2]]$mapping)$colour) &
351
"who_region" == rlang::get_expr(c(vertical_world$layers[[1]]$mapping)$colour), is_true())
352
} )} else {
353
test_that('who_region should map to colour.', {
354
expect_true("who_region" == rlang::get_expr(properties$colour))
355
})
356
}
357
358
if (length(vertical_world$layers) == 2) { # if there are 2 layers then check if the two layers are points and lines
359
test_that('vertical_world should be a line plot.', {
360
expect_that("GeomPoint" %in% c(class(vertical_world$layers[[1]]$geom)) &
361
"GeomLine" %in% c(class(vertical_world$layers[[2]]$geom)) |
362
"GeomPoint" %in% c(class(vertical_world$layers[[2]]$geom)) &
363
"GeomLine" %in% c(class(vertical_world$layers[[1]]$geom)), is_true())
364
} )} else {
365
test_that('vertical_world should be a line plot.', {
366
expect_that("GeomLine" %in% c(class(vertical_world$layers[[1]]$geom)) , is_true())
367
})
368
}
369
test_that('Labels on the axes and legend should be descriptive and human readable.', {
370
expect_that((labels$y) == 'pct_vaccinated', is_false())
371
expect_that((labels$x) == 'yr', is_false())
372
expect_that((labels$colour) == 'who_region', is_false())
373
})
374
print("Success!")
375
}
376
377
test_1.9.2 <- function(){
378
test_that('Solution is incorrect', {
379
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
380
})
381
print("Success!")
382
}
383
384
test_2.1 <- function(){
385
test_that('Solution is incorrect', {
386
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
387
})
388
print("Success!")
389
}
390
391
test_2.2 <- function(){
392
test_that('Did not create an object named fast_food', {
393
expect_true(exists("fast_food"))
394
})
395
test_that('fast_food does not contain the correct number of rows and/or columns.', {
396
expect_equal(dim(fast_food), c(10000, 2))
397
})
398
test_that('Columns in fast_food contain incorrect values.', {
399
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
400
expect_equal(digest(as.character(fast_food[[4,2]])), 'd599245d7d7e3f56863ba3a6112ca71b')
401
})
402
print("Success!")
403
}
404
405
test_2.3 <- function(){
406
test_that('Did not create an object named top_restaurants', {
407
expect_true(exists("top_restaurants"))
408
})
409
test_that('top_restaurants does not contain the correct number of rows and/or columns.', {
410
expect_equal(dim(top_restaurants), c(9, 2))
411
})
412
test_that('Columns in fast_food contain incorrect values.', {
413
expect_equal(digest(sum(as.numeric(top_restaurants$n, na.rm = TRUE))), '4745bd90664c8cb9935cfb1a4cf51d77')
414
})
415
print("Success!")
416
}
417
418
test_2.4 <- function(){
419
properties <- c(count_bar_chart$layers[[1]]$mapping, count_bar_chart$mapping)
420
labels <- count_bar_chart$labels
421
test_that('Did not create a plot named count_bar_chart', {
422
expect_true(exists("count_bar_chart"))
423
})
424
test_that('name should be on the x-axis.', {
425
expect_true("name" == rlang::get_expr(properties$x))
426
})
427
test_that('n should be on the y-axis.', {
428
expect_true("n" == rlang::get_expr(properties$y))
429
})
430
test_that('vertical_world should be a bar plot.', {
431
expect_that("GeomBar" %in% c(class(count_bar_chart$layers[[1]]$geom)) , is_true())
432
})
433
test_that('Labels on the axes and legend should be descriptive and human readable.', {
434
expect_that((labels$y) == 'n', is_false())
435
expect_that((labels$x) == 'name', is_false())
436
})
437
print("Success!")
438
}
439
440
test_2.5_A <- function(){
441
properties <- c(count_bar_chart_A$layers[[1]]$mapping, count_bar_chart_A$mapping)
442
labels <- count_bar_chart_A$labels
443
test_that('Did not create a plot named count_bar_chart_A', {
444
expect_true(exists("count_bar_chart_A"))
445
})
446
test_that('name should be on the x-axis.', {
447
expect_true("name" == rlang::get_expr(properties$x))
448
})
449
test_that('x-axis (bar) labels should be at an angle between 20 and 90 degrees.', {
450
expect_that(count_bar_chart_A$theme$axis.text.x$angle <= 90 & count_bar_chart_A$theme$axis.text.x$angle >= 20, is_true())
451
})
452
test_that('hjust should equal 1', {
453
expect_equal(digest(as.numeric(count_bar_chart_A$theme$axis.text.x$hjust)), '6717f2823d3202449301145073ab8719')
454
})
455
test_that('n should be on the y-axis.', {
456
expect_true("n" == rlang::get_expr(properties$y))
457
})
458
test_that('vertical_world should be a bar plot.', {
459
expect_that("GeomBar" %in% c(class(count_bar_chart_A$layers[[1]]$geom)) , is_true())
460
})
461
test_that('Labels on the axes and legend should be descriptive and human readable.', {
462
expect_that((labels$y) == 'n', is_false())
463
expect_that((labels$x) == 'name', is_false())
464
})
465
print("Success!")
466
}
467
468
test_2.5_B <- function(){
469
properties <- c(count_bar_chart_B$layers[[1]]$mapping, count_bar_chart_B$mapping)
470
labels <- count_bar_chart_B$labels
471
test_that('Did not create a plot named count_bar_chart_B', {
472
expect_true(exists("count_bar_chart_B"))
473
})
474
test_that('name should be on the x-axis.', {
475
expect_true("name" == rlang::get_expr(properties$x))
476
})
477
test_that('The coordinate axes should be flipped', {
478
expect_true("CoordFlip" %in% class(ggplot_build(count_bar_chart_B)$layout$coord))
479
})
480
test_that('n should be on the y-axis.', {
481
expect_true("n" == rlang::get_expr(properties$y))
482
})
483
test_that('vertical_world should be a bar plot.', {
484
expect_that("GeomBar" %in% c(class(count_bar_chart_B$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) == 'name', is_false())
489
})
490
print("Success!")
491
}
492
493
test_2.6 <- function(){
494
test_that('Solution is incorrect', {
495
expect_equal(digest(answer2.6), '948a9b527842ee791d4f18fb5594fbf7')
496
})
497
print("Success!")
498
}
499
500
test_2.7 <- function(){
501
test_that('Did not create an object named state_counts', {
502
expect_true(exists("state_counts"))
503
})
504
test_that('The state column in state_counts should only contain CA, OR, and WA', {
505
expect_equal((state_counts$st), c("CA", "OR", "WA"))
506
})
507
test_that('state_counts does not contain the correct number of rows and/or columns.', {
508
expect_equal(dim(state_counts), c(3, 2))
509
})
510
test_that('Columns in state_counts contain incorrect values.', {
511
expect_equal(digest(sum(as.numeric(state_counts$n, na.rm = TRUE))), '4745bd90664c8cb9935cfb1a4cf51d77')
512
})
513
print("Success!")
514
}
515
516
test_2.8 <- function(){
517
properties <- c(state_counts_plot$layers[[1]]$mapping, state_counts_plot$mapping)
518
labels <- state_counts_plot$labels
519
test_that('Did not create a plot named state_counts_plot', {
520
expect_true(exists("state_counts_plot"))
521
})
522
test_that('state should be on the x-axis.', {
523
expect_true("st" == rlang::get_expr(properties$x))
524
})
525
test_that('n should be on the y-axis.', {
526
expect_true("n" == rlang::get_expr(properties$y))
527
})
528
test_that('state_counts_plot should be a bar plot.', {
529
expect_that("GeomBar" %in% c(class(state_counts_plot$layers[[1]]$geom)) , is_true())
530
})
531
test_that('state_counts_plot should not be filled.', {
532
expect_that("PositionFill" %in% class(state_counts_plot$layers[[1]]$position), is_false())
533
})
534
test_that('Labels on the axes and legend should be descriptive and human readable.', {
535
expect_that((labels$y) == 'n', is_false())
536
expect_that((labels$x) == 'st', is_false())
537
})
538
print("Success!")
539
}
540
541
test_2.9.1 <- function(){
542
test_that('Solution is incorrect', {
543
expect_equal(digest(answer2.9.1), '2bedd54d48692762c119b27f5ec7a320')
544
})
545
print("Success!")
546
}
547
548
test_2.9.2 <- function(){
549
test_that('Did not create an object named top_n_state', {
550
expect_true(exists("top_n_state"))
551
})
552
test_that('The state column in top_n_state should only contain CA, OR, and WA', {
553
expect_equal(digest(top_n_state$st), '8e6ddcfd9ebaf85379a6d46f7949bce0')
554
})
555
test_that('top_n_state does not contain the correct number of rows and/or columns.', {
556
expect_equal(dim(top_n_state), c(27, 3))
557
})
558
test_that('Columns in top_n_state contain incorrect values.', {
559
expect_equal(digest(sum(as.numeric(top_n_state$n, na.rm = TRUE))), '4745bd90664c8cb9935cfb1a4cf51d77')
560
})
561
print("Success!")
562
}
563
564
test_2.9.3 <- function(){
565
properties <- c(top_n_state_plot$layers[[1]]$mapping, top_n_state_plot$mapping)
566
labels <- top_n_state_plot$labels
567
test_that('Did not create a plot named top_n_state_plot', {
568
expect_true(exists("top_n_state_plot"))
569
})
570
test_that('state should be on the x-axis.', {
571
expect_true("st" == rlang::get_expr(properties$x))
572
})
573
test_that('n should be on the y-axis.', {
574
expect_true("n" == rlang::get_expr(properties$y))
575
})
576
test_that('name should be used to determine bar fill colour.', {
577
expect_true("name" == rlang::get_expr(properties$fill))
578
})
579
test_that('top_n_state_plot should be a bar plot.', {
580
expect_that("GeomBar" %in% c(class(top_n_state_plot$layers[[1]]$geom)) , is_true())
581
})
582
test_that('top_n_state_plot position should be dodge.', {
583
expect_that("PositionDodge" %in% class(top_n_state_plot$layers[[1]]$position) , is_true())
584
})
585
test_that('Labels on the axes and legend should be descriptive and human readable.', {
586
expect_that((labels$y) == 'n', is_false())
587
expect_that((labels$x) == 'st', is_false())
588
expect_that((labels$fill) == 'name', is_false())
589
})
590
print("Success!")
591
}
592
593
test_2.9.4 <- function(){
594
properties <- c(top_n_state_plot$layers[[1]]$mapping, top_n_state_plot$mapping)
595
labels <- top_n_state_plot$labels
596
test_that('Did not create a plot named top_n_state_plot', {
597
expect_true(exists("top_n_state_plot"))
598
})
599
test_that('state should be on the x-axis.', {
600
expect_true("st" == rlang::get_expr(properties$x))
601
})
602
test_that('n should be on the y-axis.', {
603
expect_true("n" == rlang::get_expr(properties$y))
604
})
605
test_that('name should be used to determine bar fill colour.', {
606
expect_true("name" == rlang::get_expr(properties$fill))
607
})
608
test_that('top_n_state_plot should be a bar plot.', {
609
expect_that("GeomBar" %in% c(class(top_n_state_plot$layers[[1]]$geom)) , is_true())
610
})
611
test_that('top_n_state_plot position should be fill', {
612
expect_that("PositionFill" %in% class(top_n_state_plot$layers[[1]]$position) , is_true())
613
})
614
test_that('Labels on the axes and legend should be descriptive and human readable.', {
615
expect_that((labels$y) == 'n', is_false())
616
expect_that((labels$x) == 'st', is_false())
617
expect_that((labels$x) == 'name', is_false())
618
})
619
print("Success!")
620
}
621
622
test_2.9.5 <- function(){
623
test_that('Solution is incorrect', {
624
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
625
})
626
print("Success!")
627
}
628