tests/testthat/test-arg-contracts.R

# Argument contracts.
#
# These lock the public signatures and the input validation that the rest of
# the package relies on. They deliberately avoid TensorFlow where possible:
# every validation they exercise runs before Python is initialised, so they
# also pass on a machine with no TensorFlow installed.

test_that("fit_swrc keeps val_df as its third argument", {
  # monotone was briefly inserted ahead of val_df, which silently redirected
  # fit_swrc(train, x, valid) into the architecture switch. Positions 1-11
  # are part of the public contract; new switches go after them.
  fo <- names(formals(fit_swrc))
  expect_identical(fo[1:3], c("train_df", "x_inputs", "val_df"))
  expect_true(match("monotone", fo) > match("K", fo))
})

test_that("fit_swrc rejects a non-logical monotone before reaching Python", {
  df <- data.frame(clay = runif(20, 5, 50), silt = runif(20, 5, 40),
                   theta_n = runif(20), matric_head = 10^runif(20, 0, 4))
  x  <- c("clay", "silt")

  # A 1x1 data frame is the dangerous case: `if (!monotone)` evaluates it
  # without error and selects the free head.
  expect_error(fit_swrc(df, x, monotone = data.frame(a = 0)), "is.logical")
  expect_error(fit_swrc(df, x, monotone = NA),                "is.na")
  expect_error(fit_swrc(df, x, monotone = c(TRUE, FALSE)),     "length")
  expect_error(fit_swrc(df, x, monotone = "TRUE"),            "is.logical")
})

test_that("fit_swrc validates train_df, x_inputs and val_df", {
  df <- data.frame(clay = runif(20, 5, 50), silt = runif(20, 5, 40))
  expect_error(fit_swrc("not a data frame", c("clay", "silt")), "is.data.frame")
  expect_error(fit_swrc(df, character(0)),                      "x_inputs")
  expect_error(fit_swrc(df, c("clay", "silt"), val_df = 1:10),   "val_df")
})

test_that("s3_form only accepts the two documented forms", {
  df <- data.frame(clay = runif(20, 5, 50), silt = runif(20, 5, 40))
  expect_error(fit_swrc(df, c("clay", "silt"), s3_form = "nonsense"),
               "should be one of")
  # the published form is the default
  expect_identical(eval(formals(fit_swrc)$s3_form)[1], "inequality")
  expect_identical(eval(formals(compute_physics_loss)$s3_form)[1], "inequality")
})

test_that("build_swrc_model validates monotone before building a graph", {
  expect_error(build_swrc_model(n_covariates = 5L, monotone = NA), "is.na")
  expect_error(build_swrc_model(n_covariates = 5L, monotone = "yes"),
               "is.logical")
  expect_error(build_swrc_model(n_covariates = 5L, monotone = logical(0)),
               "length")
})

test_that("knot_grid must agree with K", {
  df <- data.frame(clay = runif(30, 5, 50), silt = runif(30, 5, 40),
                   theta_n = runif(30), matric_head = 10^runif(30, 0, 4))
  x  <- c("clay", "silt")
  sc <- fit_minmax(df, x)

  # Supplying both inconsistently used to recycle the knot column silently.
  expect_error(
    make_obs_matrices(df, x, sc, K = 16L, knot_grid = seq(0, 1, length.out = 32)),
    "length\\(knot_grid\\)"
  )
  expect_error(
    make_profile_array(df, x, sc, K = 16L, knot_grid = seq(0, 1, length.out = 8)),
    "length\\(knot_grid\\)"
  )
  # and the defaults track K, so the common case still works
  expect_silent(make_obs_matrices(df, x, sc, K = 16L))
})

# Note: argument/documentation agreement is not tested here. roxygen2
# generates \usage from the formals and \arguments from @param, and
# `R CMD check` reports "Undocumented arguments" and "Documented arguments
# not in \usage" for any disagreement, so a test reading the installed help
# would only duplicate it -- less reliably.

# ---------------------------------------------------------------------------
# Missing observations must never reach the loss.
#
# The data loss is a sum over the batch, so one non-finite target makes the
# loss NaN, every gradient NaN and every weight NaN after a single step. The
# model then predicts NaN for everything while training reports no error.
# These are TensorFlow-free: they test the matrices, which is where the
# non-finite values used to get through.
# ---------------------------------------------------------------------------

test_that("make_obs_matrices drops rows that cannot be trained on", {
  df <- data.frame(
    clay = c(20, 30, 40, 50), silt = c(30, 30, 30, 30),
    matric_head = c(10, 100, 1000, 10000),
    theta_n = c(0.3, NA, 0.2, 0.1)
  )
  x  <- c("clay", "silt")
  sc <- fit_minmax(df, x)
  expect_message(m <- make_obs_matrices(df, x, sc, K = 8L), "dropped 1 of 4")
  expect_equal(dim(m$Xseq)[1], 3L)
  expect_equal(attr(m, "n_dropped"), 1L)
  expect_equal(attr(m, "rows_kept"), c(1L, 3L, 4L))
  expect_true(all(is.finite(m$y)))
})

test_that("make_obs_matrices catches non-finite covariates and heads too", {
  base <- data.frame(clay = c(20, 30, 40), silt = c(30, 30, 30),
                     matric_head = c(10, 100, 1000),
                     theta_n = c(0.3, 0.25, 0.2))
  x <- c("clay", "silt")

  bad_cov <- base; bad_cov$clay[2] <- NA
  sc <- fit_minmax(base, x)
  expect_message(m1 <- make_obs_matrices(bad_cov, x, sc, K = 8L), "dropped 1 of 3")
  expect_equal(dim(m1$Xseq)[1], 2L)

  bad_head <- base; bad_head$matric_head[3] <- Inf
  expect_message(m2 <- make_obs_matrices(bad_head, x, sc, K = 8L), "dropped 1 of 3")
  expect_true(all(is.finite(m2$pf)))
})

test_that("make_obs_matrices refuses data it cannot use at all", {
  df <- data.frame(clay = c(20, 30), silt = c(30, 30),
                   matric_head = c(10, 100), theta_n = c(NA, NA))
  sc <- fit_minmax(df, c("clay", "silt"))
  expect_error(suppressMessages(make_obs_matrices(df, c("clay", "silt"), sc, K = 8L)),
               "No usable observations")

  no_theta <- data.frame(clay = 20, silt = 30, matric_head = 10)
  expect_error(make_obs_matrices(no_theta, c("clay", "silt"), sc, K = 8L),
               "no `theta_n` column")
})

test_that("the bundled example data yields finite training targets", {
  # swrc_example carries 48 missing water contents by design. Before 0.1.8
  # they reached the loss and destroyed every weight on the first step.
  df <- prepare_swrc_data(swrc_example, depth_col = "depth")
  expect_gt(sum(is.na(df$theta_n)), 0)      # the missingness is really there

  x  <- c("clay", "silt", "bd_gcm3", "soc", "Depth_num")
  sc <- fit_minmax(df, x)
  m  <- suppressMessages(make_obs_matrices(df, x, sc, K = 16L))

  expect_true(all(is.finite(m$y)))
  expect_true(all(is.finite(m$Xseq)))
  expect_true(all(is.finite(m$pf)))
  expect_true(all(is.finite(m$w)))
  expect_equal(dim(m$Xseq)[1], nrow(df) - attr(m, "n_dropped"))
})

Try the soilFlux package in your browser

Any scripts or data that you put into this service are public.

soilFlux documentation built on Dec. 4, 2026, 5:10 p.m.