tests/testthat/test-storage_s3_hysplit_rds.R

store_name <- "testthat_s3"
bucket <- "rostluft"

is_s3_admin <- function() {
  return(Sys.getenv("AWS_ADMIN") == "TRUE")
}

destroy_s3_store <- function() {
  if (is_s3_admin()) {
    objects <- aws.s3::get_bucket(bucket, prefix = store_name, max = Inf) # nolint
    objects <- dplyr::bind_rows(purrr::map_dfr(objects, purrr::flatten))
    purrr::map(objects$Key, aws.s3::delete_object, bucket = bucket) # nolint
  }
}


#TODO tests for meta data handling

# just to be sure, there is nothing before and after the tests
local_cleanup_storage(store_name)


test_that("put into store", {
  testthat::skip_if_not(is_s3_admin(), "Skip Test: no administator rights")
  testthat::expect_message(
    store <- storage_s3_rds(store_name, format_hysplit(), bucket, prefix = store_name,  read.only = FALSE)
  )
  p <- system.file("extdata", package = "rOstluft.data", mustWork = TRUE)
  suppressMessages(
    import_directory(store, p, readRDS, glob = "*hysplit.rds")
  )

  content <- store$get_content()
  testthat::expect_equal(nrow(content), 4)
  testthat::expect_equal(sum(content$n), 2 * 70810 + 2 * 70183)
})


test_that("get from store", {
  testthat::skip_if_not(is_s3_admin(), "Skip Test: no administator rights")
  store <- storage_s3_rds(store_name, format_hysplit(), bucket, prefix = store_name)

  data_files <- fs::dir_ls(store$data_path, recurse = TRUE, type = "file")

  fs::file_delete(data_files[1])
  fs::file_delete(data_files[3])

  res <- store$get(site = c("ZH-Kaserne-hysplit", "SG-BP-hysplit"), year = 2017:2018)
  testthat::expect_equal(nrow(res), 2 * 70810 + 2 * 70183)
})
Ostluft/rOstluft documentation built on Feb. 6, 2024, 1:26 a.m.