From 55f17e91c2fdfa742b2de5856fa006605e88fb24 Mon Sep 17 00:00:00 2001 From: Fausto Lopez <92821116+flopez-bao@users.noreply.github.com> Date: Mon, 15 May 2023 09:37:04 -0400 Subject: [PATCH] Update getMetadataEndpoint.R (#114) * Update getMetadataEndpoint.R - added removal of nas to return NA value instead of erroring out * linting -fix linting * Update getAnalytics.Rd * add unit test add unit test pointed at play for NA handling --------- Co-authored-by: sam-bao --- R/getMetadataEndpoint.R | 2 +- man/getAnalytics.Rd | 2 +- .../2.37.2/api/categoryOptions.json-28e9ae.json | 12 ++++++++++++ tests/testthat/test-getMetadataEndpoint.r | 8 ++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 tests/testthat/play.dhis2.org/2.37.2/api/categoryOptions.json-28e9ae.json diff --git a/R/getMetadataEndpoint.R b/R/getMetadataEndpoint.R index 238e6e0c..ffee209c 100644 --- a/R/getMetadataEndpoint.R +++ b/R/getMetadataEndpoint.R @@ -149,7 +149,7 @@ duplicateResponse <- function(resp, expand, by) { unique_values <- unique(values) #break up url to multiple calls if needed - if (sum(nchar(unique_values)) > 2000) { + if (sum(nchar(unique_values), na.rm = TRUE) > 2000) { values_list <- .splitUrlComponent(unique_values, 2000) diff --git a/man/getAnalytics.Rd b/man/getAnalytics.Rd index f91ccf59..e482208f 100644 --- a/man/getAnalytics.Rd +++ b/man/getAnalytics.Rd @@ -19,7 +19,7 @@ getAnalytics( return_names = FALSE, d2_session = dynGet("d2_default_session", inherits = TRUE), retry = 1, - timeout = 60, + timeout = 180, verbose = FALSE, quiet = TRUE ) diff --git a/tests/testthat/play.dhis2.org/2.37.2/api/categoryOptions.json-28e9ae.json b/tests/testthat/play.dhis2.org/2.37.2/api/categoryOptions.json-28e9ae.json new file mode 100644 index 00000000..a23ced81 --- /dev/null +++ b/tests/testthat/play.dhis2.org/2.37.2/api/categoryOptions.json-28e9ae.json @@ -0,0 +1,12 @@ +{ + "categoryOptions": [ + { + "name": "10-14", + "id": "jcGQdcpPSJP" + }, + { + "name": "35-39", + "id": "R32YPF38CJJ" + } + ] +} diff --git a/tests/testthat/test-getMetadataEndpoint.r b/tests/testthat/test-getMetadataEndpoint.r index 23296176..af3b97e7 100644 --- a/tests/testthat/test-getMetadataEndpoint.r +++ b/tests/testthat/test-getMetadataEndpoint.r @@ -730,3 +730,11 @@ test_that( testthat::expect_s3_class(resp, "data.frame") rm(resp) }) + +# test NAs are handled properly +httptest::with_mock_api({ + # na value is passed to the api to test handling + age_option_uid <- c(NA, "R32YPF38CJJ", "jcGQdcpPSJP") + res <- datimutils::getCatOptions(age_option_uid, d2_session = play2372) + testthat::expect_identical(res, c(NA, "35-39", "10-14")) +})