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_10/tests_tutorial_10.R
2051 views
1
library(testthat)
2
library(digest)
3
4
test_1.0 <- function(){
5
test_that('Did not create an object named pm_data', {
6
expect_true(exists("pm_data"))
7
})
8
test_that('pm_data should be a data frame.', {
9
expect_true('data.frame' %in% class(pm_data))
10
})
11
test_that('pm_data does not contain the correct number of rows and/or columns.', {
12
expect_equal(dim(pm_data), c(800, 13))
13
})
14
test_that('pm_data is missing columns.', {
15
expect_true('Name' %in% colnames(pm_data))
16
expect_true('HP' %in% colnames(pm_data))
17
expect_true('Attack' %in% colnames(pm_data))
18
expect_true('Defense' %in% colnames(pm_data))
19
expect_true('#' %in% colnames(pm_data))
20
expect_true('Type 1' %in% colnames(pm_data))
21
expect_true('Type 2' %in% colnames(pm_data))
22
expect_true('Total' %in% colnames(pm_data))
23
expect_true('Sp. Atk' %in% colnames(pm_data))
24
expect_true('Sp. Def' %in% colnames(pm_data))
25
expect_true('Speed' %in% colnames(pm_data))
26
expect_true('Generation' %in% colnames(pm_data))
27
expect_true('Legendary' %in% colnames(pm_data))
28
})
29
print("Success!")
30
}
31
32
test_1.1 <- function(){
33
test_that('Did not create a plot named pm_pairs', {
34
expect_true(exists("pm_pairs"))
35
})
36
test_that('pm_pairs should be using data from pm_data', {
37
expect_equal(nrow(pm_pairs$data), 800)
38
expect_equal(ncol(pm_pairs$data), 7)
39
})
40
test_that('pm_pairs should be a pairwise plot matrix.', {
41
expect_true('ggmatrix' %in% c(class(pm_pairs)))
42
})
43
test_that('pm_pairs should plot columns 5 to 11', {
44
expect_equal(pm_pairs$yAxisLabels %in% c("Total", "HP", "Attack", "Defense", "Sp. Atk", "Sp. Def", "Speed"), c(TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE))
45
})
46
print("Success!")
47
}
48
49
test_1.2 <- function(){
50
properties <- c(pm_scatter$layers[[1]]$mapping, pm_scatter$mapping)
51
labels <- pm_scatter$labels
52
test_that('Did not create a plot named pm_scatter', {
53
expect_true(exists("pm_scatter"))
54
})
55
test_that('Speed should be on the x-axis.', {
56
expect_true("Speed" == rlang::get_expr(properties$x))
57
})
58
test_that('Defense should be on the y-axis.', {
59
expect_true("Defense" == rlang::get_expr(properties$y))
60
})
61
test_that('pm_scatter should be a scatter plot.', {
62
expect_that("GeomPoint" %in% c(class(pm_scatter$layers[[1]]$geom)) , is_true())
63
})
64
test_that('Labels on the axes and legend need to be changed to be descriptive, nicely formatted, and human readable.', {
65
expect_that((labels$y) == 'Defense', is_false())
66
expect_that((labels$x) == 'Speed', is_false())
67
})
68
print("Success!")
69
}
70
71
72
73
test_1.3 <- function(){
74
test_that('km_data should contain the columns Speed and Defense', {
75
expect_true('Speed' %in% colnames(km_data))
76
expect_true('Defense' %in% colnames(km_data))
77
})
78
test_that('km_data should contain 800 rows and 2 columns.', {
79
expect_equal(ncol(km_data), 2)
80
expect_equal(nrow(km_data), 800)
81
})
82
print("Success!")
83
}
84
85
test_1.4.2 <- function(){
86
test_that('The pokemon_clusters model should have 4 centers.', {
87
expect_equal(nrow(pokemon_clusters$centers), 4)
88
})
89
test_that('The pokemon_clusters model should be using Speed and Defense to create the clusters.', {
90
expect_equal(ncol(pokemon_clusters$centers), 2)
91
expect_true('Speed' %in% colnames(pokemon_clusters$centers))
92
expect_true('Defense' %in% colnames(pokemon_clusters$centers))
93
})
94
test_that('The pokemon_clusters model should be of class kmeans', {
95
expect_equal(class(pokemon_clusters), 'kmeans')
96
})
97
print("Success!")
98
}
99
100
test_1.5 <- function(){
101
properties <- c(answer1.5$layers[[1]]$mapping, answer1.5$mapping)
102
labels <- answer1.5$labels
103
test_that('Did not create a plot named answer1.5', {
104
expect_true(exists("answer1.5"))
105
})
106
test_that('Speed should be on the x-axis.', {
107
expect_true("Speed" == rlang::get_expr(properties$x))
108
})
109
test_that('Defense should be on the y-axis.', {
110
expect_true("Defense" == rlang::get_expr(properties$y))
111
})
112
test_that('answer1.5 should be a scatter plot.', {
113
expect_that("GeomPoint" %in% c(class(answer1.5$layers[[1]]$geom)) , is_true())
114
})
115
test_that('Labels on the axes and legend need to be changed to be descriptive, nicely formatted, and human readable.', {
116
expect_that((labels$y) == 'Defense', is_false())
117
expect_that((labels$x) == 'Speed', is_false())
118
expect_that((labels$colour) == '.cluster', is_false())
119
})
120
print("Success!")
121
}
122
123
test_1.7 <- function(){
124
test_that('elbow_stats should contain k from 1 to 10', {
125
expect_equal(nrow(elbow_stats), 10)
126
})
127
test_that('Solution is incorrect', {
128
expect_equal(sum(c('k', 'tot.withinss') %in% colnames(elbow_stats)), 2)
129
})
130
print("Success!")
131
}
132