-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
download.AmerifluxLBL: respect
overwrite
argument (#3382)
* doanload.AmerifluxLBL: respect `overwrite` argument * guess I was testing on a site with only one var * deps
- Loading branch information
Showing
6 changed files
with
174 additions
and
92 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
76 changes: 76 additions & 0 deletions
76
modules/data.atmosphere/tests/testthat/test.download.AmerifluxLBL.R
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,76 @@ | ||
|
||
test_that("download respects overwrite argument", { | ||
outdir <- withr::local_tempdir() | ||
zippath <- file.path(outdir, "AMF_US-Akn_BASE-BADM_6-5.zip") | ||
csvpath <- sub("-BADM(.*).zip", "_HH\\1.csv", zippath) | ||
|
||
# Mock out amerifluxr functions to test our code without network calls | ||
local_mocked_bindings( | ||
amf_download_base = \(...) { | ||
tmp_csv <- basename(csvpath) | ||
withr::with_tempdir({ | ||
writeLines( | ||
c( "# fake file", | ||
"#", | ||
"TIMESTAMP_START,TIMESTAMP_END", | ||
"201101010000,201101010030", | ||
"201112310000,201112310030"), | ||
tmp_csv) | ||
zip(zippath, tmp_csv, flags = "-qr0X")}) | ||
zippath | ||
}, | ||
amf_var_info = \(...) data.frame( | ||
Site_ID = "US-Akn", | ||
BASE_Version = "6-5"), | ||
.package = "amerifluxr" | ||
) | ||
|
||
# wrapper, just to skip retyping args | ||
dl_akn <- function(...) download.AmerifluxLBL( | ||
site = "US-Akn", | ||
outfolder = outdir, | ||
start_date = "2011-01-01", | ||
end_date = "2011-10-01", | ||
...) | ||
|
||
# Case 0: new download | ||
expect_false(file.exists(zippath)) | ||
expect_false(file.exists(csvpath)) | ||
dl_akn() | ||
expect_true(file.exists(zippath)) | ||
expect_true(file.exists(csvpath)) | ||
|
||
|
||
# Case 1: reuse existing download | ||
ziptime <- file.mtime(zippath) | ||
csvtime <- file.mtime(csvpath) | ||
expect_log( | ||
dl_akn(overwrite = FALSE), | ||
"skipping download.*skipping extraction") | ||
expect_equal(file.mtime(zippath), ziptime) | ||
expect_equal(file.mtime(csvpath), csvtime) | ||
|
||
# Case 2: overwrite existing download | ||
dl_akn(overwrite = TRUE) | ||
expect_gt(file.mtime(zippath), ziptime) | ||
expect_gt(file.mtime(csvpath), csvtime) | ||
|
||
# Case 3: Freshen csv without clobbering zip | ||
file.remove(csvpath) | ||
ziptime <- file.mtime(zippath) | ||
expect_log(dl_akn(overwrite = FALSE), "skipping download") | ||
expect_true(file.exists(csvpath)) | ||
expect_equal(file.mtime(zippath), ziptime) | ||
|
||
# Case 4: Re-download zip without clobbering CSV | ||
# (Note: I'm not sure this is desirable! For consistency it may be better | ||
# to overwrite the CSV so we know it matches the zip file. | ||
# If you change the behavior, go ahead and update this test to match.) | ||
file.remove(zippath) | ||
csvtime <- file.mtime(csvpath) | ||
expect_log( | ||
dl_akn(overwrite = FALSE), | ||
"skipping extraction") | ||
expect_true(file.exists(zippath)) | ||
expect_equal(file.mtime(csvpath), csvtime) | ||
}) |