Skip to content

Commit

Permalink
make tests for plot_spawning_biomass
Browse files Browse the repository at this point in the history
  • Loading branch information
sbreitbart-NOAA committed Dec 31, 2024
1 parent 65d6926 commit a3fba23
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions tests/testthat/test-plot_spawning_biomass.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
test_that("plot_spawning_biomass generates plots without errors", {

# read in sample dataset
dat <- utils::read.csv(
system.file("tests", "testthat", "fixtures", "sample_data", "petrale_sole-after_2020.csv", package = "satf")
)

# expect error-free plot with minimal arguments
expect_no_error(
satf::plot_spawning_biomass(dat)
)

# expect plot with warnings if ref_point not indicated
expect_warning(
satf::plot_spawning_biomass(dat)
)

# expect error-free plot with many arguments
expect_no_error(
satf::plot_spawning_biomass(
dat,
unit_label = "metric tons",
scale_amount = 1,
ref_line = "target",
end_year = 2030
)
)

# expect error-free plot when setting relative to T
expect_no_error(
satf::plot_spawning_biomass(
dat,
unit_label = "metric tons",
scale_amount = 1,
ref_point = 100,
end_year = 2030,
relative = TRUE
)
)

# expect ggplot object is returned
expect_s3_class(
satf::plot_spawning_biomass(
dat,
unit_label = "metric tons",
scale_amount = 1,
ref_point = 100,
end_year = 2030,
relative = TRUE
),
"gg"
)

})

test_that("plot_spawning_biomass plots contain reference point when indicated", {

# read in sample dataset
dat <- utils::read.csv(
system.file("tests", "testthat", "fixtures", "sample_data", "petrale_sole-after_2020.csv", package = "satf")
)


# expect plot with a reference point (horizontal line) contains 4 layers while
# plot w/o ref pt contains only 3 layers

# make sb plot with reference point
sb_ref <- satf::plot_spawning_biomass(dat,
ref_point = 18000)
# extract number of layers (should be 4)
sb_ref_layers <- sb_ref[["layers"]] |>
length()

# make sb plot without reference point
sb_no_ref <- satf::plot_spawning_biomass(dat)
# extract number of layers (should be 3)
sb_no_ref_layers <- sb_no_ref[["layers"]] |>
length()

expect_equal((sb_ref_layers - 1),
sb_no_ref_layers)


})

# test_that("rda file made when indicated",{
#
# # read in sample dataset
# dat <- utils::read.csv(
# system.file("tests", "testthat", "fixtures", "sample_data", "petrale_sole-after_2020.csv", package = "satf")
# )
#
# # export rda
# plot_spawning_biomass(
# dat,
# rda_dir = here::here(),
# make_rda = TRUE,
# end_year = 2023
# )
#
# # expect that both rda_files dir and the biomass_figure.rda file exist
# expect_true(dir.exists(file.path(here::here(), "rda_files")))
# expect_true(file.exists(file.path(here::here(), "rda_files", "biomass_figure.rda")))
#
#
# })

0 comments on commit a3fba23

Please sign in to comment.