Skip to content

Commit 3d0f06d

Browse files
committed
add tests for plotting functions #58
openjournals/joss-reviews#3888
1 parent 5ec150a commit 3d0f06d

File tree

3 files changed

+199
-0
lines changed

3 files changed

+199
-0
lines changed

tests/testthat/test-ggradar.R

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
test_that("check that ggradar produces ggplot object",
2+
{
3+
plot.data <- data.frame(
4+
bycatch = factor(c(
5+
"Higher end of bycatch range",
6+
"Midpoint of bycatch range",
7+
"Lower end of bycatch range"
8+
),
9+
,levels = c(
10+
"Higher end of bycatch range",
11+
"Midpoint of bycatch range",
12+
"Lower end of bycatch range"
13+
)),
14+
prebuild50 = c(0, 0, 1),
15+
prebuild100 = c(0, 0, 1),
16+
abundrel10 = c(0, 0, 0.29),
17+
abundrel20 = c(0, 0, 0.34),
18+
abundrel50 = c(0, 0, 0.53)
19+
)
20+
21+
p1 <- ggradar(
22+
plot.data = plot.data,
23+
axis.label.size = 4,
24+
grid.label.size = 4,
25+
palette.vec = c("#99D594","#E6F598", "#FEE08B")
26+
)
27+
28+
expect_true(class(p1)[2]=="ggplot")
29+
30+
})

tests/testthat/test-multiplot_proj.R

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Test multiplot_proj
2+
test_that("multiplot_proj returns three-panel figure", {
3+
parms <- list(
4+
S0 = 0.944, S1plus = 0.99,
5+
K1plus = 9000,
6+
AgeMat = 18, nages = 20,
7+
z = 2.39, lambdaMax = 1.02
8+
)
9+
InitDepl.vec <- c(0.1, 0.5, 0.9)
10+
BycatchCV <- 0.2
11+
nyears <- 100
12+
13+
high.list <- lapply(
14+
X = InitDepl.vec,
15+
function(x) {
16+
projections(
17+
NOut = 50,
18+
ConstantRateBycatch = list(Rate = 0.3, CV = BycatchCV),
19+
InitDepl = x,
20+
lh.params = parms,
21+
nyears = nyears,
22+
obs_CV = 0.2
23+
)
24+
}
25+
)
26+
27+
med.list <- lapply(
28+
X = InitDepl.vec,
29+
function(x) {
30+
projections(
31+
NOut = 50,
32+
ConstantRateBycatch = list(Rate = 0.02, CV = BycatchCV),
33+
InitDepl = x,
34+
lh.params = parms,
35+
nyears = nyears,
36+
obs_CV = 0.2
37+
)
38+
}
39+
)
40+
low.list <- lapply(
41+
X = InitDepl.vec,
42+
function(x) {
43+
projections(
44+
NOut = 50,
45+
ConstantRateBycatch = list(Rate = 0.001, CV = BycatchCV),
46+
InitDepl = x,
47+
lh.params = parms,
48+
nyears = nyears,
49+
obs_CV = 0.2
50+
)
51+
}
52+
)
53+
p1 <- multiplot_proj(
54+
high.d1 = high.list[[1]], # d1 is the lowest depletion
55+
med.d1 = med.list[[1]],
56+
low.d1 = low.list[[1]],
57+
high.d2 = high.list[[2]],
58+
med.d2 = med.list[[2]],
59+
low.d2 = low.list[[2]],
60+
high.d3 = high.list[[3]],
61+
med.d3 = med.list[[3]],
62+
low.d3 = low.list[[3]],
63+
years.to.plot = nyears
64+
)
65+
66+
expect_equal(3, length(ggplot2::ggplot_build(p1)$layout$layout$PANEL))
67+
})

tests/testthat/test-plot_proj.R

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# plot_proj test
2+
test_that("plot_proj() errors when it tries to plot more years than in projections", {
3+
parms <- list(
4+
S0 = 0.77, S1plus = 0.88,
5+
K1plus = 9000,
6+
AgeMat = 3, nages = 10,
7+
z = 2.39, lambdaMax = 1.12
8+
)
9+
initdepl <- 0.5
10+
high.simple <- projections(
11+
NOut = 50,
12+
ConstantBycatch = list(
13+
Catch = 100,
14+
CV = 0.3
15+
),
16+
InitDepl = initdepl,
17+
lh.params = parms,
18+
nyears = 100
19+
)
20+
med.simple <- projections(
21+
NOut = 50,
22+
ConstantBycatch = list(
23+
Catch = 50,
24+
CV = 0.3
25+
),
26+
InitDepl = initdepl,
27+
lh.params = parms,
28+
nyears = 100
29+
)
30+
31+
low.simple <- projections(
32+
NOut = 50,
33+
ConstantBycatch = list(
34+
Catch = 10,
35+
CV = 0.3
36+
),
37+
InitDepl = initdepl,
38+
lh.params = parms,
39+
nyears = 100
40+
)
41+
42+
expect_error(plot_proj(
43+
high = high.simple,
44+
med = med.simple,
45+
low = low.simple,
46+
years.plot = 101,
47+
ylims = c(0, parms$K1plus), InitDepl = initdepl,
48+
K1plus = parms$K1plus
49+
))
50+
})
51+
52+
test_that("plot_proj() errors when it tries to plot more years than in projections", {
53+
parms <- list(
54+
S0 = 0.77, S1plus = 0.88,
55+
K1plus = 9000,
56+
AgeMat = 3, nages = 10,
57+
z = 2.39, lambdaMax = 1.12
58+
)
59+
initdepl <- 0.5
60+
high.simple <- projections(
61+
NOut = 50,
62+
ConstantBycatch = list(
63+
Catch = 100,
64+
CV = 0.3
65+
),
66+
InitDepl = initdepl,
67+
lh.params = parms,
68+
nyears = 100
69+
)
70+
med.simple <- projections(
71+
NOut = 50,
72+
ConstantBycatch = list(
73+
Catch = 50,
74+
CV = 0.3
75+
),
76+
InitDepl = initdepl,
77+
lh.params = parms,
78+
nyears = 100
79+
)
80+
81+
low.simple <- projections(
82+
NOut = 50,
83+
ConstantBycatch = list(
84+
Catch = 10,
85+
CV = 0.3
86+
),
87+
InitDepl = initdepl,
88+
lh.params = parms,
89+
nyears = 100
90+
)
91+
92+
p1 <- plot_proj(
93+
high = high.simple,
94+
med = med.simple,
95+
low = low.simple,
96+
years.plot = 100,
97+
ylims = c(0, parms$K1plus), InitDepl = initdepl,
98+
K1plus = parms$K1plus
99+
)
100+
101+
expect_true(ggplot2::is.ggplot(p1))
102+
})

0 commit comments

Comments
 (0)