SmartCane/r_app/tests/test_date_functions.R
Timon 07aee7bed1 –[200~Improve CI report visualization and migrate to terra package
- Replace raster package with terra throughout the codebase
- Update map visualizations with better layout and legends
- Add descriptive headers to report sections
- Improve map legend positioning and sizing
- Enhance error handling for missing data
- Remove redundant legends in field-specific visualizations
- Optimize figure dimensions to prevent page overflow
- Expand documentation of CI index and report components
- Update package dependencies in packages.
2025-04-22 20:55:02 +02:00

68 lines
2.3 KiB
R

# test_date_functions.R
#
# Tests for date-related functions in ci_extraction_utils.R
#
# Load the test framework
source("tests/test_framework.R")
# Set up test environment
env <- setup_test_env()
# Load the functions to test
source("../ci_extraction_utils.R")
# Test the date_list function
test_that("date_list creates correct date sequences", {
# Test with a specific date and offset
dates <- date_list(as.Date("2023-01-15"), 7)
# Check the structure
expect_type(dates, "list")
expect_equal(names(dates), c("week", "year", "days_filter", "start_date", "end_date"))
# Check the values
expect_equal(dates$week, lubridate::week(as.Date("2023-01-09")))
expect_equal(dates$year, 2023)
expect_equal(dates$start_date, as.Date("2023-01-09"))
expect_equal(dates$end_date, as.Date("2023-01-15"))
expect_equal(length(dates$days_filter), 7)
expect_equal(dates$days_filter[1], "2023-01-09")
expect_equal(dates$days_filter[7], "2023-01-15")
# Test with a different offset
dates_short <- date_list(as.Date("2023-01-15"), 3)
expect_equal(length(dates_short$days_filter), 3)
expect_equal(dates_short$days_filter, c("2023-01-13", "2023-01-14", "2023-01-15"))
# Test with string date
dates_string <- date_list("2023-01-15", 5)
expect_equal(dates_string$days_filter,
c("2023-01-11", "2023-01-12", "2023-01-13", "2023-01-14", "2023-01-15"))
# Test error handling
expect_error(date_list("invalid-date", 7),
"Invalid end_date provided")
expect_error(date_list("2023-01-15", -1),
"Invalid offset provided")
})
# Test the date_extract function
test_that("date_extract correctly extracts dates from file paths", {
# Test with various file path formats
expect_equal(date_extract("/some/path/2023-01-15_image.tif"), "2023-01-15")
expect_equal(date_extract("path/to/planet_2023-01-15.tif"), "2023-01-15")
expect_equal(date_extract("c:\\path\\with\\windows\\2023-01-15_file.tif"), "2023-01-15")
expect_equal(date_extract("2023-01-15.tif"), "2023-01-15")
expect_equal(date_extract("prefix-2023-01-15-suffix.tif"), "2023-01-15")
# Test with invalid file paths
expect_warning(result <- date_extract("no-date-here.tif"), "Could not extract date")
expect_true(is.na(result))
})
# Clean up
teardown_test_env()
# Print success message
cat("Date function tests completed successfully\n")