tests/testthat/test-api.R

library(EMLaide)
library(vcr)
library(testthat)
# Define variables -------------------------------------------------------------
eml_file_path <- "edi.678.1.xml"
user_id <- "ecain"

# Test data package id reservation ----------------------------------------------
test_that("reserve_edi_id() errors when arguments are not supplied", {
  expect_error(reserve_edi_id(user_id = user_id), "argument \"password\" is missing, with no default")
  expect_error(reserve_edi_id(password = Sys.getenv("APIKEY")), "argument \"user_id\" is missing, with no default")
})

test_that("reserve_edi_id() returns an edi number", {
  vcr::use_cassette("reserve_edi_id_responses", {
    id <- reserve_edi_id(user_id, Sys.getenv("APIKEY"))
  })
  expect_type(id, "character")
})

# Test data package evaluation function ----------------------------------------
# TODO fix 500 error on evaluate test (no longer have access to API Key)
test_that("evaluate_edi_package() errors when arguments are not supplied", {
  expect_error(evaluate_edi_package(user_id = user_id, password = Sys.getenv("APIKEY")), "argument \"eml_file_path\" is missing, with no default")
  expect_error(evaluate_edi_package(password = Sys.getenv("APIKEY")))
})

test_that("evaluate_edi_package() returns the anticipated response object", {
  expected_return <- structure(list(Status = c("valid", "valid", "valid", "valid", 
                                               "valid", "valid", "valid", "valid", "info", "info", "info", "valid", 
                                               "valid", "warn", "valid", "valid", "valid", "valid", "info", 
                                               "info", "valid", "valid", "warn", "valid", "valid", "valid", 
                                               "valid", "valid"), 
                                    `Element Checked` = c("packageId pattern matches \"scope.identifier.revision\"", 
                                                                                        "EML version 2.1.0 or beyond", "Document is schema-valid EML", 
                                                                                        "Document is EML parser-valid", "Dereferenced document is schema-valid EML", 
                                                                                        "keyword element is present", "A 'methods' element is present", 
                                                                                        "coverage element is present", "geographicCoverage is present", 
                                                                                        "taxonomicCoverage is present", "temporalCoverage is present", 
                                                                                        "An alternateIdentifier with a DOI system attribute that looks like it is generated by PASTA should not be present", 
                                                                                        "Dataset title length is at 5 least words.", "check to see if funding element is present", 
                                                                                        "Dataset abstract element is a minimum of 20 words", "There are no duplicate entity names", 
                                                                                        "Length of entityName is not excessive (less than 100 char)", 
                                                                                        "An entity description is present", "'numHeaderLines' element is present", 
                                                                                        "'numFooterLines' element is present", "Field delimiter is a single character", 
                                                                                        "Record delimiter is present", "Is the numberOfRecords element present?", 
                                                                                        "Check for presence of an entity size element", "A physical/authentication element is present and specifies a method attribute with a value of MD5 or SHA-1", 
                                                                                        "dateTime/formatString specified in metadata is from a preferred set of values", 
                                                                                        "dateTime/formatString specified in metadata is from a preferred set of values", 
                                                                                        "Attribute names are unique"), 
                                    `Suggestion to fix/imporve` = c("",  "", "", "", "", "", "", "", 
                                                                    "If sampling EML is used within methods, does that obviate geographicCoverage? Or should those sites be repeated or referenced?", 
                                                                    "Could search title, abstract, keywords for any taxonomic name (huge). Could search keywordType=\"taxonomic\".", 
                                                                    "Most datasets have a temporal range.", "", "", "Include the funding source, please.", 
                                                                    "", "Declare a non-empty entityName and ensure that there are no duplicate entityName values in the document", 
                                                                    "", "", "", "Add 'numFooterLines' element if needed.", "", "", 
                                                                    "Add a numberOfRecords element and store the number of entity records",  "Include an entity size in bytes", 
                                                                    "Add a physical/authentication element and store the entity checksum or hash value in it using a method such as MD5 or SHA-1.", 
                                                                    "Modify the dateTime/formatString, selecting from among the preferred values one that best matches the data format", 
                                                                    "Modify the dateTime/formatString, selecting from among the preferred values one that best matches the data format", "")), 
                               row.names = c(NA, -28L), class = c("tbl_df", "tbl", "data.frame" ))

  expect_equal(evaluate_edi_package(user_id = user_id, 
                                    password = Sys.getenv("APIKEY"), 
                                    eml_file_path = eml_file_path), expected_return)
})

# TODO add tests for update and upload functions
CVPIA-OSC/EMLaide documentation built on Aug. 25, 2023, 8:53 a.m.