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_07/tests_tutorial_07.R
2051 views
1
library(testthat)
2
library(digest)
3
library(rlang)
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_1.0 <- function(){
19
test_that('Solution is incorrect', {
20
expect_true(exists('answer1.0'))
21
expect_equal(digest(answer1.0), 'c1f86f7430df7ddb256980ea6a3b57a4') # we hid the answer to the test here so you can't see it, but we can still run the test
22
})
23
print("Success!")
24
}
25
26
test_1.1 <- function(){
27
test_that('Solution is incorrect', {
28
expect_true(exists('answer1.1'))
29
expect_equal(digest(answer1.1), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test
30
})
31
print("Success!")
32
}
33
34
test_2.0 <- function(){
35
test_that('number_of_rows does not exist', {
36
expect_true(exists('number_of_rows'))
37
})
38
test_that('The number of rows is incorrect', {
39
expect_equal(digest(int_round(number_of_rows, 0)), 'a58969729b9f8713e03743154dfa7b57') # we hid the answer to the test here so you can't see it, but we can still run the test
40
})
41
print("Success!")
42
}
43
44
test_2.1 <- function(){
45
test_that('Solution is incorrect', {
46
expect_equal(int_round(nrow(counts), 0), 10)
47
expect_equal(digest(int_round(sum(as.integer(counts$y)), 2)), 'ae80db6ce6693e364a09f88dd4e3d553')
48
expect_equal(dim(counts), c(10, 2))
49
expect_equal(digest(int_round(sum(as.integer(counts$n)), 2)), '9ecacdf4df8df05e2501eb3f18bd13ab')
50
})
51
print("Success!")
52
}
53
54
test_2.2 <- function(){
55
test_that('answer2.2 does not exist', {
56
expect_true(exists('answer2.2'))
57
})
58
test_that('Solution is incorrect', {
59
expect_equal(digest(answer2.2), 'd2a90307aac5ae8d0ef58e2fe730d38b')
60
})
61
print("Success!")
62
}
63
64
test_2.4 <- function(){
65
test_that('Solution is incorrect', {
66
expect_equal(digest(int_round(as.integer(label_102[[1]]), 2)), '069c0949cc402f30063b90e9a31f87e1') # we hid the answer to the test here so you can't see it, but we can still run the test
67
})
68
print("Success!")
69
}
70
71
test_3.0 <- function(){
72
test_that('testing_set does not exist', {
73
expect_true(exists('testing_set'))
74
})
75
test_that('testing_set does not contain the correct data', {
76
expect_equal(dim(testing_set), c(600, 786))
77
})
78
test_that('training_set does not exist', {
79
expect_true(exists('training_set'))
80
})
81
test_that('training_set does not contain the correct data',{
82
expect_equal(dim(training_set), c(600, 785))
83
})
84
print("Success!")
85
}
86
87
test_3.1 <- function(){
88
test_that('answer3.1 does not exist', {
89
expect_true(exists('answer3.1'))
90
})
91
test_that('answer3.1 is incorrect', {
92
expect_equal(digest(answer3.1), '75f1160e72554f4270c809f041c7a776')
93
})
94
print("Success!")
95
}
96
97
test_3.2 <- function(){
98
properties <- c(cross_val_plot$layers[[1]]$mapping, cross_val_plot$mapping)
99
labels <- cross_val_plot$labels
100
test_that('Did not create an object named cross_val_plot',{
101
expect_true(exists('cross_val_plot'))
102
})
103
test_that('cross_val_plot is not a lineplot', {
104
expect_true('GeomPoint' %in% class(cross_val_plot$layers[[1]]$geom))
105
expect_true('GeomLine' %in% class(cross_val_plot$layers[[2]]$geom))
106
})
107
test_that('neighbors should be on the x-axis.', {
108
expect_true("neighbors" == rlang::get_expr(properties$x))
109
})
110
test_that('mean should be on the y-axis.', {
111
expect_true("mean" == rlang::get_expr(properties$y))
112
})
113
test_that('cross_val_plot does not contain the correct data.', {
114
expect_equal(digest(int_round(sum(cross_val_plot$data$mean), 2)), '088459b2025cd3f9cbf1451f8c2c1636')
115
expect_equal(digest(int_round(sum(cross_val_plot$data$n), 2)), '189e2f1b2fbb3743811990e9708c226a')
116
})
117
test_that('cross_val_plot should only contain accuracy.', {
118
expect_true('accuracy' %in% unique(cross_val_plot$data$.metric))
119
})
120
print("Success!")
121
}
122
123
test_3.3 <- function(){
124
test_that('Did not create an object named answer3.3', {
125
expect_true(exists('answer3.3'))
126
})
127
test_that('Solution is incorrect', {
128
expect_equal(digest(answer3.3), '75f1160e72554f4270c809f041c7a776')
129
})
130
print("Success!")
131
}
132
133
test_4.0 <- function(){
134
test_that('Did not create an object named mnist_spec', {
135
expect_true(exists("mnist_spec"))
136
})
137
test_that('k value is incorrect', {
138
expect_equal(digest(int_round(get_expr(mnist_spec$args$neighbors), 0)), '11946e7a3ed5e1776e81c0f0ecd383d0')
139
})
140
test_that('weight_func is incorrect', {
141
expect_equal(digest(as.character(get_expr(mnist_spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')
142
})
143
test_that('set_engine is incorrect', {
144
expect_equal(digest(as.character(mnist_spec$engine)), '93fe1d3f0a1fa2e625af1e1eb51a5c33')
145
})
146
test_that('mode is incorrect', {
147
expect_equal(digest(as.character(mnist_spec$mode)), 'f361ba6f6b32d068e56f61f53d35e26a')
148
})
149
test_that('Did not create an object named mnist_fit', {
150
expect_true(exists("mnist_fit"))
151
})
152
test_that('mnist_fit should be a fit model.', {
153
expect_true('workflow' %in% class(mnist_fit))
154
})
155
test_that('mnist_fit does not include training_set dataset', {
156
expect_equal(digest(int_round(sum(as.integer(unlist(mnist_fit$fit$fit$fit$data[785]))), 2)), '4eb0b7cdd24a983324274d66e0860365')
157
})
158
test_that('mnist_fit does not contain mnist_spec', {
159
expect_equal(digest(int_round(get_expr(mnist_fit$fit$fit$spec$args$neighbors), 0)), '11946e7a3ed5e1776e81c0f0ecd383d0')
160
expect_equal(digest(as.character(get_expr(mnist_fit$fit$fit$spec$args$weight_func))), '989de78e881829b4499af3610dfe54fd')
161
expect_equal(digest(as.character(mnist_fit$fit$fit$spec$mode)), 'f361ba6f6b32d068e56f61f53d35e26a')
162
})
163
print("Success!")
164
}
165
166
test_4.1 <- function(){
167
test_that('Did not create an object named mnist_predictions', {
168
expect_true(exists('mnist_predictions'))
169
})
170
test_that('mnist_predictions does not contain the correct data', {
171
expect_equal(digest(int_round(sum(as.integer(mnist_predictions$.pred_class)), 2)), 'ad1b496bd820958116aa67cdb147352b')
172
expect_equal(digest(int_round(sum(as.integer(mnist_predictions$y)), 2)), '733395e0768b9479c1feffa0b067fcdd')
173
})
174
test_that('Did not create an objected named training_results', {
175
expect_true(exists('training_results'))
176
})
177
test_that('training_results is not a confusion matrix', {
178
expect_true('conf_mat' %in% class(training_results))
179
})
180
test_that('training_results contains the incorrect data', {
181
expect_equal(digest(int_round(sum(as.integer(training_results$table)), 2)), '9ecacdf4df8df05e2501eb3f18bd13ab')
182
})
183
print("Success!")
184
}
185
186
test_4.4 <- function(){
187
test_that('Did not create an object named answer4.4', {
188
expect_true(exists('answer4.4'))
189
})
190
test_that('Solution is incorrect', {
191
expect_equal(digest(answer4.4), '05ca18b596514af73f6880309a21b5dd')
192
})
193
print("Success!")
194
}
195
196
test_4.5 <- function(){
197
test_that('Did not create an object named answer4.5', {
198
expect_true(exists('answer4.5'))
199
})
200
test_that('Solution is incorrect', {
201
expect_equal(digest(answer4.5), '3a5505c06543876fe45598b5e5e5195d')
202
})
203
print("Success!")
204
}
205
206