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_viz/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
test_0.1 <- function(){
34
test_that('Solution is incorrect', {
35
expect_equal(digest(A), 'db8e490a925a60e62212cefc7674ca02') # we hid the answer to the test here so you can't see it, but we can still run the test
36
expect_equal(digest(B), 'e5b57f323c7b3719bbaaf9f96b260d39') # we hid the answer to the test here so you can't see it, but we can still run the test
37
expect_equal(digest(C), '6717f2823d3202449301145073ab8719') # we hid the answer to the test here so you can't see it, but we can still run the test
38
expect_equal(digest(D), 'dbc09cba9fe2583fb01d63c70e1555a8') # we hid the answer to the test here so you can't see it, but we can still run the test
39
expect_equal(digest(E), '0aee9b78301d7ec8998971363be87c03') # we hid the answer to the test here so you can't see it, but we can still run the test
40
expect_equal(digest(F), '5e338704a8e069ebd8b38ca71991cf94') # we hid the answer to the test here so you can't see it, but we can still run the test
41
})
42
print("Success!")
43
}
44
45
test_0.2 <- function(){
46
test_that('Solution is incorrect', {
47
expect_equal(digest(answer0.2), '05ca18b596514af73f6880309a21b5dd') # we hid the answer to the test here so you can't see it, but we can still run the test
48
})
49
print("Success!")
50
}
51
52
test_1.1 <- function(){
53
test_that('Solution is incorrect', {
54
expect_equal(digest(answer1.1), '0590b0427c1b19a6eb612d19888aa52f') # we hid the answer to the test here so you can't see it, but we can still run the test
55
})
56
print("Success!")
57
}
58
59
test_1.2 <- function(){
60
test_that('Solution is incorrect', {
61
expect_equal(digest(answer1.2), '75f1160e72554f4270c809f041c7a776') # we hid the answer to the test here so you can't see it, but we can still run the test
62
})
63
print("Success!")
64
}
65
66
test_1.3 <- function(){
67
test_that('Did not create an object named insurance', {
68
expect_true(exists("insurance"))
69
})
70
test_that('insurance should be a data frame.', {
71
expect_true('data.frame' %in% class(insurance))
72
})
73
test_that('insurance does not contain the correct number of rows and/or columns.', {
74
expect_equal(dim(insurance), c(1338, 7))
75
})
76
test_that('Columns in insurance contain incorrect values.', {
77
expect_equal(digest(int_round(sum(insurance$age), 2)), 'de3a0ef8b98df33e016e2f91ab060fb5')
78
expect_equal(digest(int_round(length(unique(insurance$region)), 0)), '234a2a5581872457b9fe1187d1616b13') # we hid the answer to the test here so you can't see it, but we can still run the test
79
})
80
print("Success!")
81
}
82
83
test_1.5 <- function(){
84
properties <- c(bmi_plot$layers[[1]]$mapping, bmi_plot$mapping)
85
labels <- bmi_plot$labels
86
test_that('Did not create a plot named bmi_plot', {
87
expect_true(exists("bmi_plot"))
88
})
89
test_that('bmi should be on the x-axis.', {
90
expect_true("bmi" == rlang::get_expr(properties$x))
91
})
92
test_that('charges should be on the y-axis.', {
93
expect_true("charges" == rlang::get_expr(properties$y))
94
})
95
test_that('bmi_plot should be a scatter plot.', {
96
expect_true("GeomPoint" %in% c(class(bmi_plot$layers[[1]]$geom)))
97
})
98
test_that('Labels on the axes and legend should be descriptive and human readable.', {
99
expect_false((labels$y) == 'charges')
100
expect_false((labels$x) == 'bmi')
101
})
102
print("Success!")
103
}
104
105
106
test_1.9.0 <- function(){
107
test_that('Solution is incorrect', {
108
expect_equal(digest(answer1.9.0), 'd2a90307aac5ae8d0ef58e2fe730d38b') # we hid the answer to the test here so you can't see it, but we can still run the test
109
})
110
print("Success!")
111
}
112
113
test_1.9.1 <- function(){
114
test_that('Solution is incorrect', {
115
expect_equal(digest(answer1.9.1), '05ca18b596514af73f6880309a21b5dd') # we hid the answer to the test here so you can't see it, but we can still run the test
116
})
117
print("Success!")
118
}
119
120
test_1.10_old <- function(){
121
test_that('Did not create a plot named bar_plot', {
122
expect_true(exists("bar_plot"))
123
})
124
# test_that('Count up the number of rows.')
125
# expect
126
test_that('sex should be on the x-axis.', {
127
expect_true("sex" %in% c(rlang::get_expr(bar_plot$mapping$x),rlang::get_expr(bar_plot$layers[[1]]$mapping$x)))
128
})
129
test_that('bar_plot should map smoker to the colour filled in the bars.', {
130
expect_equal(digest(as.character(rlang::get_expr(bar_plot$mapping$fill))) , '059bc865bfafc56a35be79573fc6f02b')
131
})
132
test_that('bar_plot should be a bar chart.', {
133
expect_true("GeomBar" %in% c(class(bar_plot$layers[[1]]$geom)))
134
})
135
test_that('Label for the legend should be descriptive and human readable.', {
136
expect_equal(digest(bar_plot$labels$fill), '2ba2a41537b71ba968e6f15e9d2f914e')
137
})
138
# Need to test for position = fill
139
print("Success!")
140
}
141
142
test_1.10 <- function(){
143
properties <- c(bar_plot$layers[[1]]$mapping, bar_plot$mapping)
144
labels <- bar_plot$labels
145
test_that('Did not create a plot named bar_plot', {
146
expect_true(exists("bar_plot"))
147
})
148
test_that('sex should be on the x-axis.', {
149
expect_true("sex" == rlang::get_expr(properties$x))
150
})
151
test_that('the smoker variable should be used to colour fill the bars.', {
152
expect_true("smoker" == rlang::get_expr(properties$fill))
153
})
154
test_that('bar_plot should be a bar plot.', {
155
expect_true("GeomBar" %in% c(class(bar_plot$layers[[1]]$geom)))
156
})
157
test_that('bar_plot position should be fill', {
158
expect_true("PositionFill" %in% class(bar_plot$layers[[1]]$position))
159
})
160
test_that('Labels on the axes and legend should be descriptive and human readable.', {
161
expect_false((labels$x) == 'sex')
162
expect_false((labels$fill) == 'smoker')
163
})
164
print("Success!")
165
}
166
167
test_1.11 <- function(){
168
test_that('Solution is incorrect', {
169
expect_equal(digest(answer1.11), 'a92f67bf4e1efa7b94661c5af29effc2') # we hid the answer to the test here so you can't see it, but we can still run the test
170
})
171
print("Success!")
172
}
173
174