Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2019-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), '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.4 <- function(){
88
test_that('Did not create an object named happiness_report_path', {
89
expect_true(exists("happiness_report_path"))
90
})
91
test_that('Did not create an object named happiness_report', {
92
expect_true(exists("happiness_report"))
93
})
94
test_that('happiness_report should be a data frame.', {
95
expect_true('data.frame' %in% class(happiness_report))
96
})
97
test_that('Did not use the shortest relative path', {
98
expect_equal(digest(happiness_report_path), 'f82fd45a6bec54581361a0dfe13e9ea1')
99
})
100
test_that('happiness_report does not contain the correct information.', {
101
expect_equal(dim(happiness_report), c(155, 5))
102
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
103
expect_equal(colnames(happiness_report), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))
104
})
105
print("Success!")
106
}
107
108
test_3.5 <- function(){
109
test_that('Solution is incorrect', {
110
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
111
})
112
print("Success!")
113
}
114
115
test_3.6.1 <- function(){
116
test_that('Did not create an object named happy_semi_df', {
117
expect_true(exists("happy_semi_df"))
118
})
119
test_that('happy_semi_df should be a data frame.', {
120
expect_true('data.frame' %in% class(happy_semi_df))
121
})
122
test_that('happy_semi_df does not contain the correct information.', {
123
expect_equal(dim(happy_semi_df), c(155, 5))
124
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
125
expect_equal(colnames(happy_semi_df), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))
126
})
127
print("Success!")
128
}
129
130
test_3.6.2 <- function(){
131
test_that('Did not create an object named happy_semi_df2', {
132
expect_true(exists("happy_semi_df2"))
133
})
134
test_that('happy_semi_df2 should be a data frame.', {
135
expect_true('data.frame' %in% class(happy_semi_df2))
136
})
137
test_that('happy_semi_df2 does not contain the correct information.', {
138
expect_equal(dim(happy_semi_df2), c(155, 5))
139
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
140
expect_equal(colnames(happy_semi_df2), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))
141
})
142
print("Success!")
143
}
144
145
test_3.6.3 <- function(){
146
test_that('Did not create an object named happy_tsv', {
147
expect_true(exists("happy_tsv"))
148
})
149
test_that('happy_tsv should be a data frame.', {
150
expect_true('data.frame' %in% class(happy_tsv))
151
})
152
test_that('happy_tsv does not contain the correct information.', {
153
expect_equal(dim(happy_tsv), c(155, 5))
154
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
155
expect_equal(colnames(happy_tsv), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))
156
})
157
print("Success!")
158
}
159
160
test_3.6.4 <- function(){
161
test_that('Did not create an object named happy_metadata', {
162
expect_true(exists("happy_metadata"))
163
})
164
test_that('happy_metadata should be a data frame.', {
165
expect_true('data.frame' %in% class(happy_metadata))
166
})
167
test_that('happy_metadata does not contain the correct information.', {
168
expect_equal(dim(happy_metadata), c(155, 5))
169
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
170
expect_equal(colnames(happy_metadata), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))
171
})
172
print("Success!")
173
}
174
175
test_3.6.5 <- function(){
176
test_that('Did not create an object named happy_header', {
177
expect_true(exists("happy_header"))
178
})
179
test_that('happy_header should be a data frame.', {
180
expect_true('data.frame' %in% class(happy_header))
181
})
182
test_that('happy_header does not contain the correct information.', {
183
expect_equal(dim(happy_header), c(155, 5))
184
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
185
expect_equal(colnames(happy_header), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))
186
})
187
print("Success!")
188
}
189
190
test_3.7 <- function(){
191
test_that('Did not create an object named happy_xlsx', {
192
expect_true(exists("happy_xlsx"))
193
})
194
test_that('happy_xlsx should be a data frame.', {
195
expect_true('data.frame' %in% class(happy_xlsx))
196
})
197
test_that('happy_xlsx does not contain the correct information.', {
198
expect_equal(dim(happy_xlsx), c(155, 5))
199
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
200
expect_equal(colnames(happy_xlsx), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))
201
})
202
print("Success!")
203
}
204
205
test_3.8 <- function(){
206
test_that('Solution is incorrect', {
207
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
208
})
209
print("Success!")
210
}
211
212
test_3.9 <- function(){
213
test_that('Did not create a plot named header_plot', {
214
expect_true(exists("header_plot"))
215
})
216
test_that('GDP_per_capita should be on the x-axis', {
217
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())
218
})
219
test_that('life_expectancy should be on the y-axis', {
220
expect_that("life_expectancy" %in% c(rlang::get_expr(header_plot$mapping$y), rlang::get_expr(header_plot$layers[[1]]$mapping$y)) , is_true())
221
})
222
test_that('geom should be geom_point', {
223
expect_that("GeomPoint" %in% c(class(header_plot$layers[[1]]$geom)) , is_true())
224
})
225
print("Success!")
226
}
227
228
229
test_4.1 <- function(){
230
test_that('Solution is incorrect', {
231
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
232
})
233
print("Success!")
234
}
235
236
237
test_4.2 <- function(){
238
test_that('Solution is incorrect', {
239
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
240
})
241
print("Success!")
242
}
243
244
test_4.3.1 <- function(){
245
test_that('Did not create the conn object', {
246
expect_true(exists("conn"))
247
})
248
test_that('conn is not an RSQLite database connection object', {
249
expect_equal(digest(class(conn)), 'db665ff04541c433e1f30318e7715ef4')
250
})
251
test_that('conn was loaded from the wrong file',{
252
expect_true(grepl('data/flights_filtered.db', format(conn)))
253
})
254
test_that('conn is the wrong database',{
255
expect_equal(digest(dbListTables(conn)), '51a67410d71bf270db1994ea905cda13')
256
})
257
print("Success!")
258
}
259
260
test_4.3.2 <- function(){
261
test_that('table name is incorrect',{
262
expect_equal(digest(flights_table_name), '51a67410d71bf270db1994ea905cda13')
263
})
264
print('Success!')
265
}
266
267
test_4.3.3 <- function(){
268
test_that('Did not create an object named flight_data', {
269
expect_true(exists("flight_data"))
270
})
271
test_that('flight_data should be a data frame.', {
272
expect_true('tbl_SQLiteConnection' %in% class(flight_data))
273
})
274
test_that('flight_data does not contain the correct information.', {
275
expect_equal(digest(as_tibble(tally(flight_data))[[1]]), 'fe365cf3cbd6f3a8164444879f87385e')
276
expect_equal(digest(dim(flight_data)[2]), '8ae1ac7bdf62dca7c19b427a9153445c')
277
expect_equal(digest( as_tibble(summarize(flight_data, sum=sum(ARRIVAL_DELAY)))[[1]] ),'7c262436b16a92bd887fedb5d946ca58')
278
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'))
279
})
280
281
print("Success!")
282
}
283
284
test_4.4 <- function(){
285
test_that('Did not create an object named delay_data', {
286
expect_true(exists("delay_data"))
287
})
288
test_that('delay_data has the wrong dimensions', {
289
expect_equal(digest(as_tibble(tally(delay_data))[[1]]), 'ae6fbe0f998b93e033a6b3d7027d3500')
290
expect_equal(digest(dim(delay_data)[2]), 'c01f179e4b57ab8bd9de309e6d576c48')
291
})
292
test_that('delay data has the wrong variables', {
293
expect_equal(colnames(delay_data), c('DEPARTURE_DELAY', 'ARRIVAL_DELAY'))
294
})
295
print('Success!')
296
}
297
298
test_4.5 <- function(){
299
test_that('Did not create a plot named arrival_delay_plot', {
300
expect_true(exists("arrival_delay_plot"))
301
})
302
test_that('The axis labels are incorrect', {
303
expect_equal(digest(arrival_delay_plot$labels$x), "721f8fe183a39e61a2140d9dd01b6042")
304
expect_equal(digest(arrival_delay_plot$labels$y), '9ae00562a72e7b3aa91a39269590839a')
305
})
306
test_that('The x/y data are incorrect', {
307
expect_equal(digest(arrival_delay_plot$mapping$x), '550b90233ab71397604016f5474f572f')
308
expect_equal(digest(arrival_delay_plot$mapping$y), 'f9e884084b84794d762a535f3facec85')
309
})
310
test_that('arrival_delay_plot should be a histogram', {
311
expect_equal(digest(class(arrival_delay_plot$layers[[1]]$geom)[[1]]), '00af9702c1de4491cd9d59e3c94080a8')
312
})
313
print("Success!")
314
}
315
316
test_4.6 <- function(){
317
test_that('Did not create a plot named departure_delay_plot', {
318
expect_true(exists("departure_delay_plot"))
319
})
320
test_that('The axis labels are incorrect', {
321
expect_equal(digest(departure_delay_plot$labels$x), "721f8fe183a39e61a2140d9dd01b6042")
322
expect_equal(digest(departure_delay_plot$labels$y), '9ae00562a72e7b3aa91a39269590839a')
323
})
324
test_that('The x/y data are incorrect', {
325
expect_equal(digest(departure_delay_plot$mapping$x), 'c92e510b14ce856cce6eb14e740686b1')
326
expect_equal(digest(departure_delay_plot$mapping$y), 'f9e884084b84794d762a535f3facec85')
327
})
328
test_that('departure_delay_plot should be a histogram', {
329
expect_equal(digest(class(departure_delay_plot$layers[[1]]$geom)[[1]]), '00af9702c1de4491cd9d59e3c94080a8')
330
})
331
print("Success!")
332
}
333
334
test_4.7 <- function(){
335
test_that('Answer incorrect', {
336
expect_equal(digest(answer4.7), '7d9e25abb82fe492a7007d36124cee47')
337
})
338
print('Success!')
339
}
340
341
342
test_4.8 <- function(){
343
test_that('Did not create the delay_dataframe object', {
344
expect_true(exists("delay_dataframe"))
345
})
346
test_that("The delay_dataframe doesn't have the right data in it", {
347
expect_equal(digest(sum(delay_dataframe)), '4bac82174d1816fd23b4af094ccac10c')
348
})
349
test_that('Did not create the delay_data.csv file', {
350
expect_true(file.exists('delay_data.csv'))
351
})
352
test_that('delay_data.csv does not have the right data in it', {
353
expect_equal(digest(sum(read_csv('delay_data.csv'))), '4bac82174d1816fd23b4af094ccac10c')
354
})
355
print('Success!')
356
}
357
358
test_5.1.0 <- function(){
359
test_that('Solution is incorrect', {
360
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
361
})
362
print("Success!")
363
}
364
365
test_5.1.1 <- function(){
366
test_that('Solution is incorrect', {
367
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
368
})
369
print("Success!")
370
}
371
372
test_5.2 <- function(){
373
test_that('Did not create an object named gwp', {
374
expect_true(exists("gwp"))
375
})
376
test_that('gwp should not be a data frame.', {
377
expect_false('data.frame' %in% class(gwp))
378
})
379
test_that('gwp does not contain the correct information.', {
380
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
381
expect_that('xml_document' %in% attributes(gwp)$class, is_true())
382
})
383
print("Success!")
384
}
385
386
test_5.4 <- function(){
387
test_that('Did not create an object named gwp_value', {
388
expect_true(exists("gwp_value"))
389
})
390
test_that('gwp_value should be a vector containing characters.', {
391
expect_that(is.vector(gwp_value), is_true())
392
expect_equal(class(gwp_value), "character")
393
})
394
test_that('gwp_value should be 59 characters long.', {
395
expect_equal(length(gwp_value), 59)
396
})
397
print("Success!")
398
}
399
400
test_5.5 <- function(){
401
test_that('Did not create an object named gwp', {
402
expect_true(exists("gwp"))
403
})
404
test_that('gwp should be a data frame.', {
405
expect_true('data.frame' %in% class(gwp))
406
})
407
test_that('gwp should contain the columns: year and gwp_value', {
408
expect_that("year" %in% colnames(gwp), is_true())
409
expect_that("gwp_value" %in% colnames(gwp), is_true())
410
})
411
test_that('gwp does not contain the correct information.', {
412
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
413
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
414
})
415
print("Success!")
416
}
417
418
test_5.6 <- function(){
419
test_that('Did not create a plot named gwp_historical', {
420
expect_true(exists("gwp_historical"))
421
})
422
test_that('sqrt_year should be on the x-axis.', {
423
expect_that("sqrt_year" %in% c(rlang::get_expr(gwp_historical$mapping$x),rlang::get_expr(gwp_historical$layers[[1]]$mapping$x)), is_true())
424
})
425
test_that('gwp_value should be on the y-axis.', {
426
expect_that("gwp_value" %in% c(rlang::get_expr(gwp_historical$mapping$y), rlang::get_expr(gwp_historical$layers[[1]]$mapping$y)) , is_true())
427
})
428
test_that('gwp_historical should be a line plot.', {
429
expect_that("GeomLine" %in% c(class(gwp_historical$layers[[1]]$geom)) , is_true())
430
})
431
test_that('Labels on the axes should be descriptive and human readable.', {
432
expect_that((gwp_historical$labels$y) == 'gwp_value', is_false())
433
expect_that((gwp_historical$labels$x) == 'sqrt_year', is_false())
434
})
435
print("Success!")
436
}
437
438
test_5.7 <- function(){
439
test_that('Solution is incorrect', {
440
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
441
})
442
print("Success!")
443
}
444