-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from EvolEcolGroup/file_testing
File testing
- Loading branch information
Showing
4 changed files
with
76 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,26 @@ | ||
# verify that all the variables in the tables are actually found in the files | ||
# this requires all data to have been downloaded | ||
full_meta <- read.csv("./inst/rawdata_scripts/data_files/variable_table.csv") | ||
library(pastclim) | ||
full_meta <- pastclim:::dataset_list_included | ||
in_dir <- get_data_path() | ||
in_dir <- "~/project_temp/past_climate/new_meta" | ||
problem_rows <- vector() | ||
for (i in 1:nrow(full_meta)){ | ||
pastclim::download_dataset(dataset = as.character(full_meta$dataset[i]), | ||
bio_variables = full_meta$variable[i]) | ||
nc_in <- ncdf4::nc_open(file.path(in_dir, full_meta$file_name[i])) | ||
if (!full_meta$ncvar[i] %in% names(nc_in$var)){ | ||
ncdf4::nc_close(nc_in) | ||
stop("problem with ",full_meta$ncvar[i]," in ", full_meta$file_name[i]) | ||
message("problem with ",full_meta$ncvar[i]," in ", full_meta$file_name[i],"\n") | ||
stop("we had a problem") | ||
problem_rows[i]<-TRUE | ||
} else { | ||
problem_rows[i]<-FALSE | ||
} | ||
ncdf4::nc_close(nc_in) | ||
} | ||
|
||
if (any(problem_rows)){ | ||
which(problem_rows) | ||
} else { | ||
cat("all files are fine") | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
## This is a resource intensive test. It downloads all files in the dataset_list | ||
## and then validates them. It is only run if the appropriate environment | ||
## variable is set, and thus skipped most of the time | ||
## To set the environment variable, use: | ||
## Sys.setenv(PASTCLIM_TEST = "download_full") | ||
## remember to unset it once you are done | ||
## Sys.unsetenv("PASTCLIM_TEST") | ||
|
||
|
||
# set up data path for this test | ||
data_path <- file.path(tempdir(),"pastclim_data") | ||
unlink(data_path, recursive = TRUE) # it should not exist, but remove it just in case | ||
# set data path | ||
set_data_path(path_to_nc = data_path, | ||
ask = FALSE, | ||
write_config = FALSE, | ||
copy_example = TRUE) | ||
################################################################################ | ||
test_that("download and validate all files", { | ||
skip_if(Sys.getenv("PASTCLIM_TEST")!="download_full") | ||
# download all files for each dataset | ||
all_datasets <- get_available_datasets() | ||
all_datasets <- all_datasets[!all_datasets %in% "Example"] | ||
for (i_dataset in all_datasets){ | ||
expect_true(download_dataset(dataset = i_dataset)) | ||
} | ||
# now check that the files we downloaded are valid | ||
for (i_file in list.files(get_data_path())){ | ||
expect_true(validate_nc(i_file)) | ||
} | ||
# check that the variables in the table are found in the respective files | ||
meta_table <- getOption("pastclim.dataset_list") | ||
for (i_row in 1:nrow(meta_table)){ | ||
nc_in <- ncdf4::nc_open(file.path(in_dir, meta_table$file_name[i])) | ||
# check below if !! works to unquote the expression | ||
expect_true(!!meta_table$ncvar[i] %in% names(nc_in$var)) | ||
ncdf4::nc_close(nc_in) | ||
} | ||
# for each dataset, check that all variables cover the same extent and have | ||
# the same missing values | ||
} | ||
) | ||
|
||
################################################################################ | ||
# clean up for the next test | ||
unlink(data_path, recursive = TRUE) |