Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2022-spring/materials/worksheet_viz/tests.R
2051 views
1
library(testthat)
2
library(digest)
3
4
# Round double to precise integer
5
#
6
# `int_round` works to create an integer corresponding to a number that is
7
# tested up to a particular decimal point of precision. This is useful when
8
# there is a need to compare a numeric value using hashes.
9
#
10
# @param x Double vector of length one.
11
# @param digits Double vector of length one to specify decimal point of precision. Negative numbers can be used to specifying significant digits > 0.1.
12
#
13
# @return Integer vector of length one corresponding to a particular decimal point of precision.
14
#
15
# @examples
16
# # to get an integer up to two decimals of precision from 234.56789
17
# int_round(234.56789, 2)
18
#
19
# to get an integer rounded to the hundred digit from 234.56789
20
# int_round(234.56789, -2)
21
int_round <- function(x, digits){
22
x = x * 10^digits
23
xint = as.integer(x)
24
xint1 = xint + 1L
25
if (abs(xint - x) < abs(xint1 - x)){
26
return(xint)
27
}
28
else {
29
return(xint1)
30
}
31
}
32
33
test_0.1 <- function(){
34
test_that('Solution is incorrect', {
35
expect_equal(digest(answer0.1_A), '05ca18b596514af73f6880309a21b5dd')
36
expect_equal(digest(answer0.1_B), 'd2a90307aac5ae8d0ef58e2fe730d38b')
37
expect_equal(digest(answer0.1_C), '05ca18b596514af73f6880309a21b5dd')
38
expect_equal(digest(answer0.1_D), '05ca18b596514af73f6880309a21b5dd')
39
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
40
})
41
print("Success!")
42
}
43
44
test_0.2 <- function(){
45
test_that('Solution is incorrect', {
46
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
47
})
48
print("Success!")
49
}
50
51
test_0.3 <- function(){
52
test_that('Solution is incorrect', {
53
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
54
})
55
print("Success!")
56
}
57
58
test_0.4 <- function(){
59
test_that('Solution is incorrect', {
60
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
61
})
62
print("Success!")
63
}
64
65
test_0.5 <- function(){
66
test_that('Solution is incorrect', {
67
expect_equal(digest(answer0.5_1), '75f1160e72554f4270c809f041c7a776')
68
expect_equal(digest(answer0.5_2), '01a75cb73d67b0f895ff0e61449c7bf8')
69
expect_equal(digest(answer0.5_3), '3a5505c06543876fe45598b5e5e5195d')
70
expect_equal(digest(answer0.5_4), 'f76b651ab8fcb8d470f79550bf2af53a')
71
expect_equal(digest(answer0.5_5), 'c1f86f7430df7ddb256980ea6a3b57a4')
72
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
73
})
74
print("Success!")
75
}
76
77
test_1.0 <- function(){
78
test_that('Solution is incorrect', {
79
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
80
})
81
print("Success!")
82
}
83
84
test_1.1 <- function(){
85
test_that('Did not create an object named world_vaccination', {
86
expect_true(exists("world_vaccination"))
87
})
88
test_that('world_vaccination should be a data frame.', {
89
expect_true('data.frame' %in% class(world_vaccination))
90
})
91
test_that('Did not remove NA values from the pct_vaccinated column.', {
92
expect_equal(any(is.na(world_vaccination$pct_vaccinated)), FALSE)
93
})
94
test_that('world_vaccination does not contain the correct number of rows and/or columns.', {
95
expect_equal(dim(world_vaccination), c(385, 4))
96
})
97
test_that('Columns in world_vaccination contain incorrect values.', {
98
expect_equal(digest(int_round(sum(world_vaccination$yr), 2)), 'c33bf4bae928bb4b1cd449c386b9e444')
99
})
100
print("Success!")
101
}
102
103
test_1.2 <- function(){
104
test_that('Did not create a plot named world_vacc_plot', {
105
expect_true(exists("world_vacc_plot"))
106
})
107
test_that('year should be on the x-axis.', {
108
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())
109
})
110
test_that('pct_vaccinated should be on the y-axis.', {
111
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())
112
})
113
test_that('world_vacc_plot should be a scatter plot.', {
114
expect_true("GeomPoint" %in% c(class(world_vacc_plot$layers[[1]]$geom)))
115
})
116
test_that('Labels on the axes should be descriptive and human readable.', {
117
expect_false((world_vacc_plot$labels$y) == 'pct_vaccinated')
118
expect_false((world_vacc_plot$labels$x) == 'yr')
119
})
120
print("Success!")
121
}
122
123
test_1.3 <- function(){
124
test_that('Solution is incorrect', {
125
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
126
})
127
print("Success!")
128
}
129
130
test_1.4 <- function(){
131
properties <- c(compare_vacc_plot$layers[[1]]$mapping, compare_vacc_plot$mapping)
132
labels <- compare_vacc_plot$labels
133
test_that('Did not create a plot named compare_vacc_plot', {
134
expect_true(exists("compare_vacc_plot"))
135
})
136
test_that('year should be on the x-axis.', {
137
expect_true("yr" == rlang::get_expr(properties$x))
138
})
139
test_that('pct_vaccinated should be on the y-axis.', {
140
expect_true("pct_vaccinated" == rlang::get_expr(properties$y))
141
})
142
test_that('vaccine should map to colour and shape.', {
143
expect_true("vaccine" == rlang::get_expr(properties$colour))
144
})
145
test_that('vaccine should map to shape and colour.', {
146
expect_true("vaccine" == rlang::get_expr(properties$shape))
147
})
148
test_that('vaccine should map to shape and colour.', {
149
expect_false("character" %in% class(compare_vacc_plot$layers[[1]]$mapping$shape))
150
})
151
test_that('vaccine should map to shape and colour.', {
152
expect_false("character" %in% class(compare_vacc_plot$layers[[1]]$mapping$colour))
153
})
154
test_that('compare_vacc_plot should be a scatter plot.', {
155
expect_true("GeomPoint" %in% c(class(compare_vacc_plot$layers[[1]]$geom)))
156
})
157
test_that('Labels on the axes and legend need to be changed to be descriptive, nicely formatted, and human readable.', {
158
expect_false((labels$y) == 'pct_vaccinated')
159
expect_false((labels$x) == 'yr')
160
})
161
print("Success!")
162
}
163
164
test_1.5 <- function(){
165
test_that('Did not create an object named polio', {
166
expect_true(exists("polio"))
167
})
168
test_that('The vaccine column in polio should only contain the polio vaccine.', {
169
expect_equal(unique(polio$vaccine), "polio")
170
})
171
test_that('polio does not contain the correct number of rows and/or columns.', {
172
expect_equal(dim(polio), c(228, 4))
173
})
174
test_that('Columns in polio contain incorrect values.', {
175
expect_equal(digest(int_round(sum(polio$pct_vaccinated), 2)), '58fb69cd5c4b217284c6d49e313ab0e8') # we hid the answer to the test here so you can't see it, but we can still run the test
176
})
177
print("Success!")
178
}
179
180
test_1.6 <- function(){
181
properties <- c(polio_regions$layers[[1]]$mapping, polio_regions$mapping)
182
labels <- polio_regions$labels
183
test_that('Did not create a plot named polio_regions', {
184
expect_true(exists("polio_regions"))
185
})
186
test_that('year should be on the x-axis.', {
187
expect_true("yr" == rlang::get_expr(properties$x))
188
})
189
test_that('pct_vaccinated should be on the y-axis.', {
190
expect_true("pct_vaccinated" == rlang::get_expr(properties$y))
191
})
192
test_that('who_region should map to colour and shape.', {
193
expect_true("who_region" == rlang::get_expr(properties$colour))
194
})
195
test_that('who_region should map to shape and colour.', {
196
expect_true("who_region" == rlang::get_expr(properties$shape))
197
})
198
test_that('polio_regions should be a scatter plot.', {
199
expect_true("GeomPoint" %in% c(class(polio_regions$layers[[1]]$geom)))
200
})
201
test_that('Labels on the axes and legend should be descriptive and human readable.', {
202
expect_false((labels$y) == 'pct_vaccinated')
203
expect_false((labels$x) == 'yr')
204
})
205
print("Success!")
206
}
207
208
209
test_1.7.1 <- function(){
210
211
properties <- c(polio_regions_line$layers[[1]]$mapping, polio_regions_line$mapping)
212
labels <- polio_regions_line$labels
213
214
test_that('Did not create a plot named polio_regions_line', {
215
expect_true(exists("polio_regions_line"))
216
})
217
test_that('year should be on the x-axis.', {
218
expect_true("yr" == rlang::get_expr(properties$x))
219
})
220
test_that('pct_vaccinated should be on the y-axis.', {
221
expect_true("pct_vaccinated" == rlang::get_expr(properties$y))
222
})
223
test_that('who_region should map to colour.', {
224
expect_true("who_region" == rlang::get_expr(properties$colour))
225
})
226
227
if (length(polio_regions_line$layers) == 2) {
228
test_that('who_region should map to colour.', {
229
expect_true("who_region" == rlang::get_expr(c(polio_regions_line$layers[[1]]$mapping)$colour) &
230
"who_region" == rlang::get_expr(c(polio_regions_line$layers[[2]]$mapping)$colour) |
231
"who_region" == rlang::get_expr(c(polio_regions_line$layers[[2]]$mapping)$colour) &
232
"who_region" == rlang::get_expr(c(polio_regions_line$layers[[1]]$mapping)$colour))
233
} )} else {
234
test_that('who_region should map to colour.', {
235
expect_true("who_region" == rlang::get_expr(properties$colour))
236
})
237
}
238
239
if (length(polio_regions_line$layers) == 2) { # if there are 2 layers then check if the two layers are points and lines
240
test_that('polio_regions_line should be a line plot.', {
241
expect_true("GeomPoint" %in% c(class(polio_regions_line$layers[[1]]$geom)) &
242
"GeomLine" %in% c(class(polio_regions_line$layers[[2]]$geom)) |
243
"GeomPoint" %in% c(class(polio_regions_line$layers[[2]]$geom)) &
244
"GeomLine" %in% c(class(polio_regions_line$layers[[1]]$geom)))
245
} )} else {
246
test_that('polio_regions_line should be a line plot.', {
247
expect_true("GeomLine" %in% c(class(polio_regions_line$layers[[1]]$geom)))
248
})
249
}
250
test_that('Labels on the axes should be descriptive and human readable.', {
251
expect_false((labels$y) == 'pct_vaccinated')
252
expect_false((labels$x) == 'yr')
253
})
254
print("Success!")
255
}
256
257
258
test_1.7.2 <- function(){
259
properties <- c(polio_regions_line$layers[[1]]$mapping, polio_regions_line$mapping)
260
labels <- polio_regions_line$labels
261
test_that('Did not create a plot named polio_regions_line', {
262
expect_true(exists("polio_regions_line"))
263
})
264
test_that('year should be on the x-axis.', {
265
expect_true("yr" == rlang::get_expr(properties$x))
266
})
267
test_that('pct_vaccinated should be on the y-axis.', {
268
expect_true("pct_vaccinated" == rlang::get_expr(properties$y))
269
})
270
test_that('who_region should map to colour.', {
271
expect_true("who_region" == rlang::get_expr(properties$colour))
272
})
273
274
if (length(polio_regions_line$layers) == 2) {
275
test_that('who_region should map to colour.', {
276
expect_true("who_region" == rlang::get_expr(c(polio_regions_line$layers[[1]]$mapping)$colour) &
277
"who_region" == rlang::get_expr(c(polio_regions_line$layers[[2]]$mapping)$colour) |
278
"who_region" == rlang::get_expr(c(polio_regions_line$layers[[2]]$mapping)$colour) &
279
"who_region" == rlang::get_expr(c(polio_regions_line$layers[[1]]$mapping)$colour))
280
} )} else {
281
test_that('who_region should map to colour.', {
282
expect_true("who_region" == rlang::get_expr(properties$colour))
283
})
284
}
285
286
if (length(polio_regions_line$layers) == 2) { # if there are 2 layers then check if the two layers are points and lines
287
test_that('polio_regions_line should be a line plot.', {
288
expect_true("GeomPoint" %in% c(class(polio_regions_line$layers[[1]]$geom)) &
289
"GeomLine" %in% c(class(polio_regions_line$layers[[2]]$geom)) |
290
"GeomPoint" %in% c(class(polio_regions_line$layers[[2]]$geom)) &
291
"GeomLine" %in% c(class(polio_regions_line$layers[[1]]$geom)))
292
} )} else {
293
test_that('polio_regions_line should be a line plot.', {
294
expect_true("GeomLine" %in% c(class(polio_regions_line$layers[[1]]$geom)))
295
})
296
}
297
test_that('Labels on the axes and legend should be descriptive and human readable.', {
298
expect_false((labels$y) == 'pct_vaccinated')
299
expect_false((labels$x) == 'yr')
300
expect_false((labels$colour) == 'who_region')
301
})
302
print("Success!")
303
}
304
305
306
test_1.8 <- function(){
307
properties <- c(side_by_side_world$layers[[1]]$mapping, side_by_side_world$mapping)
308
labels <- side_by_side_world$labels
309
test_that('Did not create a plot named side_by_side_world', {
310
expect_true(exists("side_by_side_world"))
311
})
312
test_that('year should be on the x-axis.', {
313
expect_true("yr" == rlang::get_expr(properties$x))
314
})
315
test_that('pct_vaccinated should be on the y-axis.', {
316
expect_true("pct_vaccinated" == rlang::get_expr(properties$y))
317
})
318
test_that('side_by_side_world should be faceted by the vaccine column.', {
319
expect_true('FacetGrid' %in% class(rlang::get_expr(side_by_side_world$facet)))
320
})
321
test_that("side_by_side_world should be split horizontally", {
322
expect_true("vaccine" %in% names(rlang::get_expr(side_by_side_world$facet$params$cols)))
323
})
324
if (length(side_by_side_world$layers) == 2) {
325
test_that('who_region should map to colour.', {
326
expect_true("who_region" == rlang::get_expr(c(side_by_side_world$layers[[1]]$mapping)$colour) &
327
"who_region" == rlang::get_expr(c(side_by_side_world$layers[[2]]$mapping)$colour) |
328
"who_region" == rlang::get_expr(c(side_by_side_world$layers[[2]]$mapping)$colour) &
329
"who_region" == rlang::get_expr(c(side_by_side_world$layers[[1]]$mapping)$colour))
330
} )} else {
331
test_that('who_region should map to colour.', {
332
expect_true("who_region" == rlang::get_expr(properties$colour))
333
})
334
}
335
336
if (length(side_by_side_world$layers) == 2) { # if there are 2 layers then check if the two layers are points and lines
337
test_that('side_by_side_world should be a line plot.', {
338
expect_true("GeomPoint" %in% c(class(side_by_side_world$layers[[1]]$geom)) &
339
"GeomLine" %in% c(class(side_by_side_world$layers[[2]]$geom)) |
340
"GeomPoint" %in% c(class(side_by_side_world$layers[[2]]$geom)) &
341
"GeomLine" %in% c(class(side_by_side_world$layers[[1]]$geom)))
342
} )} else {
343
test_that('side_by_side_world should be a line plot.', {
344
expect_true("GeomLine" %in% c(class(side_by_side_world$layers[[1]]$geom)))
345
})
346
}
347
test_that('Labels on the axes and legend should be descriptive and human readable.', {
348
expect_false((labels$y) == 'pct_vaccinated')
349
expect_false((labels$x) == 'yr')
350
expect_false((labels$colour) == 'who_region')
351
})
352
print("Success!")
353
}
354
355
test_1.9.1 <- function(){
356
properties <- c(vertical_world$layers[[1]]$mapping, vertical_world$mapping)
357
labels <- vertical_world$labels
358
test_that('Did not create a plot named vertical_world', {
359
expect_true(exists("vertical_world"))
360
})
361
test_that('year should be on the x-axis.', {
362
expect_true("yr" == rlang::get_expr(properties$x))
363
})
364
test_that('pct_vaccinated should be on the y-axis.', {
365
expect_true("pct_vaccinated" == rlang::get_expr(properties$y))
366
})
367
test_that('vertical_world should be faceted by the vaccine column.', {
368
expect_true('FacetGrid' %in% class(rlang::get_expr(vertical_world$facet)))
369
})
370
test_that("vertical_world should be split vertically", {
371
expect_true("vaccine" %in% names(rlang::get_expr(vertical_world$facet$params$rows)))
372
})
373
if (length(vertical_world$layers) == 2) {
374
test_that('who_region should map to colour.', {
375
expect_true("who_region" == rlang::get_expr(c(vertical_world$layers[[1]]$mapping)$colour) &
376
"who_region" == rlang::get_expr(c(vertical_world$layers[[2]]$mapping)$colour) |
377
"who_region" == rlang::get_expr(c(vertical_world$layers[[2]]$mapping)$colour) &
378
"who_region" == rlang::get_expr(c(vertical_world$layers[[1]]$mapping)$colour))
379
} )} else {
380
test_that('who_region should map to colour.', {
381
expect_true("who_region" == rlang::get_expr(properties$colour))
382
})
383
}
384
385
if (length(vertical_world$layers) == 2) { # if there are 2 layers then check if the two layers are points and lines
386
test_that('vertical_world should be a line plot.', {
387
expect_true("GeomPoint" %in% c(class(vertical_world$layers[[1]]$geom)) &
388
"GeomLine" %in% c(class(vertical_world$layers[[2]]$geom)) |
389
"GeomPoint" %in% c(class(vertical_world$layers[[2]]$geom)) &
390
"GeomLine" %in% c(class(vertical_world$layers[[1]]$geom)))
391
} )} else {
392
test_that('vertical_world should be a line plot.', {
393
expect_true("GeomLine" %in% c(class(vertical_world$layers[[1]]$geom)))
394
})
395
}
396
test_that('Labels on the axes and legend should be descriptive and human readable.', {
397
expect_false((labels$y) == 'pct_vaccinated')
398
expect_false((labels$x) == 'yr')
399
expect_false((labels$colour) == 'who_region')
400
})
401
print("Success!")
402
}
403
404
test_1.9.2 <- function(){
405
test_that('Solution is incorrect', {
406
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
407
})
408
print("Success!")
409
}
410
411
test_2.1 <- function(){
412
test_that('Solution is incorrect', {
413
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
414
})
415
print("Success!")
416
}
417
418
test_2.2.1 <- function(){
419
test_that('Solution is incorrect', {
420
expect_equal(digest(answer2.2.1), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test
421
})
422
print("Success!")
423
}
424
425
test_2.2 <- function(){
426
test_that('Did not create an object named fast_food', {
427
expect_true(exists("fast_food"))
428
})
429
test_that('fast_food does not contain the correct number of rows and/or columns.', {
430
expect_equal(dim(fast_food), c(10000, 2))
431
})
432
test_that('Columns in fast_food contain incorrect values.', {
433
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
434
expect_equal(digest(as.character(fast_food[[4,2]])), 'd599245d7d7e3f56863ba3a6112ca71b')
435
})
436
print("Success!")
437
}
438
439
test_2.3 <- function(){
440
test_that('Did not create an object named top_restaurants', {
441
expect_true(exists("top_restaurants"))
442
})
443
test_that('top_restaurants does not contain the correct number of rows and/or columns.', {
444
expect_equal(dim(top_restaurants), c(9, 2))
445
})
446
test_that('Columns in fast_food contain incorrect values.', {
447
expect_equal(digest(int_round(sum(as.numeric(top_restaurants$n, na.rm = TRUE)), 2)), 'aa0419a07d43532e90a1f1c2d0c2b665')
448
})
449
print("Success!")
450
}
451
452
test_2.4 <- function(){
453
properties <- c(count_bar_chart$layers[[1]]$mapping, count_bar_chart$mapping)
454
labels <- count_bar_chart$labels
455
test_that('Did not create a plot named count_bar_chart', {
456
expect_true(exists("count_bar_chart"))
457
})
458
test_that('name should be on the x-axis.', {
459
expect_true("name" == rlang::get_expr(properties$x))
460
})
461
test_that('n should be on the y-axis.', {
462
expect_true("n" == rlang::get_expr(properties$y))
463
})
464
test_that('vertical_world should be a bar plot.', {
465
expect_true("GeomBar" %in% c(class(count_bar_chart$layers[[1]]$geom)))
466
})
467
test_that('Labels on the axes and legend should be descriptive and human readable.', {
468
expect_false((labels$y) == 'n')
469
expect_false((labels$x) == 'name')
470
})
471
print("Success!")
472
}
473
474
test_2.5_A <- function(){
475
properties <- c(count_bar_chart_A$layers[[1]]$mapping, count_bar_chart_A$mapping)
476
labels <- count_bar_chart_A$labels
477
test_that('Did not create a plot named count_bar_chart_A', {
478
expect_true(exists("count_bar_chart_A"))
479
})
480
test_that('name should be on the x-axis.', {
481
expect_true("name" == rlang::get_expr(properties$x))
482
})
483
test_that('x-axis (bar) labels should be at an angle between 20 and 90 degrees.', {
484
expect_true(count_bar_chart_A$theme$axis.text.x$angle <= 90 & count_bar_chart_A$theme$axis.text.x$angle >= 20)
485
})
486
test_that('hjust should equal 1', {
487
expect_equal(digest(int_round(count_bar_chart_A$theme$axis.text.x$hjust, 2)), '5d6e7fe43b3b73e5fd2961d5162486fa')
488
})
489
test_that('n should be on the y-axis.', {
490
expect_true("n" == rlang::get_expr(properties$y))
491
})
492
test_that('vertical_world should be a bar plot.', {
493
expect_true("GeomBar" %in% c(class(count_bar_chart_A$layers[[1]]$geom)))
494
})
495
test_that('Labels on the axes and legend should be descriptive and human readable.', {
496
expect_false((labels$y) == 'n')
497
expect_false((labels$x) == 'name')
498
})
499
print("Success!")
500
}
501
502
test_2.5_B <- function(){
503
properties <- c(count_bar_chart_B$layers[[1]]$mapping, count_bar_chart_B$mapping)
504
labels <- count_bar_chart_B$labels
505
test_that('Did not create a plot named count_bar_chart_B', {
506
expect_true(exists("count_bar_chart_B"))
507
})
508
test_that('name should be on the x-axis.', {
509
expect_true("name" == rlang::get_expr(properties$x))
510
})
511
test_that('The coordinate axes should be flipped', {
512
expect_true("CoordFlip" %in% class(ggplot_build(count_bar_chart_B)$layout$coord))
513
})
514
test_that('n should be on the y-axis.', {
515
expect_true("n" == rlang::get_expr(properties$y))
516
})
517
test_that('vertical_world should be a bar plot.', {
518
expect_true("GeomBar" %in% c(class(count_bar_chart_B$layers[[1]]$geom)))
519
})
520
test_that('Labels on the axes and legend should be descriptive and human readable.', {
521
expect_false((labels$y) == 'n')
522
expect_false((labels$x) == 'name')
523
})
524
print("Success!")
525
}
526
527
test_2.6 <- function(){
528
test_that('Solution is incorrect', {
529
expect_equal(digest(answer2.6), '948a9b527842ee791d4f18fb5594fbf7')
530
})
531
print("Success!")
532
}
533
534
test_2.7 <- function(){
535
test_that('Did not create an object named state_counts', {
536
expect_true(exists("state_counts"))
537
})
538
test_that('The state column in state_counts should only contain CA, OR, and WA', {
539
expect_equal((state_counts$st), c("CA", "OR", "WA"))
540
})
541
test_that('state_counts does not contain the correct number of rows and/or columns.', {
542
expect_equal(dim(state_counts), c(3, 2))
543
})
544
test_that('Columns in state_counts contain incorrect values.', {
545
expect_equal(digest(int_round(sum(as.numeric(state_counts$n, na.rm = TRUE)), 2)), 'aa0419a07d43532e90a1f1c2d0c2b665')
546
})
547
print("Success!")
548
}
549
550
test_2.8 <- function(){
551
properties <- c(state_counts_plot$layers[[1]]$mapping, state_counts_plot$mapping)
552
labels <- state_counts_plot$labels
553
test_that('Did not create a plot named state_counts_plot', {
554
expect_true(exists("state_counts_plot"))
555
})
556
test_that('state should be on the x-axis.', {
557
expect_true("st" == rlang::get_expr(properties$x))
558
})
559
test_that('n should be on the y-axis.', {
560
expect_true("n" == rlang::get_expr(properties$y))
561
})
562
test_that('state_counts_plot should be a bar plot.', {
563
expect_true("GeomBar" %in% c(class(state_counts_plot$layers[[1]]$geom)))
564
})
565
test_that('state_counts_plot should not be filled.', {
566
expect_false("PositionFill" %in% class(state_counts_plot$layers[[1]]$position))
567
})
568
test_that('Labels on the axes and legend should be descriptive and human readable.', {
569
expect_false((labels$y) == 'n')
570
expect_false((labels$x) == 'st')
571
})
572
print("Success!")
573
}
574
575
test_2.9.0 <- function(){
576
test_that('Solution is incorrect', {
577
expect_equal(digest(answer2.9.0), '2bedd54d48692762c119b27f5ec7a320')
578
})
579
print("Success!")
580
}
581
582
test_2.9.1 <- function(){
583
test_that('Solution is incorrect', {
584
expect_equal(digest(answer2.9.1), '75f1160e72554f4270c809f041c7a776') # we hid the answer to the test here so you can't see it, but we can still run the test
585
})
586
print("Success!")
587
}
588
589
test_2.9.2 <- function(){
590
test_that('Did not create an object named top_n_state', {
591
expect_true(exists("top_n_state"))
592
})
593
test_that('The state column in top_n_state should only contain CA, OR, and WA', {
594
expect_equal(digest(top_n_state$st), '8e6ddcfd9ebaf85379a6d46f7949bce0')
595
})
596
test_that('top_n_state does not contain the correct number of rows and/or columns.', {
597
expect_equal(dim(top_n_state), c(27, 3))
598
})
599
test_that('Columns in top_n_state contain incorrect values.', {
600
expect_equal(digest(int_round(sum(top_n_state$n, na.rm = TRUE), 2)), 'aa0419a07d43532e90a1f1c2d0c2b665')
601
})
602
print("Success!")
603
}
604
605
test_2.9.3 <- function(){
606
properties <- c(top_n_state_plot$layers[[1]]$mapping, top_n_state_plot$mapping)
607
labels <- top_n_state_plot$labels
608
test_that('Did not create a plot named top_n_state_plot', {
609
expect_true(exists("top_n_state_plot"))
610
})
611
test_that('state should be on the x-axis.', {
612
expect_true("st" == rlang::get_expr(properties$x))
613
})
614
test_that('n should be on the y-axis.', {
615
expect_true("n" == rlang::get_expr(properties$y))
616
})
617
test_that('name should be used to determine bar fill colour.', {
618
expect_true("name" == rlang::get_expr(properties$fill))
619
})
620
test_that('top_n_state_plot should be a bar plot.', {
621
expect_true("GeomBar" %in% c(class(top_n_state_plot$layers[[1]]$geom)))
622
})
623
test_that('top_n_state_plot position should be dodge.', {
624
expect_true("PositionDodge" %in% class(top_n_state_plot$layers[[1]]$position))
625
})
626
test_that('Labels on the axes and legend should be descriptive and human readable.', {
627
expect_false((labels$y) == 'n')
628
expect_false((labels$x) == 'st')
629
expect_false((labels$fill) == 'name')
630
})
631
print("Success!")
632
}
633
634
test_2.9.4 <- function(){
635
properties <- c(top_n_state_plot$layers[[1]]$mapping, top_n_state_plot$mapping)
636
labels <- top_n_state_plot$labels
637
test_that('Did not create a plot named top_n_state_plot', {
638
expect_true(exists("top_n_state_plot"))
639
})
640
test_that('state should be on the x-axis.', {
641
expect_true("st" == rlang::get_expr(properties$x))
642
})
643
test_that('n should be on the y-axis.', {
644
expect_true("n" == rlang::get_expr(properties$y))
645
})
646
test_that('name should be used to determine bar fill colour.', {
647
expect_true("name" == rlang::get_expr(properties$fill))
648
})
649
test_that('top_n_state_plot should be a bar plot.', {
650
expect_true("GeomBar" %in% c(class(top_n_state_plot$layers[[1]]$geom)))
651
})
652
test_that('top_n_state_plot position should be fill', {
653
expect_true("PositionFill" %in% class(top_n_state_plot$layers[[1]]$position))
654
})
655
test_that('Labels on the axes and legend should be descriptive and human readable.', {
656
expect_false((labels$y) == 'n')
657
expect_false((labels$x) == 'st')
658
expect_false((labels$x) == 'name')
659
})
660
print("Success!")
661
}
662
663
test_2.9.5 <- function(){
664
test_that('Solution is incorrect', {
665
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
666
})
667
print("Success!")
668
}
669
670