Skip to content

Commit 0e9dfd7

Browse files
author
Mike Johnson
committed
test(invariants): cover ngen slope/So/lake checks; spell-check wordlist
* test-invariants.R: add cases for slope_valid (positive / zero+negative / all-NA info / absent), So_valid (flowpaths and the network attribute table, positive vs zero), and lake_spatial_consistent (a reach far from its stamped lake fails; an in-lake reach passes; no lakes -> check not emitted). * inst/WORDLIST: add VAA, wbareacomi, waterbody, mis, errored so the spelling test stays clean with the new hf_check_invariants() documentation.
1 parent fe1b4d0 commit 0e9dfd7

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

inst/WORDLIST

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Reproject
5757
SPDX
5858
SYS
5959
Strahler
60+
VAA
6061
VPU
6162
WKB
6263
WKT
@@ -82,6 +83,7 @@ dm
8283
dplyr
8384
duckdb
8485
env
86+
errored
8587
erroring
8688
extdata
8789
filesystem
@@ -115,6 +117,7 @@ lon
115117
lynker
116118
magrittr
117119
mapshaper
120+
mis
118121
misalignments
119122
ngen
120123
nondecreasing
@@ -151,5 +154,7 @@ unsplit
151154
untagged
152155
vect
153156
vpus
157+
waterbody
158+
wbareacomi
154159
wcjochem
155160
xl

tests/testthat/test-invariants.R

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,74 @@ test_that("ngen stage detects a cycle in the fp->nexus->fp graph", {
201201
)
202202
})
203203

204+
test_that("ngen slope_valid requires strictly positive slope, all-NA is info", {
205+
base <- data.frame(flowpath_id = c("fp-1", "fp-2", "fp-3"),
206+
flowpath_toid = "0", stringsAsFactors = FALSE)
207+
208+
ok_fp <- transform(base, slope = c(0.01, 0.5, 0.002))
209+
expect_true(suppressMessages(hf_check_invariants("ngen", flowpaths = ok_fp,
210+
strict = FALSE))$checks$slope_valid$ok)
211+
212+
# a zero, a negative, and an NA each make it fail
213+
bad_fp <- transform(base, slope = c(0.01, 0, -1))
214+
expect_false(suppressMessages(hf_check_invariants("ngen", flowpaths = bad_fp,
215+
strict = FALSE))$checks$slope_valid$ok)
216+
217+
na_fp <- transform(base, slope = NA_real_)
218+
res <- suppressMessages(hf_check_invariants("ngen", flowpaths = na_fp,
219+
strict = FALSE))
220+
expect_identical(res$checks$slope_valid$kind, "info")
221+
222+
# absent column -> no check emitted
223+
expect_null(suppressMessages(hf_check_invariants("ngen", flowpaths = base,
224+
strict = FALSE))$checks$slope_valid)
225+
})
226+
227+
test_that("ngen So_valid checks flowpaths, flowlines, and the network table", {
228+
fp <- data.frame(flowpath_id = c("fp-1", "fp-2"), flowpath_toid = "0",
229+
So = c(0.003, 0.004), stringsAsFactors = FALSE)
230+
expect_true(suppressMessages(hf_check_invariants("ngen", flowpaths = fp,
231+
strict = FALSE))$checks$So_valid$ok)
232+
233+
fp_bad <- transform(fp, So = c(0.003, 0))
234+
expect_false(suppressMessages(hf_check_invariants("ngen", flowpaths = fp_bad,
235+
strict = FALSE))$checks$So_valid$ok)
236+
237+
# So carried only on the network attribute table is still validated
238+
fp_no_so <- fp[, c("flowpath_id", "flowpath_toid")]
239+
network <- data.frame(flowpath_id = c("fp-1", "fp-2"), So = c(0.01, -1))
240+
expect_false(suppressMessages(hf_check_invariants("ngen", flowpaths = fp_no_so,
241+
network = network, strict = FALSE))$checks$So_valid$ok)
242+
})
243+
244+
test_that("ngen lake_spatial_consistent flags reaches far from their lake", {
245+
skip_if_not_installed("sf")
246+
lakes <- sf::st_sf(
247+
lake_id = "L1",
248+
geometry = sf::st_sfc(.sq(5000, 5000, r = 500), crs = 5070))
249+
250+
# fp-1 sits inside the lake (distance 0); fp-2 is ~70 km away but stamped L1
251+
flowpaths <- sf::st_sf(
252+
flowpath_id = c("fp-1", "fp-2"),
253+
flowpath_toid = "0",
254+
lake_id = c("L1", "L1"),
255+
geometry = sf::st_sfc(.ls(5000, 5000, 5200, 5000),
256+
.ls(75000, 75000, 75200, 75000), crs = 5070))
257+
258+
res <- suppressMessages(hf_check_invariants("ngen", flowpaths = flowpaths,
259+
lakes = lakes, strict = FALSE))
260+
expect_false(res$checks$lake_spatial_consistent$ok)
261+
262+
# drop the far reach -> the remaining stamp is plausible
263+
ok <- suppressMessages(hf_check_invariants("ngen",
264+
flowpaths = flowpaths[1, ], lakes = lakes, strict = FALSE))
265+
expect_true(ok$checks$lake_spatial_consistent$ok)
266+
267+
# no lakes supplied -> check is not emitted
268+
expect_null(suppressMessages(hf_check_invariants("ngen",
269+
flowpaths = flowpaths, strict = FALSE))$checks$lake_spatial_consistent)
270+
})
271+
204272
test_that("hf_check_attr_bounds flags out-of-range and skips absent/zero-valid", {
205273
df <- data.frame(
206274
smcmax_mean = c(0.4, 0.95, 0.2), # 0.95 > 0.9 -> 1 bad

0 commit comments

Comments
 (0)