From 8b96e8e963711a5588d2cbaebaaafc47593213e8 Mon Sep 17 00:00:00 2001 From: roninsightrx Date: Tue, 20 Oct 2026 14:17:58 -0700 Subject: [PATCH] Fix logic bugs found while adding test coverage (#62) - luna_run: allow a NULL nmfe fallback so an unset tools.nonmem.nmfe no longer aborts when tools.modelfit.nmfe is set (or neither is set). - luna_note/luna_tag: always write the run back, so clear = TRUE without a note actually clears; abort when the run id is not in the project. - runs_as_table/luna_list: return an empty table for projects without runs. - luna_new_project: define models when import_models = FALSE, use seq_along(), and give template-based projects their metadata. - luna_clone: check the cache up front, abort when the source model (or the run folder for update_inits) is missing, and check file.copy(). - update_cache: use full folder paths for folder timestamps and avoid using the project path as a regex. - log_get_last_event: return NULL on an empty log; luna_help checks for NULL before using the event. - luna_ind: use ceiling() for the number of pages so partial pages work. - find_file_with_fallback: abort regardless of verbose; luna_tables now opts out explicitly to keep its own message. - get_flag_from_config: honour the default argument. - luna_gof: plot the selected residual (CWRES or NPDE). Co-Authored-By: Claude Opus 5.5 --- DESCRIPTION | 2 +- R/luna_clone.R | 23 ++++++++++++++---- R/luna_gof.R | 7 +++--- R/luna_help.R | 4 ++-- R/luna_ind.R | 6 +++-- R/luna_list.R | 4 ++-- R/luna_log.R | 6 ++++- R/luna_new_project.R | 22 ++++++++++-------- R/luna_note.R | 16 +++++++------ R/luna_run.R | 6 ++++- R/luna_tables.R | 5 ++-- R/misc.R | 13 +++++------ R/runs_as_table.R | 2 +- R/update_cache.R | 10 ++++---- man/luna_clone.Rd | 5 +++- tests/testthat/test-cache.R | 25 ++++++++++++++++++++ tests/testthat/test-luna_clone.R | 25 ++++++++++++++++++++ tests/testthat/test-luna_gof.R | 15 ++++++++++++ tests/testthat/test-luna_help.R | 4 ++-- tests/testthat/test-luna_ind.R | 24 +++++++++++++++++++ tests/testthat/test-luna_list.R | 6 +++++ tests/testthat/test-luna_log.R | 8 +++++++ tests/testthat/test-luna_new_project.R | 28 ++++++++++++++++++++++ tests/testthat/test-luna_note.R | 16 +++++++++++++ tests/testthat/test-luna_run.R | 32 ++++++++++++++++++++++++-- tests/testthat/test-luna_tag.R | 8 +++++++ tests/testthat/test-misc.R | 6 +++++ tests/testthat/test-runs_as_table.R | 7 ++++++ 28 files changed, 281 insertions(+), 54 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 5e3dd52..195cf7e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: luna Type: Package Title: Light-weight Universal NLME Admininstrator -Version: 0.2.1 +Version: 0.2.2 Author: Ron Keizer Maintainer: Ron Keizer Description: Organizes model development workflows in NONMEM and nlmixr2. The diff --git a/R/luna_clone.R b/R/luna_clone.R index fa1d360..e9d03de 100644 --- a/R/luna_clone.R +++ b/R/luna_clone.R @@ -3,7 +3,10 @@ #' @inheritParams luna_run #' @param new_id new run id #' @param update_inits use the final estimates from the source model -#' as initial estimates for the cloned model. Default `FALSE` +#' as initial estimates for the cloned model. Default `FALSE`. This requires +#' the source model to have been run, i.e. `//run.mod` and its +#' results must exist. Without `update_inits`, the model file `/.mod` +#' is copied. #' #' @export #' @@ -17,11 +20,21 @@ luna_clone <- function( ) { id <- validate_id(id) new_id <- validate_id(new_id) + is_luna_cache_available(abort = TRUE) if(is.null(folder)) { folder <- .luna_cache$get("project")$metadata$folder } old_file <- file.path(folder, paste0(id, ".mod")) new_file <- file.path(folder, paste0(new_id, ".mod")) + if(update_inits) { + ## final estimates are read from the run folder created by luna_run() + source_file <- file.path(folder, id, "run.mod") + } else { + source_file <- old_file + } + if(!file.exists(source_file)) { + cli::cli_abort("Source model file {source_file} not found.") + } if(file.exists(new_file)) { if(force) { if(verbose) { @@ -35,18 +48,21 @@ luna_clone <- function( cli::cli_alert_info("Cloning model {id} to {new_id}") } if(!update_inits) { - file.copy( + copied <- file.copy( old_file, new_file, overwrite = TRUE ) + if(!copied) { + cli::cli_abort("Could not copy {old_file} to {new_file}.") + } } else { cli::cli_alert_info("Updating parameters in cloned model") ## TODO: this currently doesn't work, due to a Pharmpy bug. ## Therefore, we'll be using PsN for now pharmpy_works <- FALSE if(pharmpy_works) { - model <- pharmr::read_model(old_file) + model <- pharmr::read_model(source_file) info <- luna_info(id) parameters <- replace_list_elements( model$parameters$inits, @@ -74,7 +90,6 @@ luna_clone <- function( if(verbose) { cli::cli_alert_info("Updating project YAML") } - is_luna_cache_available(abort = TRUE) ## make sure we're up to date name <- .luna_cache$get("project")$metadata$name folder <- .luna_cache$get("project")$metadata$folder diff --git a/R/luna_gof.R b/R/luna_gof.R index d51ebfd..c52773e 100644 --- a/R/luna_gof.R +++ b/R/luna_gof.R @@ -18,6 +18,7 @@ luna_gof <- function( verbose = TRUE ) { + residual <- match.arg(residual) tab <- get_table_for_plots( id = id, folder = folder, @@ -40,19 +41,19 @@ luna_gof <- function( ggplot2::xlab("Prediction") + ggplot2::ylab("Obervation (DV)") + theme() - ## CWRES vs TIME / PRED + ## Residual (CWRES/NPDE) vs TIME / PRED p2 <- tab |> dplyr::filter(MDV == 0) |> tidyr::pivot_longer(cols = c(TIME, PRED)) |> ggplot2::ggplot( - ggplot2::aes(x = value, y = CWRES) + ggplot2::aes(x = value, y = .data[[residual]]) ) + ggplot2::geom_point() + ggplot2::geom_smooth(method = smooth_method, formula = y ~ x) + ggplot2::geom_hline(yintercept = 0, linetype = "dotted") + ggplot2::facet_wrap(~name, scales = "free") + ggplot2::xlab("") + - ggplot2::ylab("Obervation (DV)") + + ggplot2::ylab(residual) + theme() ## Add log event diff --git a/R/luna_help.R b/R/luna_help.R index 46ff3d6..b232fca 100644 --- a/R/luna_help.R +++ b/R/luna_help.R @@ -26,11 +26,11 @@ luna_help <- function( is_luna_cache_available(abort = TRUE) if(is.null(prompt)) { event <- log_get_last_event() - if(verbose) - cli::cli_alert_info("Getting help on {event$event} using {model}") if(is.null(event)) { cli::cli_abort("No recent event found, please call `luna_help()` with a question.") } + if(verbose) + cli::cli_alert_info("Getting help on {event$event} using {model}") } chat <- ellmer::chat_openai( system_prompt = paste0( diff --git a/R/luna_ind.R b/R/luna_ind.R index 2d32758..ad14e68 100644 --- a/R/luna_ind.R +++ b/R/luna_ind.R @@ -113,13 +113,15 @@ luna_ind <- function( start_idx <- set_len * (page-1) + 1 end_idx <- set_len * page - max_pages <- floor(length(ids) / (ncol * nrow)) + max_pages <- ceiling(length(ids) / set_len) if(page > max_pages) { cli::cli_abort("Sorry, only {max_pages} pages available.") } if(end_idx > length(ids)) { - cli::cli_alert_info("Only first {set_len} IDs will be shown.") end_idx <- length(ids) + if(verbose) { + cli::cli_alert_info("Only {end_idx - start_idx + 1} IDs left to show on page {page}.") + } } if(verbose) { cli::cli_alert_info("Showing page {page}/{max_pages}.") diff --git a/R/luna_list.R b/R/luna_list.R index a36987e..9f64a4b 100644 --- a/R/luna_list.R +++ b/R/luna_list.R @@ -27,8 +27,8 @@ luna_list <- function( project <- .luna_cache$get("project") out <- runs_as_table(project, raw = raw) - ## select columns - if(!raw) { + ## select columns (an empty project has no columns to select from) + if(!raw && nrow(out) > 0) { cols <- config$general$list$columns if(!is.null(cols)) { sel_cols <- names(cols)[unlist(cols)] diff --git a/R/luna_log.R b/R/luna_log.R index d760e7e..e9356b1 100644 --- a/R/luna_log.R +++ b/R/luna_log.R @@ -57,5 +57,9 @@ log_add <- function( #' Get last event from log, as list object #' log_get_last_event <- function() { - .luna_cache$get("log")[[length(.luna_cache$get("log"))]] + luna_log <- .luna_cache$get("log", missing = NULL) + if(length(luna_log) == 0) { + return(NULL) + } + luna_log[[length(luna_log)]] } diff --git a/R/luna_new_project.R b/R/luna_new_project.R index 928dd94..9e94cf8 100644 --- a/R/luna_new_project.R +++ b/R/luna_new_project.R @@ -58,25 +58,27 @@ luna_new_project <- function( # create a project object from template if(!is.null(template)) { template <- stringr::str_replace(template, "\\.yaml$", "") - project <- read_yaml_safe( + project_yaml <- read_yaml_safe( file.path(system.file("templates", package = "luna"), paste0(template, ".yaml")) ) } else { - project <- list( - yaml = list( - project = list( - ) - ), - metadata = list( - name = name, - folder = folder + project_yaml <- list( + project = list( ) ) } + project <- list( + yaml = project_yaml, + metadata = list( + name = name, + folder = folder + ) + ) project$yaml$project$description <- description ## Import models, if present in folder and requested + models <- character(0) if(import_models) { models <- stringr::str_replace_all( dir(folder, pattern = "\\.mod$"), @@ -86,7 +88,7 @@ luna_new_project <- function( stringr::str_sort(numeric = TRUE) project$yaml$runs <- list() } - for(i in 1:length(models)) { + for(i in seq_along(models)) { model_file <- file.path(folder, paste0(models[i], ".mod")) if(file.exists(model_file)) { project$yaml$runs[[i]] <- list( diff --git a/R/luna_note.R b/R/luna_note.R index c5a50d2..28be320 100644 --- a/R/luna_note.R +++ b/R/luna_note.R @@ -49,6 +49,10 @@ luna_note <- function( return(run[[element]]) } else { # add or clear notes/tags + if(is.null(run)) { + cli::cli_abort("Run {id} not found in project {name}.") + } + ## Clear existing notes if(clear) { run[[element]] <- NULL @@ -59,13 +63,11 @@ luna_note <- function( run[[element]], note ) - if(!is.null(note)) { - project$yaml$runs <- insert_entry( - x = project$yaml$runs, - id = id, - entry = run - ) - } + project$yaml$runs <- insert_entry( + x = project$yaml$runs, + id = id, + entry = run + ) ## Update cache .luna_cache$set("project", project) diff --git a/R/luna_run.R b/R/luna_run.R index 8553162..d4beb5c 100644 --- a/R/luna_run.R +++ b/R/luna_run.R @@ -79,7 +79,11 @@ luna_run <- function( } console <- ifelse0(config$tools$modelfit$console, TRUE) if(is.null(nmfe)) { - nmfe <- ifelse0(config$tools$modelfit$nmfe, config$tools$nonmem$nmfe) + nmfe <- ifelse0( + config$tools$modelfit$nmfe, + config$tools$nonmem$nmfe, + allow_null = TRUE + ) } if(method == "nmfe") { if(is.null(nmfe) || identical(trimws(nmfe), "")) { diff --git a/R/luna_tables.R b/R/luna_tables.R index 0f5f0a2..13b8637 100644 --- a/R/luna_tables.R +++ b/R/luna_tables.R @@ -23,9 +23,10 @@ luna_tables <- function( folder, filename = file.path(id, paste0("run", ".mod")), fallback = paste0(id, ".mod"), - verbose = FALSE + verbose = FALSE, + abort = FALSE ) - if(! file.exists(model_file)) { + if(is.null(model_file)) { cli::cli_abort("Model file not found in run folder") } model <- pharmr::read_model(model_file) diff --git a/R/misc.R b/R/misc.R index ee0a0c1..0a78f91 100644 --- a/R/misc.R +++ b/R/misc.R @@ -109,8 +109,7 @@ find_file_with_fallback <- function(folder, filename, fallback, verbose = TRUE, f <- file.path(folder, fallback) if(! file.exists(f)) { if(abort) { - if(verbose) - cli::cli_abort("An expected file was not found.") + cli::cli_abort("An expected file was not found.") } else { return(NULL) } @@ -160,14 +159,14 @@ get_flag_from_config <- function( default = FALSE, config = NULL ) { - out <- default if(is.null(config)) { config <- get_luna_config() } - out <- isTRUE( - purrr::pluck(config, !!!flag) - ) - out + value <- purrr::pluck(config, !!!flag) + if(is.null(value)) { + return(default) + } + isTRUE(value) } #' Replace elements in a list, based on elements in a second list diff --git a/R/runs_as_table.R b/R/runs_as_table.R index cc0f4a5..caa8ec3 100644 --- a/R/runs_as_table.R +++ b/R/runs_as_table.R @@ -14,7 +14,7 @@ runs_as_table <- function( ## Print a table of models and their descriptions models <- x$yaml$runs - model_table <- data.frame() + full_table <- data.frame() if(length(models) > 0) { timestamps <- .luna_cache$get("timestamps") model_table <- data.frame( diff --git a/R/update_cache.R b/R/update_cache.R index e9a9c86..6e6867f 100644 --- a/R/update_cache.R +++ b/R/update_cache.R @@ -49,13 +49,11 @@ update_cache <- function( } ## Folders - dir_list <- stringr::str_replace_all( - list.dirs(folder, recursive = FALSE), - paste0(folder, "[\\/]"), - "" - ) + dir_list <- basename(list.dirs(folder, recursive = FALSE)) for(d in dir_list) { - timestamps$folders[[d]] <- get_time_last_updated_folder(d) + timestamps$folders[[d]] <- get_time_last_updated_folder( + file.path(folder, d) + ) } if(verbose) diff --git a/man/luna_clone.Rd b/man/luna_clone.Rd index dd9c984..57a5e19 100644 --- a/man/luna_clone.Rd +++ b/man/luna_clone.Rd @@ -22,7 +22,10 @@ is run.} \item{folder}{path to folder containing the model file. Default is current directory.} \item{update_inits}{use the final estimates from the source model -as initial estimates for the cloned model. Default \code{FALSE}} +as initial estimates for the cloned model. Default \code{FALSE}. This requires +the source model to have been run, i.e. \verb{//run.mod} and its +results must exist. Without \code{update_inits}, the model file \verb{/.mod} +is copied.} } \description{ Clone model diff --git a/tests/testthat/test-cache.R b/tests/testthat/test-cache.R index f54e1c1..980320b 100644 --- a/tests/testthat/test-cache.R +++ b/tests/testthat/test-cache.R @@ -49,3 +49,28 @@ test_that("update_cache records folder timestamps", { expect_true("run1" %in% names(ts$folders)) expect_match(ts$folders$run1, "^\\d{4}-") }) + +test_that("update_cache records folder timestamps outside the project folder", { + dir <- local_luna_project() + dir.create(file.path(dir, "run1")) + writeLines("x", file.path(dir, "run1", "out.txt")) + withr::local_dir(tempdir()) + update_cache(.luna_cache$get("project")) + ts <- .luna_cache$get("timestamps") + expect_match(ts$folders$run1, "^\\d{4}-") +}) + +test_that("update_cache handles regex special characters in folder path", { + local_luna_home() + local_no_luna_cache() + parent <- normalizePath(withr::local_tempdir()) + dir <- file.path(parent, "proj (v1.0)+") + dir.create(dir) + dir.create(file.path(dir, "run1")) + writeLines("x", file.path(dir, "run1", "out.txt")) + yaml::write_yaml(list(project = list(description = "d")), file.path(dir, "p.yaml")) + suppressMessages(luna_load_project(name = "p", folder = dir, verbose = FALSE)) + ts <- .luna_cache$get("timestamps") + expect_true("run1" %in% names(ts$folders)) + expect_match(ts$folders$run1, "^\\d{4}-") +}) diff --git a/tests/testthat/test-luna_clone.R b/tests/testthat/test-luna_clone.R index 54df9d3..5fbb236 100644 --- a/tests/testthat/test-luna_clone.R +++ b/tests/testthat/test-luna_clone.R @@ -43,6 +43,8 @@ test_that("luna_clone overwrites with force and warns when run already in YAML", test_that("luna_clone with update_inits calls PsN update_inits", { dir <- local_luna_project() + dir.create(file.path(dir, "run1")) + write_nm_model(file.path(dir, "run1", "run.mod")) mockery::stub(luna_clone, "luna_edit", function(id) NULL) psn <- mockery::mock(NULL) mockery::stub(luna_clone, "call_psn", psn) @@ -59,3 +61,26 @@ test_that("luna_clone with update_inits calls PsN update_inits", { yml <- yaml::read_yaml(file.path(dir, "testproj.yaml")) expect_equal(yml$runs[[3]]$id, "run4") }) + +test_that("luna_clone aborts when source model is missing", { + dir <- local_luna_project() + mockery::stub(luna_clone, "luna_edit", function(id) NULL) + expect_error(luna_clone("run9", "run10"), "not found") + expect_false(file.exists(file.path(dir, "run10.mod"))) + yml <- yaml::read_yaml(file.path(dir, "testproj.yaml")) + expect_length(yml$runs, 2) +}) + +test_that("luna_clone with update_inits aborts when source run folder is missing", { + dir <- local_luna_project() + mockery::stub(luna_clone, "luna_edit", function(id) NULL) + psn <- mockery::mock(NULL) + mockery::stub(luna_clone, "call_psn", psn) + expect_error(luna_clone("run1", "run4", update_inits = TRUE), "run.mod not found") + mockery::expect_called(psn, 0) +}) + +test_that("luna_clone aborts without luna cache", { + local_no_luna_cache() + expect_error(luna_clone("run1", "run2"), "Luna cache not found") +}) diff --git a/tests/testthat/test-luna_gof.R b/tests/testthat/test-luna_gof.R index 713bf08..1b3d3d7 100644 --- a/tests/testthat/test-luna_gof.R +++ b/tests/testthat/test-luna_gof.R @@ -14,3 +14,18 @@ test_that("luna_gof returns patchwork of gof plots and logs event", { expect_equal(last$event, "plot") expect_equal(last$context$plot, "gof") }) + +test_that("luna_gof plots the selected residual", { + skip_if_not_installed("ggplot2") + skip_if_not_installed("patchwork") + local_luna_project() + mockery::stub(luna_gof, "get_table_for_plots", fake_nm_table()) + p <- luna_gof("run1", residual = "NPDE", verbose = FALSE) + p2 <- p[[2]] + expect_equal(p2$labels$y, "NPDE") + built <- suppressWarnings(ggplot2::ggplot_build(p2)) + tab <- fake_nm_table() + expect_setequal(built$data[[1]]$y, tab$NPDE[tab$MDV == 0]) + p <- luna_gof("run1", verbose = FALSE) + expect_equal(p[[2]]$labels$y, "CWRES") +}) diff --git a/tests/testthat/test-luna_help.R b/tests/testthat/test-luna_help.R index dc8492d..618861d 100644 --- a/tests/testthat/test-luna_help.R +++ b/tests/testthat/test-luna_help.R @@ -57,6 +57,6 @@ test_that("luna_help aborts for other event types", { test_that("luna_help aborts when there is no event", { local_luna_project() - mockery::stub(luna_help, "log_get_last_event", NULL) - expect_error(luna_help(verbose = FALSE), "No recent event found") + .luna_cache$set("log", list()) + expect_error(luna_help(verbose = TRUE), "No recent event found") }) diff --git a/tests/testthat/test-luna_ind.R b/tests/testthat/test-luna_ind.R index 1cb660b..54de2d6 100644 --- a/tests/testthat/test-luna_ind.R +++ b/tests/testthat/test-luna_ind.R @@ -80,3 +80,27 @@ test_that("luna_ind plots only the selected subject_ids", { p <- luna_ind("run1", data = fake_nm_table(3), ncol = 1, nrow = 2, verbose = FALSE) expect_setequal(unique(p$data$ID), c(2, 3)) }) + +test_that("luna_ind shows a single partial page", { + setup_luna_ind() + mockery::stub(luna_ind, "element_blank", ggplot2::element_blank) + expect_message( + p <- luna_ind("run1", data = fake_nm_table(3), ncol = 2, nrow = 2), + "page 1/1" + ) + expect_setequal(unique(p$data$ID), 1:3) +}) + +test_that("luna_ind shows a partial last page", { + setup_luna_ind() + mockery::stub(luna_ind, "element_blank", ggplot2::element_blank) + expect_message( + p <- luna_ind("run1", data = fake_nm_table(3), ncol = 1, nrow = 2, page = 2), + "Only 1 IDs left" + ) + expect_equal(unique(p$data$ID), 3) + expect_error( + luna_ind("run1", data = fake_nm_table(3), ncol = 1, nrow = 2, page = 3), + "only 2 pages available" + ) +}) diff --git a/tests/testthat/test-luna_list.R b/tests/testthat/test-luna_list.R index 851b173..a5c3726 100644 --- a/tests/testthat/test-luna_list.R +++ b/tests/testthat/test-luna_list.R @@ -78,3 +78,9 @@ test_that("luna_list with raw = TRUE returns all information", { expect_s3_class(out, "tbl_df") expect_true(all(c("id", "reference", "tag", "notes", "dofv") %in% names(out))) }) + +test_that("luna_list works on a project without runs", { + local_luna_project(runs = list()) + expect_no_warning(out <- suppressMessages(luna_list())) + expect_equal(nrow(out), 0) +}) diff --git a/tests/testthat/test-luna_log.R b/tests/testthat/test-luna_log.R index deba4e6..2f763a0 100644 --- a/tests/testthat/test-luna_log.R +++ b/tests/testthat/test-luna_log.R @@ -44,3 +44,11 @@ test_that("luna_log returns data.frame of last n events", { out2 <- luna_log(n = 2) expect_equal(out2$event, c("error", "plot")) }) + +test_that("log_get_last_event returns NULL for empty log", { + local_luna_project() + .luna_cache$set("log", list()) + expect_null(log_get_last_event()) + .luna_cache$remove("log") + expect_null(log_get_last_event()) +}) diff --git a/tests/testthat/test-luna_new_project.R b/tests/testthat/test-luna_new_project.R index 905a148..859c11b 100644 --- a/tests/testthat/test-luna_new_project.R +++ b/tests/testthat/test-luna_new_project.R @@ -75,3 +75,31 @@ test_that("save_project writes yaml and respects force", { save_project(project, f, force = TRUE) expect_equal(yaml::read_yaml(f)$project$description, "e") }) + +test_that("luna_new_project with import_models = FALSE creates project without runs", { + dir <- local_new_project_folder() + write_nm_model(file.path(dir, "run1.mod"), "one") + suppressMessages( + luna_new_project("myproj", folder = dir, description = "x", import_models = FALSE) + ) + yml <- yaml::read_yaml(file.path(dir, "myproj.yaml")) + expect_null(yml$runs) +}) + +test_that("luna_new_project works in a folder without models", { + dir <- local_new_project_folder() + suppressMessages(luna_new_project("myproj", folder = dir, description = "x")) + yml <- yaml::read_yaml(file.path(dir, "myproj.yaml")) + expect_length(yml$runs, 0) +}) + +test_that("luna_new_project creates project from template", { + dir <- local_new_project_folder() + suppressMessages( + luna_new_project("myproj", folder = dir, description = "tmpl", template = "clean.yaml") + ) + yml <- yaml::read_yaml(file.path(dir, "myproj.yaml")) + expect_equal(yml$project$description, "tmpl") + expect_equal(.luna_cache$get("name"), "myproj") + expect_equal(.luna_cache$get("project")$metadata$folder, dir) +}) diff --git a/tests/testthat/test-luna_note.R b/tests/testthat/test-luna_note.R index 9689c7d..41a8ef5 100644 --- a/tests/testthat/test-luna_note.R +++ b/tests/testthat/test-luna_note.R @@ -44,3 +44,19 @@ test_that("luna_note uses custom element", { yml <- yaml::read_yaml(file.path(dir, "testproj.yaml")) expect_equal(yml$runs[[2]]$remarks, "abc") }) + +test_that("luna_note with clear = TRUE and no note clears notes", { + dir <- local_luna_project() + luna_note("run1", c("a", "b")) + luna_note("run1", clear = TRUE) + yml <- yaml::read_yaml(file.path(dir, "testproj.yaml")) + expect_null(yml$runs[[1]]$notes) + expect_null(pluck_entry(.luna_cache$get("project")$yaml$runs, "run1")$notes) +}) + +test_that("luna_note aborts when adding a note to an unknown run", { + dir <- local_luna_project() + expect_error(luna_note("run9", "a note"), "run9 not found") + yml <- yaml::read_yaml(file.path(dir, "testproj.yaml")) + expect_length(yml$runs, 2) +}) diff --git a/tests/testthat/test-luna_run.R b/tests/testthat/test-luna_run.R index e5c4104..6686782 100644 --- a/tests/testthat/test-luna_run.R +++ b/tests/testthat/test-luna_run.R @@ -3,10 +3,9 @@ fake_pharmpy_model <- function(dataset = data.frame(ID = 1)) { } run_config <- function(modelfit = list(method = "pharmpy", console = TRUE)) { - # tools.nonmem.nmfe is set because ifelse0() errors when the fallback is NULL list( general = list(runs = list(as_job = FALSE)), - tools = list(modelfit = modelfit, nonmem = list(nmfe = "/opt/nm/fallback")) + tools = list(modelfit = modelfit) ) } @@ -95,3 +94,32 @@ test_that("luna_run dispatches to luna_run_ferx for ferx method", { expect_equal(args$config$tools$modelfit$method, "ferx") mockery::expect_called(mock_read, 0) }) + +test_that("luna_run uses tools.modelfit.nmfe when tools.nonmem.nmfe is unset", { + local_luna_project(config = run_config(list(method = "pharmpy", nmfe = "/opt/nm/nmfe75"))) + mock_run <- mockery::mock("fit") + mockery::stub(luna_run, "pharmr::read_model", fake_pharmpy_model()) + mockery::stub(luna_run, "pharmr.extra::run_nlme", mock_run) + suppressMessages(luna_run("run1")) + expect_equal(mockery::mock_args(mock_run)[[1]]$nmfe, "/opt/nm/nmfe75") +}) + +test_that("luna_run falls back to tools.nonmem.nmfe", { + config <- run_config(list(method = "pharmpy")) + config$tools$nonmem <- list(nmfe = "/opt/nm/fallback") + local_luna_project(config = config) + mock_run <- mockery::mock("fit") + mockery::stub(luna_run, "pharmr::read_model", fake_pharmpy_model()) + mockery::stub(luna_run, "pharmr.extra::run_nlme", mock_run) + suppressMessages(luna_run("run1")) + expect_equal(mockery::mock_args(mock_run)[[1]]$nmfe, "/opt/nm/fallback") +}) + +test_that("luna_run without any nmfe configured passes NULL for pharmpy method", { + local_luna_project(config = run_config(list(method = "pharmpy"))) + mock_run <- mockery::mock("fit") + mockery::stub(luna_run, "pharmr::read_model", fake_pharmpy_model()) + mockery::stub(luna_run, "pharmr.extra::run_nlme", mock_run) + suppressMessages(luna_run("run1")) + expect_null(mockery::mock_args(mock_run)[[1]]$nmfe) +}) diff --git a/tests/testthat/test-luna_tag.R b/tests/testthat/test-luna_tag.R index 31f8052..848a16b 100644 --- a/tests/testthat/test-luna_tag.R +++ b/tests/testthat/test-luna_tag.R @@ -20,3 +20,11 @@ test_that("luna_tag clears tags before adding", { yml <- yaml::read_yaml(file.path(dir, "testproj.yaml")) expect_equal(yml$runs[[1]]$tag, "b") }) + +test_that("luna_tag with clear = TRUE and no tag clears tags", { + dir <- local_luna_project() + luna_tag("run1", "a") + luna_tag("run1", clear = TRUE) + yml <- yaml::read_yaml(file.path(dir, "testproj.yaml")) + expect_null(yml$runs[[1]]$tag) +}) diff --git a/tests/testthat/test-misc.R b/tests/testthat/test-misc.R index 1bd9b3c..3aa00d6 100644 --- a/tests/testthat/test-misc.R +++ b/tests/testthat/test-misc.R @@ -53,6 +53,10 @@ test_that("find_file_with_fallback finds file, fallback, or aborts", { expect_silent(find_file_with_fallback(dir, "none.txt", "fb.txt", verbose = FALSE)) expect_error(find_file_with_fallback(dir, "none.txt", "none2.txt"), "not found") expect_null(find_file_with_fallback(dir, "none.txt", "none2.txt", abort = FALSE)) + expect_error( + find_file_with_fallback(dir, "none.txt", "none2.txt", verbose = FALSE), + "not found" + ) }) test_that("read_yaml_safe parses `n` keys", { @@ -74,6 +78,8 @@ test_that("get_flag_from_config reads flag from given or global config", { conf <- list(tools = list(modelfit = list(force = TRUE))) expect_true(get_flag_from_config(config = conf)) expect_false(get_flag_from_config(c("tools", "other"), config = conf)) + expect_true(get_flag_from_config(c("tools", "other"), default = TRUE, config = conf)) + expect_false(get_flag_from_config(default = FALSE, config = conf |> purrr::assign_in(c("tools", "modelfit", "force"), FALSE))) local_luna_home(config = conf) local_no_luna_cache() expect_true(suppressMessages(get_flag_from_config())) diff --git a/tests/testthat/test-runs_as_table.R b/tests/testthat/test-runs_as_table.R index 5cc6dea..cb1b6e0 100644 --- a/tests/testthat/test-runs_as_table.R +++ b/tests/testthat/test-runs_as_table.R @@ -111,3 +111,10 @@ test_that("get_all_results returns empty tibble when nothing to parse", { expect_s3_class(out, "tbl_df") expect_equal(nrow(out), 0) }) + +test_that("runs_as_table returns empty table for project without runs", { + local_luna_project(runs = list()) + out <- runs_as_table(.luna_cache$get("project")) + expect_s3_class(out, "luna.run_table") + expect_equal(nrow(out), 0) +})