Skip to content

Commit

Permalink
Merge pull request #3208 from meetagrawal09/test-AmerifluxLBL
Browse files Browse the repository at this point in the history
Added Integration Tests for `download.AmerifluxLBL`
  • Loading branch information
mdietze authored Aug 28, 2023
2 parents e0b4244 + b7634c1 commit ce327b9
Showing 1 changed file with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
library(testthat)

test_download_AmerifluxLBL <- function(start_date, end_date, sitename, lat.in, lon.in) {
# putting logger to debug mode
PEcAn.logger::logger.setUseConsole(TRUE, FALSE)
on.exit(PEcAn.logger::logger.setUseConsole(TRUE, TRUE), add = TRUE)
PEcAn.logger::logger.setLevel("DEBUG")

# mocking functions
mockery::stub(PEcAn.DB::convert_input, 'dbfile.input.check', data.frame())
mockery::stub(PEcAn.DB::convert_input, 'db.query', data.frame(id = 1))

withr::with_dir(tempdir(), {
tmpdir <- getwd()
# calling download function
res <- PEcAn.DB::convert_input(
input.id = NA,
outfolder = tmpdir,
formatname = NULL,
mimetype = NULL,
site.id = 1,
start_date = start_date,
end_date = end_date,
pkg = 'PEcAn.data.atmosphere',
fcn = 'download.AmerifluxLBL',
con = NULL,
host = data.frame(name = "localhost"),
browndog = NULL,
write = FALSE,
lat.in = lat.in,
lon.in = lon.in,
sitename = sitename
)
})

# checking if the file is downloaded
test_that("Downloaded files are present at the desired location", {
expect_true(file.exists(paste0(tmpdir, "/AMF_US-Akn_BASE_HH_6-5.csv")))
})

test_that("Downloaded data files have the right format", {
firstline <- system(paste0("head -4 ", paste0(tmpdir, "/AMF_US-Akn_BASE_HH_6-5.csv")), intern = TRUE)
lastline <- system(paste0("tail -1 ", paste0(tmpdir, "/AMF_US-Akn_BASE_HH_6-5.csv")), intern = TRUE)

# checking if first line of CSV has the sitename
expect_true(grepl(sitename, firstline[1]))

# fourth and last row checked to contain non-alphabetical data since these are used to verify start and end dates
expect_false(grepl("[A-Za-z]", firstline[4]))
expect_false(grepl("[A-Za-z]", lastline[1]))
})
}

test_download_AmerifluxLBL(
start_date = "2011-01-01",
end_date = "2011-12-31",
sitename = 'US-Akn',
lat.in = 40,
lon.in = -88
)

0 comments on commit ce327b9

Please sign in to comment.