Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2020-spring/materials/worksheet_09/tests_worksheet_09.R
2051 views
1
library(testthat)
2
library(digest)
3
4
test_1.0 <- function() {
5
test_that('Solution is incorrect', {
6
expect_equal(digest(answer1.0), '3a5505c06543876fe45598b5e5e5195d')
7
})
8
print("Success!")
9
}
10
11
test_1.1 <- function() {
12
test_that('Solution is incorrect', {
13
expect_equal(digest(answer1.1), '475bf9280aab63a82af60791302736f6')
14
})
15
print("Success!")
16
}
17
18
test_1.2 <- function() {
19
test_that('Solution is incorrect', {
20
expect_equal(digest(answer1.2), '75f1160e72554f4270c809f041c7a776')
21
})
22
print("Success!")
23
}
24
25
test_2.0 <- function() {
26
test_that('Solution is incorrect', {
27
expect_that(digest(round(answer2.0, 1)) %in% c('3c3b9d75cc0e8cfcc29f40abd17afe8a', '03b3b7e9967823ca8ae75c138a3aa39c', 'af04a6f39588915a4dcac626c46434de'), is_true())
28
})
29
print("Success!")
30
}
31
32
test_2.1 <- function() {
33
test_that('Solution is incorrect', {
34
expect_that(digest(round(answer2.1, 1)) %in% c('3c3b9d75cc0e8cfcc29f40abd17afe8a', '03b3b7e9967823ca8ae75c138a3aa39c', 'af04a6f39588915a4dcac626c46434de'), is_true())
35
})
36
print("Success!")
37
}
38
39
test_2.2 <- function() {
40
test_that('Solution is incorrect', {
41
expect_that(digest(round(answer2.2, 1)) %in% c('f23662d0838c244acb308b71749ac22e', '6a8e65e0821e8011c0f04d886dce9323', 'e4de8af39ae8ed8bf8976830f6c8e989'), is_true())
42
})
43
print("Success!")
44
}
45
46
test_2.3 <- function() {
47
test_that('Solution is incorrect', {
48
expect_equal(digest(answer2.3), '475bf9280aab63a82af60791302736f6')
49
})
50
print("Success!")
51
}
52
53
test_3.0 <- function() {
54
test_that('Did not create an object named marathon', {
55
expect_true(exists("marathon"))
56
})
57
test_that('marathon should be a data frame.', {
58
expect_true('data.frame' %in% class(marathon))
59
})
60
test_that('marathon does not contain the correct number of rows and/or columns.', {
61
expect_equal(dim(marathon), c(929, 13))
62
})
63
test_that('The marathon data frame is missing columns.', {
64
expect_that("time_hrs" %in% colnames(marathon), is_true())
65
expect_that("max" %in% colnames(marathon), is_true())
66
})
67
print("Success!")
68
}
69
70
test_3.1 <- function() {
71
test_that('Did not create an object named X_train', {
72
expect_true(exists("X_train"))
73
})
74
test_that('X_train should be a data frame.', {
75
expect_true('data.frame' %in% class(X_train))
76
})
77
test_that('X_train does not contain the correct number of rows and/or columns.', {
78
expect_equal(dim(X_train), c(698, 1))
79
})
80
test_that('X_train does not contain the column max', {
81
expect_true('max' %in% colnames(X_train))
82
})
83
test_that('Did not create an object named X_test', {
84
expect_true(exists("X_test"))
85
})
86
test_that('X_test should be a data frame.', {
87
expect_true('data.frame' %in% class(X_test))
88
})
89
test_that('X_test does not contain the correct number of rows and/or columns.', {
90
expect_equal(dim(X_test), c(231, 1))
91
})
92
test_that('X_test does not contain the column max', {
93
expect_true('max' %in% colnames(X_test))
94
})
95
test_that('Did not create an object named Y_train', {
96
expect_true(exists("Y_train"))
97
})
98
test_that('Y_train should be a numeric vector', {
99
expect_true('numeric' %in% class(Y_train))
100
})
101
test_that('Y_train is not the correct length.', {
102
expect_equal(length(Y_train), 698)
103
})
104
test_that('Did not create an object named Y_test', {
105
expect_true(exists("Y_test"))
106
})
107
test_that('Y_test should be a numeric vector', {
108
expect_true('numeric' %in% class(Y_test))
109
})
110
test_that('Y_test is not the correct length.', {
111
expect_equal(length(Y_test), 231)
112
})
113
print("Success!")
114
}
115
116
test_3.2 <- function() {
117
test_that('Did not create a plot named marathon_eda', {
118
expect_true(exists("marathon_eda"))
119
})
120
test_that('max should be on the x-axis.', {
121
expect_that("max" %in% c(rlang::get_expr(marathon_eda$mapping$x),rlang::get_expr(marathon_eda$layers[[1]]$mapping$x)), is_true())
122
})
123
test_that('time_hrs should be on the y-axis.', {
124
expect_that("time_hrs" %in% c(rlang::get_expr(marathon_eda$mapping$y), rlang::get_expr(marathon_eda$layers[[1]]$mapping$y)) , is_true())
125
})
126
test_that('marathon_eda should be a scatter plot.', {
127
expect_equal(digest(class(rlang::get_expr(marathon_eda$layers[[1]]$geom))[1]), '911e5b9debfb523f25ad2ccc01a4b2dd')
128
})
129
test_that('Labels on the axes should be descriptive and human readable.', {
130
expect_that((marathon_eda$labels$y) == 'time_hrs', is_false())
131
expect_that((marathon_eda$labels$x) == 'max', is_false())
132
})
133
test_that('Only the training data set should be used to create the plot', {
134
expect_equal(nrow(marathon_eda$data), 698)
135
})
136
print("Success!")
137
}
138
139
test_3.3 <- function() {
140
test_that('Did not create an object named lm_model', {
141
expect_true(exists("lm_model"))
142
})
143
test_that('x in lm_model should be X_train', {
144
expect_equal(as.character(lm_model$call$x), 'X_train')
145
expect_equal(dim(lm_model$trainingData), c(698, 2))
146
})
147
test_that('y in lm_model should be Y_train', {
148
expect_equal(as.character(lm_model$call$y), 'Y_train')
149
expect_equal(dim(lm_model$trainingData), c(698, 2))
150
})
151
test_that('method should be lm', {
152
expect_equal(as.character(lm_model$method), 'lm')
153
})
154
print("Success!")
155
}
156
157
test_3.4 <- function() {
158
test_that('Did not create a plot named lm_predictions', {
159
expect_true(exists("lm_predictions"))
160
})
161
test_that('max should be on the x-axis.', {
162
expect_that("max" %in% c(rlang::get_expr(lm_predictions$mapping$x),rlang::get_expr(lm_predictions$layers[[1]]$mapping$x)), is_true())
163
})
164
test_that('time_hrs should be on the y-axis.', {
165
expect_that("time_hrs" %in% c(rlang::get_expr(lm_predictions$mapping$y), rlang::get_expr(lm_predictions$layers[[1]]$mapping$y)) , is_true())
166
})
167
test_that('lm_predictions should be a scatter plot.', {
168
expect_true('GeomPoint' %in% c(class(rlang::get_expr(lm_predictions$layers[[1]]$geom)), class(rlang::get_expr(lm_predictions$layers[[2]]$geom))))
169
})
170
test_that('lm_predictions should have a best fit line using a linear regression model.', {
171
expect_true('GeomSmooth' %in% c(class(rlang::get_expr(lm_predictions$layers[[2]]$geom)), class(rlang::get_expr(lm_predictions$layers[[1]]$geom))))
172
})
173
test_that('Labels on the axes should be descriptive and human readable.', {
174
expect_that((lm_predictions$labels$y) == 'time_hrs', is_false())
175
expect_that((lm_predictions$labels$x) == 'max', is_false())
176
})
177
print("Success!")
178
}
179
180
test_3.5 <- function() {
181
test_that('lm_rmse is incorrect', {
182
expect_equal(digest(round(lm_rmse, 3)), '788b714543df6e5fa9954cdefb85fde8')
183
})
184
print("Success!")
185
}
186
187
test_3.6 <- function() {
188
test_that('lm_rmspe is incorrect', {
189
expect_equal(digest(round(lm_rmspe, 3)), '53a185c37dade264b27beeead0c1e823')
190
})
191
print("Success!")
192
}
193
194
test_3.61 <- function() {
195
test_that('Did not create a plot named lm_predictions_test', {
196
expect_true(exists("lm_predictions_test"))
197
})
198
test_that('max should be on the x-axis.', {
199
expect_that("max" %in% c(rlang::get_expr(lm_predictions_test$mapping$x),rlang::get_expr(lm_predictions_test$layers[[1]]$mapping$x)), is_true())
200
})
201
test_that('time_hrs should be on the y-axis.', {
202
expect_that("time_hrs" %in% c(rlang::get_expr(lm_predictions_test$mapping$y), rlang::get_expr(lm_predictions_test$layers[[1]]$mapping$y)) , is_true())
203
})
204
test_that('lm_predictions_test should be a scatter plot.', {
205
expect_equal(digest(class(rlang::get_expr(lm_predictions_test$layers[[1]]$geom))[1]), '911e5b9debfb523f25ad2ccc01a4b2dd')
206
})
207
test_that('Labels on the axes should be descriptive and human readable.', {
208
expect_that((lm_predictions_test$labels$y) == 'time_hrs', is_false())
209
expect_that((lm_predictions_test$labels$x) == 'max', is_false())
210
})
211
test_that('Only the testing data set should be used to create the plot', {
212
expect_equal(nrow(lm_predictions_test$data), 231)
213
})
214
print("Success!")
215
}
216
217
test_3.7 <- function() {
218
test_that('Solution is incorrect', {
219
expect_equal(digest(answer3.7), '75f1160e72554f4270c809f041c7a776')
220
})
221
print("Success!")
222
}
223
224
test_3.8 <- function() {
225
test_that('Solution is incorrect', {
226
expect_equal(digest(answer3.8), '3a5505c06543876fe45598b5e5e5195d')
227
})
228
print("Success!")
229
}
230
231
232
233