From 4902f470aaebf6ef82da6e8084cf4a4386526710 Mon Sep 17 00:00:00 2001 From: henrykironde Date: Sat, 16 Dec 2023 02:42:35 -0500 Subject: [PATCH] Add tests for zip and unszip --- .gitignore | 1 + tests/testthat/test-23-fill_dir.R | 49 ++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 4c519a7e5..d412504b5 100644 --- a/.gitignore +++ b/.gitignore @@ -20,3 +20,4 @@ testing_dir/* inst/app/models.knit.md *.DS_Store* .Rproj.user +*Rplots.pdf* \ No newline at end of file diff --git a/tests/testthat/test-23-fill_dir.R b/tests/testthat/test-23-fill_dir.R index 6a68c4f44..d5b8a1c41 100644 --- a/tests/testthat/test-23-fill_dir.R +++ b/tests/testthat/test-23-fill_dir.R @@ -74,4 +74,51 @@ test_that(desc = "fill_models adds the models control list and any model scripts expect_equal(sort(files), sort(checks)) -}) \ No newline at end of file +}) + + +zip_name <- c("forecast_id_2019-11-15.zip") +eval_zip <- c("forecasts_evaluations.zip") +resources_forecast <- file.path(main3, "resources/portal-forecasts/forecasts") +expected <- c( + "forecast_id_1.01_forecast_table.csv", + "forecast_id_1.01_metadata.yaml", + "forecast_id_1.01_model_forecast.json", + "forecasts_evaluations.csv", + "forecasts_metadata.csv" +) +monk_forecast <- file.path(tempdir(), "forecasts/") +#Create the monk_forecast directory if it doesn't exist +if (!file.exists(monk_forecast)) { + dir.create(monk_forecast, recursive = TRUE) +} +#Copy files from resources_forecast to monk_forecast +file.copy(file.path(resources_forecast, expected), monk_forecast, overwrite = TRUE) +#Check if files are successfully copied +copied_files <- list.files(monk_forecast) +if (all(copied_files %in% expected)) { + print("Files copied successfully.") +} else { + warning("Failed to copy files.") +} + + +test_that("Zip All Forecast Files", { + skip_on_cran() + zip_unzip(type = "zip", forecast_path = monk_forecast) + expect_true(file.exists(file.path(monk_forecast, zip_name)), + info = paste("File", zip_name, "does not exist in the directory.")) + expect_true(file.exists(file.path(monk_forecast, eval_zip)), + info = paste("File", zip_name, "does not exist in the directory.")) +}) + + +test_that("Unzip All Forecast Files", { + skip_on_cran() + zip_unzip(type = "unzip", forecast_path = monk_forecast) + file_existence <- sapply(expected, function(file_name) { + full_path <- file.path(resources_forecast, file_name) + file.exists(full_path) + }) + expect_true(all(file_existence), info = "Not all expected files exist in the directory.") +})