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_09/tests_tutorial_09.R
2051 views
1
# +
2
library(testthat)
3
library(digest)
4
library(rlang)
5
6
int_round <- function(x, digits){
7
x = x*10^digits
8
xint = as.integer(x)
9
xint1 = xint + 1L
10
if (abs(xint - x) < abs(xint1 - x)){
11
return(xint)
12
}
13
else {
14
return(xint1)
15
}
16
}
17
# -
18
19
test_1.0 <- function() {
20
test_that('Did not create an object named credit', {
21
expect_true(exists("credit"))
22
})
23
test_that('credit should be a tibble.', {
24
expect_true('tbl' %in% class(credit))
25
})
26
test_that('credit does not contain the correct number of rows and/or columns.', {
27
expect_equal(dim(credit), c(400, 12))
28
})
29
test_that('The credit tibble is missing columns.', {
30
expect_true("Income" %in% colnames(credit))
31
expect_true("Balance" %in% colnames(credit))
32
})
33
test_that('credit does not contain the correct data.', {
34
expect_equal(digest(int_round(sum(credit$Income), 2)), '7b41cc2ef140f2cfb4b6eb86ccebf416')
35
expect_equal(digest(int_round(sum(credit$Limit), 2)), '1bc8a53a9b0cc2ea3cf99f2306872029')
36
})
37
print("Success!")
38
}
39
40
test_1.1 <- function() {
41
test_that('credit should be a tibble.', {
42
expect_true('tbl' %in% class(credit))
43
})
44
test_that('credit does not contain the correct number of rows and/or columns.', {
45
expect_equal(dim(credit), c(400, 3))
46
})
47
test_that('The credit data frame should not contain the column X1', {
48
expect_false("X1" %in% colnames(credit))
49
})
50
test_that('The credit data frame is missing columns.', {
51
expect_true("Income" %in% colnames(credit))
52
expect_true("Balance" %in% colnames(credit))
53
expect_true("Rating" %in% colnames(credit))
54
})
55
print("Success!")
56
}
57
58
test_1.2 <- function() {
59
test_that('Did not create an object called credit_split.', {
60
expect_true(exists('credit_split'))
61
})
62
test_that('credit_split is not an r_split object.', {
63
expect_true('rsplit' %in% class(credit_split))
64
})
65
test_that('Did not create an object called credit_training.', {
66
expect_true(exists('credit_training'))
67
})
68
test_that('credit_training is not a tibble.',{
69
expect_true('tbl' %in% class(credit_training))
70
})
71
test_that('credit_training does not contain 60% of the data.', {
72
expect_equal(dim(credit_training), c(241, 3))
73
expect_equal(digest(int_round(sum(credit_training$Balance), 2)), '273157f18270727a00bfb2d4bff79903')
74
expect_equal(digest(int_round(sum(credit_training$Income), 2)), 'b9f9e7fe1b89fc1fc7f54425e5688322')
75
expect_equal(digest(int_round(sum(credit_training$Rating), 2)), 'd34e38964a1d4ddb00ddd7f6118c5fc6')
76
})
77
test_that('Did not create an object called credit_testing.', {
78
expect_true(exists('credit_testing'))
79
})
80
test_that('credit_testing is not a tibble.', {
81
expect_true('tbl' %in% class(credit_training))
82
})
83
test_that('credit_testing does not contain the remaining 40% of the data.', {
84
expect_equal(dim(credit_testing), c(159, 3))
85
expect_equal(digest(int_round(sum(credit_testing$Balance), 2)), 'b0fe1ffce8158f92072c0c27ad1f181f')
86
expect_equal(digest(int_round(sum(credit_testing$Income), 2)), '581f089d90262775f263a2eb16c22480')
87
expect_equal(digest(int_round(sum(credit_testing$Rating), 2)), '26a325d7669edb2146d4a17935fe2636')
88
})
89
print("Success!")
90
}
91
92
test_1.3 <- function() {
93
test_that('credit_eda should be a pairwise plot matrix.', {
94
expect_true('ggmatrix' %in% c(class(credit_eda)))
95
})
96
test_that('credit_eda should be using data from the credit data frame.', {
97
expect_equal(int_round(nrow(credit_eda$data), 0), 241)
98
})
99
test_that('credit_eda should be using the Balance, Income, Rating, Limit columns.', {
100
expect_equal(int_round(ncol(credit_eda$data), 0), 3)
101
})
102
print("Success!")
103
}
104
105
test_1.4 <- function() {
106
test_that('Did not create an object called answer1.4.', {
107
expect_true(exists('answer1.4'))
108
})
109
test_that('Solution is incorrect.',{
110
expect_equal(digest(answer1.4), '475bf9280aab63a82af60791302736f6')
111
})
112
print("Success!")
113
}
114
115
test_1.5 <- function() {
116
test_that('Did not create an object called lm_spec.', {
117
expect_true(exists('lm_spec'))
118
})
119
test_that('lm_spec should be a linear regression model specification.', {
120
expect_true('linear_reg' %in% class(lm_spec))
121
})
122
test_that('Did not create an object called credit_recipe.', {
123
expect_true(exists('credit_recipe'))
124
})
125
test_that('credit_recipe should be a recipe.', {
126
expect_true('recipe' %in% class(credit_recipe))
127
})
128
test_that('credit_recipe does not contain the training data.', {
129
expect_equal(dim(credit_recipe$template), c(241, 3))
130
expect_equal(digest(int_round(sum(credit_recipe$template$Income), 2)), 'b9f9e7fe1b89fc1fc7f54425e5688322')
131
expect_equal(digest(int_round(sum(credit_recipe$template$Rating), 2)), 'd34e38964a1d4ddb00ddd7f6118c5fc6')
132
expect_equal(digest(int_round(sum(credit_recipe$template$Balance), 2)), '273157f18270727a00bfb2d4bff79903')
133
})
134
print("Success!")
135
}
136
137
test_1.6 <- function() {
138
test_that('Did not create an object called credit_fit.', {
139
expect_true(exists('credit_fit'))
140
})
141
test_that('credit_fit should be a workflow.', {
142
expect_true('workflow' %in% class(credit_fit))
143
})
144
test_that('credit_fit does not contain the training data.', {
145
expect_equal(digest(int_round(sum(credit_fit$pre$mold$predictors$Income), 2)), 'b9f9e7fe1b89fc1fc7f54425e5688322')
146
expect_equal(digest(int_round(sum(credit_fit$pre$mold$predictors$Rating), 2)), 'd34e38964a1d4ddb00ddd7f6118c5fc6')
147
})
148
print("Success!")
149
}
150
151
test_1.7 <- function(){
152
test_that('Did not create an object called answer1.7.')
153
expect_true(exists('answer1.7'))
154
test_that('Solution is incorrect.', {
155
expect_equal(digest(answer1.7), '75f1160e72554f4270c809f041c7a776')
156
})
157
}
158
159
test_1.8 <- function() {
160
test_that('Solution is incorrect.', {
161
expect_equal(digest(int_round(lm_rmse, 2)), 'fc3357631f940d71d5027270ad54a2cf')
162
})
163
print("Success!")
164
}
165
166
test_1.9 <- function() {
167
test_that('Solution is incorrect.', {
168
expect_equal(digest(int_round(lm_rmspe, 2)), 'ec726f8469b8d46ed8231dcbcca420e2')
169
})
170
print("Success!")
171
}
172
173
test_1.9.2 <- function(){
174
test_that('Did not create an object called answer1.9.2', {
175
expect_true(exists('answer1.9.2'))
176
})
177
test_that('Solution is incorrect', {
178
expect_equal(digest(answer1.9.2), '475bf9280aab63a82af60791302736f6')
179
})
180
print("Success!")
181
}
182
183