Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2022-spring/materials/tutorial_reading/tests.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
34
test_1.1 <- function() {
35
test_that("Solution is incorrect", {
36
expect_equal(digest(answer1.1), "475bf9280aab63a82af60791302736f6")
37
})
38
print("Success!")
39
}
40
41
test_1.2 <- function() {
42
test_that("Solution is incorrect", {
43
expect_equal(digest(answer1.2), "c1f86f7430df7ddb256980ea6a3b57a4")
44
})
45
print("Success!")
46
}
47
48
test_1.3.0 <- function() {
49
test_that("Did not create an object named happy_df_csv", {
50
expect_true(exists("happy_df_csv"))
51
})
52
test_that("happy_df_csv should be a data frame.", {
53
expect_true("data.frame" %in% class(happy_df_csv))
54
})
55
test_that("happy_df_csv does not contain the correct data.", {
56
expect_equal(dim(happy_df_csv), c(1562, 19))
57
expect_equal(digest(int_round(sum(happy_df_csv$year), 2)), 'b3c7645d3b175832c58747e47a9c7bff')
58
})
59
print("Success!")
60
}
61
62
test_1.3.1 <- function() {
63
test_that("Did not create an object named happy_df", {
64
expect_true(exists("happy_df"))
65
})
66
test_that("happy_df should be a data frame.", {
67
expect_true("data.frame" %in% class(happy_df))
68
})
69
test_that("happy_df does not contain the correct data.", {
70
expect_equal(dim(happy_df), c(1562, 19))
71
expect_equal(digest(int_round(sum(happy_df$year), 2)), 'b3c7645d3b175832c58747e47a9c7bff')
72
})
73
print("Success!")
74
}
75
76
test_1.3.2 <- function() {
77
test_that("Did not create an object named happy_step1", {
78
expect_true(exists("happy_step1"))
79
})
80
test_that("Did not create an object named happy_step2", {
81
expect_true(exists("happy_step2"))
82
})
83
test_that("Did not create an object named reduced_happy_df", {
84
expect_true(exists("reduced_happy_df"))
85
})
86
test_that("reduced_happy_df does not contain the correct number of rows and/or columns.", {
87
expect_setequal(dim(reduced_happy_df), c(141, 2))
88
})
89
test_that("The year column in reduced_happy_df should only contain 2017", {
90
expect_equal(int_round(unique(happy_step1$year), 0), 2017)
91
})
92
test_that("Columns in reduced_happy_df contain incorrect values.", {
93
expect_equal(digest(int_round(sum(reduced_happy_df$Positive.affect.scaled, na.rm = TRUE), 2)), '0a667e2dfc8faca592dde223c1ec3dec')
94
expect_equal(digest(int_round(sum(reduced_happy_df$Healthy.life.expectancy.at.birth, na.rm = TRUE), 2)), '90a8884408be62b74a0558ab7fb40db0')
95
})
96
print("Success!")
97
}
98
99
test_1.4 <- function() {
100
test_that("Did not create a plot named happy_plot", {
101
expect_true(exists("happy_plot"))
102
})
103
properties <- c(happy_plot$layers[[1]]$mapping, happy_plot$mapping)
104
105
test_that("Healthy.life.expectancy.at.birth should be on the x-axis.", {
106
expect_true("Healthy.life.expectancy.at.birth" == rlang::get_expr(properties$x))
107
})
108
test_that("Positive.affect.scaled should be on the y-axis.", {
109
expect_true("Positive.affect.scaled" == rlang::get_expr(properties$y))
110
})
111
test_that("happy_plot should be a scatter plot.", {
112
expect_true("GeomPoint" %in% c(class(happy_plot$layers[[1]]$geom)))
113
})
114
test_that("Labels on the axes should be descriptive and human readable.", {
115
expect_false(happy_plot$labels$y == "Positive.affect.scaled")
116
expect_false(happy_plot$labels$x == "Healthy.life.expectancy.at.birth")
117
})
118
print("Success!")
119
}
120
121
test_2.1 <- function() {
122
test_that("Solution is incorrect", {
123
expect_equal(digest(as.character(answer2.1)), "475bf9280aab63a82af60791302736f6")
124
})
125
print("Success!")
126
}
127
128
test_2.2.0 <- function() {
129
test_that("Did not create an object named whistler_2018", {
130
expect_true(exists("whistler_2018"))
131
})
132
test_that("whistler_2018 should be a data frame.", {
133
expect_true("data.frame" %in% class(whistler_2018))
134
})
135
test_that("whistler_2018 does not contain the correct number of rows and/or columns.", {
136
expect_equal(dim(whistler_2018), c(365, 27))
137
})
138
test_that("whistler_2018 does not contain the correct data.", {
139
expect_equal(digest(int_round(sum(whistler_2018$`Total Snow (cm)`, na.rm = TRUE), 2)), '2f5608a20784d5f19ac88237c04a1e10')
140
})
141
print("Success!")
142
}
143
144
test_2.2.1 <- function() {
145
test_that("whistler_2018 does not contain the correct number of rows and/or columns.", {
146
expect_equal(dim(whistler_2018), c(365, 27))
147
})
148
test_that("whistler_2018 still contains white space in its column names.", {
149
expect_equal(int_round(length(grep(pattern = " ", x = colnames(whistler_2018))), 0), 0)
150
})
151
print("Success!")
152
}
153
154
test_2.3 <- function() {
155
test_that("Did not create a plot named whistler_2018_plot", {
156
expect_true(exists("whistler_2018_plot"))
157
})
158
properties <- c(whistler_2018_plot$layers[[1]]$mapping, whistler_2018_plot$mapping)
159
160
test_that("Date.Time should be on the x-axis.", {
161
expect_true("Date.Time" == rlang::get_expr(properties$x))
162
})
163
test_that("Total.Snow..cm. should be on the y-axis.", {
164
expect_true("Total.Snow..cm." == rlang::get_expr(properties$y))
165
})
166
test_that("whistler_2018_plot should be a line plot.", {
167
expect_true("GeomLine" %in% c(class(whistler_2018_plot$layers[[1]]$geom)))
168
})
169
test_that("Labels on the axes should be descriptive and human readable.", {
170
expect_false(whistler_2018_plot$labels$y == "Total.Snow..cm.")
171
expect_false(whistler_2018_plot$labels$x == "Date.Time")
172
})
173
print("Success!")
174
}
175
176
test_3.0 <- function() {
177
test_that("Did not create a database connected called connection", {
178
expect_true(exists("connection"))
179
expect_equal(digest(class(connection)), "df66af2493a2405d1c7763f4bfbfebc0")
180
})
181
test_that("Did not create a table called project_data", {
182
expect_true(exists("project_data"))
183
expect_equal(digest(class(project_data))[[1]], "98f4e628b20def80372d0acca8b5edb0")
184
})
185
test_that("The project_data table has the wrong information in it", {
186
expect_equal(digest(colnames(project_data)[[13]]), "fd0b87dfaa67d4c90844080d5d9355ec")
187
expect_equal(digest(colnames(project_data)[[14]]), "2f2ec36ffb7792b1f8e274387e520edd")
188
expect_equal(digest(int_round(length(colnames(project_data)), 0)), "4d4c1ad2286f1a7670a024467dd10808")
189
expect_equal(digest(round(as_tibble(summarize(project_data, sum = sum(goal, na.rm = TRUE)))[[1]], 0)), '46e3118b0210dec665d9c8dacc68aacb') # cant apply int_round
190
})
191
print("Success!")
192
}
193
194
test_3.1 <- function() {
195
test_that("Incorrect answer", {
196
expect_equal(digest(answer3.1), "3a5505c06543876fe45598b5e5e5195d")
197
})
198
print("Success!")
199
}
200
201
202
test_3.2 <- function() {
203
test_that("Did not create a plot named funding_over_time_plot", {
204
expect_true(exists("funding_over_time_plot"))
205
})
206
test_that("The axis labels are incorrect", {
207
expect_equal(digest(funding_over_time_plot$labels$x), "bd1055a7d6af03e74a4462edbc5b35c4")
208
expect_equal(digest(funding_over_time_plot$labels$y), "48c36ecb38a110dffe3f40add37ccdc1")
209
})
210
test_that("The x/y data are incorrect", {
211
expect_equal(digest(funding_over_time_plot$mapping$x), "c5a397ebe58129a88408811144869370")
212
expect_equal(digest(funding_over_time_plot$mapping$y), "5d75d853465e03f6828b25a1808b073b")
213
})
214
test_that("arrival_delay_plot should be a scatter plot", {
215
expect_equal(digest(class(funding_over_time_plot$layers[[1]]$geom)[[1]]), "911e5b9debfb523f25ad2ccc01a4b2dd")
216
})
217
print("Success!")
218
}
219
220
221
222
test_3.4 <- function() {
223
test_that("Did not create project_data.csv in the data/ folder.", {
224
expect_true(file.exists("data/project_data.csv"))
225
})
226
test_that("the project_data.csv file does not contain the right data", { # read in data file as a string, check student's data = that string
227
expect_equal(digest(file.info("data/project_data.csv")$size), "5cb5f9f655e59768877bd995511acb2c")
228
})
229
test_that("the project_df object does not exist", {
230
expect_true(exists("project_df"))
231
})
232
test_that("the project_df object does not have the right number of rows & columns", {
233
expect_equal(digest(int_round(nrow(project_df), 0)), 'bef07e5e73d03523a0385aa9c06dadb4')
234
expect_equal(digest(int_round(ncol(project_df), 0)), '234a2a5581872457b9fe1187d1616b13')
235
})
236
test_that("the project_df object does not have the right data", {
237
expect_equal(digest(int_round(sum(project_df$goal), 0)), 'e5730c99d7f2abf710e5ebbdb4738bff')
238
})
239
print("Success!")
240
}
241
242