Skip to content

Commit 42cb1a5

Browse files
author
alex-omophub
committed
Add tests for error handling in resolve_batch with NULL and structured list errors
- Introduced tests to cover scenarios where resolve_batch handles NULL errors and missing error fields, ensuring proper mapping to NA_character_ in status_detail. - Added coverage for structured list errors, verifying that the first usable error message, code, or detail is returned correctly in status_detail. - Enhanced overall test coverage for the resolve_batch function to improve robustness and reliability.
1 parent 6ee8ffa commit 42cb1a5

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

tests/testthat/test-fhir.R

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,3 +507,68 @@ test_that("resolve_batch(as_tibble = TRUE) handles unusual scalar error shapes",
507507
expect_equal(nrow(tbl), 2L)
508508
expect_equal(tbl$status_detail, c("42", "TRUE"))
509509
})
510+
511+
test_that("resolve_batch(as_tibble = TRUE) handles NULL and missing error", {
512+
# Coverage for the `is.null(err)` early return: a failed item with no
513+
# `error` field at all maps to NA_character_ in status_detail.
514+
base_req <- httr2::request("https://api.omophub.com/v1")
515+
resource <- FhirResource$new(base_req)
516+
517+
local_mocked_bindings(
518+
perform_post = function(req, path, body = NULL, query = NULL) {
519+
list(
520+
results = list(
521+
list(input = list(system = "http://snomed.info/sct", code = "a")),
522+
list(error = NULL)
523+
),
524+
summary = list(total = 2L, resolved = 0L, failed = 2L)
525+
)
526+
}
527+
)
528+
529+
codings <- list(
530+
list(system = "http://snomed.info/sct", code = "a"),
531+
list(system = "http://snomed.info/sct", code = "b")
532+
)
533+
534+
tbl <- resource$resolve_batch(codings, as_tibble = TRUE)
535+
expect_equal(nrow(tbl), 2L)
536+
expect_true(all(tbl$status == "failed"))
537+
expect_true(all(is.na(tbl$status_detail)))
538+
})
539+
540+
test_that("resolve_batch(as_tibble = TRUE) extracts message/code from list error", {
541+
# Coverage for the structured-list error branch: picks the first
542+
# usable of err$message, err$code, err$detail and returns it as
543+
# status_detail rather than a flattened name=value concatenation.
544+
base_req <- httr2::request("https://api.omophub.com/v1")
545+
resource <- FhirResource$new(base_req)
546+
547+
local_mocked_bindings(
548+
perform_post = function(req, path, body = NULL, query = NULL) {
549+
list(
550+
results = list(
551+
list(error = list(
552+
code = "concept_not_found",
553+
message = "No matching OMOP concept"
554+
)),
555+
list(error = list(code = "vocabulary_restricted")), # message missing
556+
list(error = list(detail = "just a detail field")) # only detail
557+
),
558+
summary = list(total = 3L, resolved = 0L, failed = 3L)
559+
)
560+
}
561+
)
562+
563+
codings <- list(
564+
list(system = "http://snomed.info/sct", code = "a"),
565+
list(system = "http://snomed.info/sct", code = "b"),
566+
list(system = "http://snomed.info/sct", code = "c")
567+
)
568+
569+
tbl <- resource$resolve_batch(codings, as_tibble = TRUE)
570+
expect_equal(nrow(tbl), 3L)
571+
expect_equal(tbl$status_detail[1], "No matching OMOP concept") # message wins
572+
expect_equal(tbl$status_detail[2], "vocabulary_restricted") # code fallback
573+
expect_equal(tbl$status_detail[3], "just a detail field") # detail fallback
574+
})

0 commit comments

Comments
 (0)