# 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")