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_04/tests_tutorial_04.R
2051 views
1
library(testthat)
2
library(digest)
3
4
5
6
7
test_0.1 <- function(){
8
test_that('Solution is incorrect', {
9
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
10
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
11
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
12
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
13
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
14
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
15
})
16
print("Success!")
17
}
18
19
test_0.2 <- function(){
20
test_that('Solution is incorrect', {
21
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
22
})
23
print("Success!")
24
}
25
26
test_1.1 <- function(){
27
test_that('Solution is incorrect', {
28
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
29
})
30
print("Success!")
31
}
32
33
test_1.2 <- function(){
34
test_that('Solution is incorrect', {
35
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
36
})
37
print("Success!")
38
}
39
40
test_1.3 <- function(){
41
test_that('Did not create an object named insurance', {
42
expect_true(exists("insurance"))
43
})
44
test_that('insurance should be a data frame.', {
45
expect_true('data.frame' %in% class(insurance))
46
})
47
test_that('insurance does not contain the correct number of rows and/or columns.', {
48
expect_equal(dim(insurance), c(1338, 7))
49
})
50
test_that('Columns in insurance contain incorrect values.', {
51
expect_equal(digest(as.integer(sum(insurance$age))), '61160677dd5eaf5b041347b9ff0c9b2d')
52
expect_equal(digest(as.integer(length(unique(insurance$region)))), '234a2a5581872457b9fe1187d1616b13') # we hid the answer to the test here so you can't see it, but we can still run the test
53
})
54
print("Success!")
55
}
56
57
test_1.5 <- function(){
58
properties <- c(bmi_plot$layers[[1]]$mapping, bmi_plot$mapping)
59
labels <- bmi_plot$labels
60
test_that('Did not create a plot named bmi_plot', {
61
expect_true(exists("bmi_plot"))
62
})
63
test_that('bmi should be on the x-axis.', {
64
expect_true("bmi" == rlang::get_expr(properties$x))
65
})
66
test_that('charges should be on the y-axis.', {
67
expect_true("charges" == rlang::get_expr(properties$y))
68
})
69
test_that('bmi_plot should be a scatter plot.', {
70
expect_that("GeomPoint" %in% c(class(bmi_plot$layers[[1]]$geom)) , is_true())
71
})
72
test_that('Labels on the axes and legend should be descriptive and human readable.', {
73
expect_that((labels$y) == 'charges', is_false())
74
expect_that((labels$x) == 'bmi', is_false())
75
})
76
print("Success!")
77
}
78
79
80
test_1.9.0 <- function(){
81
test_that('Solution is incorrect', {
82
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
83
})
84
print("Success!")
85
}
86
87
test_1.9.1 <- function(){
88
test_that('Solution is incorrect', {
89
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
90
})
91
print("Success!")
92
}
93
94
test_1.10_old <- function(){
95
test_that('Did not create a plot named bar_plot', {
96
expect_true(exists("bar_plot"))
97
})
98
# test_that('Count up the number of rows.')
99
# expect
100
test_that('sex should be on the x-axis.', {
101
expect_that("sex" %in% c(rlang::get_expr(bar_plot$mapping$x),rlang::get_expr(bar_plot$layers[[1]]$mapping$x)), is_true())
102
})
103
test_that('bar_plot should map smoker to the colour filled in the bars.', {
104
expect_equal(digest(as.character(rlang::get_expr(bar_plot$mapping$fill))) , '059bc865bfafc56a35be79573fc6f02b')
105
})
106
test_that('bar_plot should be a bar chart.', {
107
expect_that("GeomBar" %in% c(class(bar_plot$layers[[1]]$geom)) , is_true())
108
})
109
test_that('Label for the legend should be descriptive and human readable.', {
110
expect_equal(digest(bar_plot$labels$fill), '2ba2a41537b71ba968e6f15e9d2f914e')
111
})
112
# Need to test for position = fill
113
print("Success!")
114
}
115
116
test_1.10 <- function(){
117
properties <- c(bar_plot$layers[[1]]$mapping, bar_plot$mapping)
118
labels <- bar_plot$labels
119
test_that('Did not create a plot named bar_plot', {
120
expect_true(exists("bar_plot"))
121
})
122
test_that('sex should be on the x-axis.', {
123
expect_true("sex" == rlang::get_expr(properties$x))
124
})
125
test_that('the smoker variable should be used to colour fill the bars.', {
126
expect_true("smoker" == rlang::get_expr(properties$fill))
127
})
128
test_that('bar_plot should be a bar plot.', {
129
expect_that("GeomBar" %in% c(class(bar_plot$layers[[1]]$geom)) , is_true())
130
})
131
test_that('bar_plot position should be fill', {
132
expect_that("PositionFill" %in% class(bar_plot$layers[[1]]$position) , is_true())
133
})
134
test_that('Labels on the axes and legend should be descriptive and human readable.', {
135
expect_that((labels$x) == 'sex', is_false())
136
expect_that((labels$fill) == 'smoker', is_false())
137
})
138
print("Success!")
139
}
140
141
test_1.11 <- function(){
142
test_that('Solution is incorrect', {
143
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
144
})
145
print("Success!")
146
}
147