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