Skip to content

Commit

Permalink
Emit a message/warning when the search results are truncated.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Nov 25, 2024
1 parent b7ff3c2 commit 4005692
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: SewerRat
Version: 0.3.1
Date: 2024-11-06
Version: 0.3.2
Date: 2024-11-25
Title: Client for the SewerRat API
Description:
Search metadata files across a shared filesystem via the SewerRat API.
Expand Down
25 changes: 23 additions & 2 deletions R/query.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#' @param until A \link{POSIXt} object to filter out newer files, i.e., only files older than \code{until} will be retained.
#' If missing, no filtering is applied to remove new files.
#' @param number Integer specifying the maximum number of results to return.
#' @param on.truncation String specifying what to do when the number of results exceeds \code{number}.
#' Either \code{"warning"}, \code{"message"}, or \code{"none"}.
#' @param url String containing the URL to the SewerRat REST API.
#' @param results List containing the output of \code{query}.
#'
Expand Down Expand Up @@ -83,7 +85,8 @@
#' formatQueryResults(q)
#' @export
#' @import httr2
query <- function(text, user, path, from, until, url, number=100) {
#' @importFrom utils head
query <- function(text, user, path, from, until, url, number=100, on.truncation=c("message", "warning", "none")) {
conditions <- list()

if (!missing(text)) {
Expand Down Expand Up @@ -114,7 +117,13 @@ query <- function(text, user, path, from, until, url, number=100) {
stop("at least one search filter must be present")
}

stub <- paste0("/query?translate=true&limit=", number)
on.truncation <- match.arg(on.truncation)
if (on.truncation != "none") {
original.number <- number
number <- number + 1L
}

stub <- paste0("/query?translate=true")
collected <- list()

while (length(collected) < number) {
Expand All @@ -133,6 +142,18 @@ query <- function(text, user, path, from, until, url, number=100) {
}
}

if (on.truncation != "none") {
if (original.number < length(collected)) {
msg <- sprintf("truncated query results to the first %i matches", original.number)
if (on.truncation == "warning") {
warning(msg)
} else {
message(msg)
}
}
collected <- head(collected, original.number)
}

collected
}

Expand Down
14 changes: 13 additions & 1 deletion man/query.Rd

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

16 changes: 16 additions & 0 deletions tests/testthat/test-query.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ write(file=file.path(mydir, "diet", "metadata.json"),
# Registering it:
register(mydir, "metadata.json", url=info$url)

test_that("query works as expected", {
q <- query("lun*", url=info$url)
expect_gte(length(q), 2L)
})

test_that("query works with truncation", {
expect_message(q <- query("lun*", url=info$url, number=0), "truncated")
expect_identical(length(q), 0L)

expect_warning(q <- query("lun*", url=info$url, number=0, on.truncation="warning"), "truncated")
expect_identical(length(q), 0L)

expect_warning(q <- query("lun*", url=info$url, number=0, on.truncation="none"), NA)
expect_identical(length(q), 0L)
})

test_that("formatQueryResults works properly", {
q <- query("lun*", url=info$url)
res <- formatQueryResults(q)
Expand Down

0 comments on commit 4005692

Please sign in to comment.