Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenturner committed Aug 18, 2024
1 parent 91ee1e0 commit 42f2525
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
10 changes: 6 additions & 4 deletions R/biorecap.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 <-
Expand Down Expand Up @@ -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
Expand All @@ -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),
...)
}
5 changes: 4 additions & 1 deletion man/biorecap_report.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions tests/testthat/test-biorecap.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})


0 comments on commit 42f2525

Please sign in to comment.