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