Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2020-fall/materials/tutorial_06/tests_tutorial_06.R
2051 views
1
# +
2
library(testthat)
3
library(digest)
4
library(rlang)
5
6
int_round <- function(x, digits = 2){
7
x = x*10^digits
8
xint = as.integer(x)
9
xint1 = xint + 1L
10
if (abs(xint - x) < abs(xint1 - x)){
11
return(xint)
12
}
13
else {
14
return(xint1)
15
}
16
}
17
# -
18
19
test_0.1 <- function(){
20
test_that('Solution is incorrect', {
21
expect_true(exists('answer0.1'))
22
expect_equal(digest(answer0.1), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test
23
24
})
25
print("Success!")
26
}
27
28
test_1.0 <- function(){
29
test_that('Did not create an object named fruit_data', {
30
expect_true(exists("fruit_data"))
31
})
32
test_that('fruit_data does not contain the correct number of rows and/or columns.', {
33
expect_equal(dim(fruit_data), c(59, 7))
34
})
35
test_that('The fruit_name column in fruit_data should be of class factor.', {
36
expect_true(is.factor(fruit_data$fruit_name))
37
})
38
test_that('Columns in fruit_data contain incorrect values.', {
39
expect_equal(digest(int_round(fruit_data$mass)), 'b26195f974598925edc40c2377152892') # we hid the answer to the test here so you can't see it, but we can still run the test
40
})
41
test_that('as_factor() function not used.',{
42
expect_equal(c(levels(fruit_data$fruit_name)), c("apple", "mandarin", "orange", "lemon"))
43
})
44
print("Success!")
45
}
46
47
test_1.1 <- function(){
48
test_that('Solution is incorrect', {
49
expect_true(exists('answer1.1'))
50
expect_equal(digest(answer1.1), '75f1160e72554f4270c809f041c7a776') # we hid the answer to the test here so you can't see it, but we can still run the test
51
52
})
53
print("Success!")
54
}
55
56
test_1.0.1 <- function(){
57
test_that('Solution is incorrect', {
58
expect_equal(digest(answer1.0.1), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test
59
})
60
print("Success!")
61
}
62
63
test_1.2 <- function(){
64
test_that('Did not create an object named fruit_dist_2', {
65
expect_true(exists("fruit_dist_2"))
66
})
67
test_that('fruit_dist_2 is incorrect.', {
68
expect_equal(digest(int_round(fruit_dist_2)), 'd8ba459e9b95d6bb43cdcb8acd275179')
69
})
70
print("Success!")
71
}
72
73
test_1.3 <- function(){
74
test_that('Did not create an object named fruit_dist_44', {
75
expect_true(exists("fruit_dist_44"))
76
})
77
test_that('fruit_dist_44 is incorrect.', {
78
expect_equal(digest(int_round(fruit_dist_44)), 'ea07cf8b74030ff04b56ac69dd094adc')
79
})
80
print("Success!")
81
}
82
83
test_1.4 <- function(){
84
test_that('Solution is incorrect', {
85
expect_true(exists('answer1.4'))
86
expect_equal(digest(answer1.4), 'c1f86f7430df7ddb256980ea6a3b57a4') # we hid the answer to the test here so you can't see it, but we can still run the test
87
})
88
print("Success!")
89
}
90
91
# +
92
test_1.5 <- function(){
93
test_that('Did not create an object named fruit_data_scaled', {
94
expect_true(exists("fruit_data_scaled"))
95
})
96
test_that('fruit_data_scaled does not contain the correct number of rows and/or columns.', {
97
expect_equal(dim(fruit_data_scaled), c(59, 5))
98
})
99
test_that('The fruit_name column in fruit_data_scaled should be of class factor.', {
100
expect_true(is.factor(fruit_data_scaled$fruit_name))
101
})
102
test_that('Columns in fruit_data_scaled are not centered.', {
103
expect_equal(digest(int_round(mean(fruit_data_scaled$mass, na.rm = TRUE))), '1473d70e5646a26de3c52aa1abd85b1f') # we hid the answer to the test here so you can't see it, but we can still run the test
104
expect_equal(digest(int_round(mean(fruit_data_scaled$height, na.rm = TRUE))), '1473d70e5646a26de3c52aa1abd85b1f') # we hid the answer to the test here so you can't see it, but we can still run the test
105
expect_equal(digest(int_round(mean(fruit_data_scaled$width, na.rm = TRUE))), '1473d70e5646a26de3c52aa1abd85b1f') # we hid the answer to the test here so you can't see it, but we can still run the test
106
expect_equal(digest(int_round(mean(fruit_data_scaled$color_score, na.rm = TRUE))), '1473d70e5646a26de3c52aa1abd85b1f') # we hid the answer to the test here so you can't see it, but we can still run the test
107
108
})
109
test_that('Columns in fruit_data_scaled are not scaled.', {
110
expect_equal(digest(int_round(sd(fruit_data_scaled$mass, na.rm = TRUE))), '5d6e7fe43b3b73e5fd2961d5162486fa') # we hid the answer to the test here so you can't see it, but we can still run the test
111
expect_equal(digest(int_round(sd(fruit_data_scaled$height, na.rm = TRUE))), '5d6e7fe43b3b73e5fd2961d5162486fa') # we hid the answer to the test here so you can't see it, but we can still run the test
112
expect_equal(digest(int_round(sd(fruit_data_scaled$width, na.rm = TRUE))), '5d6e7fe43b3b73e5fd2961d5162486fa') # we hid the answer to the test here so you can't see it, but we can still run the test
113
expect_equal(digest(int_round(sd(fruit_data_scaled$color_score, na.rm = TRUE))), '5d6e7fe43b3b73e5fd2961d5162486fa') # we hid the answer to the test here so you can't see it, but we can still run the test
114
})
115
print("Success!")
116
}
117
# -
118
119
test_1.6 <- function(){
120
test_that('Did not create an object named distance_44', {
121
expect_true(exists("distance_44"))
122
})
123
test_that('Did not create an object named distance_2', {
124
expect_true(exists("distance_2"))
125
})
126
test_that('distance_44 should be a distance.', {
127
expect_true('dist' %in% class(distance_44))
128
})
129
test_that('distance_2 should be a distance.', {
130
expect_true('dist' %in% class(distance_2))
131
})
132
test_that('distance_44 is incorrect.', {
133
expect_equal(digest(int_round(distance_44)), '78f799aab6957dffdfd2bfb504f8cab5')
134
})
135
test_that('distance_2 is incorrect.', {
136
expect_equal(digest(int_round(distance_2)), '192b298ed4661ab6d9a4a193b2e60b49')
137
})
138
print("Success!")
139
}
140
141
test_1.7 <- function(){
142
properties <- c(fruit_plot$layers[[1]]$mapping, fruit_plot$mapping)
143
labels <- fruit_plot$labels
144
test_that('Did not create a plot named fruit_plot', {
145
expect_true(exists("fruit_plot"))
146
})
147
test_that('scaled_mass should be on the x-axis.', {
148
expect_true("mass" == rlang::get_expr(properties$x))
149
})
150
test_that('scaled_color should be on the y-axis.', {
151
expect_true("color_score" == rlang::get_expr(properties$y))
152
})
153
test_that('fruit_name should be mapped to colour', {
154
expect_true("fruit_name" == rlang::get_expr(properties$colour))
155
})
156
test_that('fruit_plot should be a scatter plot.', {
157
expect_true("GeomPoint" %in% c(class(fruit_plot$layers[[1]]$geom)))
158
})
159
test_that('Labels on the axes should be descriptive and human readable.', {
160
expect_false((labels$y) == 'color_score')
161
expect_false((labels$x) == 'mass')
162
expect_false((labels$colour) == 'fruit_name')
163
})
164
print("Success!")
165
}
166
167
test_1.8 <- function(){
168
test_that('Did not create an object named answer1.8', {
169
expect_true(exists("answer1.8"))
170
})
171
test_that('incorrect answer', {
172
expect_equal(digest(answer1.8), '3a5505c06543876fe45598b5e5e5195d')
173
})
174
print("Success!")
175
}
176
177
test_1.9 <- function(){
178
test_that('Did not create an object named knn_spec', {
179
expect_true(exists("knn_spec"))
180
})
181
test_that('k is incorrect', {
182
expect_equal(digest(int_round(get_expr(knn_spec$args$neighbors))), '75e76f8b41d0944779e119afd3513844')
183
})
184
test_that('weight_func is incorrect', {
185
expect_equal(digest(as.character(get_expr(knn_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')
186
})
187
test_that('set_engine is incorrect', {
188
expect_equal(digest(as.character(knn_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')
189
})
190
test_that('mode is incorrect', {
191
expect_equal(digest(as.character(knn_spec$mode)), 'f361ba6f6b32d068e56f61f53d35e26a')
192
})
193
test_that('Did not create an object named fruit_fit', {
194
expect_true(exists("fruit_fit"))
195
})
196
test_that('fruit_fit should be a fit model.', {
197
expect_true('workflow' %in% class(fruit_fit))
198
})
199
test_that('fruit_fit does not contain knn_spec', {
200
expect_equal(digest(int_round(get_expr(fruit_fit$fit$actions$model$spec$args$neighbors))), '75e76f8b41d0944779e119afd3513844')
201
expect_equal(digest(as.character(get_expr(fruit_fit$fit$actions$model$spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')
202
expect_equal(digest(fruit_fit$fit$actions$model$spec$mode), 'f361ba6f6b32d068e56f61f53d35e26a')
203
})
204
print("Success!")
205
}
206
207
test_1.10 <- function(){
208
test_that('Prediction is incorrect', {
209
expect_equal(digest(as.character(fruit_predicted)), 'd19d62a873f08af0488f0df720cfd293')
210
})
211
print("Success!")
212
}
213
214
test_1.11 <- function(){
215
test_that('answer1.11 does not exist', {
216
expect_true(exists("answer1.11"))
217
})
218
test_that('Answer is incorrect', {
219
expect_equal(digest(answer1.11), '475bf9280aab63a82af60791302736f6')
220
})
221
print("Success!")
222
}
223
224
test_1.12 <- function(){
225
test_that('fruit_all_predicted does not exist',{
226
expect_true(exists("fruit_all_predicted"))
227
})
228
test_that('prediction is incorrect', {
229
expect_equal(digest(as.character(fruit_all_predicted$.pred_class)), '5e55351cc3517b5b1031d95040455de5')
230
})
231
print("Success!")
232
}
233
234
test_1.13 <- function(){
235
test_that('Answer is incorrect', {
236
expect_equal(digest(answer1.13), '3a5505c06543876fe45598b5e5e5195d')
237
})
238
print("Success!")
239
}
240
241
test_2.0 <- function(){
242
test_that('incorrect classification', {
243
expect_equal(digest(int_round(as.integer(seed_predict$.pred_class))), 'ca21e364698e573f63da3ecac022c6e3') # can't apply int_round
244
})
245
print("Success!")
246
}
247
248
test_2.1 <- function(){
249
test_that('answer2.1 does not exist',{
250
expect_true(exists("answer2.1"))
251
})
252
test_that('Answer is incorrect', {
253
expect_equal(digest(answer2.1), '475bf9280aab63a82af60791302736f6')
254
})
255
print("Success!")
256
}
257
258