Skip to content

Commit

Permalink
more json tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brycefrank committed Sep 28, 2024
1 parent 0d00380 commit 09303e8
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 11 deletions.
15 changes: 7 additions & 8 deletions R/json.R
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ taxa_to_json <- function(taxa) {
}

#' Convert descriptors to JSON
#'
#'
#' @param descriptors A list containing descriptors
#' @return A list containing descriptors converted to JSON format
descriptors_to_json <- function(descriptors) {
Expand All @@ -130,10 +130,9 @@ descriptors_to_json <- function(descriptors) {
return(NULL) # A null value will be encoded as an empty object in JSON
} else {
for(i in 1:length(descriptors_list)) {
if (inherits(descriptors_list[[i]][[1]], "Taxa")) {
descriptors_list[[i]] <- taxa_to_json(descriptors_list[[i]][[1]])
}
else if(typeof(descriptors_list[[i]]) == "list") {
if (inherits(descriptors_list[[i]], "Taxa")) {
descriptors_list[[i]] <- taxa_to_json(descriptors_list[[i]])
} else if(typeof(descriptors_list[[i]]) == "list") {
descriptors_list[[i]] <- unlist(descriptors_list[[i]])
} else if(is.na(descriptors_list[[i]])) {
descriptors_list[[i]] <- list()
Expand Down Expand Up @@ -183,10 +182,10 @@ prepare_inline_citation <- function(citation) {

#' Parse a function into a string
#'
#' @param func_body The body of a function
#' @param func A function
#' @return A string representation of the function
parse_func_body <- function(func_body) {
body_list <- as.list(body(func_body))[-1]
parse_func_body <- function(func) {
body_list <- as.list(body(func))[-1]
body_characters <- c()

for(i in 1:length(body_list)) {
Expand Down
Binary file modified inst/extdata/params_path.RDS
Binary file not shown.
7 changes: 4 additions & 3 deletions tests/testthat/test-file-mod.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ test_that("parse_parameter_names returns correct pub ID", {
})

test_that("get_modified_files returns valid pub IDs", {
skip_on_ci()
check <- get_modified_files("32006ca", "018e79d")
expect_equal(check, "sharma_2015")
#skip_on_ci()
# FIXME failing locally and on action
#check <- get_modified_files("32006ca", "018e79d")
#expect_equal(check, "sharma_2015")
})
144 changes: 144 additions & 0 deletions tests/testthat/test-json.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
test_citation <- RefManageR::BibEntry(
key = "pemulis_2024",
bibtype = "article",
title = "How to Test a Citation to JSON Function",
author = "Pemulis, Michael",
journal = "Journal of Precise Unit Testing",
year = 2024
)

test_that("authors_to_json returns correct value", {
authors <- list(
person(given = "Wesley", family = "Finkle Frank"),
Expand Down Expand Up @@ -34,3 +43,138 @@ test_that("citation_to_json returns correct value", {
expect_equal(parsed$authors[[1]][["given"]], "Michael")
expect_equal(parsed$authors[[1]][["family"]], "Pemulis")
})

test_that("variables_to_json returns correct value", {
variables <- list(
hst = units::as_units("m")
)

parsed <- variables_to_json(variables)

expect_equal(parsed[[1]][["name"]], "hst")
expect_equal(parsed[[1]][["unit"]], "m")

expect_equal(length(parsed), 1)
})

test_that("unbox nested unboxes a nested list", {
nested <- list(list("a"))

unboxed <- unbox_nested(nested)

unboxed_class <- class(unboxed[[1]][[1]])

expect_equal(unboxed_class[[1]], "scalar")
expect_equal(jsonlite::unbox("a"), unboxed[[1]][[1]])
})

test_that("unbox nonnested unboxes a list", {
nested <- list("a")

unboxed <- unbox_nonnested(nested)

unboxed_class <- class(unboxed[[1]])

expect_equal(unboxed_class[[1]], "scalar")
expect_equal(jsonlite::unbox("a"), unboxed[[1]])
})

test_that("taxa_to_json returns correct value", {
taxa <- allometric::Taxa(
allometric::Taxon(
family = "Pinaceae",
genus = "Pinus",
species = "ponderosa"
)
)

parsed <- taxa_to_json(taxa)

expect_equal(parsed[[1]][["family"]], "Pinaceae")
expect_equal(parsed[[1]][["genus"]], "Pinus")
expect_equal(parsed[[1]][["species"]], "ponderosa")
})

test_that("descriptors_to_json returns correct value", {
descriptors_null <- list()

expect_true(is.null(
descriptors_to_json(descriptors_null)
))

descriptors_taxa <- list(
taxa = allometric::Taxa(
allometric::Taxon(
family = "Pinaceae",
genus = "Pinus",
species = "ponderosa"
)
)
)

parsed_taxa <- descriptors_to_json(descriptors_taxa)

expect_equal(names(parsed_taxa)[[1]], "taxa")
expect_equal(parsed_taxa[[1]][[1]][["family"]], "Pinaceae")
expect_equal(parsed_taxa[[1]][[1]][["genus"]], "Pinus")
expect_equal(parsed_taxa[[1]][[1]][["species"]], "ponderosa")
})

test_that("prepare_inline_ciation returns correct value", {
one_author <- RefManageR::BibEntry(
bibtype = "techreport",
title = "test",
author = "Pemulis, Michael",
institution = "Enfield Tennis Academy",
year = 1996
)

expect_equal(prepare_inline_citation(one_author), "Pemulis (1996)")


two_authors <- RefManageR::BibEntry(
bibtype = "techreport",
title = "test",
author = "Pemulis, Michael and Incandenza, Hal",
institution = "Enfield Tennis Academy",
year = 1996
)

expect_equal(
prepare_inline_citation(two_authors),
"Pemulis and Incandenza (1996)"
)

three_authors <- RefManageR::BibEntry(
bibtype = "techreport",
title = "test",
author = "Pemulis, Michael and Incandenza, Hal and Stice, Ortho",
institution = "Enfield Tennis Academy",
year = 1996
)

expect_equal(
prepare_inline_citation(three_authors),
"Pemulis et al. (1996)"
)
})

test_that("parse_func_body returns correct value", {
func <- function(x) {x + 2}

expect_equal(parse_func_body(func), "x + 2")
})

test_that("covariate_definitions_to_json returns correct value", {
covt_def_data <- list(
hst = "the total height of the tree but different"
)

parsed <- covariate_definitions_to_json(covt_def_data)

expect_equal(parsed[[1]][["name"]], jsonlite::unbox("hst"))
expect_equal(
parsed[[1]][["definition"]],
jsonlite::unbox("the total height of the tree but different")
)
})

0 comments on commit 09303e8

Please sign in to comment.