Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2020-fall/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(round(as.numeric(sum(avocado$small_hass_volume)))), 'd462f87e11e563c8fb50ea6c6ac51815') # 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", "yr", "region", "wk"))
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
properties <- c(avocado_plot$layers[[1]]$mapping, avocado_plot$mapping)
66
test_that('Date should be on the x-axis.', {
67
expect_true("Date" == rlang::get_expr(properties$x))
68
})
69
test_that('average_price should be on the y-axis.', {
70
expect_true("average_price" == rlang::get_expr(properties$y))
71
})
72
test_that('avocado_plot should be a scatter plot.', {
73
expect_true("GeomPoint" %in% c(class(avocado_plot$layers[[1]]$geom)))
74
})
75
test_that('Labels on the axes should be descriptive and human readable.', {
76
expect_false(avocado_plot$labels$y == 'average_price')
77
})
78
print("Success!")
79
}
80
81
test_1.6 <- function(){
82
test_that('Did not create an object named avocado_aggregate', {
83
expect_true(exists("avocado_aggregate"))
84
})
85
test_that('avocado_aggregate does not contain the correct number of rows and/or columns.', {
86
expect_equal(dim(avocado_aggregate), c(53, 2))
87
})
88
test_that('Columns in avocado_aggregate contain incorrect values.', {
89
expect_equal(digest(as.numeric(sum(avocado_aggregate$wk))), 'd27e825e408c446c586593f719b5545e') # we hid the answer to the test here so you can't see it, but we can still run the test
90
expect_equal(digest(round(as.numeric(sum(avocado_aggregate$average_price)))), 'f67fbc496dfabdb88e8a3761809759ab') # we hid the answer to the test here so you can't see it, but we can still run the test
91
})
92
print("Success!")
93
}
94
95
test_1.7 <- function(){
96
test_that('Did not create a plot named avocado_aggregate_plot', {
97
expect_true(exists("avocado_aggregate_plot"))
98
})
99
properties <- c(avocado_aggregate_plot$layers[[1]]$mapping, avocado_aggregate_plot$mapping)
100
test_that('week should be on the x-axis.', {
101
expect_true("wk" == rlang::get_expr(properties$x))
102
})
103
test_that('average_price should be on the y-axis.', {
104
expect_true("average_price" == rlang::get_expr(properties$y))
105
})
106
test_that('avocado_aggregate_plot should be a scatter plot.', {
107
expect_true("GeomPoint" %in% c(class(avocado_aggregate_plot$layers[[1]]$geom)))
108
})
109
test_that('Labels on the axes should be descriptive and human readable.', {
110
expect_false(avocado_aggregate_plot$labels$y == 'average_price')
111
expect_false(avocado_aggregate_plot$labels$x == 'wk')
112
})
113
print("Success!")
114
}
115
116
test_1.8 <- function(){
117
test_that('Did not create an object named avocado', {
118
expect_true(exists("avocado"))
119
})
120
test_that('avocado does not contain the correct number of rows and/or columns.', {
121
expect_equal(dim(avocado), c(17911, 10))
122
})
123
test_that('Columns in avocado contain incorrect values.', {
124
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
125
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
126
})
127
print("Success!")
128
}
129
130
test_1.9 <- function(){
131
test_that('Did not create an object named avocado_aggregate_2', {
132
expect_true(exists("avocado_aggregate_2"))
133
})
134
test_that('avocado_aggregate_2 does not contain the correct number of rows and/or columns.', {
135
expect_equal(dim(avocado_aggregate_2), c(53, 2))
136
})
137
test_that('Columns in avocado_aggregate_2 contain incorrect values.', {
138
expect_equal(digest(round(as.numeric(sum(avocado_aggregate_2$total_volume)))), 'cf6644bdab5e986758065d29039b4ab1') # we hid the answer to the test here so you can't see it, but we can still run the test
139
expect_equal(digest(as.numeric(sum(avocado_aggregate_2$wk))), 'd27e825e408c446c586593f719b5545e') # we hid the answer to the test here so you can't see it, but we can still run the test
140
})
141
print("Success!")
142
}
143
144
test_1.10 <- function(){
145
test_that('Did not create a plot named avocado_aggregate_plot_2', {
146
expect_true(exists("avocado_aggregate_plot_2"))
147
})
148
properties <- c(avocado_aggregate_plot_2$layers[[1]]$mapping, avocado_aggregate_plot_2$mapping)
149
test_that('week should be on the x-axis.', {
150
expect_true("wk" == rlang::get_expr(properties$x))
151
})
152
test_that('total_volume should be on the y-axis.', {
153
expect_true("total_volume" == rlang::get_expr(properties$y))
154
})
155
test_that('avocado_aggregate_plot_2 should be a scatter plot.', {
156
expect_true("GeomPoint" %in% c(class(avocado_aggregate_plot_2$layers[[1]]$geom)))
157
})
158
test_that('Labels on the axes should be descriptive and human readable.', {
159
expect_false(avocado_aggregate_plot_2$labels$y == 'total_volume')
160
expect_false(avocado_aggregate_plot_2$labels$x == 'wk')
161
})
162
print("Success!")
163
}
164
165
test_2.1 <- function(){
166
test_that('Solution is incorrect', {
167
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
168
})
169
print("Success!")
170
}
171
172
test_2.2 <- function(){
173
test_that('Solution is incorrect', {
174
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
175
})
176
print("Success!")
177
}
178
179
test_2.3 <- function(){
180
test_that('Did not create an object named sea_surface', {
181
expect_true(exists("sea_surface"))
182
})
183
test_that('sea_surface should be a data frame.', {
184
expect_true('data.frame' %in% class(sea_surface))
185
})
186
test_that('sea_surface does not contain the correct data.', {
187
expect_equal(dim(sea_surface), c(105, 13))
188
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
189
expect_equal(colnames(sea_surface), c("Year", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
190
})
191
print("Success!")
192
}
193
194
test_2.3.1 <- function(){
195
test_that('Solution is incorrect', {
196
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
197
})
198
print("Success!")
199
}
200
201
202
test_2.4 <- function(){
203
test_that('Did not create an object named tidy_temp', {
204
expect_true(exists("tidy_temp"))
205
})
206
test_that('tidy_temp does not contain the correct number of rows and/or columns.', {
207
expect_equal(dim(tidy_temp), c(1260, 3))
208
})
209
test_that('Columns in tidy_temp contain incorrect values.', {
210
expect_equal(digest(round(as.numeric(sum(tidy_temp$Temperature, na.rm = TRUE)))), 'af04dd98a55b36f9b8344dc9146adec7') # we hid the answer to the test here so you can't see it, but we can still run the test
211
})
212
print("Success!")
213
}
214
215
test_2.5 <- function(){
216
test_that('Did not create a plot named nov_temp_plot', {
217
expect_true(exists("nov_temp_plot"))
218
})
219
properties <- c(nov_temp_plot$layers[[1]]$mapping, nov_temp_plot$mapping)
220
test_that('The Month column in nov_temp_plot should only contain November.', {
221
expect_equal(unique(nov_temp_plot$data$Month), "Nov")
222
})
223
test_that('Year should be on the x-axis.', {
224
expect_true("Year" == rlang::get_expr(properties$x))
225
})
226
test_that('Temperature should be on the y-axis.', {
227
expect_true("Temperature" == rlang::get_expr(properties$y))
228
})
229
test_that('nov_temp_plot should be a scatter plot.', {
230
expect_true("GeomPoint" %in% c(class(nov_temp_plot$layers[[1]]$geom)))
231
})
232
print("Success!")
233
}
234
235
test_2.6 <- function(){
236
test_that('Did not create a plot named all_temp_plot', {
237
expect_true(exists("all_temp_plot"))
238
})
239
properties <- c(all_temp_plot$layers[[1]]$mapping, all_temp_plot$mapping)
240
test_that('Need to use tidy_temp as the data!', {
241
expect_true("Month" %in% colnames(all_temp_plot$data))
242
})
243
test_that('Should use facet_wrap to facet by month', {
244
expect_true('FacetWrap' %in% class(all_temp_plot$facet))
245
})
246
test_that('Year should be on the x-axis.', {
247
expect_true("Year" == rlang::get_expr(properties$x))
248
})
249
test_that('Temperature should be on the y-axis.', {
250
expect_true("Temperature" == rlang::get_expr(properties$y))
251
})
252
test_that('all_temp_plot should be a scatter plot.', {
253
expect_true("GeomPoint" %in% c(class(all_temp_plot$layers[[1]]$geom)))
254
})
255
print("Success!")
256
}
257
258
test_3.1 <- function(){
259
test_that('Solution is incorrect', {
260
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
261
})
262
print("Success!")
263
}
264
265
test_3.2 <- function(){
266
test_that('Did not create an object named madrid', {
267
expect_true(exists("madrid"))
268
})
269
test_that('madrid should be a data frame.', {
270
expect_true('data.frame' %in% class(madrid))
271
})
272
test_that('madrid does not contain the correct data.', {
273
expect_equal(dim(madrid), c(51864, 17))
274
expect_equal(digest(round(sum(madrid$BEN, na.rm = TRUE))), '2cd026ed377da614f376230e021330a0') # we hid the answer to the test here so you can't see it, but we can still run the test
275
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"))
276
})
277
print("Success!")
278
}
279
280
test_3.3 <- function(){
281
test_that('Did not create a plot named EBE_pollution', {
282
expect_true(exists("EBE_pollution"))
283
})
284
properties <- c(EBE_pollution$layers[[1]]$mapping, EBE_pollution$mapping)
285
test_that('date should be on the x-axis.', {
286
expect_true("date" == rlang::get_expr(properties$x))
287
})
288
test_that('EBE should be on the y-axis.', {
289
expect_true("EBE" == rlang::get_expr(properties$y))
290
})
291
test_that('EBE_pollution should be a scatter plot.', {
292
expect_true("GeomPoint" %in% c(class(EBE_pollution$layers[[1]]$geom)))
293
})
294
test_that('Labels on the axes should be descriptive and human readable.', {
295
expect_false(EBE_pollution$labels$y == 'EBE')
296
expect_false(EBE_pollution$labels$x == 'date')
297
})
298
print("Success!")
299
}
300
301
test_3.4 <- function(){
302
test_that('Did not create an object named madrid_pollution', {
303
expect_true(exists("madrid_pollution"))
304
})
305
test_that('madrid_pollution does not contain the correct number of rows and/or columns.', {
306
expect_equal(dim(madrid_pollution), c(72, 3))
307
})
308
test_that('Columns in avocado_aggregate_2 contain incorrect values.', {
309
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
310
})
311
print("Success!")
312
}
313
314
test_3.5 <- function(){
315
test_that('Did not create a plot named madrid_plot', {
316
expect_true(exists("madrid_plot"))
317
})
318
properties <- c(madrid_plot$layers[[1]]$mapping, madrid_plot$mapping)
319
test_that('month should be on the x-axis.', {
320
expect_true("mnth" == rlang::get_expr(properties$x))
321
})
322
test_that('max_ebe should be on the y-axis.', {
323
expect_true("max_ebe" == rlang::get_expr(properties$y))
324
})
325
test_that('madrid_plot should be a scatter plot.', {
326
expect_true("GeomPoint" %in% c(class(madrid_plot$layers[[1]]$geom)))
327
})
328
test_that('Labels on the axes should be descriptive and human readable.', {
329
expect_false(madrid_plot$labels$y == 'max_ebe')
330
expect_false(madrid_plot$labels$x == 'mnth')
331
})
332
print("Success!")
333
}
334
335
test_3.6 <- function(){
336
test_that('Did not create an object named pollution_2001', {
337
expect_true(exists("pollution_2001"))
338
})
339
test_that('pollution_2001 does not contain the correct number of rows and/or columns.', {
340
expect_equal(dim(pollution_2001), c(1, 14))
341
})
342
test_that('Columns in pollution_2001 contain incorrect values.', {
343
expect_equal(digest(round(as.numeric(pollution_2001$MXY))), '60b03cfa348171e330bfce8c904e05e1') # we hid the answer to the test here so you can't see it, but we can still run the test
344
expect_equal(digest(round(as.numeric(sum(pollution_2001)))), '0b3b179a82413c4c7e8c77ced0af64bc') # we hid the answer to the test here so you can't see it, but we can still run the test
345
})
346
print("Success!")
347
}
348
349
test_3.7 <- function(){
350
test_that('Did not create an object named pollution_2006', {
351
expect_true(exists("pollution_2006"))
352
})
353
test_that('pollution_2006 does not contain the correct number of rows and/or columns.', {
354
expect_equal(dim(pollution_2006), c(1, 14))
355
})
356
test_that('Columns in pollution_2006 contain incorrect values.', {
357
expect_equal(digest(round(as.numeric(pollution_2006$MXY))), 'ab6d52266d2102fdb86c7ebecd3a881e') # we hid the answer to the test here so you can't see it, but we can still run the test
358
expect_equal(digest(round(as.numeric(sum(pollution_2006)))), '43861b3e08cca196ae86cc7682d51cbc') # 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.8 <- function(){
364
test_that('Solution is incorrect', {
365
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
366
})
367
print("Success!")
368
}
369
370
test_3.9 <- function(){
371
test_that('Did not create an object named pollution_diff', {
372
expect_true(exists("pollution_diff"))
373
})
374
test_that('pollution_diff does not contain the correct number of rows and/or columns.', {
375
expect_equal(dim(pollution_diff), c(14, 2))
376
})
377
test_that('pollution_diff does not contain the correct columns: pollutant and value.', {
378
expect_equal(colnames(pollution_diff), c('pollutant', 'value'))
379
})
380
test_that('Columns in pollution_diff contain incorrect values.', {
381
expect_equal(digest(round(as.numeric(sum(pollution_diff$value)))), 'e2e3d4936bf34e5a6d239da763a0e19a') # 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.10 <- function(){
387
test_that('Did not create an object named max_pollution_diff', {
388
expect_true(exists("max_pollution_diff"))
389
})
390
test_that('max_pollution_diff does not contain the correct number of rows and/or columns.', {
391
expect_equal(dim(max_pollution_diff), c(1, 2))
392
})
393
test_that('max_pollution_diff does not contain the correct columns: pollutant and value.', {
394
expect_equal(colnames(max_pollution_diff), c('pollutant', 'value'))
395
})
396
test_that('Columns in pollution_diff contain incorrect values.', {
397
expect_equal(digest(round(as.numeric(sum(max_pollution_diff$value)))), '2ee915b9eb2a629faf934dc8dd9cdc46') # we hid the answer to the test here so you can't see it, but we can still run the test
398
})
399
print("Success!")
400
}
401