Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2020-spring/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), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test
7
})
8
print("Success!")
9
}
10
11
test_1.2 <- function(){
12
test_that('Solution is incorrect', {
13
expect_equal(digest(answer2), 'c1f86f7430df7ddb256980ea6a3b57a4') # we hid the answer to the test here so you can't see it, but we can still run the test
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') # we hid the answer to the test here so you can't see it, but we can still run the test
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') # we hid the answer to the test here so you can't see it, but we can still run the test
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(as.numeric(sum(reduced_happy_df$Positive.affect, na.rm = TRUE))), '3c52d24546a2dbf7437bfbf2b953614f')
64
expect_equal(digest(as.numeric(sum(reduced_happy_df$Healthy.life.expectancy.at.birth, na.rm = TRUE))), 'dee33d6f7860c9997daacdbfded438cf')
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
test_that('Healthy.life.expectancy.at.birth should be on the x-axis.', {
74
expect_that("Healthy.life.expectancy.at.birth" %in% c(rlang::get_expr(happy_plot$mapping$x),rlang::get_expr(happy_plot$layers[[1]]$mapping$x)), is_true())
75
})
76
test_that('Positive.affect should be on the y-axis.', {
77
expect_that("Positive.affect" %in% c(rlang::get_expr(happy_plot$mapping$y), rlang::get_expr(happy_plot$layers[[1]]$mapping$y)) , is_true())
78
})
79
test_that('happy_plot should be a scatter plot.', {
80
expect_that("GeomPoint" %in% c(class(happy_plot$layers[[1]]$geom)) , is_true())
81
})
82
test_that('Labels on the axes should be descriptive and human readable.', {
83
expect_that((happy_plot$labels$y) == 'Positive.affect', is_false())
84
expect_that((happy_plot$labels$x) == 'Healthy.life.expectancy.at.birth', is_false())
85
})
86
print("Success!")
87
}
88
89
test_2.1 <- function(){
90
test_that('Solution is incorrect', {
91
expect_equal(digest(as.character(answer2.1)), '475bf9280aab63a82af60791302736f6') # we hid the answer to the test here so you can't see it, but we can still run the test
92
})
93
print("Success!")
94
}
95
96
test_2.2.0 <- function(){
97
test_that('Did not create an object named whistler_2018', {
98
expect_true(exists("whistler_2018"))
99
})
100
test_that('whistler_2018 should be a data frame.', {
101
expect_true('data.frame' %in% class(whistler_2018))
102
})
103
test_that('whistler_2018 does not contain the correct number of rows and/or columns.', {
104
expect_equal(dim(whistler_2018), c(365, 27))
105
})
106
test_that('whistler_2018 does not contain the correct data.', {
107
expect_equal(digest(as.numeric(sum(whistler_2018$Snow_ground, na.rm = TRUE))), '908d1fd10b357ed0ceaaec823abf81bc') # we hid the answer to the test here so you can't see it, but we can still run the test
108
})
109
print("Success!")
110
}
111
112
test_2.2.1 <- function(){
113
test_that('whistler_2018 does not contain the correct number of rows and/or columns.', {
114
expect_equal(dim(whistler_2018), c(365, 27))
115
})
116
test_that('whistler_2018 still contains white space in its column names.', {
117
expect_equal(length(grep(pattern = " ", x = colnames(whistler_2018))), 0)
118
})
119
print("Success!")
120
}
121
122
test_2.3 <- function(){
123
test_that('Did not create a plot named whistler_2018_plot', {
124
expect_true(exists("whistler_2018_plot"))
125
})
126
test_that('Date.Time should be on the x-axis.', {
127
expect_that("Date.Time" %in% c(rlang::get_expr(whistler_2018_plot$mapping$x),rlang::get_expr(whistler_2018_plot$layers[[1]]$mapping$x)), is_true())
128
})
129
test_that('Total.Snow..cm. should be on the y-axis.', {
130
expect_that("Total.Snow..cm." %in% c(rlang::get_expr(whistler_2018_plot$mapping$y), rlang::get_expr(whistler_2018_plot$layers[[1]]$mapping$y)) , is_true())
131
})
132
test_that('whistler_2018_plot should be a line plot.', {
133
expect_that("GeomLine" %in% c(class(whistler_2018_plot$layers[[1]]$geom)) , is_true())
134
})
135
test_that('Labels on the axes should be descriptive and human readable.', {
136
expect_that((whistler_2018_plot$labels$y) == 'Total.Snow..cm.', is_false())
137
expect_that((whistler_2018_plot$labels$x) == 'Date.Time', is_false())
138
})
139
print("Success!")
140
}
141
142
test_3.0 <- function(){
143
test_that('Did not create a database connected called connection', {
144
expect_true(exists("connection"))
145
expect_equal(digest(class(connection)), 'df66af2493a2405d1c7763f4bfbfebc0')
146
})
147
test_that('Did not create a table called project_data', {
148
expect_true(exists('project_data'))
149
expect_equal(digest(class(project_data))[[1]], '98f4e628b20def80372d0acca8b5edb0')
150
})
151
test_that('The project_data table has the wrong information in it', {
152
expect_equal(digest(colnames(project_data)[[13]]), 'fd0b87dfaa67d4c90844080d5d9355ec')
153
expect_equal(digest(colnames(project_data)[[14]]), '2f2ec36ffb7792b1f8e274387e520edd')
154
expect_equal(digest(length(colnames(project_data))), '4d4c1ad2286f1a7670a024467dd10808')
155
expect_equal(digest(as_tibble(summarize(project_data, sum=sum(goal, na.rm=TRUE)))[[1]]), 'f8d1fabeacced2ef4c82748ca212b724')
156
})
157
print('Success!')
158
}
159
160
test_3.1 <- function(){
161
test_that('Incorrect answer', {
162
expect_equal(digest(answer3.1), '3a5505c06543876fe45598b5e5e5195d')
163
})
164
print('Success!')
165
}
166
167
168
test_3.2 <- function(){
169
test_that('Did not create a plot named funding_over_time_plot', {
170
expect_true(exists("funding_over_time_plot"))
171
})
172
test_that('The axis labels are incorrect', {
173
expect_equal(digest(funding_over_time_plot$labels$x), "bd1055a7d6af03e74a4462edbc5b35c4")
174
expect_equal(digest(funding_over_time_plot$labels$y), '48c36ecb38a110dffe3f40add37ccdc1')
175
})
176
test_that('The x/y data are incorrect', {
177
expect_equal(digest(funding_over_time_plot$mapping$x), 'c5a397ebe58129a88408811144869370')
178
expect_equal(digest(funding_over_time_plot$mapping$y), '5d75d853465e03f6828b25a1808b073b')
179
})
180
test_that('arrival_delay_plot should be a scatter plot', {
181
expect_equal(digest(class(funding_over_time_plot$layers[[1]]$geom)[[1]]), '911e5b9debfb523f25ad2ccc01a4b2dd')
182
})
183
print("Success!")
184
}
185
186
187
188
test_3.4 <- function(){
189
test_that('Did not create project_data.csv in the data/ folder.', {
190
expect_true(file.exists("data/project_data.csv"))
191
})
192
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
193
expect_equal(digest(file.info('data/project_data.csv')$size), "8e5009fb2a1bed6bf7ae2a76deef7fea")
194
})
195
test_that('the project_df object does not exist', {
196
expect_true(exists('project_df'))
197
})
198
test_that('the project_df object does not have the right number of rows & columns',{
199
expect_equal(digest(nrow(project_df)), '40202635f105eddd66654976af5d9e1f')
200
expect_equal(digest(ncol(project_df)), '11946e7a3ed5e1776e81c0f0ecd383d0')
201
})
202
test_that('the project_df object does not have the right data',{
203
expect_equal(digest(sum(project_df)), 'daca6d59f60da17a8beacb0b8caa51b9')
204
})
205
print("Success!")
206
}
207
208
209
210
211
212
213