From bf530279a1029d08a809578680c9c352edb130df Mon Sep 17 00:00:00 2001 From: jspijker Date: Thu, 27 Aug 2026 11:04:32 +0200 Subject: [PATCH 1/2] Add list_doc() to list meta document references by type Closes #25 Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- NAMESPACE | 1 + NEWS.md | 1 + R/list_doc.R | 36 ++++++++++++++++++++++++++++++++++ man/list_doc.Rd | 25 +++++++++++++++++++++++ tests/testthat/test-list_doc.R | 32 ++++++++++++++++++++++++++++++ 5 files changed, 95 insertions(+) create mode 100644 R/list_doc.R create mode 100644 man/list_doc.Rd create mode 100644 tests/testthat/test-list_doc.R diff --git a/NAMESPACE b/NAMESPACE index 10abc7d..330390d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -10,4 +10,5 @@ export(drop_database_tables) export(get_doc) export(get_download_ranges) export(insert_location_info) +export(list_doc) export(remove_doc) diff --git a/NEWS.md b/NEWS.md index ee596bf..f178849 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,4 @@ # ATdatabase 0.0.1 * Start of development +* Add `list_doc()` to list all references of meta documents for a given type (#25) diff --git a/R/list_doc.R b/R/list_doc.R new file mode 100644 index 0000000..aa24ab3 --- /dev/null +++ b/R/list_doc.R @@ -0,0 +1,36 @@ +#' Lists all references for a meta-document type +#' +#' This function lists the references (ref) of all meta-data documents +#' in the database for a given type. +#' +#' @param type character string with type +#' @param conn database connection object +#' +#' @return character vector of references (ref) for the given type, or +#' NA if no documents of that type exist +#' +#' A meta-data document is an R object which is stored in the database. +#' Each object has a type and reference (ref) so they are findable. +#' This function returns all references belonging to a certain type. +#' +#' @export +#' + + +list_doc <- function(type, conn) { + + if(!is.character(type)) { + stop("ERROR list_doc: type is not character") + } + + qry <- glue::glue_sql("SELECT ref FROM meta WHERE type={type};", + .con = conn) + + res <- pool::dbGetQuery(conn, qry) + if(length(res$ref) == 0) { + ref <- NA + } else { + ref <- res$ref + } + return(ref) +} diff --git a/man/list_doc.Rd b/man/list_doc.Rd new file mode 100644 index 0000000..a2a83ff --- /dev/null +++ b/man/list_doc.Rd @@ -0,0 +1,25 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/list_doc.R +\name{list_doc} +\alias{list_doc} +\title{Lists all references for a meta-document type} +\usage{ +list_doc(type, conn) +} +\arguments{ +\item{type}{character string with type} + +\item{conn}{database connection object} +} +\value{ +character vector of references (ref) for the given type, or +NA if no documents of that type exist + +A meta-data document is an R object which is stored in the database. +Each object has a type and reference (ref) so they are findable. +This function returns all references belonging to a certain type. +} +\description{ +This function lists the references (ref) of all meta-data documents +in the database for a given type. +} diff --git a/tests/testthat/test-list_doc.R b/tests/testthat/test-list_doc.R new file mode 100644 index 0000000..76ad9f5 --- /dev/null +++ b/tests/testthat/test-list_doc.R @@ -0,0 +1,32 @@ +test_that("list_doc input", { + + create_database_tables(dbconn) + + expect_error(list_doc(type = 1, conn = dbconn)) + + drop_database_tables(dbconn) + +}) + + + +test_that("list_doc output", { + + create_database_tables(dbconn) + x <- data.frame(x = rnorm(5), y = rnorm(5)) + + add_doc(type = "test", ref = "a", doc = x, conn = dbconn) + add_doc(type = "test", ref = "b", doc = x, conn = dbconn) + add_doc(type = "other", ref = "c", doc = x, conn = dbconn) + + refs <- list_doc(type = "test", conn = dbconn) + expect_equal(sort(refs), c("a", "b")) + + refs2 <- list_doc(type = "other", conn = dbconn) + expect_equal(refs2, "c") + + refs3 <- list_doc(type = "nonexistent", conn = dbconn) + expect_true(is.na(refs3)) + + drop_database_tables(dbconn) +}) From e0a8bb57adf357948e92345af79b94292899066b Mon Sep 17 00:00:00 2001 From: Spijker Date: Thu, 27 Aug 2026 11:12:39 +0200 Subject: [PATCH 2/2] Changed some code lay-out, #25 --- NEWS.md | 2 -- R/list_doc.R | 4 ++-- tests/testthat/test-list_doc.R | 32 +++++++++++++++----------------- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/NEWS.md b/NEWS.md index f178849..b648db6 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,2 @@ # ATdatabase 0.0.1 -* Start of development -* Add `list_doc()` to list all references of meta documents for a given type (#25) diff --git a/R/list_doc.R b/R/list_doc.R index aa24ab3..8d3159e 100644 --- a/R/list_doc.R +++ b/R/list_doc.R @@ -19,7 +19,7 @@ list_doc <- function(type, conn) { - if(!is.character(type)) { + if (!is.character(type)) { stop("ERROR list_doc: type is not character") } @@ -27,7 +27,7 @@ list_doc <- function(type, conn) { .con = conn) res <- pool::dbGetQuery(conn, qry) - if(length(res$ref) == 0) { + if (length(res$ref) == 0) { ref <- NA } else { ref <- res$ref diff --git a/tests/testthat/test-list_doc.R b/tests/testthat/test-list_doc.R index 76ad9f5..d10c49d 100644 --- a/tests/testthat/test-list_doc.R +++ b/tests/testthat/test-list_doc.R @@ -1,10 +1,8 @@ test_that("list_doc input", { - create_database_tables(dbconn) - - expect_error(list_doc(type = 1, conn = dbconn)) - - drop_database_tables(dbconn) + create_database_tables(dbconn) + expect_error(list_doc(type = 1, conn = dbconn)) + drop_database_tables(dbconn) }) @@ -12,21 +10,21 @@ test_that("list_doc input", { test_that("list_doc output", { - create_database_tables(dbconn) - x <- data.frame(x = rnorm(5), y = rnorm(5)) + create_database_tables(dbconn) + x <- data.frame(x = rnorm(5), y = rnorm(5)) - add_doc(type = "test", ref = "a", doc = x, conn = dbconn) - add_doc(type = "test", ref = "b", doc = x, conn = dbconn) - add_doc(type = "other", ref = "c", doc = x, conn = dbconn) + add_doc(type = "test", ref = "a", doc = x, conn = dbconn) + add_doc(type = "test", ref = "b", doc = x, conn = dbconn) + add_doc(type = "other", ref = "c", doc = x, conn = dbconn) - refs <- list_doc(type = "test", conn = dbconn) - expect_equal(sort(refs), c("a", "b")) + refs <- list_doc(type = "test", conn = dbconn) + expect_equal(sort(refs), c("a", "b")) - refs2 <- list_doc(type = "other", conn = dbconn) - expect_equal(refs2, "c") + refs2 <- list_doc(type = "other", conn = dbconn) + expect_equal(refs2, "c") - refs3 <- list_doc(type = "nonexistent", conn = dbconn) - expect_true(is.na(refs3)) + refs3 <- list_doc(type = "nonexistent", conn = dbconn) + expect_true(is.na(refs3)) - drop_database_tables(dbconn) + drop_database_tables(dbconn) })