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