Skip to content

Commit

Permalink
Merge pull request #161 from RobLBaker/main
Browse files Browse the repository at this point in the history
add test_content_units
  • Loading branch information
RobLBaker authored Mar 8, 2025
2 parents d743e15 + 88dd6b4 commit 395992f
Show file tree
Hide file tree
Showing 16 changed files with 227 additions and 7 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Imports:
httr,
jsonlite,
QCkit,
EMLeditor,
lifecycle
Remotes:
https://github.com/NCEAS/arcticdatautils,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export(load_data)
export(load_metadata)
export(run_congruence_checks)
export(test_attribute_defs)
export(test_content_units)
export(test_creator)
export(test_cui_dissemination)
export(test_datatable_urls)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# DPchecker 1.0.1 (under development)
## 2025-03-07
* Add `test_content_units()` function to test for the presence of NPS content unit links. Add `test_content_units()` function to list of functions run by `run_congruence_checks()`. Add unit tests for `test_content_units()`. Add documentation about `test_content_units()` to the Articles.
* Add EMLeditor as a dependency to support unit tests for `test_content_units()`.
## 2025-02-25
* Update `CONTRIBUTING.md`
## 2025-02-22
Expand Down
69 changes: 67 additions & 2 deletions R/optional_eml_elements.R
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ test_public_points <- function(metadata = load_metadata(directory)){
cli::cli_inform(c("v" = "CUI is set to PUBLIC and all GPS coordinates will be publicly available."))
}
else {
# if CUI is not public, warn the user that GPS coordiantes will be public.
# if CUI is not public, warn the user that GPS coordinates will be public.
cli::cli_warn(c("!" = "CUI is not set to PUBLIC. GPS coordinates detected in metadata will be publicly available. Are you sure?"))
}
}
Expand Down Expand Up @@ -545,7 +545,7 @@ test_project <- function (metadata = load_metadata(directory)) {
proj_test <- unlist(proj)
DS_proj <- "id" %in% names(proj_test)
if (!DS_proj) {
msg <- paste0("No project associated with the metadata. ",
msg <- paste0("No DataStore project associated with the metadata. ",
"To add a DataStore project, use ",
"{.fun EMLeditor::set_project}.")
cli::cli_warn(c("!" = msg))
Expand All @@ -554,5 +554,70 @@ test_project <- function (metadata = load_metadata(directory)) {

msg <- "The metadata contains at least one DataStore Project reference."
cli::cli_inform(c("v" = msg))
return(invisible(metadata))
}

#' Test for content unit links
#'
#' @inheritParams test_pub_date
#'
#' @return invisible(metadata)
#' @export
#'
#' @examples
#' \dontrun{
#' test_content_units()
#' }
test_content_units <- function(metadata = load_metadata(directory)) {
#check whether EML is schema valid
is_eml(metadata)

#get geographic coverage from metadata
geo_cov <- metadata[["dataset"]][["coverage"]][["geographicCoverage"]]

#drop `@context` item from geo_cov
geo_cov$`@context` <- NULL

# If there's only geographic coverage element, geo_cov ends up with one less level of nesting. Re-nest it so that the rest of the code works consistently
if ("geographicDescription" %in% names(geo_cov)) {
geo_cov <- list(geo_cov)
}

#test for existence of geography; if absent warn and suggest solution
if (is.null(geo_cov)) {
msg1 <- "Metadata does not contain park content unit links."
msg <- "to add content unit links."
cli::cli_warn(
c("!" = "{msg1}, Use {.fn EMLeditor::set_content_units} {msg}"))
}

#if geography is present:
else {
units <- FALSE
for (i in 1:length(seq_along(geo_cov))) {
#test for content unit links:
if (grepl("NPS Content Unit Link:",
geo_cov[[i]]$geographicDescription)) {
units <- TRUE
}
# exit the for-loop after the first instance of a content unit:
if (units == TRUE) {
break
}
}
# if content units are present, pass the test
if (units) {
cli::cli_inform(c("v" = "Metadata contains NPS content unit links."))
}
#if content units are not present, fail with a warning and suggest solution
if (!units) {
msg1 <- "Metadata does not contain park content unit links."
msg <- "to add content unit links."
cli::cli_warn(
c("!" = "{msg1}, Use {.fn EMLeditor::set_content_units} {msg}"))

}
}

return(invisible(metadata))
}
9 changes: 9 additions & 0 deletions R/run_checks.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ run_congruence_checks <- function(directory = here::here(),
warn_count <<- warn_count + 1
cli::cli_bullets(c(w$message, w$body))
})
tryCatch(test_content_units(metadata),
error = function(e) {
err_count <<- err_count + 1
cli::cli_bullets(c(e$message, e$body))
},
warning = function(w) {
warn_count <<- warn_count + 1
cli::cli_bullets(c(w$message, w$body))
})
tryCatch(test_doi(metadata),
error = function(e) {
err_count <<- err_count + 1
Expand Down
1 change: 1 addition & 0 deletions docs/articles/DPchecker.html

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

6 changes: 4 additions & 2 deletions docs/news/index.html

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

2 changes: 1 addition & 1 deletion docs/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ pkgdown: 2.1.1
pkgdown_sha: ~
articles:
DPchecker: DPchecker.html
last_built: 2025-02-25T21:14Z
last_built: 2025-03-07T18:37Z
6 changes: 6 additions & 0 deletions docs/reference/index.html

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

4 changes: 3 additions & 1 deletion docs/reference/run_congruence_checks.html

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

84 changes: 84 additions & 0 deletions docs/reference/test_content_units.html

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

2 changes: 1 addition & 1 deletion docs/search.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<url><loc>/reference/run_congruence_checks.html</loc></url>
<url><loc>/reference/test_attribute_defs.html</loc></url>
<url><loc>/reference/test_by_for_nps.html</loc></url>
<url><loc>/reference/test_content_units.html</loc></url>
<url><loc>/reference/test_creator.html</loc></url>
<url><loc>/reference/test_cui_dissemination.html</loc></url>
<url><loc>/reference/test_datatable_urls.html</loc></url>
Expand Down
22 changes: 22 additions & 0 deletions man/test_content_units.Rd

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

22 changes: 22 additions & 0 deletions tests/testthat/test-optional_eml_elements.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,25 @@ test_that("test_project warns when no project is present",
"No project associated with the metadata.*")
}
)

# ---- test_content_units
test_that("test_content_units warns when no content units links are present",
{
expect_warning(test_content_units(metadata = bicy_meta),
"Metadata does not contain park content unit*")
})

test_that("test_content_units passes when content unit links are present",
{
meta2 <- EMLeditor::set_content_units(bicy_meta, "ROMO")
expect_message(test_content_units(meta2),
"Metadata contains NPS content unit links.")
})

test_that("test_content_units warns when no geography present",
{
meta <- bicy_meta
meta[["dataset"]][["coverage"]][["geographicCoverage"]] <- NULL
expect_warning(test_content_units(metadata = meta),
"Metadata does not contain park content unit links.*")
})
1 change: 1 addition & 0 deletions vignettes/DPchecker.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ These tests determine whether the metadata is schema valid and adheres to some r
* Metadata indicates that data files do not have footers ([`test_footer()`](../reference/test_footer.html))
* Metadata contains taxonomic coverage element ([`test_taxonomic_cov()`](../reference/test_taxonomic_cov.html))
* Metadata contains geographic coverage element ([`test_geographic_cov()`](../reference/test_geographic_cov.html))
* Metadata contain NPS content unit links ([test_content_units()](../reference/test_content_units.html))
* Metadata contains a Digital Object Identifier (DOI) ([`test_doi()`](../reference/test_doi.html))
* Metadata DOI is properly formatted ([`test_doi_format()`](../reference/test_doi_format.html))
* Metadata contains URLs for each data table ([test_datatable_urls](../reference/test_datatable_urls.html))
Expand Down

0 comments on commit 395992f

Please sign in to comment.