From 42f252558e7e0379446e0b759efbe3bf178fc584 Mon Sep 17 00:00:00 2001 From: Stephen Turner Date: Sun, 18 Aug 2024 13:31:18 -0400 Subject: [PATCH] add tests --- R/biorecap.R | 10 ++++++---- man/biorecap_report.Rd | 5 ++++- tests/testthat/test-biorecap.R | 21 +++++++++++++++++++++ 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/R/biorecap.R b/R/biorecap.R index 71a0d18..d392a71 100644 --- a/R/biorecap.R +++ b/R/biorecap.R @@ -81,7 +81,7 @@ get_preprints <- function(subject="all", baseurl="https://connect.biorxiv.org/bi dplyr::bind_rows(.id="subject") |> dplyr::select("subject", title="item_title", url="item_link", abstract="item_description") |> dplyr::mutate(dplyr::across(dplyr::everything(), trimws)) - if (nrow(preprints)<1L) stop("Something went wrong. No papers found for subject ", subject) + if (nrow(preprints)<1L) stop("Something went wrong. No papers found for subject ", subject) #nocov if (clean) { preprints <- @@ -229,6 +229,7 @@ tt_preprints <- function(preprints, cols=c("title", "summary"), width=c(1,3)) { #' @param nsentences Number of sentences to summarize each paper in. #' @param model The model to use for generating summaries. See [ollamar::list_models()]. #' @param use_example_preprints Use the example preprints data included with the package instead of fetching new data from bioRxiv. For diagnostic/testing purposes only. +#' @param ... Other arguments passed to [rmarkdown::render()]. #' #' @return Nothing; called for its side effects to produce a report. #' @export @@ -240,15 +241,16 @@ tt_preprints <- function(preprints, cols=c("title", "summary"), width=c(1,3)) { #' biorecap_report(subject=c("bioinformatics", "genomics", "synthetic_biology"), #' output_dir=output_dir) #' } -biorecap_report <- function(output_dir=".", subject=NULL, nsentences=2L, model="llama3.1", use_example_preprints=FALSE) { +biorecap_report <- function(output_dir=".", subject=NULL, nsentences=2L, model="llama3.1", use_example_preprints=FALSE, ...) { skeleton <- system.file("rmarkdown/templates/biorecap/skeleton/skeleton.Rmd", package="biorecap", mustWork = TRUE) output_dir <- normalizePath(output_dir) output_file <- paste0("biorecap-report-", format(Sys.time(), "%Y-%m-%d-%H%M%S"), ".html") output_csv <- file.path(output_dir, sub("\\.html$", ".csv", output_file)) if (!use_example_preprints && is.null(subject)) stop("You must provide a subject. See ?subjects.") - if (tools::file_ext(output_file) != "html") stop("Output file must have an .html extension.") + if (tools::file_ext(output_file) != "html") stop("Output file must have an .html extension.") #nocov rmarkdown::render(input=skeleton, output_file=output_file, output_dir=output_dir, - params=list(subject=subject, nsentences=nsentences, model=model, use_example_preprints=use_example_preprints, output_csv=output_csv)) + params=list(subject=subject, nsentences=nsentences, model=model, use_example_preprints=use_example_preprints, output_csv=output_csv), + ...) } diff --git a/man/biorecap_report.Rd b/man/biorecap_report.Rd index 14c349c..4934d67 100644 --- a/man/biorecap_report.Rd +++ b/man/biorecap_report.Rd @@ -9,7 +9,8 @@ biorecap_report( subject = NULL, nsentences = 2L, model = "llama3.1", - use_example_preprints = FALSE + use_example_preprints = FALSE, + ... ) } \arguments{ @@ -22,6 +23,8 @@ biorecap_report( \item{model}{The model to use for generating summaries. See \code{\link[ollamar:list_models]{ollamar::list_models()}}.} \item{use_example_preprints}{Use the example preprints data included with the package instead of fetching new data from bioRxiv. For diagnostic/testing purposes only.} + +\item{...}{Other arguments passed to \code{\link[rmarkdown:render]{rmarkdown::render()}}.} } \value{ Nothing; called for its side effects to produce a report. diff --git a/tests/testthat/test-biorecap.R b/tests/testthat/test-biorecap.R index 9ef0da4..d3b7ac6 100644 --- a/tests/testthat/test-biorecap.R +++ b/tests/testthat/test-biorecap.R @@ -18,5 +18,26 @@ test_that("add_prompt", { expect_true(inherits(res, "tbl")) expect_error(expect_warning(add_prompt(iris))) expect_error(add_prompt("invalid")) + expect_warning(add_prompt(data.frame(title=character(), abstract=character()))) + expect_error(add_prompt(data.frame(title=character()))) + expect_error(add_prompt(data.frame(abstract=character()))) }) +test_that("add_summary", { + expect_error(expect_warning(add_summary(data.frame()))) + expect_warning(add_summary(data.frame(prompt=character()))) + expect_silent(add_summary(structure(data.frame(prompt=character()), class=c("preprints_prompt", "data.frame")))) +}) + +test_that("tt_preprints", { + expect_silent(res <- tt_preprints(example_preprints[1:2,])) + expect_true(inherits(res, "tinytable")) +}) + +test_that("tt_preprints", { + output_dir <- tempdir() + expect_silent(biorecap_report(use_example_preprints=TRUE, output_dir=output_dir, quiet=TRUE)) + expect_error(biorecap_report(use_example_preprints=FALSE, output_dir=output_dir)) +}) + +