Skip to content

Commit

Permalink
Converted some more tests to edition 3
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavdelius committed Oct 30, 2022
1 parent 92fdeeb commit 572037f
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 18 deletions.
9 changes: 9 additions & 0 deletions tests/testthat/_snaps/setResource.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# We can set and get resource parameters

Code
params <- setResource(params, r_pp = r_pp, kappa = kappa, lambda = lambda, n = n,
w_pp_cutoff = w_pp_cutoff)
Message <simpleMessage>
The resource intrinsic growth rate has been commented and therefore will not be recalculated from the resource parameters.
The resource carrying capacity has been commented and therefore will not be recalculated from the resource parameters.

10 changes: 10 additions & 0 deletions tests/testthat/_snaps/steady.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# steady works

c("1" = 1.12523946834203, "2" = 0.378015488322643, "3" = 0.124113033074317,
"4" = 0.0412195840646914)

---

c("1" = 1.12523946834203, "2" = 0.378015488322643, "3" = 0.124113033074317,
"4" = 0.0412195840646914)

1 change: 1 addition & 0 deletions tests/testthat/test-animateSpectra.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local_edition(3)
test_that("animateSpectra does not throw error", {
sim <- project(NS_params, t_max = 2, t_save = 1, effort = 1)
expect_error(animateSpectra(sim, species = c("Cod", "Haddock"),
Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-calibrate.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local_edition(3)
# calibrateBiomass ----
test_that("calibrateBiomass works", {
params <- NS_params
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/test-compareParams.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local_edition(3)

test_that("compareParams", {
local_reproducible_output()
params <- NS_params
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test-setResource.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local_edition(3)
test_that("We can set and get resource parameters", {
params <- NS_params
rp <- resource_params(params)
Expand Down Expand Up @@ -39,10 +40,9 @@ test_that("We can set and get resource parameters", {
expect_identical(getResourceRate(params), resource_rate)
expect_identical(getResourceCapacity(params), resource_capacity)
# Check that comments protect
expect_message(params <- setResource(params, r_pp = r_pp, kappa = kappa,
expect_snapshot(params <- setResource(params, r_pp = r_pp, kappa = kappa,
lambda = lambda, n = n,
w_pp_cutoff = w_pp_cutoff),
"The resource carrying capacity has been commented")
w_pp_cutoff = w_pp_cutoff))
comment(params@cc_pp) <- NULL
expect_message(params <- setResource(params, r_pp = r_pp, kappa = kappa,
lambda = lambda, n = n,
Expand All @@ -56,8 +56,8 @@ test_that("We can set and get resource parameters", {
r_pp * params@w_full^(n - 1))
resource_capacity <- getResourceCapacity(params)
cc_pp <- kappa*params@w_full^(-lambda)
expect_equivalent(cc_pp[params@w_full <= w_pp_cutoff],
resource_capacity[params@w_full <= w_pp_cutoff])
expect_equal(cc_pp[params@w_full <= w_pp_cutoff],
unname(resource_capacity[params@w_full <= w_pp_cutoff]))
# resource_params<- sets rates correctly
resource_params(NS_params) <- resource_params(params)
expect_unchanged(NS_params, params)
Expand Down Expand Up @@ -89,7 +89,7 @@ test_that("Comment works on resource_rate", {
p <- setResource(params, reset = TRUE)
expect_equal(p@rr_pp[1],
params@w_full[1] ^ (params@resource_params$n - 1),
check.attributes = FALSE)
ignore_attr = TRUE)
expect_warning(setResource(params, resource_rate = resource_rate,
reset = TRUE),
"Because you set `reset = TRUE`, the")
Expand Down Expand Up @@ -121,7 +121,7 @@ test_that("Comment works on resource_capacity", {
p <- setResource(params, reset = TRUE)
expect_equal(p@cc_pp[1],
params@w_full[1] ^ (-params@resource_params$lambda),
check.attributes = FALSE)
ignore_attr = TRUE)
expect_warning(setResource(params, resource_capacity = resource_capacity,
reset = TRUE),
"Because you set `reset = TRUE`, the")
Expand Down
28 changes: 17 additions & 11 deletions tests/testthat/test-steady.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local_edition(3)
# projectToSteady ----
test_that("projectToSteady() works", {
params <- NS_params
Expand Down Expand Up @@ -35,7 +36,7 @@ test_that("projectToSteady() works", {

# Check extinction
params@psi[5:6, ] <- 0
expect_warning(projectToSteady(params),
expect_warning(projectToSteady(params) |> suppressMessages(),
"Dab, Whiting are going extinct.")
})

Expand All @@ -49,36 +50,41 @@ test_that("steady works", {
"Increased no_w to 36")
params@species_params$gamma[2] <- 2000
params <- setSearchVolume(params)
p <- steady(params, t_per = 2)
expect_known_value(getRDD(p), "values/steady")
p <- steady(params, t_per = 2) |>
suppressMessages()
expect_snapshot_value(getRDD(p), style = "deparse")
# and works the same when returning sim
sim <- steady(params, t_per = 2, return_sim = TRUE)
expect_is(sim, "MizerSim")
expect_known_value(getRDD(sim@params), "values/steady")
sim <- steady(params, t_per = 2, return_sim = TRUE) |>
suppressMessages()
expect_s4_class(sim, "MizerSim")
expect_snapshot_value(getRDD(sim@params), style = "deparse")
})

test_that("steady() preserves erepro", {
test_that("steady() preserves reproduction function", {
params <- NS_params
params@rates_funcs$RDD <- "noRDD"
p2 <- steady(params, t_per = 1)
p2 <- steady(params, t_per = 1, t_max = 1) |> suppressMessages()
expect_equal(params@rates_funcs$RDD, "noRDD")
})
test_that("steady() preserves erepro", {
params <- NS_params
species_params(params)$R_max <- 1.01 * species_params(params)$R_max
p2 <- steady(params, t_per = 1, preserve = "erepro")
p2 <- steady(params, t_per = 1, preserve = "erepro") |>
suppressMessages()
expect_equal(p2@species_params$erepro, params@species_params$erepro)
})
test_that("steady() preserves reproduction level", {
params <- NS_params
species_params(params)$R_max <- 1.01 * species_params(params)$R_max
p2 <- steady(params, t_per = 1, preserve = "reproduction_level")
p2 <- steady(params, t_per = 1, preserve = "reproduction_level") |>
suppressMessages()
expect_equal(getReproductionLevel(p2), getReproductionLevel(params))
})
test_that("steady() preserves R_max", {
params <- NS_params
species_params(params)$erepro <- 1.01 * species_params(params)$erepro
expect_warning(p2 <- steady(params, t_per = 1, preserve = "R_max"),
expect_warning(p2 <- steady(params, t_per = 1, preserve = "R_max") |>
suppressMessages(),
"The following species require an unrealistic reproductive")
expect_equal(p2@species_params$R_max, params@species_params$R_max)
})
Expand Down

0 comments on commit 572037f

Please sign in to comment.