test_that("get_fit returns underlying lm model", { dat <- data.frame( x = 1:20, y = 2 + 3 * (1:20) + rnorm(20) ) m <- fit_biomix( y ~ x, data = dat ) fit <- get_fit(m) expect_true(inherits(fit, "lm")) }) test_that("get_fit works for ordinary BioMixModel", { m <- fit_biomix( y ~ x, data.frame( y = rnorm(20), x = rnorm(20) ) ) expect_s3_class(get_fit(m), "lm") }) # --------------------------------------------------------- # Invalid object # --------------------------------------------------------- test_that("get_fit rejects non-BioMixModel objects", { expect_error( get_fit(lm(y ~ x, data = data.frame( y = rnorm(10), x = rnorm(10) ))), "model must be a BioMixModel object" ) expect_error( get_fit(list(fit = "something")), "model must be a BioMixModel object" ) expect_error( get_fit(NULL), "model must be a BioMixModel object" ) }) # --------------------------------------------------------- # Fallback to fixed component # --------------------------------------------------------- test_that("get_fit returns fixed component when fit is NULL", { m <- structure( list( fit = NULL, fixed = c( intercept = 2, slope = 3 ) ), class = c("biomix_test", "biomix") ) result <- get_fit(m) expect_equal( result, c( intercept = 2, slope = 3 ) ) expect_type(result, "double") }) # --------------------------------------------------------- # No fitted model available # --------------------------------------------------------- test_that("get_fit errors when fit and fixed are both unavailable", { m <- structure( list( fit = NULL, fixed = NULL ), class = "biomix" ) expect_error( get_fit(m), "No fitted model found" ) }) # --------------------------------------------------------- # Missing fit component # --------------------------------------------------------- test_that("get_fit errors when no fit component exists", { m <- structure( list( fixed = NULL ), class = "biomix" ) expect_error( get_fit(m), "No fitted model found" ) }) # --------------------------------------------------------- # Missing fixed component # --------------------------------------------------------- test_that("get_fit errors when fit and fixed are missing", { m <- structure( list(), class = "biomix" ) expect_error( get_fit(m), "No fitted model found" ) }) get_fit <- function(model) { if (!inherits(model, "biomix")) { stop("model must be a BioMixModel object.", call. = FALSE) } if (!is.null(model$fit)) { return(model$fit) } if (!is.null(model$fixed)) { return(model$fixed) } stop("No fitted model found.", call. = FALSE) } # --------------------------------------------------------- # Invalid object # --------------------------------------------------------- test_that("get_fit rejects non-BioMixModel objects", { expect_error( get_fit(lm(y ~ x, data = data.frame( y = rnorm(10), x = rnorm(10) ))), "model must be a BioMixModel object" ) expect_error( get_fit(list(fit = "something")), "model must be a BioMixModel object" ) expect_error( get_fit(NULL), "model must be a BioMixModel object" ) }) # --------------------------------------------------------- # Fallback to fixed component # --------------------------------------------------------- test_that("get_fit returns fixed component when fit is NULL", { m <- structure( list( fit = NULL, fixed = c( intercept = 2, slope = 3 ) ), class = c("biomix_test", "biomix") ) result <- get_fit(m) expect_equal( result, c( intercept = 2, slope = 3 ) ) expect_type(result, "double") }) # --------------------------------------------------------- # No fitted model available # --------------------------------------------------------- test_that("get_fit errors when fit and fixed are both unavailable", { m <- structure( list( fit = NULL, fixed = NULL ), class = "biomix" ) expect_error( get_fit(m), "No fitted model found" ) }) # --------------------------------------------------------- # Missing fit component # --------------------------------------------------------- test_that("get_fit errors when no fit component exists", { m <- structure( list( fixed = NULL ), class = "biomix" ) expect_error( get_fit(m), "No fitted model found" ) }) # --------------------------------------------------------- # Missing fixed component # --------------------------------------------------------- test_that("get_fit errors when fit and fixed are missing", { m <- structure( list(), class = "biomix" ) expect_error( get_fit(m), "No fitted model found" ) })