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_03/tests_tutorial_03.R
2051 views
1
library(testthat)
2
library(digest)
3
4
test_0.1 <- function(){
5
test_that('Solution is incorrect', {
6
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
7
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
8
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
9
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
10
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
11
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
12
})
13
print("Success!")
14
}
15
16
test_1.1 <- function(){
17
test_that('Did not create an object named avocado', {
18
expect_true(exists("avocado"))
19
})
20
test_that('avocado should be a data frame.', {
21
expect_true('data.frame' %in% class(avocado))
22
})
23
test_that('avocado does not contain the correct number of rows and/or columns.', {
24
expect_equal(dim(avocado), c(17911, 9))
25
})
26
test_that('avocado does not contain the correct data.', {
27
expect_equal(digest(round(sum(avocado$average_price))), 'd478458402bf9895986c7e8a50ad4b61')
28
expect_equal(colnames(avocado), c("Date", "average_price", "small_hass_volume", "large_hass_volume", "extra_l_hass_volume", "type", "yr", "region", "wk"))
29
})
30
print("Success!")
31
}
32
33
test_1.2 <- function(){
34
test_that('Did not create an object named cheapest', {
35
expect_true(exists("cheapest"))
36
})
37
test_that('cheapest should be a data frame.', {
38
expect_true('data.frame' %in% class(cheapest))
39
})
40
test_that('avocado does not contain the correct number of rows and/or columns.', {
41
expect_equal(dim(cheapest), c(1, 2))
42
})
43
test_that('cheapest does not contain the correct data.', {
44
expect_equal(digest(cheapest$region[0]), '5152ac13bdd09110d9ee9c169a3d9237') # we hid the answer to the test here so you can't see it, but we can still run the test
45
expect_equal(digest(as.numeric(unlist(select(cheapest, -region)))), '481ea83b5704f345de5f42f139ee11c7') # we hid the answer to the test here so you can't see it, but we can still run the test
46
})
47
print("Success!")
48
}
49
50
test_1.3 <- function(){
51
test_that('Did not create a plot named avocado_plot', {
52
expect_true(exists("avocado_plot"))
53
})
54
properties <- c(avocado_plot$layers[[1]]$mapping, avocado_plot$mapping)
55
test_that('total_volume should be on the x-axis.', {
56
expect_true("total_volume" == rlang::get_expr(properties$x))
57
})
58
test_that('average_price should be on the y-axis.', {
59
expect_true("average_price" == rlang::get_expr(properties$y))
60
})
61
test_that('region should be Houston.', {
62
expect_true(unique(avocado_plot$data$region) == "Houston")
63
})
64
test_that('avocado_plot should be a scatter plot.', {
65
expect_true("GeomPoint" %in% c(class(avocado_plot$layers[[1]]$geom)))
66
})
67
test_that('Labels on the axes should be descriptive and human readable.', {
68
expect_false(avocado_plot$labels$y == 'average_price')
69
expect_false(avocado_plot$labels$x == 'total_volume')
70
})
71
print("Success!")
72
}
73
74
test_3.1 <- function(){
75
test_that('Did not create an object named sea_surface', {
76
expect_true(exists("sea_surface"))
77
})
78
test_that('sea_surface should be a data frame.', {
79
expect_true('data.frame' %in% class(sea_surface))
80
})
81
test_that('sea_surface does not contain the correct number of rows and/or columns.', {
82
expect_equal(dim(sea_surface), c(105, 13))
83
})
84
test_that('sea_surface does not contain the correct data.', {
85
expect_equal(digest(round(sum(sea_surface$Dec, na.rm = TRUE))), 'b077728cf0193c3d5dbe79a925096b26')
86
expect_equal(colnames(sea_surface), c("Year", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
87
})
88
print("Success!")
89
}
90