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_02/tests_worksheet_02.R
2707 views
1
library(testthat)
2
library(digest)
3
4
test_1.1 <- function(){
5
test_that('Solution is incorrect', {
6
expect_equal(digest(answer1), 'c1f86f7430df7ddb256980ea6a3b57a4') # we hid the answer to the test here so you can't see it, but we can still run the test
7
})
8
print("Success!")
9
}
10
11
test_1.2 <- function(){
12
test_that('Solution is incorrect', {
13
expect_equal(digest(answer2), '05ca18b596514af73f6880309a21b5dd') # we hid the answer to the test here so you can't see it, but we can still run the test
14
})
15
print("Success!")
16
}
17
18
test_1.3 <- function(){
19
test_that('Solution is incorrect', {
20
expect_equal(digest(A), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test
21
expect_equal(digest(B), 'e5b57f323c7b3719bbaaf9f96b260d39') # we hid the answer to the test here so you can't see it, but we can still run the test
22
expect_equal(digest(C), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test
23
expect_equal(digest(D), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test
24
expect_equal(digest(E), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test
25
expect_equal(digest(F), 'e5b57f323c7b3719bbaaf9f96b260d39') # we hid the answer to the test here so you can't see it, but we can still run the test
26
})
27
print("Success!")
28
}
29
30
test_1.4 <- function(){
31
test_that('Solution is incorrect', {
32
expect_equal(digest(answer4), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test
33
})
34
print("Success!")
35
}
36
37
test_1.5 <- function(){
38
test_that('Solution is incorrect', {
39
expect_equal(digest(A), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test
40
expect_equal(digest(B), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test
41
expect_equal(digest(C), '5e338704a8e069ebd8b38ca71991cf94') # we hid the answer to the test here so you can't see it, but we can still run the test
42
expect_equal(digest(D), 'dbc09cba9fe2583fb01d63c70e1555a8') # we hid the answer to the test here so you can't see it, but we can still run the test
43
expect_equal(digest(E), 'e5b57f323c7b3719bbaaf9f96b260d39') # 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_2.1 <- function(){
49
test_that('Solution is incorrect', {
50
expect_equal(digest(G), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test
51
expect_equal(digest(H), 'e5b57f323c7b3719bbaaf9f96b260d39') # we hid the answer to the test here so you can't see it, but we can still run the test
52
expect_equal(digest(I), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test
53
expect_equal(digest(J), 'dbc09cba9fe2583fb01d63c70e1555a8') # 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_2.2 <- function(){
59
test_that('Solution is incorrect', {
60
expect_equal(digest(answer2.2), '05ca18b596514af73f6880309a21b5dd') # 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_2.3 <- function(){
66
test_that('Solution is incorrect', {
67
expect_that(exists('answer2.3'), is_true())
68
expect_equal(digest(answer2.3), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test
69
})
70
print("Success!")
71
}
72
73
test_3.1 <- function(){
74
test_that('Solution is incorrect', {
75
expect_equal(digest(answer3.1), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test
76
})
77
print("Success!")
78
}
79
80
test_3.2 <- function(){
81
test_that('Solution is incorrect', {
82
expect_equal(digest(answer3.2), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test
83
})
84
print("Success!")
85
}
86
87
test_3.3 <- function(){
88
test_that('Solution is incorrect', {
89
expect_equal(digest(A), '0590b0427c1b19a6eb612d19888aa52f') # we hid the answer to the test here so you can't see it, but we can still run the test
90
expect_equal(digest(B), 'e6d21e822242a5c1c9f58806024e77ba') # we hid the answer to the test here so you can't see it, but we can still run the test
91
expect_equal(digest(C), 'f2375c071669f9b6399f28e06a598e57') # we hid the answer to the test here so you can't see it, but we can still run the test
92
expect_equal(digest(D), '0590b0427c1b19a6eb612d19888aa52f') # we hid the answer to the test here so you can't see it, but we can still run the test
93
expect_equal(digest(E), '863dfc36ab2bfe97404cc8fc074a5241') # we hid the answer to the test here so you can't see it, but we can still run the test
94
expect_equal(digest(F), '3697e3a900218295975a04d2205e3518') # we hid the answer to the test here so you can't see it, but we can still run the test
95
})
96
print("Success!")
97
}
98
99
100
test_3.4 <- function(){
101
test_that('Did not create an object named happiness_report_path', {
102
expect_true(exists("happiness_report_path"))
103
})
104
test_that('Did not create an object named happiness_report', {
105
expect_true(exists("happiness_report"))
106
})
107
test_that('happiness_report should be a data frame.', {
108
expect_true('data.frame' %in% class(happiness_report))
109
})
110
test_that('Did not use the shortest relative path', {
111
expect_equal(digest(happiness_report_path), 'f82fd45a6bec54581361a0dfe13e9ea1')
112
})
113
test_that('happiness_report does not contain the correct information.', {
114
expect_equal(dim(happiness_report), c(155, 5))
115
expect_equal(digest(as.integer(sum(happiness_report$freedom))), '5dfdba537fcd9880bb235099a0b2d71b') # we hid the answer to the test here so you can't see it, but we can still run the test
116
expect_equal(colnames(happiness_report), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))
117
})
118
print("Success!")
119
}
120
121
test_3.5 <- function(){
122
test_that('Solution is incorrect', {
123
expect_equal(digest(answer3.5), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test
124
})
125
print("Success!")
126
}
127
128
test_3.6.1 <- function(){
129
test_that('Did not create an object named happy_semi_df', {
130
expect_true(exists("happy_semi_df"))
131
})
132
test_that('happy_semi_df should be a data frame.', {
133
expect_true('data.frame' %in% class(happy_semi_df))
134
})
135
test_that('happy_semi_df does not contain the correct information.', {
136
expect_equal(dim(happy_semi_df), c(155, 5))
137
expect_equal(digest(sum(as.numeric(happy_semi_df$freedom))), '9c9393e1464352cd4fbea94dfadfa02a') # we hid the answer to the test here so you can't see it, but we can still run the test
138
expect_equal(colnames(happy_semi_df), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))
139
})
140
print("Success!")
141
}
142
143
test_3.6.2 <- function(){
144
test_that('Did not create an object named happy_semi_df2', {
145
expect_true(exists("happy_semi_df2"))
146
})
147
test_that('happy_semi_df2 should be a data frame.', {
148
expect_true('data.frame' %in% class(happy_semi_df2))
149
})
150
test_that('happy_semi_df2 does not contain the correct information.', {
151
expect_equal(dim(happy_semi_df2), c(155, 5))
152
expect_equal(digest(as.integer(sum(happy_semi_df2$happiness_score))), 'c3ad708acb2b90a9e40e48f729083e69') # we hid the answer to the test here so you can't see it, but we can still run the test
153
expect_equal(colnames(happy_semi_df2), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))
154
})
155
print("Success!")
156
}
157
158
test_3.6.3 <- function(){
159
test_that('Did not create an object named happy_tsv', {
160
expect_true(exists("happy_tsv"))
161
})
162
test_that('happy_tsv should be a data frame.', {
163
expect_true('data.frame' %in% class(happy_tsv))
164
})
165
test_that('happy_tsv does not contain the correct information.', {
166
expect_equal(dim(happy_tsv), c(155, 5))
167
expect_equal(digest(as.integer(sum(happy_tsv$freedom))), '5dfdba537fcd9880bb235099a0b2d71b') # we hid the answer to the test here so you can't see it, but we can still run the test
168
expect_equal(colnames(happy_tsv), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))
169
})
170
print("Success!")
171
}
172
173
test_3.6.4 <- function(){
174
test_that('Did not create an object named happy_metadata', {
175
expect_true(exists("happy_metadata"))
176
})
177
test_that('happy_metadata should be a data frame.', {
178
expect_true('data.frame' %in% class(happy_metadata))
179
})
180
test_that('happy_metadata does not contain the correct information.', {
181
expect_equal(dim(happy_metadata), c(155, 5))
182
expect_equal(digest(as.integer(sum(happy_metadata$freedom))), '5dfdba537fcd9880bb235099a0b2d71b') # we hid the answer to the test here so you can't see it, but we can still run the test
183
expect_equal(colnames(happy_metadata), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))
184
})
185
print("Success!")
186
}
187
188
test_3.6.5 <- function(){
189
test_that('Did not create an object named happy_header', {
190
expect_true(exists("happy_header"))
191
})
192
test_that('happy_header should be a data frame.', {
193
expect_true('data.frame' %in% class(happy_header))
194
})
195
test_that('happy_header does not contain the correct information.', {
196
expect_equal(dim(happy_header), c(155, 5))
197
expect_equal(digest(as.integer(sum(happy_header$freedom))), '5dfdba537fcd9880bb235099a0b2d71b') # we hid the answer to the test here so you can't see it, but we can still run the test
198
expect_equal(colnames(happy_header), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))
199
})
200
print("Success!")
201
}
202
203
test_3.7 <- function(){
204
test_that('Did not create an object named happy_xlsx', {
205
expect_true(exists("happy_xlsx"))
206
})
207
test_that('happy_xlsx should be a data frame.', {
208
expect_true('data.frame' %in% class(happy_xlsx))
209
})
210
test_that('happy_xlsx does not contain the correct information.', {
211
expect_equal(dim(happy_xlsx), c(155, 5))
212
expect_equal(digest(as.integer(sum(happy_xlsx$freedom))), '5dfdba537fcd9880bb235099a0b2d71b') # we hid the answer to the test here so you can't see it, but we can still run the test
213
expect_equal(colnames(happy_xlsx), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))
214
})
215
print("Success!")
216
}
217
218
test_3.8 <- function(){
219
test_that('Solution is incorrect', {
220
expect_equal(digest(answer3.8), '0590b0427c1b19a6eb612d19888aa52f') # we hid the answer to the test here so you can't see it, but we can still run the test
221
})
222
print("Success!")
223
}
224
225
test_3.9 <- function(){
226
test_that('Did not create a plot named header_plot', {
227
expect_true(exists("header_plot"))
228
})
229
test_that('GDP_per_capita should be on the x-axis', {
230
expect_that("GDP_per_capita" %in% c(rlang::get_expr(header_plot$mapping$x), rlang::get_expr(header_plot$layers[[1]]$mapping$x)) , is_true())
231
})
232
test_that('life_expectancy should be on the y-axis', {
233
expect_that("life_expectancy" %in% c(rlang::get_expr(header_plot$mapping$y), rlang::get_expr(header_plot$layers[[1]]$mapping$y)) , is_true())
234
})
235
test_that('geom should be geom_point', {
236
expect_that("GeomPoint" %in% c(class(header_plot$layers[[1]]$geom)) , is_true())
237
})
238
print("Success!")
239
}
240
241
242
test_4.1 <- function(){
243
test_that('Solution is incorrect', {
244
expect_equal(digest(answer4.1), 'd2a90307aac5ae8d0ef58e2fe730d38b') # we hid the answer to the test here so you can't see it, but we can still run the test
245
})
246
print("Success!")
247
}
248
249
250
test_4.2 <- function(){
251
test_that('Solution is incorrect', {
252
expect_equal(digest(answer4.2), 'c1f86f7430df7ddb256980ea6a3b57a4') # we hid the answer to the test here so you can't see it, but we can still run the test
253
})
254
print("Success!")
255
}
256
257
test_4.3.1 <- function(){
258
test_that('Did not create the conn object', {
259
expect_true(exists("conn"))
260
})
261
test_that('conn is not an RSQLite database connection object', {
262
expect_equal(digest(class(conn)), 'db665ff04541c433e1f30318e7715ef4')
263
})
264
test_that('conn was loaded from the wrong file',{
265
expect_true(grepl('data/flights_filtered.db', format(conn)))
266
})
267
test_that('conn is the wrong database',{
268
expect_equal(digest(dbListTables(conn)), '51a67410d71bf270db1994ea905cda13')
269
})
270
print("Success!")
271
}
272
273
test_4.3.2 <- function(){
274
test_that('table name is incorrect',{
275
expect_equal(digest(flights_table_name), '51a67410d71bf270db1994ea905cda13')
276
})
277
print('Success!')
278
}
279
280
test_4.3.3 <- function(){
281
test_that('Did not create an object named flight_data', {
282
expect_true(exists("flight_data"))
283
})
284
test_that('flight_data should be a data frame.', {
285
expect_true('tbl_SQLiteConnection' %in% class(flight_data))
286
})
287
test_that('flight_data does not contain the correct information.', {
288
expect_equal(digest(as_tibble(tally(flight_data))[[1]]), 'fe365cf3cbd6f3a8164444879f87385e')
289
expect_equal(digest(dim(flight_data)[2]), '8ae1ac7bdf62dca7c19b427a9153445c')
290
expect_equal(digest( as_tibble(summarize(flight_data, sum=sum(ARRIVAL_DELAY)))[[1]] ),'7c262436b16a92bd887fedb5d946ca58')
291
expect_equal(colnames(flight_data), c('YEAR', 'MONTH', 'DAY', 'DAY_OF_WEEK', 'ORIGIN_AIRPORT', 'DESTINATION_AIRPORT', 'DISTANCE', 'SCHEDULED_DEPARTURE', 'DEPARTURE_DELAY', 'SCHEDULED_ARRIVAL', 'ARRIVAL_DELAY', 'DIVERTED', 'CANCELLED'))
292
})
293
294
print("Success!")
295
}
296
297
test_4.4 <- function(){
298
test_that('Did not create an object named delay_data', {
299
expect_true(exists("delay_data"))
300
})
301
test_that('delay_data has the wrong dimensions', {
302
expect_equal(digest(as_tibble(tally(delay_data))[[1]]), 'ae6fbe0f998b93e033a6b3d7027d3500')
303
expect_equal(digest(dim(delay_data)[2]), 'c01f179e4b57ab8bd9de309e6d576c48')
304
})
305
test_that('delay data has the wrong variables', {
306
expect_setequal(colnames(delay_data), c('DEPARTURE_DELAY', 'ARRIVAL_DELAY'))
307
})
308
print('Success!')
309
}
310
311
test_4.5 <- function(){
312
test_that('Did not create a plot named arrival_delay_plot', {
313
expect_true(exists("arrival_delay_plot"))
314
})
315
test_that('The axis labels are incorrect', {
316
expect_equal(digest(arrival_delay_plot$labels$x), "721f8fe183a39e61a2140d9dd01b6042")
317
expect_equal(digest(arrival_delay_plot$labels$y), '9ae00562a72e7b3aa91a39269590839a')
318
})
319
test_that('The x/y data are incorrect', {
320
expect_equal(digest(arrival_delay_plot$mapping$x), '550b90233ab71397604016f5474f572f')
321
expect_equal(digest(arrival_delay_plot$mapping$y), 'f9e884084b84794d762a535f3facec85')
322
})
323
test_that('arrival_delay_plot should be a histogram', {
324
expect_equal(digest(class(arrival_delay_plot$layers[[1]]$geom)[[1]]), '00af9702c1de4491cd9d59e3c94080a8')
325
})
326
print("Success!")
327
}
328
329
test_4.6 <- function(){
330
test_that('Did not create a plot named departure_delay_plot', {
331
expect_true(exists("departure_delay_plot"))
332
})
333
test_that('The axis labels are incorrect', {
334
expect_equal(digest(departure_delay_plot$labels$x), "721f8fe183a39e61a2140d9dd01b6042")
335
expect_equal(digest(departure_delay_plot$labels$y), '9ae00562a72e7b3aa91a39269590839a')
336
})
337
test_that('The x/y data are incorrect', {
338
expect_equal(digest(departure_delay_plot$mapping$x), 'c92e510b14ce856cce6eb14e740686b1')
339
expect_equal(digest(departure_delay_plot$mapping$y), 'f9e884084b84794d762a535f3facec85')
340
})
341
test_that('departure_delay_plot should be a histogram', {
342
expect_equal(digest(class(departure_delay_plot$layers[[1]]$geom)[[1]]), '00af9702c1de4491cd9d59e3c94080a8')
343
})
344
print("Success!")
345
}
346
347
test_4.7 <- function(){
348
test_that('Answer incorrect', {
349
expect_equal(digest(answer4.7), '7d9e25abb82fe492a7007d36124cee47')
350
})
351
print('Success!')
352
}
353
354
test_4.8.1 <- function(){
355
test_that('Answer incorrect', {
356
expect_equal(digest(answer4.8.1), '75f1160e72554f4270c809f041c7a776')
357
})
358
print('Success!')
359
}
360
361
362
test_4.8.2 <- function(){
363
test_that('Did not create the delay_dataframe object', {
364
expect_true(exists("delay_dataframe"))
365
})
366
test_that("The delay_dataframe doesn't have the right data in it", {
367
expect_equal(digest(sum(delay_dataframe)), '4bac82174d1816fd23b4af094ccac10c')
368
})
369
test_that('Did not create the delay_data.csv file', {
370
expect_true(file.exists('data/delay_data.csv'))
371
})
372
test_that('delay_data.csv does not have the right data in it', {
373
expect_equal(digest(sum(read_csv('data/delay_data.csv'))), '4bac82174d1816fd23b4af094ccac10c')
374
})
375
print('Success!')
376
}
377
378
test_5.1.0 <- function(){
379
test_that('Solution is incorrect', {
380
expect_equal(digest(as.character(answer5.1.0)), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test
381
})
382
print("Success!")
383
}
384
385
test_5.1.1 <- function(){
386
test_that('Solution is incorrect', {
387
expect_equal(digest(as.character(answer5.1.1)), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test
388
})
389
print("Success!")
390
}
391
392
test_5.2 <- function(){
393
test_that('Did not create an object named gwp', {
394
expect_true(exists("gwp"))
395
})
396
test_that('gwp should not be a data frame.', {
397
expect_false('data.frame' %in% class(gwp))
398
})
399
test_that('gwp does not contain the correct information.', {
400
expect_equal(digest(as.numeric(length(gwp))), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test
401
expect_that('xml_document' %in% attributes(gwp)$class, is_true())
402
})
403
print("Success!")
404
}
405
406
test_5.4 <- function(){
407
test_that('Did not create an object named gwp_value', {
408
expect_true(exists("gwp_value"))
409
})
410
test_that('gwp_value should be a vector containing characters.', {
411
expect_that(is.vector(gwp_value), is_true())
412
expect_equal(class(gwp_value), "character")
413
})
414
test_that('gwp_value should be 59 characters long.', {
415
expect_equal(length(gwp_value), 59)
416
})
417
print("Success!")
418
}
419
420
test_5.5 <- function(){
421
test_that('Did not create an object named gwp', {
422
expect_true(exists("gwp"))
423
})
424
test_that('gwp should be a data frame.', {
425
expect_true('data.frame' %in% class(gwp))
426
})
427
test_that('gwp should contain the columns: year and gwp_value', {
428
expect_that("year" %in% colnames(gwp), is_true())
429
expect_that("gwp_value" %in% colnames(gwp), is_true())
430
})
431
test_that('gwp does not contain the correct information.', {
432
expect_equal(digest(as.numeric(sum(gwp$year))), 'a531a2d37eb64bf5b3ffb7ee17dbaedc') # we hid the answer to the test here so you can't see it, but we can still run the test
433
expect_equal(digest(as.numeric(sum(gwp$gwp_value))), 'f555139e88b37cd50d8c5d170ea575bd') # we hid the answer to the test here so you can't see it, but we can still run the test
434
})
435
print("Success!")
436
}
437
438
test_5.6 <- function(){
439
test_that('Did not create a plot named gwp_historical', {
440
expect_true(exists("gwp_historical"))
441
})
442
test_that('sqrt_year should be on the x-axis.', {
443
expect_that("sqrt_year" %in% c(rlang::get_expr(gwp_historical$mapping$x),rlang::get_expr(gwp_historical$layers[[1]]$mapping$x)), is_true())
444
})
445
test_that('gwp_value should be on the y-axis.', {
446
expect_that("gwp_value" %in% c(rlang::get_expr(gwp_historical$mapping$y), rlang::get_expr(gwp_historical$layers[[1]]$mapping$y)) , is_true())
447
})
448
test_that('gwp_historical should be a line plot.', {
449
expect_that("GeomLine" %in% c(class(gwp_historical$layers[[1]]$geom)) , is_true())
450
})
451
test_that('Labels on the axes should be descriptive and human readable.', {
452
expect_that((gwp_historical$labels$y) == 'gwp_value', is_false())
453
expect_that((gwp_historical$labels$x) == 'sqrt_year', is_false())
454
})
455
print("Success!")
456
}
457
458
test_5.7 <- function(){
459
test_that('Solution is incorrect', {
460
expect_equal(digest(as.character(answer5.7)), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test
461
})
462
print("Success!")
463
}
464