Skip to content

Commit

Permalink
Add more version info on build, assign class traits.build (#126)
Browse files Browse the repository at this point in the history
* Ensure call for SHA works when not in a git repo
* Add traits.build version into main metadata (for #124)
* Add tests for util_get_SHA and adding `resource_type` to main metadata
* Assign output as class traits.build

---------

Co-authored-by: yangsophieee <[email protected]>
  • Loading branch information
dfalster and yangsophieee authored Nov 16, 2023
1 parent 9a9be4c commit b5e9696
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 7 deletions.
5 changes: 4 additions & 1 deletion R/get.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,8 @@ util_get_version <- function(path = "config/metadata.yml") {
#' @return 40-digit SHA character string for the latest commit to the repository
#' @export
util_get_SHA <- function(path = ".") {
git2r::sha(git2r::last_commit(git2r::repository(path)))
sha <- tryCatch({
git2r::sha(git2r::last_commit(git2r::repository(path)))
}, error = function(cond) {NA})
sha
}
31 changes: 28 additions & 3 deletions R/process.R
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,28 @@ dataset_process <- function(filename_data_raw,
traits %>% dplyr::filter(!(!is.na(.data$error) & (.data$error == "Missing value")))
}

# Todo - resource_metadata
# - Add contributors
# Update metadata
metadata <- resource_metadata

if (is.null(metadata[["related_identifiers"]][1])) {
metadata[["related_identifiers"]] <- list()
}

metadata[["related_identifiers"]] <-
util_append_to_list(
metadata[["related_identifiers"]],
list(
related_identifier_type = "url",
identifier = "https://github.com/traitecoevo/traits.build",
relation_type = "isCompiledBy",
resource_type = "software",
version = as.character(packageVersion("traits.build"))
)
)


# Combine for final output
ret <-
list(
traits = traits %>% dplyr::filter(is.na(.data$error)) %>% dplyr::select(-dplyr::all_of(c("error", "unit_in"))),
locations = locations,
Expand All @@ -271,9 +289,13 @@ dataset_process <- function(filename_data_raw,
sources = sources,
definitions = definitions,
schema = schema,
metadata = resource_metadata,
metadata = metadata,
build_info = list(session_info = utils::sessionInfo())
)

class(ret) <- c("list", "traits.build")

ret
}

#' Build dataset
Expand Down Expand Up @@ -1858,6 +1880,9 @@ build_combine <- function(..., d = list(...)) {
session_info = utils::sessionInfo()
)
)

class(ret) <- c("list", "traits.build")

ret
}

Expand Down
2 changes: 1 addition & 1 deletion R/testdata.R
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ dataset_test_worker <-
}

expect_list <- function(data, info) {
expect_true(class(data) == "list", info = sprintf("%s - is not a list", info))
expect_true("list" %in% class(data), info = sprintf("%s - is not a list", info))
}

expect_list_names_valid <- function(data, info, label) {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ expect_not_NA <- function(object, info = NULL, label = NULL) {


expect_list <- function(data, info) {
expect_true(class(data) == "list", info = info)
expect_true("list" %in% class(data), info = info)
}


Expand Down
1 change: 1 addition & 0 deletions tests/testthat/test-process.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ test_that("`dataset_process` is working", {
expect_no_error(austraits_names <- schema$austraits$elements %>% names())
expect_no_error(x <- dataset_process(test_data, test_config, schema, resource_metadata, unit_conversions))
expect_type(x, "list")
expect_equal(class(x), c("list", "traits.build"))
expect_length(x, 13)
expect_named(x, austraits_names)
expect_equal(nrow(x$excluded_data), 0)
Expand Down
25 changes: 24 additions & 1 deletion tests/testthat/test-setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,9 @@ test_that("`build_setup_pipeline` is working", {
expect_true(file.copy("data/Test_2022/test-metadata.yml", "data/Test_2022/metadata.yml", overwrite = TRUE))

expect_no_error(zip::unzip("config/testgit.zip"))
expect_no_error(sha <- git2r::sha(git2r::last_commit()))
# Expect no error if not within a git repo
expect_no_error(sha <- util_get_SHA("../../.."))
expect_no_error(sha <- util_get_SHA())
# Expect error if path or method is wrong
expect_error(build_setup_pipeline(path = "Datas"))
expect_error(build_setup_pipeline(method = "grrrr"))
Expand Down Expand Up @@ -666,8 +668,29 @@ test_that("`build_setup_pipeline` is working", {
expect_equal(austraits$build_info$git_SHA, sha)
expect_equal(austraits$build_info$git_SHA, "6c73238d8d048781d9a4f5239a03813be313f0dd")

# Check output lists have required parts
## Todo add mode here

## sources

## metadata

## schema
expect_equal(austraits$schema, get_schema())

## Compiled by traits.build
traits.build_tag <- last(austraits$metadata$related_identifiers)
expected_output <- list(
related_identifier_type = "url",
identifier = "https://github.com/traitecoevo/traits.build",
relation_type = "isCompiledBy",
resource_type = "software",
version = as.character(packageVersion("traits.build"))
)
expect_equal(traits.build_tag, expected_output)
#expect_length(austraits_raw$taxa, 14) #not valid test with new `dataset_update_taxonomy setup`
#expect_length(austraits$taxa, 14) #not valid test with new `dataset_update_taxonomy setup`

expect_equal(nrow(austraits$taxa), nrow(austraits_raw$taxa))

# Compare products from three methods, except `build_info`
Expand Down

0 comments on commit b5e9696

Please sign in to comment.