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_02/tests_tutorial_02.R
2051 views
1
library(testthat)
2
library(digest)
3
4
test_1.1 <- function() {
5
test_that("Solution is incorrect", {
6
expect_equal(digest(answer1.1), "475bf9280aab63a82af60791302736f6")
7
})
8
print("Success!")
9
}
10
11
test_1.2 <- function() {
12
test_that("Solution is incorrect", {
13
expect_equal(digest(answer1.2), "c1f86f7430df7ddb256980ea6a3b57a4")
14
})
15
print("Success!")
16
}
17
18
test_1.3.0 <- function() {
19
test_that("Did not create an object named happy_df_csv", {
20
expect_true(exists("happy_df_csv"))
21
})
22
test_that("happy_df_csv should be a data frame.", {
23
expect_true("data.frame" %in% class(happy_df_csv))
24
})
25
test_that("happy_df_csv does not contain the correct data.", {
26
expect_equal(dim(happy_df_csv), c(1562, 19))
27
expect_equal(digest(as.numeric(sum(happy_df_csv$year))), "17201cb95c3eb622e848455bd480ef23")
28
})
29
print("Success!")
30
}
31
32
test_1.3.1 <- function() {
33
test_that("Did not create an object named happy_df", {
34
expect_true(exists("happy_df"))
35
})
36
test_that("happy_df should be a data frame.", {
37
expect_true("data.frame" %in% class(happy_df))
38
})
39
test_that("happy_df does not contain the correct data.", {
40
expect_equal(dim(happy_df), c(1562, 19))
41
expect_equal(digest(as.numeric(sum(happy_df$year))), "17201cb95c3eb622e848455bd480ef23")
42
})
43
print("Success!")
44
}
45
46
test_1.3.2 <- function() {
47
test_that("Did not create an object named happy_step1", {
48
expect_true(exists("happy_step1"))
49
})
50
test_that("Did not create an object named happy_step2", {
51
expect_true(exists("happy_step2"))
52
})
53
test_that("Did not create an object named reduced_happy_df", {
54
expect_true(exists("reduced_happy_df"))
55
})
56
test_that("reduced_happy_df does not contain the correct number of rows and/or columns.", {
57
expect_setequal(dim(reduced_happy_df), c(141, 2))
58
})
59
test_that("The year column in reduced_happy_df should only contain 2017", {
60
expect_equal(unique(happy_step1$year), 2017)
61
})
62
test_that("Columns in reduced_happy_df contain incorrect values.", {
63
expect_equal(digest(round(as.numeric(sum(reduced_happy_df$Positive.affect.scaled, na.rm = TRUE)))), "1c760b1821e90b04ede5ecc5e6d3a0fc")
64
expect_equal(digest(round(as.numeric(sum(reduced_happy_df$Healthy.life.expectancy.at.birth, na.rm = TRUE)))), "a2ef97bc5692bdc875485fa1683a243b")
65
})
66
print("Success!")
67
}
68
69
test_1.4 <- function() {
70
test_that("Did not create a plot named happy_plot", {
71
expect_true(exists("happy_plot"))
72
})
73
properties <- c(happy_plot$layers[[1]]$mapping, happy_plot$mapping)
74
75
test_that("Healthy.life.expectancy.at.birth should be on the x-axis.", {
76
expect_true("Healthy.life.expectancy.at.birth" == rlang::get_expr(properties$x))
77
})
78
test_that("Positive.affect.scaled should be on the y-axis.", {
79
expect_true("Positive.affect.scaled" == rlang::get_expr(properties$y))
80
})
81
test_that("happy_plot should be a scatter plot.", {
82
expect_true("GeomPoint" %in% c(class(happy_plot$layers[[1]]$geom)))
83
})
84
test_that("Labels on the axes should be descriptive and human readable.", {
85
expect_false(happy_plot$labels$y == "Positive.affect.scaled")
86
expect_false(happy_plot$labels$x == "Healthy.life.expectancy.at.birth")
87
})
88
print("Success!")
89
}
90
91
test_2.1 <- function() {
92
test_that("Solution is incorrect", {
93
expect_equal(digest(as.character(answer2.1)), "475bf9280aab63a82af60791302736f6")
94
})
95
print("Success!")
96
}
97
98
test_2.2.0 <- function() {
99
test_that("Did not create an object named whistler_2018", {
100
expect_true(exists("whistler_2018"))
101
})
102
test_that("whistler_2018 should be a data frame.", {
103
expect_true("data.frame" %in% class(whistler_2018))
104
})
105
test_that("whistler_2018 does not contain the correct number of rows and/or columns.", {
106
expect_equal(dim(whistler_2018), c(365, 27))
107
})
108
test_that("whistler_2018 does not contain the correct data.", {
109
expect_equal(digest(round(as.numeric(sum(whistler_2018$`Total Snow (cm)`, na.rm = TRUE)))), "58b219918cd2f36aeaf9b9b3dc071c76")
110
})
111
print("Success!")
112
}
113
114
test_2.2.1 <- function() {
115
test_that("whistler_2018 does not contain the correct number of rows and/or columns.", {
116
expect_equal(dim(whistler_2018), c(365, 27))
117
})
118
test_that("whistler_2018 still contains white space in its column names.", {
119
expect_equal(length(grep(pattern = " ", x = colnames(whistler_2018))), 0)
120
})
121
print("Success!")
122
}
123
124
test_2.3 <- function() {
125
test_that("Did not create a plot named whistler_2018_plot", {
126
expect_true(exists("whistler_2018_plot"))
127
})
128
properties <- c(whistler_2018_plot$layers[[1]]$mapping, whistler_2018_plot$mapping)
129
130
test_that("Date.Time should be on the x-axis.", {
131
expect_true("Date.Time" == rlang::get_expr(properties$x))
132
})
133
test_that("Total.Snow..cm. should be on the y-axis.", {
134
expect_true("Total.Snow..cm." == rlang::get_expr(properties$y))
135
})
136
test_that("whistler_2018_plot should be a line plot.", {
137
expect_true("GeomLine" %in% c(class(whistler_2018_plot$layers[[1]]$geom)))
138
})
139
test_that("Labels on the axes should be descriptive and human readable.", {
140
expect_false(whistler_2018_plot$labels$y == "Total.Snow..cm.")
141
expect_false(whistler_2018_plot$labels$x == "Date.Time")
142
})
143
print("Success!")
144
}
145
146
test_3.0 <- function() {
147
test_that("Did not create a database connected called connection", {
148
expect_true(exists("connection"))
149
expect_equal(digest(class(connection)), "df66af2493a2405d1c7763f4bfbfebc0")
150
})
151
test_that("Did not create a table called project_data", {
152
expect_true(exists("project_data"))
153
expect_equal(digest(class(project_data))[[1]], "98f4e628b20def80372d0acca8b5edb0")
154
})
155
test_that("The project_data table has the wrong information in it", {
156
expect_equal(digest(colnames(project_data)[[13]]), "fd0b87dfaa67d4c90844080d5d9355ec")
157
expect_equal(digest(colnames(project_data)[[14]]), "2f2ec36ffb7792b1f8e274387e520edd")
158
expect_equal(digest(length(colnames(project_data))), "4d4c1ad2286f1a7670a024467dd10808")
159
expect_equal(digest(as_tibble(summarize(project_data, sum = sum(goal, na.rm = TRUE)))[[1]]), "f8d1fabeacced2ef4c82748ca212b724")
160
})
161
print("Success!")
162
}
163
164
test_3.1 <- function() {
165
test_that("Incorrect answer", {
166
expect_equal(digest(answer3.1), "3a5505c06543876fe45598b5e5e5195d")
167
})
168
print("Success!")
169
}
170
171
172
test_3.2 <- function() {
173
test_that("Did not create a plot named funding_over_time_plot", {
174
expect_true(exists("funding_over_time_plot"))
175
})
176
test_that("The axis labels are incorrect", {
177
expect_equal(digest(funding_over_time_plot$labels$x), "bd1055a7d6af03e74a4462edbc5b35c4")
178
expect_equal(digest(funding_over_time_plot$labels$y), "48c36ecb38a110dffe3f40add37ccdc1")
179
})
180
test_that("The x/y data are incorrect", {
181
expect_equal(digest(funding_over_time_plot$mapping$x), "c5a397ebe58129a88408811144869370")
182
expect_equal(digest(funding_over_time_plot$mapping$y), "5d75d853465e03f6828b25a1808b073b")
183
})
184
test_that("arrival_delay_plot should be a scatter plot", {
185
expect_equal(digest(class(funding_over_time_plot$layers[[1]]$geom)[[1]]), "911e5b9debfb523f25ad2ccc01a4b2dd")
186
})
187
print("Success!")
188
}
189
190
191
192
test_3.4 <- function() {
193
test_that("Did not create project_data.csv in the data/ folder.", {
194
expect_true(file.exists("data/project_data.csv"))
195
})
196
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
197
expect_equal(digest(file.info("data/project_data.csv")$size), "8e5009fb2a1bed6bf7ae2a76deef7fea")
198
})
199
test_that("the project_df object does not exist", {
200
expect_true(exists("project_df"))
201
})
202
test_that("the project_df object does not have the right number of rows & columns", {
203
expect_equal(digest(nrow(project_df)), "40202635f105eddd66654976af5d9e1f")
204
expect_equal(digest(ncol(project_df)), "11946e7a3ed5e1776e81c0f0ecd383d0")
205
})
206
test_that("the project_df object does not have the right data", {
207
expect_equal(digest(sum(project_df)), "daca6d59f60da17a8beacb0b8caa51b9")
208
})
209
print("Success!")
210
}
211
212