Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
UBC-DSCI
GitHub Repository: UBC-DSCI/dsci-100-assets
Path: blob/master/2019-fall/materials/tutorial_03/tests_tutorial_03.R
2051 views
1
library(testthat)
2
library(digest)
3
4
test_revision <- 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(sum(avocado$average_price)), 'b987e782cc203bbe06ef8fde8da42991')
28
expect_equal(colnames(avocado), c("Date", "average_price", "small_hass_volume", "large_hass_volume", "extra_l_hass_volume", "type", "year", "region", "week"))
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
test_that('total_volume should be on the x-axis.', {
55
expect_that("total_volume" %in% c(rlang::get_expr(avocado_plot$mapping$x),rlang::get_expr(avocado_plot$layers[[1]]$mapping$x)), is_true())
56
})
57
test_that('average_price should be on the y-axis.', {
58
expect_that("average_price" %in% c(rlang::get_expr(avocado_plot$mapping$y), rlang::get_expr(avocado_plot$layers[[1]]$mapping$y)) , is_true())
59
})
60
test_that('avocado_plot should be a scatter plot.', {
61
expect_that("GeomPoint" %in% c(class(avocado_plot$layers[[1]]$geom)) , is_true())
62
})
63
test_that('Labels on the axes should be descriptive and human readable.', {
64
expect_that((avocado_plot$labels$y) == 'average_price', is_false())
65
expect_that((avocado_plot$labels$x) == 'total_volume', is_false())
66
})
67
print("Success!")
68
}
69
70
test_3.1 <- function(){
71
test_that('Did not create an object named sea_surface', {
72
expect_true(exists("sea_surface"))
73
})
74
test_that('sea_surface should be a data frame.', {
75
expect_true('data.frame' %in% class(sea_surface))
76
})
77
test_that('sea_surface does not contain the correct number of rows and/or columns.', {
78
expect_equal(dim(sea_surface), c(105, 13))
79
})
80
test_that('sea_surface does not contain the correct data.', {
81
expect_equal(digest(sum(sea_surface$Dec)), '9c9393e1464352cd4fbea94dfadfa02a')
82
expect_equal(colnames(sea_surface), c("Year", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
83
})
84
print("Success!")
85
}
86