Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2021-summer/materials/worksheet_10/tests_worksheet_10.R
2051 views
1
# +
2
library(testthat)
3
library(digest)
4
5
int_round <- function(x, digits){
6
x = x*10^digits
7
xint = as.integer(x)
8
xint1 = xint + 1L
9
if (abs(xint - x) < abs(xint1 - x)){
10
return(xint)
11
}
12
else {
13
return(xint1)
14
}
15
}
16
# -
17
18
# Round double to precise integer
19
#
20
# `int_round` works to create an integer corresponding to a number that is
21
# tested up to a particular decimal point of precision. This is useful when
22
# there is a need to compare a numeric value using hashes.
23
#
24
# @param x Double vector of length one.
25
# @param digits Double vector of length one to specify decimal point of precision. Negative numbers can be used to specifying significant digits > 0.1.
26
#
27
# @return Integer vector of length one corresponding to a particular decimal point of precision.
28
#
29
# @examples
30
# # to get an integer up to two decimals of precision from 234.56789
31
# int_round(234.56789, 2)
32
#
33
# to get an integer rounded to the hundred digit from 234.56789
34
# int_round(234.56789, -2)
35
36
test_0.0 <- function(){
37
test_that('Solution is incorrect', {
38
expect_equal(digest(answer0.0), '01a75cb73d67b0f895ff0e61449c7bf8')
39
})
40
print("Success!")
41
}
42
test_0.1 <- function(){
43
test_that('Solution is incorrect', {
44
expect_equal(digest(answer0.1), 'd19d62a873f08af0488f0df720cfd293')
45
})
46
print("Success!")
47
}
48
49
test_1.0 <- function(){
50
test_that('Did not create an object named beer', {
51
expect_true(exists("beer"))
52
})
53
test_that('beer should be a tibble.', {
54
expect_true('tbl' %in% class(beer))
55
})
56
test_that('beer does not contain the correct number of rows and/or columns.', {
57
expect_equal(dim(beer), c(2410, 8))
58
})
59
test_that('The beer tibble is missing columns.', {
60
expect_true("abv" %in% colnames(beer))
61
expect_true("ibu" %in% colnames(beer))
62
expect_true("id" %in% colnames(beer))
63
expect_true("name" %in% colnames(beer))
64
expect_true("style" %in% colnames(beer))
65
expect_true("brewery_id" %in% colnames(beer))
66
expect_true("ounces" %in% colnames(beer))
67
})
68
print("Success!")
69
}
70
71
test_1.1 <- function(){
72
properties <- c(beer_eda$layers[[1]]$mapping, beer_eda$mapping)
73
labels <- beer_eda$labels
74
test_that('Did not create a plot named beer_eda', {
75
expect_true(exists("beer_eda"))
76
})
77
test_that('ibu should be on the x-axis.', {
78
expect_true("ibu" == rlang::get_expr(properties$x))
79
})
80
test_that('abv should be on the y-axis.', {
81
expect_true("abv" == rlang::get_expr(properties$y))
82
})
83
test_that('beer_eda should be a scatter plot.', {
84
expect_true("GeomPoint" %in% c(class(beer_eda$layers[[1]]$geom)))
85
})
86
test_that('Labels on the axes should be descriptive and human readable.', {
87
expect_false((labels$y) == 'abv')
88
expect_false((labels$x) == 'ibu')
89
})
90
print("Success!")
91
}
92
93
test_1.2 <- function(){
94
test_that('Did not create an object named clean_beer', {
95
expect_true(exists("clean_beer"))
96
})
97
test_that('clean_beer should be a tibble.', {
98
expect_true('tbl' %in% class(clean_beer))
99
})
100
test_that('clean_beer should only contain the columns ibu and abv', {
101
expect_true("ibu" %in% colnames(clean_beer))
102
expect_true("abv" %in% colnames(clean_beer))
103
expect_false("id" %in% colnames(clean_beer))
104
expect_false("name" %in% colnames(clean_beer))
105
expect_false("style" %in% colnames(clean_beer))
106
expect_false("brewery_id" %in% colnames(clean_beer))
107
expect_false("ounces" %in% colnames(clean_beer))
108
})
109
test_that('clean_beer does not contain the correct number of rows and/or columns.', {
110
expect_equal(dim(clean_beer), c(1405, 2))
111
})
112
113
print("Success!")
114
}
115
116
test_1.3.1 <- function(){
117
test_that('Solution is incorrect', {
118
expect_equal(digest(answer1.3.1), '75f1160e72554f4270c809f041c7a776')
119
})
120
print("Success!")
121
}
122
123
test_1.3.2 <- function(){
124
test_that('Did not create an object named scaled_beer', {
125
expect_true(exists("scaled_beer"))
126
})
127
test_that('scaled_beer should be a tibble.', {
128
expect_true('tbl' %in% class(scaled_beer))
129
})
130
test_that('scaled_beer does not contain the correct number of rows and/or columns.', {
131
expect_equal(dim(scaled_beer), c(1405, 2))
132
})
133
test_that('scaled_beer should only contain the columns ibu and abv', {
134
expect_true("ibu" %in% colnames(clean_beer))
135
expect_true("abv" %in% colnames(clean_beer))
136
expect_false("id" %in% colnames(clean_beer))
137
expect_false("name" %in% colnames(clean_beer))
138
expect_false("style" %in% colnames(clean_beer))
139
expect_false("brewery_id" %in% colnames(clean_beer))
140
expect_false("ounces" %in% colnames(clean_beer))
141
})
142
test_that('Columns in scaled_beer are not scaled correctly.', {
143
expect_true(min(scaled_beer$ibu) < 1)
144
expect_true(max(scaled_beer$ibu) < 4)
145
expect_true(min(scaled_beer$abv) < -2)
146
expect_true(max(scaled_beer$abv) < 5)
147
})
148
print("Success!")
149
}
150
151
test_1.4 <- function(){
152
test_that('beer_cluster_k2 class should be kmeans', {
153
expect_equal(class(beer_cluster_k2), 'kmeans')
154
})
155
test_that('beer_cluster_k2 should have 2 centers', {
156
expect_equal(int_round(nrow(beer_cluster_k2$centers), 0), 2)
157
})
158
test_that('Solution is incorrect', {
159
expect_equal(int_round(beer_cluster_k2$tot.withinss, 0), 1110)
160
})
161
print("Success!")
162
}
163
164
test_1.5 <- function(){
165
test_that('tidy_beer_cluster_k2 should contain the columns: abv, ibu, and .cluster', {
166
expect_true('abv' %in% colnames(tidy_beer_cluster_k2))
167
expect_true('ibu' %in% colnames(tidy_beer_cluster_k2))
168
expect_true('.cluster' %in% colnames(tidy_beer_cluster_k2))
169
})
170
test_that('tidy_beer_cluster_k2 contains an incorrect number of rows and/or columns.', {
171
expect_equal(int_round(nrow(tidy_beer_cluster_k2), 0), 1405)
172
expect_equal(int_round(ncol(tidy_beer_cluster_k2), 0), 3)
173
})
174
print("Success!")
175
}
176
177
test_1.6 <- function(){
178
properties <- c(tidy_beer_cluster_k2_plot$layers[[1]]$mapping, tidy_beer_cluster_k2_plot$mapping)
179
labels <- tidy_beer_cluster_k2_plot$labels
180
test_that('Did not create a plot named tidy_beer_cluster_k2_plot', {
181
expect_true(exists("tidy_beer_cluster_k2_plot"))
182
})
183
test_that('tidy_beer_cluster_k2_plot should contain information from tidy_beer_cluster_k2', {
184
expect_equal(tidy_beer_cluster_k2_plot$data, tidy_beer_cluster_k2)
185
})
186
test_that('ibu should be on the x-axis.', {
187
expect_true("ibu" == rlang::get_expr(properties$x))
188
})
189
test_that('abv should be on the y-axis.', {
190
expect_true("abv" == rlang::get_expr(properties$y))
191
})
192
test_that('.cluster should be used to colour the points.', {
193
expect_true(".cluster" == rlang::get_expr(properties$colour))
194
})
195
test_that('tidy_beer_cluster_k2_plot should be a scatter plot.', {
196
expect_true("GeomPoint" %in% c(class(tidy_beer_cluster_k2_plot$layers[[1]]$geom)))
197
})
198
test_that('Labels on the axes should be descriptive and human readable.', {
199
expect_false((labels$y) == 'abv')
200
expect_false((labels$x) == 'ibu')
201
expect_false((labels$colour) == '.cluster')
202
})
203
print("Success!")
204
}
205
206
test_1.7.1 <- function(){
207
test_that('Solution is incorrect', {
208
expect_equal(digest(answer1.7.1), '475bf9280aab63a82af60791302736f6')
209
})
210
print("Success!")
211
}
212
213
test_1.7.2 <- function(){
214
test_that('beer_cluster_k2_model_stats should be a tibble.', {
215
expect_true('tbl' %in% class(beer_cluster_k2_model_stats))
216
})
217
test_that('beer_cluster_k2_model_stats should have 1 row of 4 different statistics.', {
218
expect_equal(dim(beer_cluster_k2_model_stats), c(1, 4))
219
})
220
test_that('beer_cluster_k2_model_stats should contain total within sum of squares (tot.withinss).', {
221
expect_true('tot.withinss' %in% colnames(beer_cluster_k2_model_stats))
222
})
223
print("Success!")
224
}
225
226
test_1.8 <- function(){
227
test_that('beer_ks should be a tbl.', {
228
expect_true('tbl' %in% class(beer_ks))
229
})
230
test_that('beer_ks should have 1 column containing k values from 1 to 10.', {
231
expect_equal(int_round(nrow(beer_ks), 0), 10)
232
expect_equal(int_round(ncol(beer_ks), 0), 1)
233
expect_equal(colnames(beer_ks), 'k')
234
})
235
print("Success!")
236
}
237
238
test_1.9 <- function(){
239
test_that('beer_clustering does not contain the correct number of rows and/or columns.', {
240
expect_equal(dim(beer_clustering), c(10, 2))
241
})
242
test_that('beer_clustering should contain the columns k and models', {
243
expect_true('k' %in% colnames(beer_clustering))
244
expect_true('models' %in% colnames(beer_clustering))
245
})
246
test_that('The models column in beer_clustering should be of class kmeans', {
247
expect_equal(class(beer_clustering$models[[1]]), 'kmeans')
248
})
249
print("Success!")
250
}
251
252
test_2.0 <- function(){
253
test_that('beer_model_stats does not contain the correct number of rows and/or columns.', {
254
expect_equal(dim(beer_model_stats), c(10, 3))
255
})
256
test_that('beer_model_stats should contain the columns k, models, and model_statistics', {
257
expect_true('k' %in% colnames(beer_model_stats))
258
expect_true('models' %in% colnames(beer_model_stats))
259
expect_true('model_statistics' %in% colnames(beer_model_stats))
260
})
261
test_that('The models column in beer_model_stats should be of class kmeans', {
262
expect_equal(class(beer_model_stats$models[[1]]), 'kmeans')
263
})
264
test_that('The model_statistics column in beer_model_stats should be a tibble.', {
265
expect_true('tbl' %in% class(beer_model_stats$model_statistics[[1]]))
266
})
267
print("Success!")
268
}
269
270
test_2.1 <- function(){
271
test_that('Solution is incorrect', {
272
expect_equal(int_round(nrow(beer_clustering_unnested), 0), 10)
273
expect_equal(int_round(ncol(beer_clustering_unnested), 0), 6)
274
expect_true('k' %in% colnames(beer_clustering_unnested))
275
expect_true('models' %in% colnames(beer_clustering_unnested))
276
expect_false('model_statistics' %in% colnames(beer_clustering_unnested))
277
expect_equal(class(beer_clustering_unnested$models[[1]]), 'kmeans')
278
expect_true('tot.withinss' %in% colnames(beer_clustering_unnested))
279
})
280
print("Success!")
281
}
282
283
284
test_2.2 <- function(){
285
properties <- c(choose_beer_k$layers[[1]]$mapping, choose_beer_k$mapping)
286
labels <- choose_beer_k$labels
287
test_that('Did not create a plot named choose_beer_k', {
288
expect_true(exists("choose_beer_k"))
289
})
290
test_that('# clusters should be on the x-axis.', {
291
expect_true("k" == rlang::get_expr(properties$x))
292
})
293
test_that('total within-cluster sum-of-squares should be on the y-axis.', {
294
expect_true("tot.withinss" == rlang::get_expr(properties$y))
295
})
296
test_that('choose_beer_k should be a line and scatter plot.', {
297
expect_true("GeomLine" %in% c(class(choose_beer_k$layers[[1]]$geom),class(choose_beer_k$layers[[2]]$geom)))
298
})
299
test_that('choose_beer_k should be a line and scatter plot.', {
300
expect_true("GeomPoint" %in% c(class(choose_beer_k$layers[[1]]$geom),class(choose_beer_k$layers[[2]]$geom)))
301
})
302
test_that('Labels on the axes should be descriptive and human readable.', {
303
expect_false((labels$y) == 'tot.withinss')
304
expect_false((labels$x) == 'k')
305
})
306
print("Success!")
307
}
308
309
test_2.3 <- function(){
310
test_that('Solution is incorrect', {
311
expect_true(digest(answer2.3) %in% c('0e4033b8c0b56afbea35dc749ced4e1d', 'd19d62a873f08af0488f0df720cfd293'))
312
})
313
print("Success!")
314
}
315
316
test_2.4 <- function(){
317
test_that('Solution is incorrect', {
318
expect_equal(digest(answer2.4), '475bf9280aab63a82af60791302736f6')
319
})
320
print("Success!")
321
}
322
323
test_2.5 <- function(){
324
test_that('Solution is incorrect', {
325
expect_equal(digest(answer2.5), '3a5505c06543876fe45598b5e5e5195d')
326
})
327
print("Success!")
328
}
329
330
test_2.6 <- function(){
331
test_that('Solution is incorrect', {
332
expect_equal(digest(answer2.6), '05ca18b596514af73f6880309a21b5dd')
333
})
334
print("Success!")
335
}
336