Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2021-spring/materials/tutorial_10/tests_tutorial_10.R
2051 views
1
library(testthat)
2
library(digest)
3
4
# Round double to precise integer
5
#
6
# `int_round` works to create an integer corresponding to a number that is
7
# tested up to a particular decimal point of precision. This is useful when
8
# there is a need to compare a numeric value using hashes.
9
#
10
# @param x Double vector of length one.
11
# @param digits Double vector of length one to specify decimal point of precision. Negative numbers can be used to specifying significant digits > 0.1.
12
#
13
# @return Integer vector of length one corresponding to a particular decimal point of precision.
14
#
15
# @examples
16
# # to get an integer up to two decimals of precision from 234.56789
17
# int_round(234.56789, 2)
18
#
19
# to get an integer rounded to the hundred digit from 234.56789
20
# int_round(234.56789, -2)
21
int_round <- function(x, digits){
22
x = x*10^digits
23
xint = as.integer(x)
24
xint1 = xint + 1L
25
if (abs(xint - x) < abs(xint1 - x)){
26
return(xint)
27
}
28
else {
29
return(xint1)
30
}
31
}
32
33
test_1.0 <- function(){
34
test_that('Did not create an object named pm_data', {
35
expect_true(exists("pm_data"))
36
})
37
test_that('pm_data should be a tibble.', {
38
expect_true('tbl' %in% class(pm_data))
39
})
40
test_that('pm_data does not contain the correct number of rows and/or columns.', {
41
expect_equal(dim(pm_data), c(800, 13))
42
})
43
test_that('pm_data is missing columns.', {
44
expect_true('Name' %in% colnames(pm_data))
45
expect_true('HP' %in% colnames(pm_data))
46
expect_true('Attack' %in% colnames(pm_data))
47
expect_true('Defense' %in% colnames(pm_data))
48
expect_true('#' %in% colnames(pm_data))
49
expect_true('Type 1' %in% colnames(pm_data))
50
expect_true('Type 2' %in% colnames(pm_data))
51
expect_true('Total' %in% colnames(pm_data))
52
expect_true('Sp. Atk' %in% colnames(pm_data))
53
expect_true('Sp. Def' %in% colnames(pm_data))
54
expect_true('Speed' %in% colnames(pm_data))
55
expect_true('Generation' %in% colnames(pm_data))
56
expect_true('Legendary' %in% colnames(pm_data))
57
})
58
print("Success!")
59
}
60
61
test_1.1 <- function(){
62
test_that('Did not create a plot named pm_pairs', {
63
expect_true(exists("pm_pairs"))
64
})
65
test_that('pm_pairs should be using data from pm_data', {
66
expect_equal(int_round(nrow(pm_pairs$data), 0), 800)
67
expect_equal(int_round(ncol(pm_pairs$data), 0), 7)
68
})
69
test_that('pm_pairs should be a pairwise plot matrix.', {
70
expect_true('ggmatrix' %in% c(class(pm_pairs)))
71
})
72
test_that('pm_pairs should plot columns 5 to 11', {
73
expect_equal(pm_pairs$yAxisLabels %in% c("Total", "HP", "Attack", "Defense", "Sp. Atk", "Sp. Def", "Speed"), c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE))
74
})
75
print("Success!")
76
}
77
78
test_1.2 <- function(){
79
test_that('km_data should contain the columns Speed and Defense', {
80
expect_true('Speed' %in% colnames(km_data))
81
expect_true('Defense' %in% colnames(km_data))
82
})
83
test_that('km_data should contain 800 rows and 2 columns.', {
84
expect_equal(int_round(ncol(km_data), 0), 2)
85
expect_equal(int_round(nrow(km_data), 0), 800)
86
})
87
print("Success!")
88
}
89
90
test_1.3 <- function(){
91
properties <- c(pm_scatter$layers[[1]]$mapping, pm_scatter$mapping)
92
labels <- pm_scatter$labels
93
test_that('Did not create a plot named pm_scatter', {
94
expect_true(exists("pm_scatter"))
95
})
96
test_that('Speed should be on the x-axis.', {
97
expect_true("Speed" == rlang::get_expr(properties$x))
98
})
99
test_that('Defense should be on the y-axis.', {
100
expect_true("Defense" == rlang::get_expr(properties$y))
101
})
102
test_that('pm_scatter should be a scatter plot.', {
103
expect_true("GeomPoint" %in% c(class(pm_scatter$layers[[1]]$geom)))
104
})
105
test_that('Labels on the axes and legend need to be changed to be descriptive, nicely formatted, and human readable.', {
106
expect_false((labels$y) == 'Defense')
107
expect_false((labels$x) == 'Speed')
108
})
109
print("Success!")
110
}
111
112
test_1.4.2 <- function(){
113
test_that('The pokemon_clusters model should have 4 centers.', {
114
expect_equal(int_round(nrow(pokemon_clusters$centers), 0), 4)
115
})
116
test_that('The pokemon_clusters model should be using Speed and Defense to create the clusters.', {
117
expect_equal(int_round(ncol(pokemon_clusters$centers), 0), 2)
118
expect_true('Speed' %in% colnames(pokemon_clusters$centers))
119
expect_true('Defense' %in% colnames(pokemon_clusters$centers))
120
})
121
test_that('The pokemon_clusters model should be of class kmeans', {
122
expect_equal(class(pokemon_clusters), 'kmeans')
123
})
124
print("Success!")
125
}
126
127
test_1.5 <- function(){
128
properties <- c(answer1.5$layers[[1]]$mapping, answer1.5$mapping)
129
labels <- answer1.5$labels
130
test_that('Did not create a plot named answer1.5', {
131
expect_true(exists("answer1.5"))
132
})
133
test_that('Speed should be on the x-axis.', {
134
expect_true("Speed" == rlang::get_expr(properties$x))
135
})
136
test_that('Defense should be on the y-axis.', {
137
expect_true("Defense" == rlang::get_expr(properties$y))
138
})
139
test_that('answer1.5 should be a scatter plot.', {
140
expect_true("GeomPoint" %in% c(class(answer1.5$layers[[1]]$geom)))
141
})
142
test_that('Labels on the axes and legend need to be changed to be descriptive, nicely formatted, and human readable.', {
143
expect_false((labels$y) == 'Defense')
144
expect_false((labels$x) == 'Speed')
145
expect_false((labels$colour) == '.cluster')
146
})
147
print("Success!")
148
}
149
150
test_1.7 <- function(){
151
test_that('elbow_stats should contain k from 1 to 10', {
152
expect_equal(int_round(nrow(elbow_stats), 0), 10)
153
})
154
test_that('Solution is incorrect', {
155
expect_equal(digest(int_round(sum(elbow_stats$tot.withinss), 2)), 'ad3673e6aed949387c2f1f4d4b9141dc')
156
})
157
test_that('poke_clusts column should be removed', {
158
expect_false("poke_clusts" %in% colnames(elbow_stats))
159
})
160
print("Success!")
161
}
162
163
test_1.8 <- function(){
164
properties <- c(elbow_plot$layers[[1]]$mapping, elbow_plot$mapping)
165
properties2 <- c(elbow_plot$later[[2]]$mapping, elbow_plot$mapping)
166
test_that('Did not create a plot called elbow_plot', {
167
expect_true(exists('elbow_plot'))
168
})
169
test_that('elbow_plot should be a line plot', {
170
expect_true("GeomPoint" %in% c(class(elbow_plot$layers[[1]]$geom)))
171
expect_true("GeomLine" %in% c(class(elbow_plot$layers[[2]]$geom)))
172
})
173
test_that('k should be on the x-axis', {
174
expect_true(rlang::get_expr(properties$x) == 'k')
175
})
176
test_that('tot.withinss should be on the y-axis', {
177
expect_true(rlang::get_expr(properties$y) == 'tot.withinss')
178
})
179
test_that('Labels on the axes should be descriptive and human readable.', {
180
# expect_false((elbow_plot$labels$x) == 'k')
181
expect_false((elbow_plot$labels$y) == 'tot.withinss')
182
})
183
print('Success!')
184
}
185
186