Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 0 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
# ATdatabase 0.0.1

* Start of development
36 changes: 36 additions & 0 deletions R/list_doc.R
Original file line number Diff line number Diff line change
@@ -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)
}
25 changes: 25 additions & 0 deletions man/list_doc.Rd

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

30 changes: 30 additions & 0 deletions tests/testthat/test-list_doc.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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)
})
Loading