-
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.
Merge pull request #3188 from meetagrawal09/test-utils
Improving test coverage for `PEcAn.utils` package
- Loading branch information
Showing
19 changed files
with
325 additions
and
4 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,23 @@ | ||
test_that("`cf2datetime()` able to convert CF-style date-time to POSIXct date-time along with taking care of leap years", { | ||
expect_equal(cf2datetime(5, "days since 1981-01-01"), as.POSIXct("1981-01-06", tz = "UTC")) | ||
expect_equal(cf2datetime(27, "minutes since 1963-01-03 12:00:00 -05:00"), as.POSIXct("1963-01-03 17:27:00", tz = "UTC")) | ||
# nom-leap year | ||
expect_equal(cf2datetime(365, "days since 1999-01-01"), as.POSIXct("2000-01-01", tz = "UTC")) | ||
# leap year | ||
expect_equal(cf2datetime(365, "days since 2000-01-01 12:00:00 -05:00"), as.POSIXct("2000-12-31 17:00:00", tz = "UTC")) | ||
}) | ||
|
||
test_that("`datetime2cf()` able to convert POSIXct date-time to CF-style date-time", { | ||
expect_equal(datetime2cf("1990-10-05", "days since 1990-01-01", tz = "UTC"), 277) | ||
expect_equal(datetime2cf("1963-01-03 17:27:00", "minutes since 1963-01-03 12:00:00 -05:00", tz = "UTC"), 27) | ||
}) | ||
|
||
test_that("`datetime2doy()` and `cf2doy()` able to extract Julian day from POSIXct or CF date-times respectively(cf2doy internally converts CF to POSIXct and calls datetime2doy)", { | ||
|
||
# POSIXct date-times | ||
expect_equal(datetime2doy("2010-01-01"), 1) | ||
expect_equal(datetime2doy("2010-01-01 12:00:00"), 1.5) | ||
|
||
# CF date-times | ||
expect_equal(cf2doy(0, "days since 2007-01-01"), 1) | ||
}) |
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,24 @@ | ||
test_that("`clear.scratch()` able to build the correct system command prompt to remove previous model run output", { | ||
mocked_res <- mockery::mock(TRUE) | ||
mockery::stub(clear.scratch, 'system', mocked_res) | ||
mockery::stub(clear.scratch, 'seq', 0) | ||
settings <- list(host = list(name = "cluster")) | ||
expect_output( | ||
clear.scratch(settings), | ||
".*Removing.*[email protected]" | ||
) | ||
args <- mockery::mock_args(mocked_res) | ||
expect_true( | ||
grepl( | ||
"ssh -T cluster qlogin -q [email protected].*clear.scratch.sh", | ||
args[[1]][[1]] | ||
) | ||
) | ||
|
||
# host name not cluster | ||
settings <- list(host = list(name = "test")) | ||
expect_output( | ||
clear.scratch(settings), | ||
".*No output to delete.*" | ||
) | ||
}) |
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,5 @@ | ||
test_that("`days_in_year()` correctly returns number of days when provided a year or a vector of years", { | ||
expect_equal(days_in_year(2010), 365) | ||
expect_equal(days_in_year(2012), 366) | ||
expect_equal(days_in_year(2010:2012), c(365, 365, 366)) | ||
}) |
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,12 @@ | ||
test_that("`download.url()` able to create the target dir for file download and passes the correct args to curl_download", { | ||
withr::with_dir(tempdir(), { | ||
mocked_res <- mockery::mock(TRUE) | ||
mockery::stub(download.url, 'url_found', TRUE) | ||
mockery::stub(download.url, 'curl::curl_download', mocked_res) | ||
res <- download.url('http://localhost/', 'test/index.html') | ||
expect_true(file.exists('test')) | ||
args <- mockery::mock_args(mocked_res) | ||
expect_equal(args[[1]]$url, 'http://localhost/') | ||
expect_equal(args[[1]]$destfile, 'test/index.html') | ||
}) | ||
}) |
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,16 @@ | ||
test_that("`get.ensemble.inputs()` able to return desired ensemble inputs from settings", { | ||
settings <- list( | ||
run = list( | ||
inputs = list( | ||
input1 = c(1, 2, 3), | ||
input2 = c("A", "B", "C"), | ||
input3 = c(TRUE, FALSE, TRUE) | ||
) | ||
) | ||
) | ||
res <- get.ensemble.inputs(settings) | ||
expect_equal( | ||
res, | ||
list(input1 = c(1, 2, 3), input2 = c(1, 2, 3), input3 = c(1, 2, 3)) | ||
) | ||
}) |
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,19 @@ | ||
test_that("`listToArgString()` able to format list of named function args in a comma separated list", { | ||
expect_equal( | ||
listToArgString(c(host = 'pecan', settings = 'test', id = 2020)), | ||
"host='pecan', settings='test', id='2020'" | ||
) | ||
}) | ||
|
||
test_that("`.parseArg()` works for all different types of entries in the list of function args passed to listToArgString", { | ||
# character | ||
expect_equal(.parseArg('pecan'), "'pecan'") | ||
# NULL | ||
expect_equal(.parseArg(NULL), "NULL") | ||
# list | ||
expect_equal(.parseArg(list(a = 1, b = 2)), "list(a='1', b='2')") | ||
# data.frame | ||
expect_equal(.parseArg(data.frame(a = 1, b = 2)), "data.frame(a =c(' 1 '),b =c(' 2 '))") | ||
# nested list | ||
expect_equal(.parseArg(list(a = 1, b = list(c = 3, d = 4))), "list(a='1', b=list(c='3', d='4'))") | ||
}) |
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,10 @@ | ||
test_that("`load_local()` able to load file into a list", { | ||
withr::with_tempfile("tf", { | ||
x <- 1:10 | ||
y <- 11:15 | ||
save(x, y, file = tf) | ||
test_list <- load_local(tf) | ||
expect_equal(test_list$x, x) | ||
expect_equal(test_list$y, y) | ||
}) | ||
}) |
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,9 @@ | ||
test_that("`n_leap_day()` able to correctly return number of leap days between 2 specified dates", { | ||
|
||
# having leap days | ||
expect_equal(n_leap_day("2000-01-01", "2003-12-31"), 1) | ||
expect_equal(n_leap_day("2000-01-01", "2004-12-31"), 2) | ||
|
||
# no leap days | ||
expect_equal(n_leap_day("2001-01-01", "2003-12-31"), 0) | ||
}) |
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,11 @@ | ||
test_that("`need_packages()` correctly checks if the required packages are installed", { | ||
|
||
# normal condition : when packages exist | ||
expect_equal(need_packages("stats", "methods"), c("stats", "methods")) | ||
|
||
# error condition | ||
expect_error( | ||
need_packages("notapackage"), | ||
"The following packages are required but not installed: `notapackage`" | ||
) | ||
}) |
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,19 @@ | ||
test_that("`r2bugs.distributions()` able to convert R parameterization to BUGS parameterization", { | ||
priors <- data.frame(distn = c('weibull', 'lnorm', 'norm', 'gamma'), | ||
parama = c(1, 1, 1, 1), | ||
paramb = c(2, 2, 2, 2)) | ||
res <- r2bugs.distributions(priors) | ||
expect_equal(res$distn, c("weib", "lnorm", "norm", "gamma")) | ||
expect_equal(res$parama, c(1, 1, 1, 1)) | ||
expect_equal(res$paramb, c(0.50, 0.25, 0.25, 2.00)) | ||
}) | ||
|
||
test_that("`bugs2r.distributions()` able to convert BUGS parameterization to R parameterization", { | ||
priors <- data.frame(distn = c('weib', 'lnorm', 'norm', 'gamma'), | ||
parama = c(1, 1, 1, 1), | ||
paramb = c(0.50, 0.25, 0.25, 2.00)) | ||
res <- bugs2r.distributions(priors) | ||
expect_equal(res$distn, c("weibull", "lnorm", "norm", "gamma")) | ||
expect_equal(res$parama, c(1, 1, 1, 1)) | ||
expect_equal(res$paramb, c(2, 2, 2, 2)) | ||
}) |
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,8 @@ | ||
test_that("`seconds_in_year()` able to return number of seconds in a given year(also for a vector of years)", { | ||
# leap year | ||
expect_equal(seconds_in_year(2000), 31622400) | ||
# non leap year | ||
expect_equal(seconds_in_year(2001), 31536000) | ||
# vector of years | ||
expect_equal(seconds_in_year(2000:2004), c(31622400, 31536000, 31536000, 31536000, 31622400)) | ||
}) |
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,18 @@ | ||
test_that("`sendmail()` able to create the file with contents to email correctly, also able to build correct command to send the email", { | ||
withr::with_tempfile("tf", { | ||
mocked_res <- mockery::mock(TRUE) | ||
mockery::stub(sendmail, 'system2', mocked_res) | ||
mockery::stub(sendmail, 'tempfile', tf) | ||
mockery::stub(sendmail, 'unlink', NULL) | ||
sendmail('pecan@@example.com', 'carya@@example.com', 'Hi', 'Message from pecan.') | ||
sendmailfile <- readLines(tf) | ||
expect_equal(sendmailfile[1], 'From: pecan@@example.com') | ||
expect_equal(sendmailfile[2], 'Subject: Hi') | ||
expect_equal(sendmailfile[3], 'To: carya@@example.com') | ||
expect_equal(sendmailfile[5], 'Message from pecan.') | ||
args <- mockery::mock_args(mocked_res) | ||
expect_equal(args[[1]][[2]][[1]], '-f') | ||
expect_equal(args[[1]][[2]][[2]], '"pecan@@example.com"') | ||
expect_equal(args[[1]][[2]][[3]], '"carya@@example.com"') | ||
}) | ||
}) |
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,7 @@ | ||
test_that("`timezone_hour()` able to correctly return number of hours offset to UTC for a timezone", { | ||
expect_equal(timezone_hour('US/Pacific'), -8) | ||
expect_equal(timezone_hour('US/Eastern'), -5) | ||
|
||
# for numeric | ||
expect_equal(timezone_hour(-8), -8) | ||
}) |
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,6 @@ | ||
test_that("`units_are_equivalent()` able to identify if the units are equivalent or not", { | ||
# Equivalent units | ||
expect_true(units_are_equivalent("m/s", "m s-1")) | ||
# Non-equivalent units | ||
expect_error(units_are_equivalent("m/s", "m s-2")) | ||
}) |
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