Skip to content

Commit

Permalink
Merge pull request #3192 from meetagrawal09/test-visualization
Browse files Browse the repository at this point in the history
Improving test coverage for `PEcAn.visualization` package
  • Loading branch information
mdietze authored Aug 27, 2023
2 parents 0c39bde + 87b91d6 commit ddb0038
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 3 deletions.
4 changes: 3 additions & 1 deletion base/visualization/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ Imports:
stringr(>= 1.1.0)
Suggests:
grid,
mockery,
png,
raster,
sp,
testthat (>= 1.0.2)
testthat (>= 1.0.2),
withr
License: BSD_3_clause + file LICENSE
Copyright: Authors
LazyLoad: yes
Expand Down
2 changes: 1 addition & 1 deletion base/visualization/R/plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ iqr <- function(x) {
##'
##' Used to add raw data or summary statistics to the plot of a distribution.
##' The height of Y is arbitrary, and can be set to optimize visualization.
##' If SE estimates are available, the se wil be plotted
##' If SE estimates are available, the SE will be plotted
##' @name plot_data
##' @aliases plot.data
##' @title Add data to plot
Expand Down
2 changes: 1 addition & 1 deletion base/visualization/man/plot_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file not shown.
7 changes: 7 additions & 0 deletions base/visualization/tests/testthat/test.add_icon.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test_that("`add_icon()` able to create the correct output file", {
withr::with_dir(tempdir(), {
add_icon(1, 2, 3)
# check if file exists
expect_true(file.exists("Rplots.pdf"))
})
})
7 changes: 7 additions & 0 deletions base/visualization/tests/testthat/test.plot_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test_that("`plot_data()` able to create a new plot for data passed to it", {
withr::with_dir(tempdir(), {
res <- plot_data(data.frame(Y = c(1, 2), se = c(1,2), trt = c(1, 2)), base.plot = NULL, ymax = 10)
print(res)
expect_true(file.exists(paste0(getwd(), "/Rplots.pdf")))
})
})
21 changes: 21 additions & 0 deletions base/visualization/tests/testthat/test.plot_netcdf.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
test_that("`data.fetch()` able to return aggregated data with the correct label", {
nc <- ncdf4::nc_open("./data/urbana_subdaily_test.nc")
on.exit(ncdf4::nc_close(nc))

res <- data.fetch("time", nc, mean)
expect_equal(attr(res, "lbl"), "days since 1700-01-01T00:00:00Z")

res <- data.fetch("air_temperature", nc, mean)
expect_equal(attr(res, "lbl"), "3-hourly Air Temperature at 2m in K")
})

test_that("`plot_netcdf()` able to correctly plot the netcdf file data and create the plot file at the desired location", {
nc <- ncdf4::nc_open("./data/urbana_subdaily_test.nc")
withr::with_dir(tempdir(), {
mockery::stub(plot_netcdf, 'ncdf4::nc_open', nc)
res <- plot_netcdf("./data/urbana_subdaily_test.nc", "time", "air_temperature", year = 2010)

# check if file exists
expect_true(file.exists("Rplots.pdf"))
})
})

0 comments on commit ddb0038

Please sign in to comment.