-
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 #3180 from meetagrawal09/test-db
Improving test coverage for `PEcAn.DB` package
- Loading branch information
Showing
25 changed files
with
643 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,21 +66,24 @@ Suggests: | |
data.table, | ||
here, | ||
knitr, | ||
mockery, | ||
RPostgreSQL, | ||
RPostgres, | ||
RSQLite, | ||
rcrossref, | ||
rmarkdown (>= 2.19), | ||
testthat (>= 2.0.0), | ||
tidyverse | ||
tidyverse, | ||
withr | ||
X-Comment-Remotes: | ||
Installing markdown from GitHub because as of 2023-02-05, this is the | ||
easiest way to get version >= 2.19 onto Docker images that use older | ||
Rstudio Package Manager snapshots. | ||
When building on a system that finds a new enough version on CRAN, | ||
OK to remove the Remotes line and this comment. | ||
Remotes: | ||
github::rstudio/[email protected] | ||
github::rstudio/[email protected], | ||
github::r-lib/[email protected] | ||
License: BSD_3_clause + file LICENSE | ||
VignetteBuilder: knitr | ||
Copyright: Authors | ||
|
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
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,33 @@ | ||
test_that("`assign.treatments` correctly assigns control treatment", { | ||
data <- data.frame( | ||
site_id = c(1, 1, 2, 2, 3, 3), | ||
citation_id = c(101, 101, 201, 201, 301, 301), | ||
control = c(1, 0, 0, 1, 0, 0), | ||
trt_id = NA | ||
) | ||
|
||
updated_data <- assign.treatments(data) | ||
expect_equal(updated_data$trt_id, c("control", NA, NA, "control", "control", "control")) | ||
}) | ||
|
||
test_that("`assign.treatments` gives an error if no control treatment is set for a site", { | ||
data <- data.frame( | ||
site_id = c(1, 1, 2, 2, 3, 3), | ||
citation_id = c(101, 101, 201, 201, 301, 301), | ||
control = c(0, 0, 0, 1, 0, 0), | ||
trt_id = c(NA, NA, NA, NA, "not_control", NA) | ||
) | ||
|
||
expect_error(assign.treatments(data), "No control treatment set") | ||
}) | ||
|
||
test_that("`drop.columns` able to drop specified columns from data", { | ||
data <- data.frame( | ||
id = c(1, 2, 3), | ||
name = c("a", "b", "c"), | ||
value = c(1.2, 4.5, 6.7) | ||
) | ||
|
||
updated_data <- drop.columns(data, c("name", "not_a_column")) | ||
expect_equal(colnames(updated_data), c("id", "value")) | ||
}) |
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("`check.lists` returns false for appropriate cases", { | ||
x <- data.frame(id = c(1, 2, 3)) | ||
y <- data.frame(id = c(1, 2, 3, 4)) | ||
|
||
# for unequal number of rows | ||
expect_false(check.lists(x, y)) | ||
|
||
# for wrong filename passed | ||
expect_false(check.lists(x, y, filename = "wrong.csv")) | ||
|
||
# if x and y are actually unequal | ||
y <- data.frame(id = c(1, 2, 4)) | ||
expect_false(check.lists(x, y, filename = "species.csv")) | ||
}) | ||
|
||
test_that("`check.lists` able to correctly work for matching data frames to lists read from csv files", { | ||
withr::with_tempfile("tf", fileext = ".csv",{ | ||
x <- data.frame(id = c(1, 2, 3)) | ||
y <- data.frame(id = c(1, 2, 3)) | ||
write.csv(y, file = tf) | ||
expect_true(check.lists(x, read.csv(tf), filename = "species.csv")) | ||
}) | ||
}) |
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,39 @@ | ||
test_that("`convert_input()` able to call the respective download function for a data item with the correct arguments", { | ||
mocked_res <- mockery::mock(list(c("A", "B"))) | ||
|
||
mockery::stub(convert_input, 'dbfile.input.check', data.frame()) | ||
mockery::stub(convert_input, 'db.query', data.frame(id = 1)) | ||
mockery::stub(convert_input, 'PEcAn.remote::remote.execute.R', mocked_res) | ||
mockery::stub(convert_input, 'purrr::map_dfr', data.frame(missing = c(FALSE), empty = c(FALSE))) | ||
|
||
convert_input( | ||
input.id = NA, | ||
outfolder = "test", | ||
formatname = NULL, | ||
mimetype = NULL, | ||
site.id = 1, | ||
start_date = "2011-01-01", | ||
end_date = "2011-12-31", | ||
pkg = 'PEcAn.data.atmosphere', | ||
fcn = 'download.AmerifluxLBL', | ||
con = NULL, | ||
host = data.frame(name = "localhost"), | ||
browndog = NULL, | ||
write = FALSE, | ||
lat.in = 40, | ||
lon.in = -88 | ||
) | ||
|
||
args <- mockery::mock_args(mocked_res) | ||
expect_equal( | ||
args[[1]]$script, | ||
"PEcAn.data.atmosphere::download.AmerifluxLBL(lat.in=40, lon.in=-88, overwrite=FALSE, outfolder='test/', start_date='2011-01-01', end_date='2011-12-31')" | ||
) | ||
}) | ||
|
||
test_that("`.get.file.deletion.commands()` able to return correct file deletion commands", { | ||
res <- .get.file.deletion.commands(c("test")) | ||
expect_equal(res$move.to.tmp, "dir.create(c('./tmp'), recursive=TRUE, showWarnings=FALSE); file.rename(from=c('test'), to=c('./tmp/test'))") | ||
expect_equal(res$delete.tmp, "unlink(c('./tmp'), recursive=TRUE)") | ||
expect_equal(res$replace.from.tmp, "file.rename(from=c('./tmp/test'), to=c('test'));unlink(c('./tmp'), recursive=TRUE)") | ||
}) |
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,32 @@ | ||
test_that("`append.covariate` able to append new column for covariates in given data based on id", { | ||
data <- data.frame( | ||
id = c(1, 2, 3, 4), | ||
name = c("a", "b", "c", "d") | ||
) | ||
covariates.data <- data.frame( | ||
trait_id = c( 1, 2, 3, 4, 4), | ||
level = c("A", "B", "C", "D", "E"), | ||
name = c("a", "b", "c", "d", "e") | ||
) | ||
updated_data <- append.covariate(data, "new_covariates_col", covariates.data) | ||
expect_equal(updated_data$new_covariates_col, c("A", "B", "C", "D")) | ||
expect_equal(colnames(updated_data), c("id", "new_covariates_col", "name")) | ||
}) | ||
|
||
test_that("`filter_sunleaf_traits`able to filter out upper canopy leaves", { | ||
data <- data.frame( | ||
id = c(1, 2, 3, 4), | ||
name = c("a", "b", "c", "d") | ||
) | ||
covariates <- data.frame( | ||
trait_id = c(1, 2, 3, 4), | ||
name = c("leaf", "canopy_layer", "canopy_layer", "sunlight"), | ||
level = c(1.2, 0.5, 0.7, 0.67) | ||
) | ||
|
||
updated_data <- filter_sunleaf_traits(data, covariates) | ||
expect_equal(updated_data$name, c("a", "c", "d")) | ||
|
||
# temporary column gets removed | ||
expect_equal(colnames(updated_data), c("id", "name")) | ||
}) |
Oops, something went wrong.