Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ VignetteBuilder:
knitr
Config/roxygen2/version: 8.1.0
Config/testthat/edition: 3
Config/testthat/parallel: false
Config/testthat/parallel: true
Config/testthat/start-first: save_all_plots*, convert_output*
Encoding: UTF-8
Language: en-US
LazyData: true
4 changes: 2 additions & 2 deletions R/convert_output.R
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ convert_output <- function(
for (i in intersect(tolower(fleet_names), colnames(df3))) {
col_name <- glue::glue("exploitation_{i}")
df3 <- df3 |>
dplyr::rename(!!col_name := i)
dplyr::rename(!!col_name := dplyr::all_of(i))
}
}
}
Expand Down Expand Up @@ -488,7 +488,7 @@ convert_output <- function(
if (length(err_col_id) > 0) {
uncert_name <- stringr::str_extract(err_col_id, paste0(errors, collapse = "|"))
df3 <- df3 |>
dplyr::rename(!!uncert_name := err_col_id)
dplyr::rename(!!uncert_name := dplyr::all_of(err_col_id))
}
df4 <- df3 |>
tidyr::pivot_longer(
Expand Down
2 changes: 1 addition & 1 deletion R/table_total_catch.R
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ table_total_catch <- function(
}

catch <- catch |>
dplyr::select(-cols_to_remove)
dplyr::select(-dplyr::all_of(cols_to_remove))

if ("uncertainty" %in% colnames(catch)) {
catch <- catch |>
Expand Down
32 changes: 32 additions & 0 deletions tests/testthat/helper-test_functions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#' Check for the existence and remove .rda files from test environment
#'
#' Checks for the existence of `dir` and `filename` as well as the
#' existence of "captions_alt_text.csv" and "key_quantities.csv".
#' Files are removed upon completion of checks.
#'
#' For testing only.
#'
#' @param filename String. filename of figure/table .rda file
#' @param dir String. directory where filename exists
#' @param figs_or_tables String. whether check for a "figures" or "tables" internal directory
#'
#' DEFAULT: "figures"
#'
check_and_remove_tmp_files <- function(filename, dir=fs::path(getwd()), figs_or_tables="figures"){
# expect that both figures dir and the abundance_at_age_figure.rda file exist
expect_true(dir.exists(dir)) # this check needs to exist in case save dir isnt getwd()
expect_true(dir.exists(fs::path(dir, figs_or_tables)))
expect_true(file.exists(fs::path(dir, figs_or_tables, filename)))

# erase temporary testing files
captions_file <- fs::path(dir, "captions_alt_text.csv")
keyquant_file <- fs::path(dir, "key_quantities.csv")

if(fs::file_exists(captions_file))
fs::file_delete(captions_file)

if(fs::file_exists(keyquant_file))
fs::file_delete(keyquant_file)

unlink(fs::path(dir, figs_or_tables), recursive = T)
}
14 changes: 12 additions & 2 deletions tests/testthat/test-convert_output.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,21 @@ test_that("convert_output works for SS3", {
recursive = FALSE
)

model_names <- c(
"Hake_2018",
"platoons",
"Empirical_wtatage_Age_Selex",
"three_area_nomove",
"BigSkate_2019"
)

models_to_test <- all_models[grepl(paste0(model_names, collapse="|"), all_models)]

# Loop through each model directory and check convert_output function
for (i in seq_along(all_models)) {
for (i in seq_along(models_to_test)) {
# Ensure no errors occur while converting SS3 output
expect_no_error(result <- convert_output(
file = file.path(all_models[i], "Report.sso")
file = file.path(models_to_test[i], "Report.sso")
))

# Check that the result has exactly 35 columns
Expand Down
38 changes: 21 additions & 17 deletions tests/testthat/test-plot_abundance_at_age.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
test_that("plot_abundance_at_age generates plots without errors", {
# expect error-free plot with minimal arguments
expect_no_error(
plot_abundance_at_age(stockplotr::example_data)
plot_abundance_at_age(
stockplotr::example_data,
interactive = FALSE
)
)

# expect error-free plot with many arguments
Expand All @@ -11,6 +14,7 @@ test_that("plot_abundance_at_age generates plots without errors", {
facet = "area",
unit_label = "fish",
scale_amount = 1000,
interactive = FALSE,
make_rda = FALSE,
figures_dir = getwd()
)
Expand All @@ -22,30 +26,26 @@ test_that("plot_abundance_at_age generates plots without errors", {
stockplotr::example_data,
unit_label = "fish",
scale_amount = 1,
interactive = FALSE,
make_rda = FALSE,
figures_dir = getwd()
),
"gg"
)
})

test_that("rda file made when indicated", {
# export rda
plot_abundance_at_age(
stockplotr::example_data,
figures_dir = getwd(),
make_rda = TRUE
)

# expect that both figures dir and the abundance_at_age_figure.rda file exist
expect_true(dir.exists(fs::path(getwd(), "figures")))
expect_true(file.exists(fs::path(getwd(), "figures", "abundance_at_age_figure.rda")))
# test_that("rda file made when indicated", {
# # export rda
# plot_abundance_at_age(
# stockplotr::example_data,
# figures_dir = getwd(),
# interactive = FALSE,
# make_rda = TRUE
# )

# erase temporary testing files
file.remove(fs::path(getwd(), "captions_alt_text.csv"))
file.remove(fs::path(getwd(), "key_quantities.csv"))
unlink(fs::path(getwd(), "figures"), recursive = T)
})
# # expect that both figures dir and the abundance_at_age_figure.rda file exist
# check_and_remove_tmp_files("abundance_at_age_figure.rda")
# })

test_that("plot_abundance_at_age generates error with scaling <1", {
# expect error
Expand All @@ -54,6 +54,7 @@ test_that("plot_abundance_at_age generates error with scaling <1", {
stockplotr::example_data,
unit_label = "fish",
scale_amount = 0.1,
interactive = FALSE,
make_rda = FALSE,
figures_dir = getwd()
)
Expand Down Expand Up @@ -131,6 +132,7 @@ test_that("plot_abundance_at_age generates error when abundance label is not fou
sample_data,
unit_label = "fish",
scale_amount = 1,
interactive = FALSE,
make_rda = FALSE,
figures_dir = getwd()
)
Expand All @@ -143,6 +145,7 @@ test_that("plot is made proportional and does not contain a legend", {
unit_label = "fish",
proportional = TRUE,
scale_amount = 1,
interactive = FALSE,
make_rda = FALSE,
figures_dir = getwd()
)
Expand All @@ -155,6 +158,7 @@ test_that("plot contains a legend", {
proportional = FALSE,
unit_label = "fish",
scale_amount = 1,
interactive = FALSE,
make_rda = FALSE,
figures_dir = getwd()
)
Expand Down
41 changes: 21 additions & 20 deletions tests/testthat/test-plot_biomass.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
test_that("plot_biomass generates plots without errors", {
# expect error-free plot with minimal arguments
expect_no_error(
plot_biomass(stockplotr::example_data)
plot_biomass(
stockplotr::example_data,
interactive = FALSE
)
)

# TODO: Update test
Expand All @@ -42,10 +45,11 @@ test_that("plot_biomass generates plots without errors", {
expect_no_error(
plot_biomass(
stockplotr::example_data,
ref_point = c("target" = 18000),
ref_line = c("target" = 18000),
unit_label = "metric tons",
scale_amount = 1,
relative = FALSE,
interactive = FALSE,
make_rda = FALSE,
figures_dir = getwd(),
module = "TIME_SERIES"
Expand All @@ -58,7 +62,8 @@ test_that("plot_biomass generates plots without errors", {
stockplotr::example_data,
unit_label = "mt",
# scale_amount = 10,
relative = TRUE
relative = TRUE,
interactive = FALSE
)
)

Expand All @@ -68,6 +73,7 @@ test_that("plot_biomass generates plots without errors", {
stockplotr::example_data,
unit_label = "metric tons",
scale_amount = 1,
interactive = FALSE,
make_rda = FALSE,
figures_dir = getwd(),
module = "TIME_SERIES"
Expand All @@ -76,21 +82,16 @@ test_that("plot_biomass generates plots without errors", {
)
})

test_that("rda file made when indicated", {
# export rda
plot_biomass(
stockplotr::example_data,
figures_dir = getwd(),
make_rda = TRUE,
module = "TIME_SERIES"
)

# expect that both figures dir and the biomass_figure.rda file exist
expect_true(dir.exists(fs::path(getwd(), "figures")))
expect_true(file.exists(fs::path(getwd(), "figures", "biomass_figure.rda")))
# test_that("rda file made when indicated", {
# # export rda
# plot_biomass(
# stockplotr::example_data,
# figures_dir = getwd(),
# interactive = FALSE,
# make_rda = TRUE,
# module = "TIME_SERIES"
# )

# erase temporary testing files
file.remove(fs::path(getwd(), "captions_alt_text.csv"))
file.remove(fs::path(getwd(), "key_quantities.csv"))
unlink(fs::path(getwd(), "figures"), recursive = T)
})
# # expect that both figures dir and the biomass_figure.rda file exist
# check_and_remove_tmp_files("biomass_figure.rda")
# })
40 changes: 21 additions & 19 deletions tests/testthat/test-plot_biomass_at_age.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
test_that("plot_biomass_at_age generates plots without errors", {
# expect error-free plot with minimal arguments
expect_no_error(
plot_biomass_at_age(stockplotr::example_data,
module = "BIOMASS_AT_AGE"
plot_biomass_at_age(
stockplotr::example_data,
module = "BIOMASS_AT_AGE",
interactive = FALSE
)
)

Expand All @@ -14,6 +16,7 @@ test_that("plot_biomass_at_age generates plots without errors", {
proportional = FALSE,
scale_amount = 10,
module = "BIOMASS_AT_AGE",
interactive = FALSE,
make_rda = FALSE,
figures_dir = getwd()
)
Expand All @@ -26,31 +29,27 @@ test_that("plot_biomass_at_age generates plots without errors", {
unit_label = "metric tons",
scale_amount = 1,
module = "BIOMASS_AT_AGE",
interactive = FALSE,
make_rda = FALSE,
figures_dir = getwd()
),
"gg"
)
})

test_that("rda file made when indicated", {
# export rda
plot_biomass_at_age(
stockplotr::example_data,
module = "BIOMASS_AT_AGE",
figures_dir = getwd(),
make_rda = TRUE
)

# expect that both figures dir and the biomass_at_age_figure.rda file exist
expect_true(dir.exists(fs::path(getwd(), "figures")))
expect_true(file.exists(fs::path(getwd(), "figures", "biomass_at_age_figure.rda")))
# test_that("rda file made when indicated", {
# # export rda
# plot_biomass_at_age(
# stockplotr::example_data,
# module = "BIOMASS_AT_AGE",
# interactive = FALSE,
# figures_dir = getwd(),
# make_rda = TRUE
# )

# erase temporary testing files
file.remove(fs::path(getwd(), "captions_alt_text.csv"))
file.remove(fs::path(getwd(), "key_quantities.csv"))
unlink(fs::path(getwd(), "figures"), recursive = T)
})
# # expect that both figures dir and the biomass_at_age_figure.rda file exist
# check_and_remove_tmp_files("biomass_at_age_figure.rda")
# })

test_that("plot_biomass_at_age generates error when biomass label is not found", {
# generate test data set
Expand Down Expand Up @@ -123,6 +122,7 @@ test_that("plot_biomass_at_age generates error when biomass label is not found",
sample_data,
unit_label = "fish",
scale_amount = 1,
interactive = FALSE,
make_rda = FALSE,
figures_dir = getwd()
)
Expand All @@ -136,6 +136,7 @@ test_that("plot is made proportional and does not contain a legend", {
unit_label = "fish",
proportional = TRUE,
scale_amount = 1,
interactive = FALSE,
make_rda = FALSE,
figures_dir = getwd()
)
Expand All @@ -149,6 +150,7 @@ test_that("plot contains a legend", {
proportional = FALSE,
unit_label = "fish",
scale_amount = 1,
interactive = FALSE,
make_rda = FALSE,
figures_dir = getwd()
)
Expand Down
Loading
Loading