Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2019-fall/materials/tutorial_09/tests_tutorial_09.R
2051 views
1
library(testthat)
2
library(digest)
3
4
test_1.0 <- function() {
5
test_that('Did not create an object named credit', {
6
expect_true(exists("credit"))
7
})
8
test_that('credit should be a data frame.', {
9
expect_true('data.frame' %in% class(credit))
10
})
11
test_that('credit does not contain the correct number of rows and/or columns.', {
12
expect_equal(dim(credit), c(400, 12))
13
})
14
test_that('The credit data frame is missing columns.', {
15
expect_that("Income" %in% colnames(credit), is_true())
16
expect_that("Balance" %in% colnames(credit), is_true())
17
})
18
print("Success!")
19
}
20
21
test_1.1 <- function() {
22
test_that('credit should be a data frame.', {
23
expect_true('data.frame' %in% class(credit))
24
})
25
test_that('credit does not contain the correct number of rows and/or columns.', {
26
expect_equal(dim(credit), c(400, 3))
27
})
28
test_that('The credit data frame should not contain the column X1', {
29
expect_that("X1" %in% colnames(credit), is_false())
30
})
31
test_that('The credit data frame is missing columns.', {
32
expect_that("Income" %in% colnames(credit), is_true())
33
expect_that("Balance" %in% colnames(credit), is_true())
34
expect_that("Rating" %in% colnames(credit), is_true())
35
})
36
print("Success!")
37
}
38
39
test_1.2 <- function() {
40
test_that('Did not create an object named X_train', {
41
expect_true(exists("X_train"))
42
})
43
test_that('X_train should be a data frame.', {
44
expect_true('data.frame' %in% class(X_train))
45
})
46
test_that('X_train does not contain the correct number of rows and/or columns.', {
47
expect_equal(dim(X_train), c(241, 2))
48
})
49
test_that('X_train should not contain the column Balance.', {
50
expect_false("Balance" %in% colnames(X_train))
51
})
52
test_that('Did not create an object named X_test', {
53
expect_true(exists("X_test"))
54
})
55
test_that('X_test should be a data frame.', {
56
expect_true('data.frame' %in% class(X_test))
57
})
58
test_that('X_test does not contain the correct number of rows and/or columns.', {
59
expect_equal(dim(X_test), c(159, 2))
60
})
61
test_that('X_test should not contain the column Balance', {
62
expect_false("Balance" %in% colnames(X_test))
63
})
64
test_that('Did not create an object named Y_train', {
65
expect_true(exists("Y_train"))
66
})
67
test_that('Y_train should be a numeric list', {
68
expect_true('numeric' %in% class(Y_train))
69
})
70
test_that('Y_train is not the correct length.', {
71
expect_equal(length(Y_train), 241)
72
})
73
test_that('Did not create an object named Y_test', {
74
expect_true(exists("Y_test"))
75
})
76
test_that('Y_test should be a numeric list', {
77
expect_true('numeric' %in% class(Y_test))
78
})
79
test_that('Y_test is not the correct length.', {
80
expect_equal(length(Y_test), 159)
81
})
82
print("Success!")
83
}
84
85
test_1.3 <- function() {
86
test_that('credit_eda should be a pairwise plot matrix.', {
87
expect_true('ggmatrix' %in% c(class(credit_eda)))
88
})
89
test_that('credit_eda should be using data from the credit data frame.', {
90
expect_equal(nrow(credit_eda$data), 241)
91
})
92
test_that('credit_eda should be using the Balance, Income, Rating, Limit columns.', {
93
expect_equal(ncol(credit_eda$data), 3)
94
})
95
print("Success!")
96
}
97
98
test_1.5 <- function() {
99
test_that('Did not create an object named lm_model', {
100
expect_true(exists("lm_model"))
101
})
102
test_that('x in lm_model should be X_train', {
103
expect_equal(as.character(lm_model$call$x), 'X_train')
104
expect_equal(dim(lm_model$trainingData), c(241, 3))
105
})
106
test_that('y in lm_model should be Y_train', {
107
expect_equal(as.character(lm_model$call$y), 'Y_train')
108
expect_equal(colnames(lm_model$trainingData), c('Income', 'Rating', '.outcome'))
109
})
110
test_that('method should be lm', {
111
expect_equal(as.character(lm_model$method), 'lm')
112
})
113
test_that('lm_model is incorrect.', {
114
expect_true(lm_model$results$intercept)
115
})
116
print("Success!")
117
}
118
119
test_1.6 <- function() {
120
test_that('Did not create an object named lm_coeffs', {
121
expect_true(exists("lm_coeffs"))
122
})
123
test_that('lm_coeffs should be a data frame.', {
124
expect_equal(class(lm_coeffs), 'data.frame')
125
})
126
test_that('lm_coeffs should have 1 row of 3 different coefficients.', {
127
expect_equal(dim(lm_coeffs), c(1, 3))
128
})
129
test_that('column names should include Income', {
130
expect_true("Income" %in% colnames(lm_coeffs))
131
})
132
test_that('column names should include Income', {
133
expect_true("Rating" %in% colnames(lm_coeffs))
134
})
135
print("Success!")
136
}
137
138
test_1.8 <- function() {
139
test_that('Solution is incorrect', {
140
expect_equal(digest(round(lm_rmse)), '506438c6bb7e021d303ffa42da8c4f3a')
141
})
142
print("Success!")
143
}
144
145
test_1.9 <- function() {
146
test_that('Solution is incorrect', {
147
expect_equal(digest(round(lm_rmspe)), 'c2c9156cd03604826326da8eab46a3e1')
148
})
149
print("Success!")
150
}
151
152
test_2.1 <- function() {
153
test_that('X_train should be a data frame.', {
154
expect_true('data.frame' %in% class(X_train))
155
})
156
test_that('X_train does not contain the correct number of rows and/or columns.', {
157
expect_equal(dim(X_train), c(2052, 5))
158
})
159
test_that('X_train should not contain the column sale_price.', {
160
expect_false('sale_price' %in% colnames(X_train))
161
})
162
test_that('Did not create an object named X_test', {
163
expect_true(exists("X_test"))
164
})
165
test_that('X_test should be a data frame.', {
166
expect_true('data.frame' %in% class(X_test))
167
})
168
test_that('X_test does not contain the correct number of rows and/or columns.', {
169
expect_equal(dim(X_test), c(877, 5))
170
})
171
test_that('X_test should not contain the column sale_price', {
172
expect_false('sale_price' %in% colnames(X_test))
173
})
174
test_that('Did not create an object named Y_train', {
175
expect_true(exists("Y_train"))
176
})
177
test_that('Y_train should be a numeric list', {
178
expect_true('numeric' %in% class(Y_train))
179
})
180
test_that('Y_train is not the correct length.', {
181
expect_equal(length(Y_train), 2052)
182
})
183
test_that('Did not create an object named Y_test', {
184
expect_true(exists("Y_test"))
185
})
186
test_that('Y_test should be a numeric list', {
187
expect_true('numeric' %in% class(Y_test))
188
})
189
test_that('Y_test is not the correct length.', {
190
expect_equal(length(Y_test), 877)
191
})
192
print("Success!")
193
}
194
195