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_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
test_0.0 <- function(){
19
test_that('Solution is incorrect', {
20
expect_equal(digest(answer0.0), '01a75cb73d67b0f895ff0e61449c7bf8')
21
})
22
print("Success!")
23
}
24
test_0.1 <- function(){
25
test_that('Solution is incorrect', {
26
expect_equal(digest(answer0.1), 'd19d62a873f08af0488f0df720cfd293')
27
})
28
print("Success!")
29
}
30
31
test_1.0 <- function(){
32
test_that('Did not create an object named beer', {
33
expect_true(exists("beer"))
34
})
35
test_that('beer should be a tibble.', {
36
expect_true('tbl' %in% class(beer))
37
})
38
test_that('beer does not contain the correct number of rows and/or columns.', {
39
expect_equal(dim(beer), c(2410, 8))
40
})
41
test_that('The beer tibble is missing columns.', {
42
expect_true("abv" %in% colnames(beer))
43
expect_true("ibu" %in% colnames(beer))
44
expect_true("id" %in% colnames(beer))
45
expect_true("name" %in% colnames(beer))
46
expect_true("style" %in% colnames(beer))
47
expect_true("brewery_id" %in% colnames(beer))
48
expect_true("ounces" %in% colnames(beer))
49
})
50
print("Success!")
51
}
52
53
test_1.1 <- function(){
54
properties <- c(beer_eda$layers[[1]]$mapping, beer_eda$mapping)
55
labels <- beer_eda$labels
56
test_that('Did not create a plot named beer_eda', {
57
expect_true(exists("beer_eda"))
58
})
59
test_that('ibu should be on the x-axis.', {
60
expect_true("ibu" == rlang::get_expr(properties$x))
61
})
62
test_that('abv should be on the y-axis.', {
63
expect_true("abv" == rlang::get_expr(properties$y))
64
})
65
test_that('beer_eda should be a scatter plot.', {
66
expect_true("GeomPoint" %in% c(class(beer_eda$layers[[1]]$geom)))
67
})
68
test_that('Labels on the axes should be descriptive and human readable.', {
69
expect_false((labels$y) == 'abv')
70
expect_false((labels$x) == 'ibu')
71
})
72
print("Success!")
73
}
74
75
test_1.2 <- function(){
76
test_that('Did not create an object named clean_beer', {
77
expect_true(exists("clean_beer"))
78
})
79
test_that('clean_beer should be a tibble.', {
80
expect_true('tbl' %in% class(clean_beer))
81
})
82
test_that('clean_beer should only contain the columns ibu and abv', {
83
expect_true("ibu" %in% colnames(clean_beer))
84
expect_true("abv" %in% colnames(clean_beer))
85
expect_false("id" %in% colnames(clean_beer))
86
expect_false("name" %in% colnames(clean_beer))
87
expect_false("style" %in% colnames(clean_beer))
88
expect_false("brewery_id" %in% colnames(clean_beer))
89
expect_false("ounces" %in% colnames(clean_beer))
90
})
91
test_that('clean_beer does not contain the correct number of rows and/or columns.', {
92
expect_equal(dim(clean_beer), c(1405, 2))
93
})
94
95
print("Success!")
96
}
97
98
test_1.3.1 <- function(){
99
test_that('Solution is incorrect', {
100
expect_equal(digest(answer1.3.1), '75f1160e72554f4270c809f041c7a776')
101
})
102
print("Success!")
103
}
104
105
test_1.3.2 <- function(){
106
test_that('Did not create an object named scaled_beer', {
107
expect_true(exists("scaled_beer"))
108
})
109
test_that('scaled_beer should be a tibble.', {
110
expect_true('tbl' %in% class(scaled_beer))
111
})
112
test_that('scaled_beer does not contain the correct number of rows and/or columns.', {
113
expect_equal(dim(scaled_beer), c(1405, 2))
114
})
115
test_that('scaled_beer should only contain the columns ibu and abv', {
116
expect_true("ibu" %in% colnames(clean_beer))
117
expect_true("abv" %in% colnames(clean_beer))
118
expect_false("id" %in% colnames(clean_beer))
119
expect_false("name" %in% colnames(clean_beer))
120
expect_false("style" %in% colnames(clean_beer))
121
expect_false("brewery_id" %in% colnames(clean_beer))
122
expect_false("ounces" %in% colnames(clean_beer))
123
})
124
test_that('Columns in scaled_beer are not scaled correctly.', {
125
expect_true(min(scaled_beer$ibu) < 1)
126
expect_true(max(scaled_beer$ibu) < 4)
127
expect_true(min(scaled_beer$abv) < -2)
128
expect_true(max(scaled_beer$abv) < 5)
129
})
130
print("Success!")
131
}
132
133
test_1.4 <- function(){
134
test_that('beer_cluster_k2 class should be kmeans', {
135
expect_equal(class(beer_cluster_k2), 'kmeans')
136
})
137
test_that('beer_cluster_k2 should have 2 centers', {
138
expect_equal(int_round(nrow(beer_cluster_k2$centers), 0), 2)
139
})
140
test_that('Solution is incorrect', {
141
expect_equal(int_round(beer_cluster_k2$tot.withinss, 0), 1110)
142
})
143
print("Success!")
144
}
145
146
test_1.5 <- function(){
147
test_that('tidy_beer_cluster_k2 should contain the columns: abv, ibu, and .cluster', {
148
expect_true('abv' %in% colnames(tidy_beer_cluster_k2))
149
expect_true('ibu' %in% colnames(tidy_beer_cluster_k2))
150
expect_true('.cluster' %in% colnames(tidy_beer_cluster_k2))
151
})
152
test_that('tidy_beer_cluster_k2 contains an incorrect number of rows and/or columns.', {
153
expect_equal(int_round(nrow(tidy_beer_cluster_k2), 0), 1405)
154
expect_equal(int_round(ncol(tidy_beer_cluster_k2), 0), 3)
155
})
156
print("Success!")
157
}
158
159
test_1.6 <- function(){
160
properties <- c(tidy_beer_cluster_k2_plot$layers[[1]]$mapping, tidy_beer_cluster_k2_plot$mapping)
161
labels <- tidy_beer_cluster_k2_plot$labels
162
test_that('Did not create a plot named tidy_beer_cluster_k2_plot', {
163
expect_true(exists("tidy_beer_cluster_k2_plot"))
164
})
165
test_that('tidy_beer_cluster_k2_plot should contain information from tidy_beer_cluster_k2', {
166
expect_equal(tidy_beer_cluster_k2_plot$data, tidy_beer_cluster_k2)
167
})
168
test_that('ibu should be on the x-axis.', {
169
expect_true("ibu" == rlang::get_expr(properties$x))
170
})
171
test_that('abv should be on the y-axis.', {
172
expect_true("abv" == rlang::get_expr(properties$y))
173
})
174
test_that('.cluster should be used to colour the points.', {
175
expect_true(".cluster" == rlang::get_expr(properties$colour))
176
})
177
test_that('tidy_beer_cluster_k2_plot should be a scatter plot.', {
178
expect_true("GeomPoint" %in% c(class(tidy_beer_cluster_k2_plot$layers[[1]]$geom)))
179
})
180
test_that('Labels on the axes should be descriptive and human readable.', {
181
expect_false((labels$y) == 'abv')
182
expect_false((labels$x) == 'ibu')
183
expect_false((labels$colour) == '.cluster')
184
})
185
print("Success!")
186
}
187
188
test_1.7.1 <- function(){
189
test_that('Solution is incorrect', {
190
expect_equal(digest(answer1.7.1), '475bf9280aab63a82af60791302736f6')
191
})
192
print("Success!")
193
}
194
195
test_1.7.2 <- function(){
196
test_that('beer_cluster_k2_model_stats should be a tibble.', {
197
expect_true('tbl' %in% class(beer_cluster_k2_model_stats))
198
})
199
test_that('beer_cluster_k2_model_stats should have 1 row of 4 different statistics.', {
200
expect_equal(dim(beer_cluster_k2_model_stats), c(1, 4))
201
})
202
test_that('beer_cluster_k2_model_stats should contain total within sum of squares (tot.withinss).', {
203
expect_true('tot.withinss' %in% colnames(beer_cluster_k2_model_stats))
204
})
205
print("Success!")
206
}
207
208
test_1.8 <- function(){
209
test_that('beer_ks should be a tbl.', {
210
expect_true('tbl' %in% class(beer_ks))
211
})
212
test_that('beer_ks should have 1 column containing k values from 1 to 10.', {
213
expect_equal(int_round(nrow(beer_ks), 0), 10)
214
expect_equal(int_round(ncol(beer_ks), 0), 1)
215
expect_equal(colnames(beer_ks), 'k')
216
})
217
print("Success!")
218
}
219
220
test_1.9 <- function(){
221
test_that('beer_clustering does not contain the correct number of rows and/or columns.', {
222
expect_equal(dim(beer_clustering), c(10, 2))
223
})
224
test_that('beer_clustering should contain the columns k and models', {
225
expect_true('k' %in% colnames(beer_clustering))
226
expect_true('models' %in% colnames(beer_clustering))
227
})
228
test_that('The models column in beer_clustering should be of class kmeans', {
229
expect_equal(class(beer_clustering$models[[1]]), 'kmeans')
230
})
231
print("Success!")
232
}
233
234
test_2.0 <- function(){
235
test_that('beer_model_stats does not contain the correct number of rows and/or columns.', {
236
expect_equal(dim(beer_model_stats), c(10, 3))
237
})
238
test_that('beer_model_stats should contain the columns k, models, and model_statistics', {
239
expect_true('k' %in% colnames(beer_model_stats))
240
expect_true('models' %in% colnames(beer_model_stats))
241
expect_true('model_statistics' %in% colnames(beer_model_stats))
242
})
243
test_that('The models column in beer_model_stats should be of class kmeans', {
244
expect_equal(class(beer_model_stats$models[[1]]), 'kmeans')
245
})
246
test_that('The model_statistics column in beer_model_stats should be a tibble.', {
247
expect_true('tbl' %in% class(beer_model_stats$model_statistics[[1]]))
248
})
249
print("Success!")
250
}
251
252
test_2.1 <- function(){
253
test_that('Solution is incorrect', {
254
expect_equal(int_round(nrow(beer_clustering_unnested), 0), 10)
255
expect_equal(int_round(ncol(beer_clustering_unnested), 0), 6)
256
expect_true('k' %in% colnames(beer_clustering_unnested))
257
expect_true('models' %in% colnames(beer_clustering_unnested))
258
expect_false('model_statistics' %in% colnames(beer_clustering_unnested))
259
expect_equal(class(beer_clustering_unnested$models[[1]]), 'kmeans')
260
expect_true('tot.withinss' %in% colnames(beer_clustering_unnested))
261
})
262
print("Success!")
263
}
264
265
266
test_2.2 <- function(){
267
properties <- c(choose_beer_k$layers[[1]]$mapping, choose_beer_k$mapping)
268
labels <- choose_beer_k$labels
269
test_that('Did not create a plot named choose_beer_k', {
270
expect_true(exists("choose_beer_k"))
271
})
272
test_that('# clusters should be on the x-axis.', {
273
expect_true("k" == rlang::get_expr(properties$x))
274
})
275
test_that('total within-cluster sum-of-squares should be on the y-axis.', {
276
expect_true("tot.withinss" == rlang::get_expr(properties$y))
277
})
278
test_that('choose_beer_k should be a line and scatter plot.', {
279
expect_true("GeomLine" %in% c(class(choose_beer_k$layers[[1]]$geom),class(choose_beer_k$layers[[2]]$geom)))
280
})
281
test_that('choose_beer_k should be a line and scatter plot.', {
282
expect_true("GeomPoint" %in% c(class(choose_beer_k$layers[[1]]$geom),class(choose_beer_k$layers[[2]]$geom)))
283
})
284
test_that('Labels on the axes should be descriptive and human readable.', {
285
expect_false((labels$y) == 'tot.withinss')
286
expect_false((labels$x) == 'k')
287
})
288
print("Success!")
289
}
290
291
test_2.3 <- function(){
292
test_that('Solution is incorrect', {
293
expect_true(digest(answer2.3) %in% c('0e4033b8c0b56afbea35dc749ced4e1d', 'd19d62a873f08af0488f0df720cfd293'))
294
})
295
print("Success!")
296
}
297
298