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_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.1), '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(answer1.2), '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(answer1.4), '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(round(sum(as.numeric(sub(",", ".", happy_semi_df$freedom, fixed = TRUE))))), '1cb54c4e3d3eb5af32ce10cfb9e6a19a') # 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
230
properties <- c(header_plot$layers[[1]]$mapping, header_plot$mapping)
231
labels <- header_plot$labels
232
233
test_that('GDP_per_capita should be on the x-axis.', {
234
expect_true("GDP_per_capita" == rlang::get_expr(properties$x))
235
})
236
test_that('life_expectancy should be on the y-axis.', {
237
expect_true("life_expectancy" == rlang::get_expr(properties$y))
238
})
239
test_that('header_plot should be a scatter plot.', {
240
expect_that("GeomPoint" %in% c(class(header_plot$layers[[1]]$geom)) , is_true())
241
})
242
test_that('Labels on the axes should be descriptive and human readable.', {
243
expect_that((header_plot$labels$x) == 'GDP_per_capita', is_false())
244
expect_that((header_plot$labels$y) == 'life_expectancy', is_false())
245
})
246
print("Success!")
247
}
248
249
250
test_4.1 <- function(){
251
test_that('Solution is incorrect', {
252
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
253
})
254
print("Success!")
255
}
256
257
258
test_4.2 <- function(){
259
test_that('Solution is incorrect', {
260
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
261
})
262
print("Success!")
263
}
264
265
test_4.3.1 <- function(){
266
test_that('Did not create the conn object', {
267
expect_true(exists("conn"))
268
})
269
test_that('conn is not an RSQLite database connection object', {
270
expect_equal(digest(class(conn)), 'db665ff04541c433e1f30318e7715ef4')
271
})
272
test_that('conn was loaded from the wrong file',{
273
expect_true(grepl('data/flights_filtered.db', format(conn)))
274
})
275
test_that('conn is the wrong database',{
276
expect_equal(digest(dbListTables(conn)), '51a67410d71bf270db1994ea905cda13')
277
})
278
print("Success!")
279
}
280
281
test_4.3.2 <- function(){
282
test_that('table name is incorrect',{
283
expect_equal(digest(flights_table_name), '51a67410d71bf270db1994ea905cda13')
284
})
285
print('Success!')
286
}
287
288
test_4.3.3 <- function(){
289
test_that('Did not create an object named flight_data', {
290
expect_true(exists("flight_data"))
291
})
292
test_that('flight_data should be a data frame.', {
293
expect_true('tbl_SQLiteConnection' %in% class(flight_data))
294
})
295
test_that('flight_data does not contain the correct information.', {
296
expect_equal(digest(as_tibble(tally(flight_data))[[1]]), 'fe365cf3cbd6f3a8164444879f87385e')
297
expect_equal(digest(dim(flight_data)[2]), '8ae1ac7bdf62dca7c19b427a9153445c')
298
expect_equal(digest( as_tibble(summarize(flight_data, sum=sum(ARRIVAL_DELAY)))[[1]] ),'7c262436b16a92bd887fedb5d946ca58')
299
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'))
300
})
301
302
print("Success!")
303
}
304
305
test_4.4 <- function(){
306
test_that('Did not create an object named delay_data', {
307
expect_true(exists("delay_data"))
308
})
309
test_that('delay_data has the wrong dimensions', {
310
expect_equal(digest(as_tibble(tally(delay_data))[[1]]), 'ae6fbe0f998b93e033a6b3d7027d3500')
311
expect_equal(digest(dim(delay_data)[2]), 'c01f179e4b57ab8bd9de309e6d576c48')
312
})
313
test_that('delay data has the wrong variables', {
314
expect_setequal(colnames(delay_data), c('DEPARTURE_DELAY', 'ARRIVAL_DELAY'))
315
})
316
print('Success!')
317
}
318
319
test_4.5 <- function(){
320
test_that('Did not create a plot named arrival_delay_plot', {
321
expect_true(exists("arrival_delay_plot"))
322
})
323
test_that('The axis labels are incorrect', {
324
expect_equal(digest(arrival_delay_plot$labels$x), "721f8fe183a39e61a2140d9dd01b6042")
325
expect_equal(digest(arrival_delay_plot$labels$y), '9ae00562a72e7b3aa91a39269590839a')
326
})
327
test_that('The x/y data are incorrect', {
328
expect_equal(digest(arrival_delay_plot$mapping$x), '550b90233ab71397604016f5474f572f')
329
expect_equal(digest(arrival_delay_plot$mapping$y), 'f9e884084b84794d762a535f3facec85')
330
})
331
test_that('arrival_delay_plot should be a histogram', {
332
expect_equal(digest(class(arrival_delay_plot$layers[[1]]$geom)[[1]]), '00af9702c1de4491cd9d59e3c94080a8')
333
})
334
print("Success!")
335
}
336
337
test_4.6 <- function(){
338
test_that('Did not create a plot named departure_delay_plot', {
339
expect_true(exists("departure_delay_plot"))
340
})
341
test_that('The axis labels are incorrect', {
342
expect_equal(digest(departure_delay_plot$labels$x), "721f8fe183a39e61a2140d9dd01b6042")
343
expect_equal(digest(departure_delay_plot$labels$y), '9ae00562a72e7b3aa91a39269590839a')
344
})
345
test_that('The x/y data are incorrect', {
346
expect_equal(digest(departure_delay_plot$mapping$x), 'c92e510b14ce856cce6eb14e740686b1')
347
expect_equal(digest(departure_delay_plot$mapping$y), 'f9e884084b84794d762a535f3facec85')
348
})
349
test_that('departure_delay_plot should be a histogram', {
350
expect_equal(digest(class(departure_delay_plot$layers[[1]]$geom)[[1]]), '00af9702c1de4491cd9d59e3c94080a8')
351
})
352
print("Success!")
353
}
354
355
test_4.7 <- function(){
356
test_that('Answer incorrect', {
357
expect_equal(digest(answer4.7), '7d9e25abb82fe492a7007d36124cee47')
358
})
359
print('Success!')
360
}
361
362
test_4.8.1 <- function(){
363
test_that('Answer incorrect', {
364
expect_equal(digest(answer4.8.1), '75f1160e72554f4270c809f041c7a776')
365
})
366
print('Success!')
367
}
368
369
370
test_4.8.2 <- function(){
371
test_that('Did not create the delay_dataframe object', {
372
expect_true(exists("delay_dataframe"))
373
})
374
test_that("The delay_dataframe doesn't have the right data in it", {
375
expect_equal(digest(sum(delay_dataframe)), '4bac82174d1816fd23b4af094ccac10c')
376
})
377
test_that('Did not create the delay_data.csv file', {
378
expect_true(file.exists('data/delay_data.csv'))
379
})
380
test_that('delay_data.csv does not have the right data in it', {
381
expect_equal(digest(sum(read_csv('data/delay_data.csv'))), '4bac82174d1816fd23b4af094ccac10c')
382
})
383
print('Success!')
384
}
385
386
test_5.1.0 <- function(){
387
test_that('Solution is incorrect', {
388
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
389
})
390
print("Success!")
391
}
392
393
test_5.1.1 <- function(){
394
test_that('Solution is incorrect', {
395
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
396
})
397
print("Success!")
398
}
399
400
test_5.2 <- function(){
401
test_that('Did not create an object named gwp', {
402
expect_true(exists("gwp"))
403
})
404
test_that('gwp should not be a data frame.', {
405
expect_false('data.frame' %in% class(gwp))
406
})
407
test_that('gwp does not contain the correct information.', {
408
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
409
expect_that('xml_document' %in% attributes(gwp)$class, is_true())
410
})
411
print("Success!")
412
}
413
414
test_5.4 <- function(){
415
test_that('Did not create an object named gwp_value', {
416
expect_true(exists("gwp_value"))
417
})
418
test_that('gwp_value should be a vector containing characters.', {
419
expect_that(is.vector(gwp_value), is_true())
420
expect_equal(class(gwp_value), "character")
421
})
422
test_that('gwp_value should be 59 characters long.', {
423
expect_equal(length(gwp_value), 59)
424
})
425
print("Success!")
426
}
427
428
test_5.5 <- function(){
429
test_that('Did not create an object named gwp', {
430
expect_true(exists("gwp"))
431
})
432
test_that('gwp should be a data frame.', {
433
expect_true('data.frame' %in% class(gwp))
434
})
435
test_that('gwp should contain the columns: year and gwp_value', {
436
expect_that("year" %in% colnames(gwp), is_true())
437
expect_that("gwp_value" %in% colnames(gwp), is_true())
438
})
439
test_that('gwp does not contain the correct information.', {
440
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
441
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
442
})
443
print("Success!")
444
}
445
446
test_5.6 <- function(){
447
test_that('Did not create a plot named gwp_historical', {
448
expect_true(exists("gwp_historical"))
449
})
450
451
properties <- c(gwp_historical$layers[[1]]$mapping, gwp_historical$mapping)
452
labels <- gwp_historical$labels
453
454
test_that('sqrt_year should be on the x-axis.', {
455
expect_true("sqrt_year" == rlang::get_expr(properties$x))
456
})
457
test_that('gwp_value should be on the y-axis.', {
458
expect_true("gwp_value" == rlang::get_expr(properties$y))
459
})
460
test_that('gwp_historical should be a line plot.', {
461
expect_that("GeomLine" %in% c(class(gwp_historical$layers[[1]]$geom)) , is_true())
462
})
463
test_that('Labels on the axes should be descriptive and human readable.', {
464
expect_that((gwp_historical$labels$y) == 'gwp_value', is_false())
465
expect_that((gwp_historical$labels$x) == 'sqrt_year', is_false())
466
})
467
print("Success!")
468
}
469
470
test_5.7 <- function(){
471
test_that('Solution is incorrect', {
472
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
473
})
474
print("Success!")
475
}
476