Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2021-fall/materials/worksheet_02/tests_worksheet_02.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_1.1 <- function(){
34
test_that('Solution is incorrect', {
35
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
36
})
37
print("Success!")
38
}
39
40
test_1.2 <- function(){
41
test_that('Solution is incorrect', {
42
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
43
})
44
print("Success!")
45
}
46
47
test_1.3 <- function(){
48
test_that('Solution is incorrect', {
49
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
50
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
51
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
52
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
53
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
54
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
55
})
56
print("Success!")
57
}
58
59
test_1.4 <- function(){
60
test_that('Solution is incorrect', {
61
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
62
})
63
print("Success!")
64
}
65
66
test_1.5 <- function(){
67
test_that('Solution is incorrect', {
68
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
69
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
70
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
71
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
72
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
73
})
74
print("Success!")
75
}
76
77
test_2.1 <- function(){
78
test_that('Solution is incorrect', {
79
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
80
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
81
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
82
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
83
})
84
print("Success!")
85
}
86
87
test_2.2 <- function(){
88
test_that('Solution is incorrect', {
89
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
90
})
91
print("Success!")
92
}
93
94
test_2.3 <- function(){
95
test_that('Solution is incorrect', {
96
expect_that(exists('answer2.3'), is_true())
97
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
98
})
99
print("Success!")
100
}
101
102
test_3.1 <- function(){
103
test_that('Solution is incorrect', {
104
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
105
})
106
print("Success!")
107
}
108
109
test_3.2 <- function(){
110
test_that('Solution is incorrect', {
111
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
112
})
113
print("Success!")
114
}
115
116
test_3.3 <- function(){
117
test_that('Solution is incorrect', {
118
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
119
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
120
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
121
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
122
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
123
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
124
})
125
print("Success!")
126
}
127
128
129
test_3.4 <- function(){
130
test_that('Did not create an object named happiness_report_path', {
131
expect_true(exists("happiness_report_path"))
132
})
133
test_that('Did not create an object named happiness_report', {
134
expect_true(exists("happiness_report"))
135
})
136
test_that('happiness_report should be a data frame.', {
137
expect_true('data.frame' %in% class(happiness_report))
138
})
139
test_that('Did not use the shortest relative path', {
140
expect_equal(digest(happiness_report_path), 'f82fd45a6bec54581361a0dfe13e9ea1')
141
})
142
test_that('happiness_report does not contain the correct information.', {
143
expect_equal(dim(happiness_report), c(155, 5))
144
expect_equal(digest(int_round(sum(happiness_report$freedom), 2)), 'fb345985a119a2b0f99a7896efa4d2c7') # we hid the answer to the test here so you can't see it, but we can still run the test
145
expect_equal(colnames(happiness_report), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))
146
})
147
print("Success!")
148
}
149
150
test_3.5 <- function(){
151
test_that('Solution is incorrect', {
152
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
153
})
154
print("Success!")
155
}
156
157
test_3.6.1 <- function(){
158
test_that('Did not create an object named happy_semi_df', {
159
expect_true(exists("happy_semi_df"))
160
})
161
test_that('happy_semi_df should be a data frame.', {
162
expect_true('data.frame' %in% class(happy_semi_df))
163
})
164
test_that('happy_semi_df does not contain the correct information.', {
165
expect_equal(dim(happy_semi_df), c(155, 5))
166
expect_equal(digest(int_round(sum(as.integer(sub(",", ".", happy_semi_df$freedom, fixed = TRUE))), 2)), '1473d70e5646a26de3c52aa1abd85b1f') # we hid the answer to the test here so you can't see it, but we can still run the test
167
expect_equal(colnames(happy_semi_df), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))
168
})
169
print("Success!")
170
}
171
172
test_3.6.2 <- function(){
173
test_that('Did not create an object named happy_semi_df2', {
174
expect_true(exists("happy_semi_df2"))
175
})
176
test_that('happy_semi_df2 should be a data frame.', {
177
expect_true('data.frame' %in% class(happy_semi_df2))
178
})
179
test_that('happy_semi_df2 does not contain the correct information.', {
180
expect_equal(dim(happy_semi_df2), c(155, 5))
181
expect_equal(digest(int_round(sum(happy_semi_df2$happiness_score), 2)), '5c379e06a33669ccfd7208cb56366ca2') # we hid the answer to the test here so you can't see it, but we can still run the test
182
expect_equal(colnames(happy_semi_df2), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))
183
})
184
print("Success!")
185
}
186
187
test_3.6.3 <- function(){
188
test_that('Did not create an object named happy_tsv', {
189
expect_true(exists("happy_tsv"))
190
})
191
test_that('happy_tsv should be a data frame.', {
192
expect_true('data.frame' %in% class(happy_tsv))
193
})
194
test_that('happy_tsv does not contain the correct information.', {
195
expect_equal(dim(happy_tsv), c(155, 5))
196
expect_equal(digest(int_round(sum(happy_tsv$freedom), 2)), 'fb345985a119a2b0f99a7896efa4d2c7') # we hid the answer to the test here so you can't see it, but we can still run the test
197
expect_equal(colnames(happy_tsv), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))
198
})
199
print("Success!")
200
}
201
202
test_3.6.4 <- function(){
203
test_that('Did not create an object named happy_metadata', {
204
expect_true(exists("happy_metadata"))
205
})
206
test_that('happy_metadata should be a data frame.', {
207
expect_true('data.frame' %in% class(happy_metadata))
208
})
209
test_that('happy_metadata does not contain the correct information.', {
210
expect_equal(dim(happy_metadata), c(155, 5))
211
expect_equal(digest(int_round(sum(happy_metadata$freedom), 2)), 'fb345985a119a2b0f99a7896efa4d2c7') # we hid the answer to the test here so you can't see it, but we can still run the test
212
expect_equal(colnames(happy_metadata), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))
213
})
214
print("Success!")
215
}
216
217
test_3.6.5 <- function(){
218
test_that('Did not create an object named happy_header', {
219
expect_true(exists("happy_header"))
220
})
221
test_that('happy_header should be a data frame.', {
222
expect_true('data.frame' %in% class(happy_header))
223
})
224
test_that('happy_header does not contain the correct information.', {
225
expect_equal(dim(happy_header), c(155, 5))
226
expect_equal(digest(int_round(sum(happy_header$freedom), 2)), 'fb345985a119a2b0f99a7896efa4d2c7') # we hid the answer to the test here so you can't see it, but we can still run the test
227
expect_equal(colnames(happy_header), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))
228
})
229
print("Success!")
230
}
231
232
test_3.7 <- function(){
233
test_that('Did not create an object named happy_xlsx', {
234
expect_true(exists("happy_xlsx"))
235
})
236
test_that('happy_xlsx should be a data frame.', {
237
expect_true('data.frame' %in% class(happy_xlsx))
238
})
239
test_that('happy_xlsx does not contain the correct information.', {
240
expect_equal(dim(happy_xlsx), c(155, 5))
241
expect_equal(digest(int_round(sum(happy_xlsx$freedom), 2)), 'fb345985a119a2b0f99a7896efa4d2c7') # we hid the answer to the test here so you can't see it, but we can still run the test
242
expect_equal(colnames(happy_xlsx), c("country", "happiness_score", "GDP_per_capita", "life_expectancy", "freedom"))
243
})
244
print("Success!")
245
}
246
247
test_3.8 <- function(){
248
test_that('Solution is incorrect', {
249
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
250
})
251
print("Success!")
252
}
253
254
test_3.9 <- function(){
255
test_that('Did not create a plot named header_plot', {
256
expect_true(exists("header_plot"))
257
})
258
259
properties <- c(header_plot$layers[[1]]$mapping, header_plot$mapping)
260
labels <- header_plot$labels
261
262
test_that('GDP_per_capita should be on the x-axis.', {
263
expect_true("GDP_per_capita" == rlang::get_expr(properties$x))
264
})
265
test_that('life_expectancy should be on the y-axis.', {
266
expect_true("life_expectancy" == rlang::get_expr(properties$y))
267
})
268
test_that('header_plot should be a scatter plot.', {
269
expect_that("GeomPoint" %in% c(class(header_plot$layers[[1]]$geom)) , is_true())
270
})
271
test_that('Labels on the axes should be descriptive and human readable.', {
272
expect_that((header_plot$labels$x) == 'GDP_per_capita', is_false())
273
expect_that((header_plot$labels$y) == 'life_expectancy', is_false())
274
})
275
print("Success!")
276
}
277
278
279
test_4.1 <- function(){
280
test_that('Solution is incorrect', {
281
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
282
})
283
print("Success!")
284
}
285
286
287
test_4.2 <- function(){
288
test_that('Solution is incorrect', {
289
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
290
})
291
print("Success!")
292
}
293
294
test_4.3.1 <- function(){
295
test_that('Did not create the conn object', {
296
expect_true(exists("conn"))
297
})
298
test_that('conn is not an RSQLite database connection object', {
299
expect_equal(digest(class(conn)), 'db665ff04541c433e1f30318e7715ef4')
300
})
301
test_that('conn was loaded from the wrong file',{
302
expect_true(grepl('data/flights_filtered.db', format(conn)))
303
})
304
test_that('conn is the wrong database',{
305
expect_equal(digest(dbListTables(conn)), '51a67410d71bf270db1994ea905cda13')
306
})
307
print("Success!")
308
}
309
310
test_4.3.2 <- function(){
311
test_that('table name is incorrect',{
312
expect_equal(digest(flights_table_name), '51a67410d71bf270db1994ea905cda13')
313
})
314
print('Success!')
315
}
316
317
test_4.3.3 <- function(){
318
test_that('Did not create an object named flight_data', {
319
expect_true(exists("flight_data"))
320
})
321
test_that('flight_data should be a data frame.', {
322
expect_true('tbl_SQLiteConnection' %in% class(flight_data))
323
})
324
test_that('flight_data does not contain the correct information.', {
325
expect_equal(digest(int_round(as_tibble(tally(flight_data))[[1]], 2)), '6bf4cc4788963306e4991f74f4edfd06')
326
expect_equal(digest(int_round(dim(flight_data)[2], 2)), '306a937dfa0335e74514e4c6044755f6')
327
expect_equal(digest(int_round(as_tibble(summarize(flight_data, sum=sum(ARRIVAL_DELAY)))[[1]], 2)), '18935ad0b2d8d671ed44f695a1324b62')
328
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'))
329
})
330
331
print("Success!")
332
}
333
334
test_4.4 <- function(){
335
test_that('Did not create an object named delay_data', {
336
expect_true(exists("delay_data"))
337
})
338
test_that('delay_data has the wrong dimensions', {
339
expect_equal(digest(int_round(as_tibble(tally(delay_data))[[1]], 2)), 'c73696e1f5ebc74021271a447e6a027d')
340
expect_equal(digest(int_round(dim(delay_data)[2], 2)), '2567f3d5adc004a73dc268884026f3bd')
341
})
342
test_that('delay data has the wrong variables', {
343
expect_setequal(colnames(delay_data), c('DEPARTURE_DELAY', 'ARRIVAL_DELAY'))
344
})
345
print('Success!')
346
}
347
348
test_4.5 <- function(){
349
test_that('Did not create a plot named arrival_delay_plot', {
350
expect_true(exists("arrival_delay_plot"))
351
})
352
test_that('The axis labels are incorrect', {
353
expect_equal(digest(arrival_delay_plot$labels$x), "721f8fe183a39e61a2140d9dd01b6042")
354
expect_equal(digest(arrival_delay_plot$labels$y), '9ae00562a72e7b3aa91a39269590839a')
355
})
356
test_that('The x/y data are incorrect', {
357
expect_equal(digest(arrival_delay_plot$mapping$x), '550b90233ab71397604016f5474f572f')
358
expect_equal(digest(arrival_delay_plot$mapping$y), 'f9e884084b84794d762a535f3facec85')
359
})
360
test_that('arrival_delay_plot should be a histogram', {
361
expect_equal(digest(class(arrival_delay_plot$layers[[1]]$geom)[[1]]), '00af9702c1de4491cd9d59e3c94080a8')
362
})
363
print("Success!")
364
}
365
366
test_4.6 <- function(){
367
test_that('Did not create a plot named departure_delay_plot', {
368
expect_true(exists("departure_delay_plot"))
369
})
370
test_that('The axis labels are incorrect', {
371
expect_equal(digest(departure_delay_plot$labels$x), "721f8fe183a39e61a2140d9dd01b6042")
372
expect_equal(digest(departure_delay_plot$labels$y), '9ae00562a72e7b3aa91a39269590839a')
373
})
374
test_that('The x/y data are incorrect', {
375
expect_equal(digest(departure_delay_plot$mapping$x), 'c92e510b14ce856cce6eb14e740686b1')
376
expect_equal(digest(departure_delay_plot$mapping$y), 'f9e884084b84794d762a535f3facec85')
377
})
378
test_that('departure_delay_plot should be a histogram', {
379
expect_equal(digest(class(departure_delay_plot$layers[[1]]$geom)[[1]]), '00af9702c1de4491cd9d59e3c94080a8')
380
})
381
print("Success!")
382
}
383
384
test_4.7 <- function(){
385
test_that('Answer incorrect', {
386
expect_equal(digest(answer4.7), '7d9e25abb82fe492a7007d36124cee47')
387
})
388
print('Success!')
389
}
390
391
test_4.8.1 <- function(){
392
test_that('Answer incorrect', {
393
expect_equal(digest(answer4.8.1), '75f1160e72554f4270c809f041c7a776')
394
})
395
print('Success!')
396
}
397
398
399
test_4.8.2 <- function(){
400
test_that('Did not create the delay_dataframe object', {
401
expect_true(exists("delay_dataframe"))
402
})
403
test_that("The delay_dataframe doesn't have the right data in it", {
404
expect_equal(digest(int_round(sum(delay_dataframe), 2)), 'd142b116cb76f4e5c3fab6e96bd89a22')
405
})
406
test_that('Did not create the delay_data.csv file', {
407
expect_true(file.exists('data/delay_data.csv'))
408
})
409
test_that('delay_data.csv does not have the right data in it', {
410
expect_equal(digest(int_round(sum(read_csv('data/delay_data.csv')), 2)), 'd142b116cb76f4e5c3fab6e96bd89a22')
411
})
412
print('Success!')
413
}
414
415
test_5.1.0 <- function(){
416
test_that('Solution is incorrect', {
417
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
418
})
419
print("Success!")
420
}
421
422
test_5.1.1 <- function(){
423
test_that('Solution is incorrect', {
424
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
425
})
426
print("Success!")
427
}
428
429
test_5.2 <- function(){
430
test_that('Did not create an object named gwp', {
431
expect_true(exists("gwp"))
432
})
433
test_that('gwp should not be a data frame.', {
434
expect_false('data.frame' %in% class(gwp))
435
})
436
test_that('gwp does not contain the correct information.', {
437
expect_equal(digest(int_round(length(gwp), 2)), '2567f3d5adc004a73dc268884026f3bd') # we hid the answer to the test here so you can't see it, but we can still run the test
438
expect_true('xml_document' %in% attributes(gwp)$class)
439
})
440
print("Success!")
441
}
442
443
test_5.4 <- function(){
444
test_that('Did not create an object named gwp_value', {
445
expect_true(exists("gwp_value"))
446
})
447
test_that('gwp_value should be a vector containing characters.', {
448
expect_that(is.vector(gwp_value), is_true())
449
expect_equal(class(gwp_value), "character")
450
})
451
print("Success!")
452
}
453
454
test_5.5 <- function(){
455
test_that('Did not create an object named gwp', {
456
expect_true(exists("gwp"))
457
})
458
test_that('gwp should be a data frame.', {
459
expect_true('data.frame' %in% class(gwp))
460
})
461
test_that('gwp should contain the columns: year and gwp_value', {
462
expect_that("year" %in% colnames(gwp), is_true())
463
expect_that("gwp_value" %in% colnames(gwp), is_true())
464
})
465
print("Success!")
466
}
467
468
test_5.6 <- function(){
469
test_that('Did not create a plot named gwp_historical', {
470
expect_true(exists("gwp_historical"))
471
})
472
473
properties <- c(gwp_historical$layers[[1]]$mapping, gwp_historical$mapping)
474
labels <- gwp_historical$labels
475
476
test_that('sqrt_year should be on the x-axis.', {
477
expect_true("sqrt_year" == rlang::get_expr(properties$x))
478
})
479
test_that('gwp_value should be on the y-axis.', {
480
expect_true("gwp_value" == rlang::get_expr(properties$y))
481
})
482
test_that('gwp_historical should be a line plot.', {
483
expect_that("GeomLine" %in% c(class(gwp_historical$layers[[1]]$geom)) , is_true())
484
})
485
test_that('Labels on the axes should be descriptive and human readable.', {
486
expect_that((gwp_historical$labels$y) == 'gwp_value', is_false())
487
expect_that((gwp_historical$labels$x) == 'sqrt_year', is_false())
488
})
489
print("Success!")
490
}
491
492
test_5.7 <- function(){
493
test_that('Solution is incorrect', {
494
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
495
})
496
print("Success!")
497
}
498
499