Skip to content

Commit

Permalink
testthat
Browse files Browse the repository at this point in the history
  • Loading branch information
kuadrat committed Oct 11, 2023
1 parent 520f486 commit 254bab4
Show file tree
Hide file tree
Showing 15 changed files with 603 additions and 18 deletions.
7 changes: 6 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ License: MIT + file LICENSE
RoxygenNote: 7.2.3
Encoding: UTF-8
Imports: rlang, R6, utils, Rdpack
Suggests: ggplot2, patchwork
Suggests:
ggplot2,
patchwork,
testthat (>= 3.0.0)
Depends:
R (>= 2.10)
RdMacros: Rdpack
LazyData: true
Config/testthat/edition: 3
Config/testthat/start-first: setup, pscan, run
7 changes: 5 additions & 2 deletions R/environment.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ ModvegeEnvironment = R6Class(
#'
initialize = function(site_name,
run_name = "-",
years = "all",
years = NULL,
param_file = "-",
weather_file = "-",
management_file = "-",
Expand Down Expand Up @@ -117,9 +117,12 @@ ModvegeEnvironment = R6Class(
self$parameters = ModvegeParameters$new(file.path(self$input_dir,
self$param_file))
self$weather = WeatherData$new(file.path(self$input_dir,
self$weather_file))
self$weather_file), self$years)
self$management = ManagementData$new(file.path(self$input_dir,
self$management_file))
if (is.null(self$years)) {
self$years = self$weather$years
}
},

#' @description Ensure a readable filename for given *run_name*.
Expand Down
11 changes: 7 additions & 4 deletions R/management.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ ManagementData = R6Class(
#-Public-attributes-----------------------------------------------------------
management_file = NULL,
is_empty = TRUE,
#' @field years List of unique years for which data is available.
years = NULL,
cut_years = NULL,
cut_DOY = NULL,
intensity = NULL,
Expand All @@ -36,7 +38,7 @@ ManagementData = R6Class(
#' extracted.
#'
#' @seealso [ManagementData$read_management()]
initialize = function(management_file = NULL, years = "all") {
initialize = function(management_file = NULL, years = NULL) {
if (!is.null(management_file)) {
self$read_management(management_file, years)
}
Expand All @@ -46,23 +48,24 @@ ManagementData = R6Class(
#'
#' @param management_file Path to or name of file containing management data.
#' @param years Years for which the management is to be extracted.
#' Default is "all" to read in all found years.
#' Default (NULL) is to read in all found years.
#'
read_management = function(management_file, years = "all") {
read_management = function(management_file, years = NULL) {
# Prepare the resulting container
self[["management_file"]] = management_file
if (file.exists(management_file)) {
logger(sprintf("Loading management data from %s.", management_file),
level = DEBUG)
cut_data = read.table(management_file, header = TRUE)
# Only consider specified years
if (years == "all") {
if (is.null(years)) {
selector = TRUE
} else {
selector = cut_data$year %in% years
}
self$is_empty = FALSE
self$cut_years = cut_data$year[selector]
self$years = unique(self$cut_years)
self$cut_DOY = cut_data$DOY[selector]
self$intensity = NA
} else {
Expand Down
2 changes: 1 addition & 1 deletion R/parameter_scan.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ run_parameter_scan = function(environment, param_values, force = FALSE,
}
logger("Completed parameter scan.", level = INFO)
logger(paste0("Time used: ", Sys.time() - start_time), level = INFO)
if (length(outfilename) > "") {
if (outfilename != "") {
logger(sprintf("Storing results as `%s`.", outfilename))
saveRDS(results, outfilename)
}
Expand Down
10 changes: 10 additions & 0 deletions R/setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,13 @@ setup_directory = function(root = ".", include_examples = TRUE) {
print(sprintf("Copied example files to respective directories."))
}

dirs = c("input", "output", "data")
files = c("example_config.txt", "compare.R")

## Used when testing
clean_up_dir = function() {
for (dir in c(dirs, files)) {
unlink(dir, recursive = TRUE)
}
}

10 changes: 6 additions & 4 deletions R/weather.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ WeatherData = R6Class(
#' extracted.
#'
#' @seealso WeatherData$read_weather()
initialize = function(weather_file = NULL, years = "all") {
initialize = function(weather_file = NULL, years = NULL) {
if (!is.null(weather_file)) {
self$read_weather(weather_file, years)
}
Expand All @@ -90,9 +90,9 @@ WeatherData = R6Class(
#'
#' @param weather_file Path to or name of file containing weather data.
#' @param years Years for which the weather is to be extracted.
#' Default is "all" to read in all found years.
#' Default (NULL) is to read in all found years.
#'
read_weather = function(weather_file, years = "all") {
read_weather = function(weather_file, years = NULL) {
self$years = years
self$weather_file = weather_file
# Load weather data
Expand All @@ -108,8 +108,10 @@ WeatherData = R6Class(

# Only consider relevant years and omit leap days
selector = weather$DOY < 366
if (years != "all") {
if (!is.null(years)) {
selector = selector & weather$year %in% years
} else {
self$years = unique(weather$year)
}

# Warn if no matching data was found
Expand Down
10 changes: 5 additions & 5 deletions inst/extdata/example_config.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# site name # run name # year(s) # param file # weather data # cut dates
sorens1 - 2013:2022 sorens_parameters.csv sorens_weather.txt sorens_management1.txt
sorens2 - 2013:2022 sorens_parameters.csv sorens_weather.txt sorens_management2.txt
sorens2 autocut 2013:2022 sorens_parameters.csv sorens_weather.txt high
posieux1 - 2013:1022 posieux_parameters.csv posieux_weather.txt posieux_management1.txt
posieux2 - 2013:2022 posieux_parameters.csv posieux_weather.txt posieux_management2.txt
posieux2 autocut 2013:2022 posieux_parameters.csv posieux_weather.txt high
#sorens2 - 2013:2022 sorens_parameters.csv sorens_weather.txt sorens_management2.txt
#sorens2 autocut 2013:2022 sorens_parameters.csv sorens_weather.txt high
posieux1 - 2013:2022 posieux_parameters.csv posieux_weather.txt posieux_management1.txt
#posieux2 - 2013:2022 posieux_parameters.csv posieux_weather.txt posieux_management2.txt
#posieux2 autocut 2013:2022 posieux_parameters.csv posieux_weather.txt high
4 changes: 3 additions & 1 deletion man/rmodvege-package.Rd

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

12 changes: 12 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html

library(testthat)
library(rmodvege)

test_check("rmodvege")
Loading

0 comments on commit 254bab4

Please sign in to comment.