Skip to content

Commit

Permalink
test: add multiple test datasets for integration tests
Browse files Browse the repository at this point in the history
* use test fixtures to properly generate multiple combinations of available data
* add integration tests to validate handling of missing vlaues in the datasets
  • Loading branch information
Bai-Li-NOAA committed Feb 3, 2025
1 parent 586ab1c commit 6b025eb
Show file tree
Hide file tree
Showing 16 changed files with 226 additions and 160 deletions.
9 changes: 9 additions & 0 deletions data-raw/data1.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#' * Timing:
#' * Fishery is assumed to operate over the entire year
#' * Survey occurs instantaneously at the start of the year
#' * Note: If you encounter warning messages such as `cannot open
#' the connection: warning messages from top-level task callback 'vsc.workspace'`
#' while running the code in VS Code, try calling
#' `removeTaskCallback("vsc.workspace")` before executing the code.
#'
#' @author Kathryn L. Doering and Kelli F. Johnson
#'
Expand Down Expand Up @@ -385,6 +389,11 @@ length_comp_data <- data.frame(
)
)

# Save individual dataframes to a single file for {testthat} integration tests
save(landings_data, index_data, age_data, weightatage_data, length_comp_data, length_age_data,
file = testthat::test_path("fixtures", "integration_test_data_components.RData")
)

# Add the conversion matrix and length composition data to dataframe
data1 <- rbind(data1, length_comp_data, length_age_data)

Expand Down
Binary file modified data/data1.rda
Binary file not shown.
Binary file added tests/testthat/fixtures/data_age_comp.RDS
Binary file not shown.
Binary file added tests/testthat/fixtures/data_age_comp_na.RDS
Binary file not shown.
Binary file not shown.
Binary file added tests/testthat/fixtures/data_length_comp.RDS
Binary file not shown.
Binary file added tests/testthat/fixtures/data_length_comp_na.RDS
Binary file not shown.
47 changes: 47 additions & 0 deletions tests/testthat/fixtures/generate_test_datasets.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# This R script generates multiple test datasets for use in tests located in tests/testthat.
# For example, it creates datasets containing only age composition, only length composition data,
# or data with missing values.
# Note: Run this script only after ensuring that the file tests/testthat/fixtures/integration_test_data_components.RData exists.
# The integration_test_data_components.RData is generated after running the script R/data1.R.

# Load required integration test data components
load(test_path("fixtures", "integration_test_data_components.RData"))

# Generate dataset with only age composition data
# TODO: Check if we need to create a new "length" column with NAs
data_age_comp_raw <- rbind(landings_data, index_data, age_data, weightatage_data)
data_age_comp <- FIMS::FIMSFrame(data_age_comp_raw)
saveRDS(data_age_comp, file = testthat::test_path("fixtures", "data_age_comp.RDS"))

# Generate dataset with only length composition data
data_length_comp_raw <- rbind(landings_data, index_data, weightatage_data) |>
dplyr::mutate(
length = NA,
.after = "age"
) |>
rbind(length_comp_data, length_age_data)
data_length_comp <- FIMS::FIMSFrame(data_length_comp_raw)
saveRDS(data_length_comp, file = testthat::test_path("fixtures", "data_length_comp.RDS"))

# Generate dataset with missing values in age composition for fleet1
na_index <- as.Date("2-01-01")
data_age_comp_na <- data_age_comp_raw |>
dplyr::filter(!(name == "fleet1" & type == "age" & datestart == na_index)) |>
FIMS::FIMSFrame()
saveRDS(data_age_comp_na, file = testthat::test_path("fixtures", "data_age_comp_na.RDS"))

# Generate dataset with missing values in length composition, age-to-length-conversion, and index for survey1
data_length_comp_na <- data_length_comp_raw |>
dplyr::filter(!(name == "survey1" & type %in% c("index", "length", "age-to-length-conversion") & datestart == na_index)) |>
FIMS::FIMSFrame()
saveRDS(data_length_comp_na, file = testthat::test_path("fixtures", "data_length_comp_na.RDS"))

# Generate dataset with missing values in age composition, length composition for fleets
age_na_index <- as.Date("2-01-01")
length_na_index <- as.Date("12-01-01")
data_age_length_comp_raw <- rbind(landings_data, index_data, age_data, weightatage_data)
data_age_length_comp_na <- data_age_length_comp_raw |>
dplyr::filter(!(name == "survey1" & type %in% c("age") & datestart == age_na_index)) |>
dplyr::filter(!(name == "fleet1" & type %in% c("length", "age-to-length-conversion") & datestart == length_na_index)) |>
FIMS::FIMSFrame()
saveRDS(data_age_length_comp_na, file = testthat::test_path("fixtures", "data_age_length_comp_na.RDS"))
Binary file modified tests/testthat/fixtures/integration_test_data.RData
Binary file not shown.
Binary file not shown.
3 changes: 1 addition & 2 deletions tests/testthat/test-create_default_parameters.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
data("data1")
data <- FIMSFrame(data1)
data <- FIMS::FIMSFrame(data1)

fleet1 <- survey1 <- list(
selectivity = list(form = "LogisticSelectivity"),
Expand Down
3 changes: 1 addition & 2 deletions tests/testthat/test-fimsframe.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# tests for input objects
data("data1", package = "FIMS")
fims_frame <- FIMSFrame(data1)
fims_frame <- FIMS::FIMSFrame(data1)

test_that("Can create the S4 FIMSFrame classes", {
expect_s4_class(fims_frame, "FIMSFrame")
Expand Down
3 changes: 1 addition & 2 deletions tests/testthat/test-initialize_modules.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
data("data1")
data <- FIMSFrame(data1)
data <- FIMS::FIMSFrame(data1)

fleet1 <- survey1 <- list(
selectivity = list(form = "LogisticSelectivity"),
Expand Down
Loading

0 comments on commit 6b025eb

Please sign in to comment.