Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2022-spring/materials/worksheet_wrangling/tests.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_0.0 <- function(){
34
test_that('Solution is incorrect', {
35
expect_equal(digest(as.character(answer0.0)), '475bf9280aab63a82af60791302736f6') # 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
41
test_0.1 <- function(){
42
test_that('Solution is incorrect', {
43
expect_equal(digest(as.character(answer0.1)), '3a5505c06543876fe45598b5e5e5195d') # 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_0.2 <- function(){
49
test_that('Solution is incorrect', {
50
expect_equal(digest(as.character(answer0.2)), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test
51
})
52
print("Success!")
53
}
54
55
test_0.3 <- function(){
56
test_that('Solution is incorrect', {
57
expect_equal(digest(as.character(answer0.3)), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test
58
})
59
print("Success!")
60
}
61
62
test_1.1 <- function(){
63
test_that('Solution is incorrect', {
64
expect_equal(digest(as.character(answer1.1)), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test
65
})
66
print("Success!")
67
}
68
69
test_1.2 <- function(){
70
test_that('Solution is incorrect', {
71
expect_equal(digest(as.character(answer1.2)), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test
72
})
73
print("Success!")
74
}
75
76
test_1.3 <- function(){
77
test_that('Did not create an object named avocado', {
78
expect_true(exists("avocado"))
79
})
80
test_that('avocado should be a data frame.', {
81
expect_true('data.frame' %in% class(avocado))
82
})
83
test_that('avocado does not contain the correct data.', {
84
expect_equal(dim(avocado), c(17911, 9))
85
expect_equal(digest(int_round(sum(avocado$average_price), 2)), '925e36908ff4830300330ada3666458c') # we hid the answer to the test here so you can't see it, but we can still run the test
86
expect_equal(colnames(avocado), c("Date", "average_price", "small_hass_volume", "large_hass_volume", "extra_l_hass_volume", "type", "yr", "region", "wk"))
87
})
88
print("Success!")
89
}
90
91
test_1.4 <- function(){
92
test_that('Solution is incorrect', {
93
expect_equal(digest(as.character(answer1.4)), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test
94
})
95
print("Success!")
96
}
97
98
test_1.5 <- function(){
99
test_that('Did not create a plot named avocado_plot', {
100
expect_true(exists("avocado_plot"))
101
})
102
properties <- c(avocado_plot$layers[[1]]$mapping, avocado_plot$mapping)
103
test_that('Date should be on the x-axis.', {
104
expect_true("Date" == rlang::get_expr(properties$x))
105
})
106
test_that('average_price should be on the y-axis.', {
107
expect_true("average_price" == rlang::get_expr(properties$y))
108
})
109
test_that('avocado_plot should be a scatter plot.', {
110
expect_true("GeomPoint" %in% c(class(avocado_plot$layers[[1]]$geom)))
111
})
112
test_that('Labels on the axes should be descriptive and human readable.', {
113
expect_false(avocado_plot$labels$y == 'average_price')
114
})
115
print("Success!")
116
}
117
118
test_1.6 <- function(){
119
test_that('Did not create an object named avocado_aggregate', {
120
expect_true(exists("avocado_aggregate"))
121
})
122
test_that('avocado_aggregate does not contain the correct number of rows and/or columns.', {
123
expect_equal(dim(avocado_aggregate), c(53, 2))
124
})
125
test_that('Columns in avocado_aggregate contain incorrect values.', {
126
expect_equal(digest(int_round(sum(avocado_aggregate$wk), 2)), '52e8770115417a331e4e2a539238bbf1') # we hid the answer to the test here so you can't see it, but we can still run the test
127
expect_equal(digest(int_round(sum(avocado_aggregate$average_price), 2)), '2a69e8ab5af131a648f5cb2d24e6fbcf') # we hid the answer to the test here so you can't see it, but we can still run the test
128
})
129
print("Success!")
130
}
131
132
test_1.7 <- function(){
133
test_that('Did not create a plot named avocado_aggregate_plot', {
134
expect_true(exists("avocado_aggregate_plot"))
135
})
136
properties <- c(avocado_aggregate_plot$layers[[1]]$mapping, avocado_aggregate_plot$mapping)
137
test_that('week should be on the x-axis.', {
138
expect_true("wk" == rlang::get_expr(properties$x))
139
})
140
test_that('average_price should be on the y-axis.', {
141
expect_true("average_price" == rlang::get_expr(properties$y))
142
})
143
test_that('avocado_aggregate_plot should be a scatter plot.', {
144
expect_true("GeomPoint" %in% c(class(avocado_aggregate_plot$layers[[1]]$geom)))
145
})
146
test_that('Labels on the axes should be descriptive and human readable.', {
147
expect_false(avocado_aggregate_plot$labels$y == 'average_price')
148
expect_false(avocado_aggregate_plot$labels$x == 'wk')
149
})
150
print("Success!")
151
}
152
153
test_1.8 <- function(){
154
test_that('Did not create an object named avocado', {
155
expect_true(exists("avocado"))
156
})
157
test_that('avocado does not contain the correct number of rows and/or columns.', {
158
expect_equal(dim(avocado), c(17911, 10))
159
})
160
test_that('Columns in avocado contain incorrect values.', {
161
expect_equal(digest(int_round(sum(avocado$total_volume, na.rm = TRUE), -2)), '7128826a55bfba26d8c2df46ab65dbcf') # we hid the answer to the test here so you can't see it, but we can still run the test
162
expect_equal(digest(int_round(sum(avocado$average_price, na.rm = TRUE), 2)), '925e36908ff4830300330ada3666458c') # we hid the answer to the test here so you can't see it, but we can still run the test
163
})
164
print("Success!")
165
}
166
167
test_1.9 <- function(){
168
test_that('Did not create an object named avocado_aggregate_2', {
169
expect_true(exists("avocado_aggregate_2"))
170
})
171
test_that('avocado_aggregate_2 does not contain the correct number of rows and/or columns.', {
172
expect_equal(dim(avocado_aggregate_2), c(53, 2))
173
})
174
test_that('Columns in avocado_aggregate_2 contain incorrect values.', {
175
expect_equal(digest(int_round(sum(avocado_aggregate_2$total_volume), 2)), '31b3d41174825b72e1d523622ee021e6') # we hid the answer to the test here so you can't see it, but we can still run the test
176
expect_equal(digest(int_round(sum(avocado_aggregate_2$wk), 2)), '52e8770115417a331e4e2a539238bbf1') # we hid the answer to the test here so you can't see it, but we can still run the test
177
})
178
print("Success!")
179
}
180
181
test_1.10 <- function(){
182
test_that('Did not create a plot named avocado_aggregate_plot_2', {
183
expect_true(exists("avocado_aggregate_plot_2"))
184
})
185
properties <- c(avocado_aggregate_plot_2$layers[[1]]$mapping, avocado_aggregate_plot_2$mapping)
186
test_that('week should be on the x-axis.', {
187
expect_true("wk" == rlang::get_expr(properties$x))
188
})
189
test_that('total_volume should be on the y-axis.', {
190
expect_true("total_volume" == rlang::get_expr(properties$y))
191
})
192
test_that('avocado_aggregate_plot_2 should be a scatter plot.', {
193
expect_true("GeomPoint" %in% c(class(avocado_aggregate_plot_2$layers[[1]]$geom)))
194
})
195
test_that('Labels on the axes should be descriptive and human readable.', {
196
expect_false(avocado_aggregate_plot_2$labels$y == 'total_volume')
197
expect_false(avocado_aggregate_plot_2$labels$x == 'wk')
198
})
199
print("Success!")
200
}
201
202
test_2.1 <- function(){
203
test_that('Solution is incorrect', {
204
expect_equal(digest(answer2.1), 'd2a90307aac5ae8d0ef58e2fe730d38b') # we hid the answer to the test here so you can't see it, but we can still run the test
205
})
206
print("Success!")
207
}
208
209
test_2.2 <- function(){
210
test_that('Solution is incorrect', {
211
expect_equal(digest(answer2.2), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test
212
})
213
print("Success!")
214
}
215
216
test_2.3 <- function(){
217
test_that('Did not create an object named sea_surface', {
218
expect_true(exists("sea_surface"))
219
})
220
test_that('sea_surface should be a data frame.', {
221
expect_true('data.frame' %in% class(sea_surface))
222
})
223
test_that('sea_surface does not contain the correct data.', {
224
expect_equal(dim(sea_surface), c(105, 13))
225
expect_equal(digest(int_round(sum(sea_surface$Year), 2)), 'f5530ffd8438e19b12730e111c4e2cc6') # we hid the answer to the test here so you can't see it, but we can still run the test
226
expect_equal(colnames(sea_surface), c("Year", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
227
})
228
print("Success!")
229
}
230
231
test_2.3.1 <- function(){
232
test_that('Solution is incorrect', {
233
expect_equal(digest(as.character(answer2.3.1)), '01a75cb73d67b0f895ff0e61449c7bf8') # we hid the answer to the test here so you can't see it, but we can still run the test
234
})
235
print("Success!")
236
}
237
238
239
test_2.4 <- function(){
240
test_that('Did not create an object named tidy_temp', {
241
expect_true(exists("tidy_temp"))
242
})
243
test_that('tidy_temp does not contain the correct number of rows and/or columns.', {
244
expect_equal(dim(tidy_temp), c(1260, 3))
245
})
246
test_that('Columns in tidy_temp contain incorrect values.', {
247
expect_equal(digest(int_round(sum(tidy_temp$Temperature, na.rm = TRUE), 2)), 'ffc4ca6bf87040a2a3f7f80c32c15def') # we hid the answer to the test here so you can't see it, but we can still run the test
248
})
249
print("Success!")
250
}
251
252
test_2.5 <- function(){
253
test_that('Did not create a plot named nov_temp_plot', {
254
expect_true(exists("nov_temp_plot"))
255
})
256
properties <- c(nov_temp_plot$layers[[1]]$mapping, nov_temp_plot$mapping)
257
test_that('The Month column in nov_temp_plot should only contain November.', {
258
expect_equal(unique(nov_temp_plot$data$Month), "Nov")
259
})
260
test_that('Year should be on the x-axis.', {
261
expect_true("Year" == rlang::get_expr(properties$x))
262
})
263
test_that('Temperature should be on the y-axis.', {
264
expect_true("Temperature" == rlang::get_expr(properties$y))
265
})
266
test_that('nov_temp_plot should be a scatter plot.', {
267
expect_true("GeomPoint" %in% c(class(nov_temp_plot$layers[[1]]$geom)))
268
})
269
print("Success!")
270
}
271
272
test_2.6 <- function(){
273
test_that('Did not create a plot named all_temp_plot', {
274
expect_true(exists("all_temp_plot"))
275
})
276
properties <- c(all_temp_plot$layers[[1]]$mapping, all_temp_plot$mapping)
277
test_that('Need to use tidy_temp as the data!', {
278
expect_true("Month" %in% colnames(all_temp_plot$data))
279
})
280
test_that('Should use facet_wrap to facet by month', {
281
expect_true('FacetWrap' %in% class(all_temp_plot$facet))
282
})
283
test_that('Year should be on the x-axis.', {
284
expect_true("Year" == rlang::get_expr(properties$x))
285
})
286
test_that('Temperature should be on the y-axis.', {
287
expect_true("Temperature" == rlang::get_expr(properties$y))
288
})
289
test_that('all_temp_plot should be a scatter plot.', {
290
expect_true("GeomPoint" %in% c(class(all_temp_plot$layers[[1]]$geom)))
291
})
292
print("Success!")
293
}
294
295
test_3.1 <- function(){
296
test_that('Solution is incorrect', {
297
expect_equal(digest(as.character(answer3.1)), '3a5505c06543876fe45598b5e5e5195d') # we hid the answer to the test here so you can't see it, but we can still run the test
298
})
299
print("Success!")
300
}
301
302
test_3.2 <- function(){
303
test_that('Did not create an object named madrid', {
304
expect_true(exists("madrid"))
305
})
306
test_that('madrid should be a data frame.', {
307
expect_true('data.frame' %in% class(madrid))
308
})
309
test_that('madrid does not contain the correct data.', {
310
expect_equal(dim(madrid), c(51864, 17))
311
expect_equal(digest(int_round(sum(madrid$BEN, na.rm = TRUE), 2)), 'fc1f955cfd95bfb1528ba72b1d7dbf85') # we hid the answer to the test here so you can't see it, but we can still run the test
312
expect_equal(colnames(madrid), c("date", "BEN", "CO", "EBE", "MXY", "NMHC", "NO_2", "NOx", "OXY", "O_3", "PM10", "PXY", "SO_2", "TCH", "TOL", "year", "mnth"))
313
})
314
print("Success!")
315
}
316
317
test_3.3 <- function(){
318
test_that('Did not create a plot named EBE_pollution', {
319
expect_true(exists("EBE_pollution"))
320
})
321
properties <- c(EBE_pollution$layers[[1]]$mapping, EBE_pollution$mapping)
322
test_that('date should be on the x-axis.', {
323
expect_true("date" == rlang::get_expr(properties$x))
324
})
325
test_that('EBE should be on the y-axis.', {
326
expect_true("EBE" == rlang::get_expr(properties$y))
327
})
328
test_that('EBE_pollution should be a scatter plot.', {
329
expect_true("GeomPoint" %in% c(class(EBE_pollution$layers[[1]]$geom)))
330
})
331
test_that('Labels on the axes should be descriptive and human readable.', {
332
expect_false(EBE_pollution$labels$y == 'EBE')
333
expect_false(EBE_pollution$labels$x == 'date')
334
})
335
print("Success!")
336
}
337
338
test_3.4 <- function(){
339
test_that('Did not create an object named madrid_pollution', {
340
expect_true(exists("madrid_pollution"))
341
})
342
test_that('madrid_pollution does not contain the correct number of rows and/or columns.', {
343
expect_equal(dim(madrid_pollution), c(72, 3))
344
})
345
test_that('Columns in avocado_aggregate_2 contain incorrect values.', {
346
expect_equal(digest(int_round(sum(madrid_pollution$year), 2)), 'a23f8380b73a23a2f7ad10600a6a4829') # we hid the answer to the test here so you can't see it, but we can still run the test
347
})
348
print("Success!")
349
}
350
351
test_3.5 <- function(){
352
test_that('Did not create a plot named madrid_plot', {
353
expect_true(exists("madrid_plot"))
354
})
355
properties <- c(madrid_plot$layers[[1]]$mapping, madrid_plot$mapping)
356
test_that('month should be on the x-axis.', {
357
expect_true("mnth" == rlang::get_expr(properties$x))
358
})
359
test_that('max_ebe should be on the y-axis.', {
360
expect_true("max_ebe" == rlang::get_expr(properties$y))
361
})
362
test_that('madrid_plot should be a scatter plot.', {
363
expect_true("GeomPoint" %in% c(class(madrid_plot$layers[[1]]$geom)))
364
})
365
test_that('Labels on the axes should be descriptive and human readable.', {
366
expect_false(madrid_plot$labels$y == 'max_ebe')
367
expect_false(madrid_plot$labels$x == 'mnth')
368
})
369
print("Success!")
370
}
371
372
test_3.6 <- function(){
373
test_that('Did not create an object named pollution_2001', {
374
expect_true(exists("pollution_2001"))
375
})
376
test_that('pollution_2001 does not contain the correct number of rows and/or columns.', {
377
expect_equal(dim(pollution_2001), c(1, 14))
378
})
379
test_that('Columns in pollution_2001 contain incorrect values.', {
380
expect_equal(digest(int_round(pollution_2001$MXY, 2)), '9e56083b25f55a915ef285e242392a4a') # we hid the answer to the test here so you can't see it, but we can still run the test
381
expect_equal(digest(int_round(sum(pollution_2001), 2)), 'f12c2d5045ab522d0268bc7d14b17290') # we hid the answer to the test here so you can't see it, but we can still run the test
382
})
383
print("Success!")
384
}
385
386
test_3.7 <- function(){
387
test_that('Did not create an object named pollution_2006', {
388
expect_true(exists("pollution_2006"))
389
})
390
test_that('pollution_2006 does not contain the correct number of rows and/or columns.', {
391
expect_equal(dim(pollution_2006), c(1, 14))
392
})
393
test_that('Columns in pollution_2006 contain incorrect values.', {
394
expect_equal(digest(int_round(pollution_2006$MXY, 2)), 'd1e716c1361c7b0e57cc9f66a87e1bfb') # we hid the answer to the test here so you can't see it, but we can still run the test
395
expect_equal(digest(int_round(sum(pollution_2006), 2)), 'a49327566693ae18684d9e8618ce48a1') # 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_3.8 <- function(){
401
test_that('Solution is incorrect', {
402
expect_equal(digest(as.character(answer3.8)), '1ce38a3fa8946d5768f4fc87b739ec31') # we hid the answer to the test here so you can't see it, but we can still run the test
403
})
404
print("Success!")
405
}
406
407
test_3.9 <- function(){
408
test_that('Did not create an object named pollution_diff', {
409
expect_true(exists("pollution_diff"))
410
})
411
test_that('pollution_diff does not contain the correct number of rows and/or columns.', {
412
expect_equal(dim(pollution_diff), c(14, 2))
413
})
414
test_that('pollution_diff does not contain the correct columns: pollutant and value.', {
415
expect_equal(colnames(pollution_diff), c('pollutant', 'value'))
416
})
417
test_that('Columns in pollution_diff contain incorrect values.', {
418
expect_equal(digest(int_round(sum(pollution_diff$value), 2)), 'b6181c536435d6429d51eebb59563fb7') # we hid the answer to the test here so you can't see it, but we can still run the test
419
})
420
print("Success!")
421
}
422
423
test_3.10 <- function(){
424
test_that('Did not create an object named max_pollution_diff', {
425
expect_true(exists("max_pollution_diff"))
426
})
427
test_that('max_pollution_diff does not contain the correct number of rows and/or columns.', {
428
expect_equal(dim(max_pollution_diff), c(1, 2))
429
})
430
test_that('max_pollution_diff does not contain the correct columns: pollutant and value.', {
431
expect_equal(colnames(max_pollution_diff), c('pollutant', 'value'))
432
})
433
test_that('Columns in pollution_diff contain incorrect values.', {
434
expect_equal(digest(int_round(sum(max_pollution_diff$value), 2)), '44ee23cae87e4fb16d02379e8b8536bc') # we hid the answer to the test here so you can't see it, but we can still run the test
435
})
436
print("Success!")
437
}
438
439