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