-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Weather and ManagementData input checking + tests
- Loading branch information
Showing
16 changed files
with
234 additions
and
33 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
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.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
|
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 @@ | ||
|
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,42 @@ | ||
## Write all non-function field values to a file | ||
save_temp_output = function(W) { | ||
path1 = tempfile(fileext = ".dat") | ||
|
||
tab = data.frame(year = W$year, DOY = W$DOY, Ta = W$Ta, precip = W$precip, | ||
PAR = W$PAR, ET0 = W$ET0) | ||
write.table(tab, file = path1, quote = FALSE) | ||
return(path1) | ||
} | ||
|
||
test_that("WeatherData read example data", { | ||
wd_default_snapshot = "weather_example_values.dat" | ||
announce_snapshot_file(wd_default_snapshot) | ||
|
||
extdata = system.file("extdata", package = "growR") | ||
example_weather_file = file.path(extdata, "posieux_weather.txt") | ||
W = WeatherData$new(example_weather_file) | ||
|
||
path = save_temp_output(W) | ||
expect_snapshot_file(path, wd_default_snapshot, compare = compare_file_text) | ||
}) | ||
|
||
test_that("WeatherData input checking", { | ||
# Store old options for resetting after test | ||
old_opts = options() | ||
on.exit(options(old_opts)) | ||
set_growR_verbosity(5) | ||
|
||
W = WeatherData$new() | ||
W$weather_file = "<testthat>" | ||
|
||
incomplete_data = data.frame(year = 1) | ||
expect_error(W$ensure_file_integrity(incomplete_data), | ||
regexp = "parameters were missing") | ||
|
||
# NA correction | ||
na_data = data.frame(year = 1, DOY = 1, Ta = NA, precip = NA, PAR = NA, | ||
ET0 = NA) | ||
checked_data = W$ensure_file_integrity(na_data) | ||
expect_false(any(is.na(checked_data))) | ||
|
||
}) |
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 |
---|---|---|
@@ -1,16 +1,45 @@ | ||
test_that("Reading of weather data", { | ||
datapath = system.file("extdata", package = "growR") | ||
weather_file = file.path(datapath, "posieux_weather.txt") | ||
WD = WeatherData$new(weather_file) | ||
expect_false(is.null(WD$years)) | ||
W = WD$get_weather_for_year(WD$years[1]) | ||
# Check that none of the important keys are NULL | ||
keys = c("aCO2", "year", "DOY", "Ta", "Ta_sm", "PAR", "PP", "PET", | ||
"liquidP", "melt", "snow", "ndays") | ||
for (key in keys) { | ||
if (is.null(W[[key]])) { | ||
logger(sprintf("Weather variable is NULL: %s.", key), level = 2) | ||
} | ||
expect_false(is.null(W[[key]])) | ||
} | ||
## Write all non-function field values to a file | ||
save_temp_output = function(M) { | ||
path1 = tempfile(fileext = ".dat") | ||
|
||
tab = data.frame(year = M$year, DOY = M$DOY) | ||
write.table(tab, file = path1, quote = FALSE) | ||
return(path1) | ||
} | ||
|
||
test_that("ManagementData read example data", { | ||
md_default_snapshot = "management_example_values.dat" | ||
announce_snapshot_file(md_default_snapshot) | ||
|
||
extdata = system.file("extdata", package = "growR") | ||
example_management_file = file.path(extdata, "posieux_management1.txt") | ||
M = ManagementData$new(example_management_file) | ||
|
||
path = save_temp_output(M) | ||
expect_snapshot_file(path, md_default_snapshot, compare = compare_file_text) | ||
}) | ||
|
||
test_that("ManagementData input checking", { | ||
# Store old options for resetting after test | ||
old_opts = options() | ||
on.exit(options(old_opts)) | ||
set_growR_verbosity(5) | ||
|
||
incomplete_management_data = data.frame(year = 1) | ||
M = ManagementData$new() | ||
M$management_file = "<testthat>" | ||
expect_error(M$ensure_file_integrity(incomplete_management_data), | ||
regexp = "parameters were missing") | ||
|
||
wrong_DOY_management_data = data.frame(year = c(1, 1, 1), DOY = c(1, 3, 2)) | ||
expect_error(M$ensure_file_integrity(wrong_DOY_management_data), | ||
regexp = "not monotonically increasing") | ||
|
||
wrong_DOY_management_data2 = data.frame(year = c(1, 1, 1), DOY = c(1, 2, 2)) | ||
expect_error(M$ensure_file_integrity(wrong_DOY_management_data2), | ||
regexp = "not monotonically increasing") | ||
|
||
fake_management_data = data.frame(year = c(1, 1, 1), DOY = c(1, 2, 3)) | ||
expect_no_error(M$ensure_file_integrity(fake_management_data)) | ||
|
||
}) |