SmartCane/r_app/tests/test_framework.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

120 lines
3.5 KiB
R

# test_framework.R
#
# TEST FRAMEWORK FOR SMARTCANE
# ===========================
# This script provides a simple testing framework for the SmartCane project.
# It includes utilities for setting up test environments and running tests.
#
# Install required packages if not available
if (!require("testthat", quietly = TRUE)) {
install.packages("testthat", repos = "https://cran.rstudio.com/")
}
library(testthat)
# Define paths for testing
test_root <- file.path(normalizePath(".."), "tests")
test_data_dir <- file.path(test_root, "test_data")
# Create test directories if they don't exist
dir.create(test_data_dir, recursive = TRUE, showWarnings = FALSE)
# Set up a test environment with all necessary data
setup_test_env <- function() {
# Add working directory to the path
.libPaths(c(.libPaths(), normalizePath("..")))
# Source required files with minimal dependencies
tryCatch({
source(file.path(normalizePath(".."), "packages.R"))
skip_package_loading <- TRUE
# Load minimal dependencies for tests
required_packages <- c("lubridate", "stringr", "purrr", "dplyr", "testthat")
for (pkg in required_packages) {
if (!require(pkg, character.only = TRUE, quietly = TRUE)) {
warning(paste("Package", pkg, "not available, some tests may fail"))
}
}
}, error = function(e) {
warning("Error loading dependencies: ", e$message)
})
# Set up test logging
assign("log_message", function(message, level = "INFO") {
cat(paste0("[", level, "] ", message, "\n"))
}, envir = .GlobalEnv)
# Create a mock project structure
test_project <- list(
project_dir = "test_project",
data_dir = test_data_dir,
daily_CI_vals_dir = file.path(test_data_dir, "extracted_ci", "daily_vals"),
cumulative_CI_vals_dir = file.path(test_data_dir, "extracted_ci", "cumulative_vals"),
merged_final = file.path(test_data_dir, "merged_final"),
daily_vrt = file.path(test_data_dir, "daily_vrt")
)
# Create the directories
for (dir in test_project) {
if (is.character(dir)) {
dir.create(dir, recursive = TRUE, showWarnings = FALSE)
}
}
return(test_project)
}
# Clean up test environment
teardown_test_env <- function() {
# Clean up only test-created files if needed
# We'll leave the main directories for inspection
}
# Run all tests in a directory
run_tests <- function(pattern = "^test_.+\\.R$") {
test_files <- list.files(
path = test_root,
pattern = pattern,
full.names = TRUE
)
# Exclude this file
test_files <- test_files[!grepl("test_framework\\.R$", test_files)]
if (length(test_files) == 0) {
cat("No test files found matching pattern:", pattern, "\n")
return(FALSE)
}
cat("Found", length(test_files), "test files:\n")
cat(paste(" -", basename(test_files)), sep = "\n")
cat("\n")
# Run each test file
results <- lapply(test_files, function(file) {
cat("Running tests in:", basename(file), "\n")
tryCatch({
source(file, local = TRUE)
cat("✓ Tests completed\n\n")
TRUE
}, error = function(e) {
cat("✗ Error:", e$message, "\n\n")
FALSE
})
})
# Summary
success_count <- sum(unlist(results))
cat("\nTest Summary:", success_count, "of", length(test_files),
"test files completed successfully\n")
return(all(unlist(results)))
}
# If this script is run directly, run all tests
if (!interactive() && (basename(sys.frame(1)$ofile) == "test_framework.R")) {
setup_test_env()
run_tests()
teardown_test_env()
}