Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2020-spring/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
expect_true('Income' %in% colnames(lm_model$trainingData) && 'Rating' %in% colnames(lm_model$trainingData))
106
})
107
test_that('y in lm_model should be Y_train', {
108
expect_equal(as.character(lm_model$call$y), 'Y_train')
109
})
110
test_that('method should be lm', {
111
expect_equal(as.character(lm_model$method), 'lm')
112
})
113
test_that('lm_model should have an intercept', {
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 Intercept', {
130
expect_true("Intercept" %in% colnames(lm_coeffs))
131
})
132
test_that('column names should include Income', {
133
expect_true("Income" %in% colnames(lm_coeffs))
134
})
135
test_that('column names should include Rating', {
136
expect_true("Rating" %in% colnames(lm_coeffs))
137
})
138
print("Success!")
139
}
140
141
test_1.8 <- function() {
142
test_that('Solution is incorrect', {
143
expect_equal(digest(round(lm_rmse)), '506438c6bb7e021d303ffa42da8c4f3a')
144
})
145
print("Success!")
146
}
147
148
test_1.9 <- function() {
149
test_that('Solution is incorrect', {
150
expect_equal(digest(round(lm_rmspe)), 'c2c9156cd03604826326da8eab46a3e1')
151
})
152
print("Success!")
153
}
154