Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2020-spring/materials/worksheet_03/tests_worksheet_03.R
2051 views
1
library(testthat)
2
library(digest)
3
4
test_0.1 <- function(){
5
test_that('Solution is incorrect', {
6
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
7
})
8
print("Success!")
9
}
10
11
test_0.2 <- function(){
12
test_that('Solution is incorrect', {
13
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
14
})
15
print("Success!")
16
}
17
18
test_0.3 <- function(){
19
test_that('Solution is incorrect', {
20
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
21
})
22
print("Success!")
23
}
24
25
test_1.1 <- function(){
26
test_that('Solution is incorrect', {
27
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
28
})
29
print("Success!")
30
}
31
32
test_1.2 <- function(){
33
test_that('Solution is incorrect', {
34
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
35
})
36
print("Success!")
37
}
38
39
test_1.3 <- function(){
40
test_that('Did not create an object named avocado', {
41
expect_true(exists("avocado"))
42
})
43
test_that('avocado should be a data frame.', {
44
expect_true('data.frame' %in% class(avocado))
45
})
46
test_that('avocado does not contain the correct data.', {
47
expect_equal(dim(avocado), c(17911, 9))
48
expect_equal(digest(as.numeric(sum(avocado$small_hass_volume))), '5c68093945e78d372cac50a8eacec8fa') # we hid the answer to the test here so you can't see it, but we can still run the test
49
expect_equal(colnames(avocado), c("Date", "average_price", "small_hass_volume", "large_hass_volume", "extra_l_hass_volume", "type", "year", "region", "week"))
50
})
51
print("Success!")
52
}
53
54
test_1.4 <- function(){
55
test_that('Solution is incorrect', {
56
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
57
})
58
print("Success!")
59
}
60
61
test_1.5 <- function(){
62
test_that('Did not create a plot named avocado_plot', {
63
expect_true(exists("avocado_plot"))
64
})
65
test_that('Date should be on the x-axis.', {
66
expect_that("Date" %in% c(rlang::get_expr(avocado_plot$mapping$x),rlang::get_expr(avocado_plot$layers[[1]]$mapping$x)), is_true())
67
})
68
test_that('average_price should be on the y-axis.', {
69
expect_that("average_price" %in% c(rlang::get_expr(avocado_plot$mapping$y), rlang::get_expr(avocado_plot$layers[[1]]$mapping$y)) , is_true())
70
})
71
test_that('avocado_plot should be a scatter plot.', {
72
expect_that("GeomPoint" %in% c(class(avocado_plot$layers[[1]]$geom)) , is_true())
73
})
74
test_that('Labels on the axes should be descriptive and human readable.', {
75
expect_that((avocado_plot$labels$y) == 'average_price', is_false())
76
})
77
print("Success!")
78
}
79
80
test_1.6 <- function(){
81
test_that('Did not create an object named avocado_aggregate', {
82
expect_true(exists("avocado_aggregate"))
83
})
84
test_that('avocado_aggregate does not contain the correct number of rows and/or columns.', {
85
expect_equal(dim(avocado_aggregate), c(53, 2))
86
})
87
test_that('Columns in avocado_aggregate contain incorrect values.', {
88
expect_equal(digest(as.numeric(sum(avocado_aggregate$week))), 'd27e825e408c446c586593f719b5545e') # we hid the answer to the test here so you can't see it, but we can still run the test
89
expect_equal(digest(as.numeric(sum(avocado_aggregate$average_price))), '124d515821b05dcb2a472f2999ca770e') # 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_1.7 <- function(){
95
test_that('Did not create a plot named avocado_aggregate_plot', {
96
expect_true(exists("avocado_aggregate_plot"))
97
})
98
test_that('week should be on the x-axis.', {
99
expect_that("week" %in% c(rlang::get_expr(avocado_aggregate_plot$mapping$x),rlang::get_expr(avocado_aggregate_plot$layers[[1]]$mapping$x)), is_true())
100
})
101
test_that('average_price should be on the y-axis.', {
102
expect_that("average_price" %in% c(rlang::get_expr(avocado_aggregate_plot$mapping$y), rlang::get_expr(avocado_aggregate_plot$layers[[1]]$mapping$y)) , is_true())
103
})
104
test_that('avocado_aggregate_plot should be a scatter plot.', {
105
expect_that("GeomPoint" %in% c(class(avocado_aggregate_plot$layers[[1]]$geom)) , is_true())
106
})
107
test_that('Labels on the axes should be descriptive and human readable.', {
108
expect_that((avocado_aggregate_plot$labels$y) == 'average_price', is_false())
109
expect_that((avocado_aggregate_plot$labels$x) == 'week', is_false())
110
})
111
print("Success!")
112
}
113
114
test_1.8 <- function(){
115
test_that('Did not create an object named avocado', {
116
expect_true(exists("avocado"))
117
})
118
test_that('avocado does not contain the correct number of rows and/or columns.', {
119
expect_equal(dim(avocado), c(17911, 10))
120
})
121
test_that('Columns in avocado contain incorrect values.', {
122
expect_equal(digest(round(sum(avocado$total_volume))), 'aa2d30b2344bef18ef305fb4541b78b6') # 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(round(sum(avocado$average_price, na.rm = TRUE))), 'd478458402bf9895986c7e8a50ad4b61') # 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_1.9 <- function(){
129
test_that('Did not create an object named avocado_aggregate_2', {
130
expect_true(exists("avocado_aggregate_2"))
131
})
132
test_that('avocado_aggregate_2 does not contain the correct number of rows and/or columns.', {
133
expect_equal(dim(avocado_aggregate_2), c(53, 2))
134
})
135
test_that('Columns in avocado_aggregate_2 contain incorrect values.', {
136
expect_equal(digest(as.numeric(sum(avocado_aggregate_2$total_volume))), '6c1676dd13517d0eed2be5e246dc8ef1') # we hid the answer to the test here so you can't see it, but we can still run the test
137
expect_equal(digest(as.numeric(sum(avocado_aggregate_2$week))), 'd27e825e408c446c586593f719b5545e') # we hid the answer to the test here so you can't see it, but we can still run the test
138
})
139
print("Success!")
140
}
141
142
test_1.10 <- function(){
143
test_that('Did not create a plot named avocado_aggregate_plot_2', {
144
expect_true(exists("avocado_aggregate_plot_2"))
145
})
146
test_that('week should be on the x-axis.', {
147
expect_that("week" %in% c(rlang::get_expr(avocado_aggregate_plot_2$mapping$x),rlang::get_expr(avocado_aggregate_plot_2$layers[[1]]$mapping$x)), is_true())
148
})
149
test_that('total_volume should be on the y-axis.', {
150
expect_that("total_volume" %in% c(rlang::get_expr(avocado_aggregate_plot_2$mapping$y), rlang::get_expr(avocado_aggregate_plot_2$layers[[1]]$mapping$y)) , is_true())
151
})
152
test_that('avocado_aggregate_plot_2 should be a scatter plot.', {
153
expect_that("GeomPoint" %in% c(class(avocado_aggregate_plot_2$layers[[1]]$geom)) , is_true())
154
})
155
test_that('Labels on the axes should be descriptive and human readable.', {
156
expect_that((avocado_aggregate_plot_2$labels$y) == 'total_volume', is_false())
157
expect_that((avocado_aggregate_plot_2$labels$x) == 'week', is_false())
158
})
159
print("Success!")
160
}
161
162
test_2.1 <- function(){
163
test_that('Solution is incorrect', {
164
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
165
})
166
print("Success!")
167
}
168
169
test_2.2 <- function(){
170
test_that('Solution is incorrect', {
171
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
172
})
173
print("Success!")
174
}
175
176
test_2.3 <- function(){
177
test_that('Did not create an object named sea_surface', {
178
expect_true(exists("sea_surface"))
179
})
180
test_that('sea_surface should be a data frame.', {
181
expect_true('data.frame' %in% class(sea_surface))
182
})
183
test_that('sea_surface does not contain the correct data.', {
184
expect_equal(dim(sea_surface), c(105, 13))
185
expect_equal(digest(as.numeric(sum(sea_surface$Year))), 'f56bfa7ed2b5549995da2c88407099a4') # we hid the answer to the test here so you can't see it, but we can still run the test
186
expect_equal(colnames(sea_surface), c("Year", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
187
})
188
print("Success!")
189
}
190
191
test_2.3.1 <- function(){
192
test_that('Solution is incorrect', {
193
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
194
})
195
print("Success!")
196
}
197
198
199
test_2.4 <- function(){
200
test_that('Did not create an object named tidy_temp', {
201
expect_true(exists("tidy_temp"))
202
})
203
test_that('tidy_temp does not contain the correct number of rows and/or columns.', {
204
expect_equal(dim(tidy_temp), c(1260, 3))
205
})
206
test_that('Columns in tidy_temp contain incorrect values.', {
207
expect_equal(digest(as.numeric(sum(tidy_temp$Temperature, na.rm = TRUE))), '378da034470b139e7a84f82e2fcdcb08') # 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_2.5 <- function(){
213
test_that('Did not create a plot named nov_temp_plot', {
214
expect_true(exists("nov_temp_plot"))
215
})
216
test_that('The Month column in nov_temp_plot should only contain November.', {
217
expect_equal(unique(nov_temp_plot$data$Month), "Nov")
218
})
219
test_that('Year should be on the x-axis.', {
220
expect_that("Year" %in% c(rlang::get_expr(nov_temp_plot$mapping$x),rlang::get_expr(nov_temp_plot$layers[[1]]$mapping$x)), is_true())
221
})
222
test_that('Temperature should be on the y-axis.', {
223
expect_that("Temperature" %in% c(rlang::get_expr(nov_temp_plot$mapping$y), rlang::get_expr(nov_temp_plot$layers[[1]]$mapping$y)) , is_true())
224
})
225
test_that('nov_temp_plot should be a scatter plot.', {
226
expect_that("GeomPoint" %in% c(class(nov_temp_plot$layers[[1]]$geom)) , is_true())
227
})
228
print("Success!")
229
}
230
231
test_2.6 <- function(){
232
test_that('Did not create a plot named all_temp_plot', {
233
expect_true(exists("all_temp_plot"))
234
})
235
test_that('Need to use tidy_temp as the data!', {
236
expect_true("Month" %in% colnames(all_temp_plot$data))
237
})
238
test_that('Should use facet_wrap to facet by month', {
239
expect_true('FacetWrap' %in% class(all_temp_plot$facet))
240
})
241
test_that('Year should be on the x-axis.', {
242
expect_that("Year" %in% c(rlang::get_expr(all_temp_plot$mapping$x),rlang::get_expr(all_temp_plot$layers[[1]]$mapping$x)), is_true())
243
})
244
test_that('Temperature should be on the y-axis.', {
245
expect_that("Temperature" %in% c(rlang::get_expr(all_temp_plot$mapping$y), rlang::get_expr(all_temp_plot$layers[[1]]$mapping$y)) , is_true())
246
})
247
test_that('all_temp_plot should be a scatter plot.', {
248
expect_that("GeomPoint" %in% c(class(all_temp_plot$layers[[1]]$geom)) , is_true())
249
})
250
print("Success!")
251
}
252
253
test_3.1 <- function(){
254
test_that('Solution is incorrect', {
255
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
256
})
257
print("Success!")
258
}
259
260
test_3.2 <- function(){
261
test_that('Did not create an object named madrid', {
262
expect_true(exists("madrid"))
263
})
264
test_that('madrid should be a data frame.', {
265
expect_true('data.frame' %in% class(madrid))
266
})
267
test_that('madrid does not contain the correct data.', {
268
expect_equal(dim(madrid), c(51864, 17))
269
expect_equal(digest(sum(madrid$BEN)), '9c9393e1464352cd4fbea94dfadfa02a') # we hid the answer to the test here so you can't see it, but we can still run the test
270
expect_equal(colnames(madrid), c("date", "BEN", "CO", "EBE", "MXY", "NMHC", "NO_2", "NOx", "OXY", "O_3", "PM10", "PXY", "SO_2", "TCH", "TOL", "year", "month"))
271
})
272
print("Success!")
273
}
274
275
test_3.3 <- function(){
276
test_that('Did not create a plot named EBE_pollution', {
277
expect_true(exists("EBE_pollution"))
278
})
279
test_that('date should be on the x-axis.', {
280
expect_that("date" %in% c(rlang::get_expr(EBE_pollution$mapping$x),rlang::get_expr(EBE_pollution$layers[[1]]$mapping$x)), is_true())
281
})
282
test_that('EBE should be on the y-axis.', {
283
expect_that("EBE" %in% c(rlang::get_expr(EBE_pollution$mapping$y), rlang::get_expr(EBE_pollution$layers[[1]]$mapping$y)) , is_true())
284
})
285
test_that('EBE_pollution should be a scatter plot.', {
286
expect_that("GeomPoint" %in% c(class(EBE_pollution$layers[[1]]$geom)) , is_true())
287
})
288
test_that('Labels on the axes should be descriptive and human readable.', {
289
expect_that((EBE_pollution$labels$y) == 'EBE', is_false())
290
expect_that((EBE_pollution$labels$x) == 'date', is_false())
291
})
292
print("Success!")
293
}
294
295
test_3.4 <- function(){
296
test_that('Did not create an object named madrid_pollution', {
297
expect_true(exists("madrid_pollution"))
298
})
299
test_that('madrid_pollution does not contain the correct number of rows and/or columns.', {
300
expect_equal(dim(madrid_pollution), c(72, 3))
301
})
302
test_that('Columns in avocado_aggregate_2 contain incorrect values.', {
303
expect_equal(digest(as.numeric(sum(madrid_pollution$year))), '625a52cbf322663507323f452293484f') # we hid the answer to the test here so you can't see it, but we can still run the test
304
})
305
print("Success!")
306
}
307
308
test_3.5 <- function(){
309
test_that('Did not create a plot named madrid_plot', {
310
expect_true(exists("madrid_plot"))
311
})
312
test_that('month should be on the x-axis.', {
313
expect_that("month" %in% c(rlang::get_expr(madrid_plot$mapping$x),rlang::get_expr(madrid_plot$layers[[1]]$mapping$x)), is_true())
314
})
315
test_that('max_ebe should be on the y-axis.', {
316
expect_that("max_ebe" %in% c(rlang::get_expr(madrid_plot$mapping$y), rlang::get_expr(madrid_plot$layers[[1]]$mapping$y)) , is_true())
317
})
318
test_that('madrid_plot should be a scatter plot.', {
319
expect_that("GeomPoint" %in% c(class(madrid_plot$layers[[1]]$geom)) , is_true())
320
})
321
test_that('Labels on the axes should be descriptive and human readable.', {
322
expect_that((madrid_plot$labels$y) == 'max_ebe', is_false())
323
expect_that((madrid_plot$labels$x) == 'month', is_false())
324
})
325
print("Success!")
326
}
327
328
test_3.6 <- function(){
329
test_that('Did not create an object named pollution_2001', {
330
expect_true(exists("pollution_2001"))
331
})
332
test_that('pollution_2001 does not contain the correct number of rows and/or columns.', {
333
expect_equal(dim(pollution_2001), c(1, 14))
334
})
335
test_that('Columns in pollution_2001 contain incorrect values.', {
336
expect_equal(digest(pollution_2001$MXY), '04d5d8b3f2a28a07e24c30fe05b4d105') # we hid the answer to the test here so you can't see it, but we can still run the test
337
expect_equal(digest(as.numeric(sum(pollution_2001))), '7641b9c141801f7dfd1e36295531cd81') # we hid the answer to the test here so you can't see it, but we can still run the test
338
})
339
print("Success!")
340
}
341
342
test_3.7 <- function(){
343
test_that('Did not create an object named pollution_2006', {
344
expect_true(exists("pollution_2006"))
345
})
346
test_that('pollution_2006 does not contain the correct number of rows and/or columns.', {
347
expect_equal(dim(pollution_2006), c(1, 14))
348
})
349
test_that('Columns in pollution_2006 contain incorrect values.', {
350
expect_equal(digest(pollution_2006$MXY), '64d199ddc067d7977719ecede6a11333') # we hid the answer to the test here so you can't see it, but we can still run the test
351
expect_equal(digest(as.numeric(sum(pollution_2006))), 'd5563a7b0d7d1a9eb53269ad69ed4e0b') # we hid the answer to the test here so you can't see it, but we can still run the test
352
})
353
print("Success!")
354
}
355
356
test_3.8 <- function(){
357
test_that('Solution is incorrect', {
358
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
359
})
360
print("Success!")
361
}
362
363
test_3.9 <- function(){
364
test_that('Did not create an object named pollution_diff', {
365
expect_true(exists("pollution_diff"))
366
})
367
test_that('pollution_diff does not contain the correct number of rows and/or columns.', {
368
expect_equal(dim(pollution_diff), c(14, 2))
369
})
370
test_that('pollution_diff does not contain the correct columns: pollutant and value.', {
371
expect_equal(colnames(pollution_diff), c('pollutant', 'value'))
372
})
373
test_that('Columns in pollution_diff contain incorrect values.', {
374
expect_equal(digest(as.numeric(sum(pollution_diff$value))), 'd4f4caf121e7237dc6328782e399d193') # we hid the answer to the test here so you can't see it, but we can still run the test
375
})
376
print("Success!")
377
}
378
379
test_3.10 <- function(){
380
test_that('Did not create an object named max_pollution_diff', {
381
expect_true(exists("max_pollution_diff"))
382
})
383
test_that('max_pollution_diff does not contain the correct number of rows and/or columns.', {
384
expect_equal(dim(max_pollution_diff), c(1, 2))
385
})
386
test_that('max_pollution_diff does not contain the correct columns: pollutant and value.', {
387
expect_equal(colnames(max_pollution_diff), c('pollutant', 'value'))
388
})
389
test_that('Columns in pollution_diff contain incorrect values.', {
390
expect_equal(digest(as.numeric(sum(max_pollution_diff$value))), '6372781239f4b691087e7d411c9ba64f') # we hid the answer to the test here so you can't see it, but we can still run the test
391
})
392
print("Success!")
393
}
394