Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests for time range #177

Closed
wants to merge 1 commit into from
Closed
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
Binary file modified tests/testdata/test_nc_output.nc
Binary file not shown.
30 changes: 30 additions & 0 deletions tests/testthat/test-time-range-points.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#' @author Mitchell Manware
#' @description
#' unit test for model output is within desired temporal range
#' (2018 - 2022 inclusive).
#' assumes `test_nc_output.nc` is point data.
test_that("Output times are within temporal range", {
# 1. import model output
path_results <- "./testdata/test_nc_output.nc"
model_results <- terra::vect(path_results)
# 2. function
check_temporal_range <- function(
model_output = model_results,
start_range = "2018-01-01",
end_range = "2022-12-31"
) {
# change character inputs to dates
start_date <- lubridate::as_datetime(start_range)
end_date <- lubridate::as_datetime(end_range)
# create sequence of dates
range <- seq(start_date, end_date, 86400)
# assign date values to variable
dates <- lubridate::as_datetime(model_results$Date)
# check that dates are within range
checked <- as.vector(dates %in% range)
return(checked)
}
iswithin <- check_temporal_range()
# expect all elements == TRUE
expect_equal(any(iswithin), TRUE)
})
30 changes: 30 additions & 0 deletions tests/testthat/test-time-range-raster.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#' @author Mitchell Manware
#' @description
#' unit test for model output is within desired temporal range
#' (2018 - 2022 inclusive).
#' assumes `test_nc_output.nc` is raster data.
test_that("Output times are within temporal range", {
# 1. import model output
path_results <- "./testdata/test_nc_output.nc"
model_results <- terra::rast(path_results)
# 2. function
check_temporal_range <- function(
model_output = model_results,
start_range = "2018-01-01",
end_range = "2022-12-31"
) {
# change character inputs to dates
start_date <- lubridate::as_datetime(start_range)
end_date <- lubridate::as_datetime(end_range)
# create sequence of dates
range <- seq(start_date, end_date, 86400)
# assign date values to variable
dates <- lubridate::as_datetime(model_output@ptr$time)
# check that dates are within range
checked <- as.vector(dates %in% range)
return(checked)
}
iswithin <- check_temporal_range()
# expect all elements == TRUE
expect_equal(any(iswithin), TRUE)
})
Loading