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