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