Skip to content

Commit

Permalink
#fixes 211 can import foldered data from downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
rix133 committed Nov 4, 2024
1 parent 93a477c commit 78c6a42
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 2 deletions.
18 changes: 18 additions & 0 deletions R/importRDBESDataZIP.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,24 @@ importRDBESDataZIP <- function(filenames,

# the files are not used currently but can be if we want to
files <- unique(unlist(sapply(filenames, unzipFile, tmp)))

dirs <- list.dirs(tmp, full.names = FALSE, recursive = FALSE)
if(length(dirs) > 0) {
hdirs <- dirs[grepl("H[0-9]+", dirs)]
if(length(hdirs) > 1) {
stop("You cannot import a mix of different hierarchies in one 'zip' ",
"input. To import multiple tables unzip all files and import as ",
"a folder of 'csv' files.")
}
#remove directory structure
for(d in dirs){
files <- list.files(file.path(tmp, d), full.names = FALSE)
file.rename(file.path(tmp, d, files), file.path(tmp, files))
}


}

res <- importRDBESDataCSV(tmp,
castToCorrectDataTypes = castToCorrectDataTypes)
unlink(tmp, recursive = T)
Expand Down
83 changes: 81 additions & 2 deletions data-raw/create_H1_ExampleData.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,92 @@
## Load the csv files from our data-raw
ddir <- "./data-raw/exampleData/"
outputDir <- "./tests/testthat/h1_v_1_19_18/"
outputDir <- "./tests/testthat/h1_v_1_19_26/"

H1_2023_10_16 <- createRDBESDataObject(paste0(ddir, "H1_2023_10_16.zip"),
castToCorrectDataTypes = TRUE)
validateRDBESDataObject(H1_2023_10_16)

saveRDS(H1_2023_10_16, file=paste0(outputDir,"H1_2023_10_16.rds"))
#copy the zip file to tests
file.copy(paste0(ddir, "H1_2023_10_16.zip"), paste0(outputDir, "H1_2023_10_16.zip"), overwrite = TRUE)

#saveRDS(H1_2023_10_16, file=paste0(outputDir,"H1_2023_10_16.rds"))


H1Example <- H1_2023_10_16
# Save the data
usethis::use_data(H1Example, overwrite = TRUE)

# make a different zip file according to the changed download format

# Generates random number for the temp import folder name
randInt <- paste0(sample(1:100, 3), collapse = "")
tmp <- paste0(tempdir(), "/downloadImport", randInt)
dir.create(tmp)
all_unzipped <- c()
unzipFile <- function(x, tmp) {

if (!file.exists(x)) {
return()
}

if (RDBEScore:::is.zip(x)) {
unzipped <- utils::unzip(x, exdir= tmp)
unzipped <- basename(unzipped)
unzipped <- unzipped[grep("*.csv", unzipped)]
intersected <- intersect(unzipped, all_unzipped)
if(length(intersected) != 0) {
warning(paste0("Duplicate unzipped files detected:\n", paste0("\t", intersected, "\n", collapse="\n")))
}
all_unzipped <<- c(all_unzipped, unzipped)
return(unzipped)
}

}

# the files are not used currently but can be if we want to
files <- unzipFile(paste0(ddir, "H1_2023_10_16.zip"), tmp)

#make folders in tmp
dir.create(paste0(tmp, "/H1"))
dir.create(paste0(tmp, "/HVD"))
dir.create(paste0(tmp, "/HSL"))
#put files to the folders
hsl_file <- "SpeciesList.csv"
hvd_file <- "VesselDetails.csv"
file.copy(paste0(tmp, "/", hsl_file), paste0(tmp, "/HSL"), overwrite = TRUE)
file.copy(paste0(tmp, "/", hvd_file), paste0(tmp, "/HVD"), overwrite = TRUE)
#all others files
h1_files <- setdiff(files, c(hsl_file, hvd_file))
for (file in h1_files) {
file.copy(paste0(tmp, "/", file), paste0(tmp, "/H1"), overwrite = TRUE)
}
#remove all csv files from the tmp folder
unlink(paste0(tmp, "/", files), recursive = F)



#zip the temp so it has the same format as the download
# Zip the 'H1', 'HVD', and 'HSL' folders without including the 'tmp' directory
zip(paste0(outputDir, "H1_2023_10_16_fd.zip"),
files = files_to_zip, # Relative paths inside tmp
flags = "-r",
extras = paste0("-j ", tmp))

#define the name and path of the final ZIP file
final_zip <- paste0(getwd(),file.path(outputDir, "H1_2023_10_16_fd.zip"))

zip::zipr(zipfile = final_zip,
files = c("H1", "HVD", "HSL", "Disclaimer.txt"),
root = tmp)

#make a new Directory in tmp claled H2 and zip it
final_zip <- paste0(getwd(),file.path(outputDir, "H1_H2_2023_10_16_fd.zip"))
dir.create(paste0(tmp, "/H2"))
zip::zipr(zipfile = final_zip,
files = c("H1","H2", "HVD", "HSL", "Disclaimer.txt"),
root = tmp)


# remove the temp folder
unlink(tmp, recursive = T)

Binary file added tests/testthat/h1_v_1_19_26/H1_2023_10_16_fd.zip
Binary file not shown.
Binary file not shown.
25 changes: 25 additions & 0 deletions tests/testthat/test-createRDBESDataObject.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,31 @@ capture.output({ ## suppresses printing of console output when running test()

# Test ZIP inputs ---------------------------------------------------------

test_that("importing foldered zipped H1 example data works", {
zipFiles <- c(
"H1_2023_10_16_fd.zip"
)

genObj <- createRDBESDataObject(paste0(dirH1, zipFiles),
castToCorrectDataTypes = TRUE)

expect_equal(genObj, expObjH1)

})

test_that("importing foldered zipped H1 and H2 gives an error", {
zipFiles <- c(
"H1_H2_2023_10_16_fd.zip"
)

genObj <- expect_error(
createRDBESDataObject(paste0(dirH1, zipFiles),
castToCorrectDataTypes = TRUE),
"You cannot import a mix of different hierarchies in one 'zip' input. To import multiple tables unzip all files and import as a folder of 'csv' files."
)

})

test_that("importing zipped H1 example data works", {
zipFiles <- c(
"H1_2023_10_16.zip"
Expand Down

0 comments on commit 78c6a42

Please sign in to comment.