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