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_10/tests_tutorial_10.R
2051 views
1
# +
2
library(testthat)
3
library(digest)
4
5
int_round <- function(x, digits){
6
x = x*10^digits
7
xint = as.integer(x)
8
xint1 = xint + 1L
9
if (abs(xint - x) < abs(xint1 - x)){
10
return(xint)
11
}
12
else {
13
return(xint1)
14
}
15
}
16
# -
17
18
test_1.0 <- function(){
19
test_that('Did not create an object named pm_data', {
20
expect_true(exists("pm_data"))
21
})
22
test_that('pm_data should be a tibble.', {
23
expect_true('tbl' %in% class(pm_data))
24
})
25
test_that('pm_data does not contain the correct number of rows and/or columns.', {
26
expect_equal(dim(pm_data), c(800, 13))
27
})
28
test_that('pm_data is missing columns.', {
29
expect_true('Name' %in% colnames(pm_data))
30
expect_true('HP' %in% colnames(pm_data))
31
expect_true('Attack' %in% colnames(pm_data))
32
expect_true('Defense' %in% colnames(pm_data))
33
expect_true('#' %in% colnames(pm_data))
34
expect_true('Type 1' %in% colnames(pm_data))
35
expect_true('Type 2' %in% colnames(pm_data))
36
expect_true('Total' %in% colnames(pm_data))
37
expect_true('Sp. Atk' %in% colnames(pm_data))
38
expect_true('Sp. Def' %in% colnames(pm_data))
39
expect_true('Speed' %in% colnames(pm_data))
40
expect_true('Generation' %in% colnames(pm_data))
41
expect_true('Legendary' %in% colnames(pm_data))
42
})
43
print("Success!")
44
}
45
46
test_1.1 <- function(){
47
test_that('Did not create a plot named pm_pairs', {
48
expect_true(exists("pm_pairs"))
49
})
50
test_that('pm_pairs should be using data from pm_data', {
51
expect_equal(int_round(nrow(pm_pairs$data), 0), 800)
52
expect_equal(int_round(ncol(pm_pairs$data), 0), 7)
53
})
54
test_that('pm_pairs should be a pairwise plot matrix.', {
55
expect_true('ggmatrix' %in% c(class(pm_pairs)))
56
})
57
test_that('pm_pairs should plot columns 5 to 11', {
58
expect_equal(pm_pairs$yAxisLabels %in% c("Total", "HP", "Attack", "Defense", "Sp. Atk", "Sp. Def", "Speed"), c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE))
59
})
60
print("Success!")
61
}
62
63
test_1.2 <- function(){
64
test_that('km_data should contain the columns Speed and Defense', {
65
expect_true('Speed' %in% colnames(km_data))
66
expect_true('Defense' %in% colnames(km_data))
67
})
68
test_that('km_data should contain 800 rows and 2 columns.', {
69
expect_equal(int_round(ncol(km_data), 0), 2)
70
expect_equal(int_round(nrow(km_data), 0), 800)
71
})
72
print("Success!")
73
}
74
75
test_1.3 <- function(){
76
properties <- c(pm_scatter$layers[[1]]$mapping, pm_scatter$mapping)
77
labels <- pm_scatter$labels
78
test_that('Did not create a plot named pm_scatter', {
79
expect_true(exists("pm_scatter"))
80
})
81
test_that('Speed should be on the x-axis.', {
82
expect_true("Speed" == rlang::get_expr(properties$x))
83
})
84
test_that('Defense should be on the y-axis.', {
85
expect_true("Defense" == rlang::get_expr(properties$y))
86
})
87
test_that('pm_scatter should be a scatter plot.', {
88
expect_true("GeomPoint" %in% c(class(pm_scatter$layers[[1]]$geom)))
89
})
90
test_that('Labels on the axes and legend need to be changed to be descriptive, nicely formatted, and human readable.', {
91
expect_false((labels$y) == 'Defense')
92
expect_false((labels$x) == 'Speed')
93
})
94
print("Success!")
95
}
96
97
test_1.4.2 <- function(){
98
test_that('The pokemon_clusters model should have 4 centers.', {
99
expect_equal(int_round(nrow(pokemon_clusters$centers), 0), 4)
100
})
101
test_that('The pokemon_clusters model should be using Speed and Defense to create the clusters.', {
102
expect_equal(int_round(ncol(pokemon_clusters$centers), 0), 2)
103
expect_true('Speed' %in% colnames(pokemon_clusters$centers))
104
expect_true('Defense' %in% colnames(pokemon_clusters$centers))
105
})
106
test_that('The pokemon_clusters model should be of class kmeans', {
107
expect_equal(class(pokemon_clusters), 'kmeans')
108
})
109
print("Success!")
110
}
111
112
test_1.5 <- function(){
113
properties <- c(answer1.5$layers[[1]]$mapping, answer1.5$mapping)
114
labels <- answer1.5$labels
115
test_that('Did not create a plot named answer1.5', {
116
expect_true(exists("answer1.5"))
117
})
118
test_that('Speed should be on the x-axis.', {
119
expect_true("Speed" == rlang::get_expr(properties$x))
120
})
121
test_that('Defense should be on the y-axis.', {
122
expect_true("Defense" == rlang::get_expr(properties$y))
123
})
124
test_that('answer1.5 should be a scatter plot.', {
125
expect_true("GeomPoint" %in% c(class(answer1.5$layers[[1]]$geom)))
126
})
127
test_that('Labels on the axes and legend need to be changed to be descriptive, nicely formatted, and human readable.', {
128
expect_false((labels$y) == 'Defense')
129
expect_false((labels$x) == 'Speed')
130
expect_false((labels$colour) == '.cluster')
131
})
132
print("Success!")
133
}
134
135
test_1.7 <- function(){
136
test_that('elbow_stats should contain k from 1 to 10', {
137
expect_equal(int_round(nrow(elbow_stats), 0), 10)
138
})
139
test_that('Solution is incorrect', {
140
expect_equal(digest(int_round(sum(elbow_stats$tot.withinss), 2)), '13f1e8e7d2dac6cdff06a32e20382f3b')
141
})
142
test_that('poke_clusts column should be removed', {
143
expect_false("poke_clusts" %in% colnames(elbow_stats))
144
})
145
print("Success!")
146
}
147
148
test_1.8 <- function(){
149
properties <- c(elbow_plot$layers[[1]]$mapping, elbow_plot$mapping)
150
properties2 <- c(elbow_plot$later[[2]]$mapping, elbow_plot$mapping)
151
test_that('Did not create a plot called elbow_plot', {
152
expect_true(exists('elbow_plot'))
153
})
154
test_that('elbow_plot should be a line plot', {
155
expect_true("GeomPoint" %in% c(class(elbow_plot$layers[[1]]$geom)))
156
expect_true("GeomLine" %in% c(class(elbow_plot$layers[[2]]$geom)))
157
})
158
test_that('k should be on the x-axis', {
159
expect_true(rlang::get_expr(properties$x) == 'k')
160
})
161
test_that('tot.withinss should be on the y-axis', {
162
expect_true(rlang::get_expr(properties$y) == 'tot.withinss')
163
})
164
test_that('Labels on the axes should be descriptive and human readable.', {
165
# expect_false((elbow_plot$labels$x) == 'k')
166
expect_false((elbow_plot$labels$y) == 'tot.withinss')
167
})
168
print('Success!')
169
}
170
171