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_06/tests_tutorial_06.R
2051 views
1
library(testthat)
2
library(digest)
3
4
test_1.0 <- function(){
5
test_that('Did not create an object named fruit_data', {
6
expect_true(exists("fruit_data"))
7
})
8
test_that('fruit_data should be a data frame.', {
9
expect_true('data.frame' %in% class(fruit_data))
10
})
11
test_that('fruit_data does not contain the correct number of rows and/or columns.', {
12
expect_equal(dim(fruit_data), c(59, 7))
13
})
14
test_that('fruit_data does not contain the correct data.', {
15
expect_equal(digest(as.numeric(sum(fruit_data$scaled_width))), '3043b93a18750881f7178956ba203cfd')
16
expect_equal(colnames(fruit_data), c("fruit_label", "fruit_name", "fruit_subtype", "scaled_mass", "scaled_width", "scaled_height", "scaled_color"))
17
})
18
print("Success!")
19
}
20
21
test_1.1 <- function(){
22
test_that('Solution is incorrect', {
23
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
24
})
25
print("Success!")
26
}
27
28
test_1.2 <- function(){
29
test_that('Did not create an object named fruit_data', {
30
expect_true(exists("fruit_data"))
31
})
32
test_that('fruit_data does not contain the correct number of rows and/or columns.', {
33
expect_equal(dim(fruit_data), c(59, 7))
34
})
35
test_that('The fruit_name column in fruit_data should be of class factor.', {
36
expect_true(is.factor(fruit_data$fruit_name))
37
})
38
test_that('Columns in fruit_data contain incorrect values.', {
39
expect_equal(digest(as.numeric(sum(fruit_data$scaled_mass, na.rm = TRUE))), '9c46762a9ec19d9658dc07063ead8f30') # 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_1.3 <- function(){
45
properties <- c(fruit_plot$layers[[1]]$mapping, fruit_plot$mapping)
46
labels <- fruit_plot$labels
47
test_that('Did not create a plot named fruit_plot', {
48
expect_true(exists("fruit_plot"))
49
})
50
test_that('scaled_mass should be on the x-axis.', {
51
expect_that("scaled_mass" == rlang::get_expr(properties$x), is_true())
52
})
53
test_that('scaled_color should be on the y-axis.', {
54
expect_that("scaled_color" == rlang::get_expr(properties$y), is_true())
55
})
56
test_that('fruit_name should be mapped to colour', {
57
expect_that("fruit_name" == rlang::get_expr(properties$colour), is_true())
58
})
59
test_that('fruit_plot should be a scatter plot.', {
60
expect_that("GeomPoint" %in% c(class(fruit_plot$layers[[1]]$geom)) , is_true())
61
})
62
test_that('Labels on the axes should be descriptive and human readable.', {
63
expect_that((labels$y) == 'scaled_color', is_false())
64
expect_that((labels$x) == 'scaled_mass', is_false())
65
expect_that((labels$colour) == 'fruit_name', is_false())
66
})
67
print("Success!")
68
}
69
70
test_1.4 <- function(){
71
properties <- c(fruit_plot_new$layers[[1]]$mapping, fruit_plot_new$mapping)
72
labels <- fruit_plot_new$labels
73
test_that('Did not create a plot named fruit_plot', {
74
expect_true(exists("fruit_plot_new"))
75
})
76
test_that('scaled_mass should be on the x-axis.', {
77
expect_that("scaled_mass" == rlang::get_expr(properties$x), is_true())
78
})
79
test_that('scaled_color should be on the y-axis.', {
80
expect_that("scaled_color" == rlang::get_expr(properties$y), is_true())
81
})
82
test_that('fruit_name should be mapped to colour', {
83
expect_that("fruit_name" == rlang::get_expr(properties$colour), is_true())
84
})
85
test_that('fruit_plot_new should be a scatter plot.', {
86
expect_that("GeomPoint" %in% c(class(fruit_plot_new$layers[[1]]$geom)) , is_true())
87
})
88
test_that ('There should be a new data point that has a mass and color score of 0.5', {
89
expect_true('GeomPoint' %in% class(rlang::get_expr(fruit_plot_new$layers[[2]]$geom)))
90
})
91
test_that('Labels on the axes should be descriptive and human readable.', {
92
expect_that((labels$y) == 'scaled_color', is_false())
93
expect_that((labels$x) == 'scaled_mass', is_false())
94
expect_that((labels$colour) == 'fruit_name', is_false())
95
})
96
print("Success!")
97
}
98
99
100
test_1.6 <- function(){
101
test_that('Did not create an object named X_train', {
102
expect_true(exists("X_train"))
103
})
104
test_that('X_train should be a data frame.', {
105
expect_true('data.frame' %in% class(X_train))
106
})
107
test_that('X_train does not contain the correct number of rows and/or columns.', {
108
expect_equal(dim(X_train), c(59, 2))
109
})
110
test_that('X_train does not contain the column(s) scaled_color and/or scaled_mass', {
111
expect_true('scaled_color' %in% colnames(X_train))
112
expect_true('scaled_mass' %in% colnames(X_train))
113
})
114
test_that('Did not create an object named Y_train', {
115
expect_true(exists("Y_train"))
116
})
117
#test_that('Y_train should be a character.', {
118
# expect_true('character' %in% class(Y_train))
119
# })
120
test_that('Y_train is not the correct length.', {
121
expect_equal(length(Y_train), 59)
122
})
123
print("Success!")
124
}
125
126
test_1.7 <- function(){
127
test_that('method should be knn', {
128
expect_equal(as.character(fruit_class$method), 'knn')
129
})
130
test_that('k should be 5', {
131
expect_equal(as.numeric(fruit_class$results$k), 5)
132
})
133
test_that('model_knn contains incorrect information.', {
134
expect_equal(digest(as.numeric(sum(fruit_class$trainingData$scaled_mass))), '9c46762a9ec19d9658dc07063ead8f30')
135
expect_equal(digest(as.numeric(sum(fruit_class$trainingData$scaled_color))), '2a93b1da4bcda1113ef03e891938eac4')
136
expect_equal(as.numeric(summary(fruit_class$trainingData$.outcome)[1]), 19)
137
})
138
print("Success!")
139
}
140
141
test_1.8 <- function(){
142
test_that('Prediction is incorrect', {
143
expect_equal(digest(as.character(fruit_predicted)), '17f79d7a98f732174cc5a86dc56380d6')
144
})
145
print("Success!")
146
}
147