Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/testthat-module.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
## Example caller for the testthat-module reusable workflow.
## Replace fireSense_dataPrepFit with the module name (which must match the repository name).

name: testthat-module

on:
pull_request:
branches: [main, master, development]
push:
branches: [main, master, development]
paths:
- .github/workflows/testthat-module.yaml
- fireSense_dataPrepFit.R
- R/**
- tests/**
workflow_dispatch:

jobs:
testthat-module:
## No permissions block: unlike render-module-rmd, this workflow writes nothing
## back to the repository, so the default `contents: read` it declares for
## itself is all it needs.
uses: PredictiveEcology/actions/.github/workflows/testthat-module.yaml@main
with:
module: fireSense_dataPrepFit
50 changes: 50 additions & 0 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
## Template `tests/testthat/setup.R` for a SpaDES module.
##
## The module is converted to a package rendition before the tests run (see the
## testthat-module workflow), so the module's own functions are already in a real
## namespace: a test can call `myHelper(x)` directly rather than reaching into
## `sim@.xData$.mods$<module>$myHelper`.
##
## This file only sets options and makes a scratch directory. It deliberately does
## NOT install packages or download anything: dependency resolution happens once,
## before the tests, from the module's own `reqdPkgs` metadata.

withr::local_options(
list(
reproducible.useMemoise = TRUE,
reproducible.verbose = -2,
Require.verbose = -2,
spades.moduleCodeChecks = FALSE,
spades.moduleDocument = FALSE,
spades.useRequire = FALSE
),
.local_envir = testthat::teardown_env()
)

## Where the module is, from testthat's working directory.
##
## testthat runs with the working directory set to `tests/testthat`, so the module
## directory is two levels up and the `modulePath` that SpaDES.core wants -- the
## directory *containing* modules -- is three. Getting this wrong is silent: the
## module is simply not found, and every test that needs it skips or errors on
## something unrelated.
moduleRoot <- normalizePath(file.path("..", ".."), winslash = "/", mustWork = TRUE)
moduleName <- basename(moduleRoot)
modulePath <- dirname(moduleRoot)

## A scratch tree removed when the suite finishes. Write here, never beside the
## module: the tests run against a throwaway copy, but the habit matters when they
## are run by hand.
testPaths <- local({
root <- withr::local_tempdir(.local_envir = testthat::teardown_env())
paths <- list(
cachePath = file.path(root, "cache"),
inputPath = file.path(root, "inputs"),
modulePath = modulePath,
outputPath = file.path(root, "outputs")
)
for (p in paths[c("cachePath", "inputPath", "outputPath")]) {
dir.create(p, recursive = TRUE, showWarnings = FALSE)
}
paths
})
100 changes: 100 additions & 0 deletions tests/testthat/test-metadata.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
## The module's metadata is its public contract: a project using this module binds
## to these object names and classes. Renaming or retyping one breaks every caller,
## which is exactly the class of change the raster -> terra migration makes, so it is
## worth asserting here rather than discovering downstream.
##
## When a change is deliberate, update this file in the same commit and bump the
## module version to match: removed, renamed or retyped is a MAJOR bump.

test_that("module metadata parses", {
md <- SpaDES.core::moduleMetadata(module = moduleName, path = modulePath)
expect_type(md, "list")
expect_identical(md$name, moduleName)
})

test_that("inputs are the expected names and classes", {
md <- SpaDES.core::moduleMetadata(module = moduleName, path = modulePath)
inputs <- stats::setNames(md$inputObjects$objectClass, md$inputObjects$objectName)
expect_identical(
inputs[order(names(inputs))],
c(climateVariablesForFire = "list",
cohortDatas = "list",
firePolys = "list",
firePolysForAge = "list",
historicalClimateRasters = "list",
historicalFireRaster = "SpatRaster",
ignitionFirePoints = "sf",
missingLCCgroup = "character",
nonForestedLCCGroups = "list",
pixelGroupMaps = "list",
propFlammables = "list",
rasterToMatch = "SpatRaster",
rasterToMatch_biomassParam = "SpatRaster",
rstLCCs = "list",
sppEquiv = "data.table",
spreadFirePoints = "list",
spreadFirePolys = "list",
spreadFitAdditionalColNames = "character",
standAgeMaps = "list",
studyArea = "SpatVector",
studyArea_biomassParam = "SpatVector",
studyAreaReporting = "sf")
)
})

test_that("outputs are the expected names and classes", {
md <- SpaDES.core::moduleMetadata(module = moduleName, path = modulePath)
outputs <- stats::setNames(md$outputObjects$objectClass, md$outputObjects$objectName)
expect_identical(
outputs[order(names(outputs))],
c(climateVariables = "list",
climateVariablesForFire = "list",
fireBufferedListDT = "list",
fireSense_annualSpreadFitCovariates = "list",
fireSense_escapeCovariates = "data.table",
fireSense_escapeFormula = "character",
fireSense_ignitionCovariates = "data.table",
fireSense_ignitionFormula = "character",
fireSense_nonAnnualSpreadFitCovariates = "list",
fireSense_spreadFormula = "character",
flammableRTM = "SpatRaster",
flammableRTMs = "list",
fuelClassTable = "data.table",
ignitionFirePoints = "sf",
ignitionFitRTM = "SpatRaster",
landcoverDT = "data.table",
landcoverDTs = "list",
lightningMaps = "SpatRaster",
missingLCCgroup = "character",
nonForest_timeSinceDisturbance = "SpatRaster",
nonForest_timeSinceDisturbances = "list",
nonForestedLCCGroups = "list",
propFlammable = "SpatRaster",
rstLCC = "SpatRaster",
rstLCC_RTM = "SpatRaster",
rstLCCs = "list",
sppColorVect = "character",
sppEquiv = "data.table",
sppNameVector = "character",
spreadFirePoints = "list",
spreadFirePolys = "list",
spreadFitPreRun = "data.frame",
standAgeMap = "SpatRaster",
studyAreaWithSpreadParams = "sf")
)
})

test_that("parameters are the expected names", {
md <- SpaDES.core::moduleMetadata(module = moduleName, path = modulePath)
expect_identical(
sort(md$parameters$paramName),
sort(c(".plotInterval", ".saveInitialTime", ".saveInterval", ".studyAreaName",
".useCache", "areaMultiplier", "bufferForFireRaster", "cutoffForYoungAge",
"dataYears", "estimateFuelClasses", "fireYears", "flammabilityThreshold",
"forestedLCC", "fuelClassCol", "igAggFactor", "igFocalFactor",
"minBufferSize", "modelAlgorithm", "nonflammableLCC",
"nonForestCanBeYoungAge", "sppEquivCol", "spreadFitFilename",
"spreadFitGoogleDriveFolder", "targetFuelClasses", "useCentroids",
"useRasterizedFireForSpread", "whichModulesToPrepare"))
)
})
74 changes: 0 additions & 74 deletions tests/testthat/test-template.R

This file was deleted.

8 changes: 0 additions & 8 deletions tests/unitTests.R

This file was deleted.

Loading