Commit e271c6e3 authored by bob's avatar bob
Browse files

Add basic testing framework

This allow the developer to:
- Take snapshot of data/processed/ directory
- Test if current excel workbook holding data and metadata correspond
to the last snapshot

Snapshots are stored as .Rdata files in scripts/tests/
The scripts/tests/ directory upon download by the student
parent beb669c9
......@@ -5,50 +5,51 @@
current_wd <- getwd()
current_files <- list.files(current_wd)
if(length(current_files) != 0){
if (length(current_files) != 0) {
message(paste0("Working directory is not empty. You might loose existing files.",
" \n",
"Are you sure you want to continue ?"))
valid_answer <- FALSE
while(valid_answer == FALSE) {
while (valid_answer == FALSE) {
answer <- readline()
valid_answer <- answer %in% c("Yes", "No")
if(!valid_answer) {
if (!valid_answer) {
message("Yes/No:")
}
}
if(answer == "No"){
if (answer == "No") {
stop("Please back up your files or set up the project in another directory.",
call. = FALSE)
}
}
## Download and unzip project
url_repo <- paste0("https://git.wageningenur.nl/pps/PPS_data_management/-/archive/",
"master/PPS_data_management-master.zip")
download.file(url = url_repo,
download.file(url = url_repo,
destfile = file.path(current_wd, "project.zip"))
unzip("project.zip")
## Copy project file to working directory
list_files <- list.files("./PPS_data_management-master/",
list_files <- list.files("./PPS_data_management-master/",
full.names = TRUE)
file.copy(from = list_files,
to = "./",
recursive = TRUE)
## Get rid of un necessary files
## Get rid of un necessary files
unlink(file.path(current_wd, "./PPS_data_management-master/"),
recursive = TRUE)
unlink(c("project.zip", "download_project.R", "README.md"))
unlink(c("project.zip", "download_project.R", "README.md",
"scripts/tests/"))
## Run project building script
source("./scripts/0_build_project.R")
#' Read metadata workbook from disk
#'
#' @param path_file A character sting, path to metadata workbook.
#' @return A list of data.frames corresponding to each sheet of the workbook.
read_metadata_wb <- function(path_file) {
sheet_names <- openxlsx::getSheetNames(path_file)
metadata_wb <- lapply(sheet_names, function(sheet) {
openxlsx::read.xlsx(xlsxFile = path_file, sheet = sheet)
})
names(metadata_wb) <- sheet_names
return(metadata_wb)
}
#' Remove dir path and extension
#'
#' @param path_file A character sting, path to metadata workbook.
#' @return A character sting, metadata workbook file name stripped of its extension.
get_metadata_wb_core_name <- function(path_file) {
sub("\\.xlsx", "", basename(path_file))
}
#' Save workbook content as Rdata file
save_metadata_wb_snapshot <- function(path_file) {
metadata_file_wb <- read_metadata_wb(path_file)
core_file_name <- get_metadata_wb_core_name(path_file)
save(metadata_file_wb,
file = file.path("scripts/tests/", paste0(core_file_name, ".RData"))
)
}
#' Take a snapshot of the whole data/processed/ directory
take_snapshot_processed_data <- function(path = "data/processed/") {
path_files <- list.files(path,
full.names = TRUE)
for (wb in path_files) {
save_metadata_wb_snapshot(wb)
message("Snapshot taken: ", paste0(basename(wb)))
}
}
#' Load workbook snapshot form disk
#'
#' @param path_corresponding_file A character sting, path to metadata workbook
#' corresponding to the snapshot.
load_snapshot <- function(path_corresponding_file) {
metadata_wb_core_name <- get_metadata_wb_core_name(path_corresponding_file)
pattern <- paste0("^", metadata_wb_core_name, "\\.RData$")
path_snapshot <- list.files(file.path("scripts/tests/"),
pattern = pattern,
full.names = TRUE)
# Loads metadata_file_wb within function env
load(path_snapshot)
return(metadata_file_wb)
}
#' Custom testthat expectation
#'
#' @param file A character string, name of the metadata workbook to check.
expect_macth_snapshot <- function(file) {
path_file <- file.path("data/processed/", file)
snapshot <- load_snapshot(path_file)
wb <- read_metadata_wb(path_file)
testthat::expect_identical(wb, snapshot)
}
source("scripts/tests/snapshot.R")
# take_snapshot_processed_data()
testthat::test_that("Metadata wb matching snapshots", {
expect_macth_snapshot("example_data_metadata.xlsx")
expect_macth_snapshot("Meststof proef WUR_metadata.xlsx")
})
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment