diff --git a/R/ct_check_params.R b/R/ct_check_params.R index 8a5e7bf..3884b35 100644 --- a/R/ct_check_params.R +++ b/R/ct_check_params.R @@ -251,19 +251,12 @@ check_flowCode <- function(flow_direction, update, verbose) { rlang::arg_match(flow_direction, values = valid_codes$text, multiple = TRUE) - # if one of the codes is not in the list of valid codes - # send stop signal and list problems - if (!all(flow_direction %in% valid_codes$text)) { - rlang::abort(paste0( - "The following services/commodity codes you provided are invalid: ", - paste0(setdiff(flow_direction, valid_codes$text), collapse = ", ") - )) - } else { + flow_direction <- valid_codes |> poorman::filter(text %in% flow_direction) |> poorman::pull(id) |> paste0(collapse = ",") - } + } else { flow_direction <- NULL } @@ -363,7 +356,7 @@ check_reporterCode <- function(reporter, update = FALSE, verbose = FALSE) { ) ## get multiple values or single values that are not 'all_countries' - if (length(reporter) > 1 | !any(reporter == "all_countries")) { + if (length(reporter) > 1 | !any(reporter %in% "all_countries")) { if (any(reporter == "all_countries")) { rlang::abort('"all_countries" can only be provided as a single argument.') } @@ -378,7 +371,7 @@ check_reporterCode <- function(reporter, update = FALSE, verbose = FALSE) { } # create proper ids for reporter Code - if (length(reporter) > 1 | !any(reporter == "all_countries")) { + if (length(reporter) > 1 | !any(reporter %in% "all_countries")) { reporter <- reporter_codes |> poorman::filter(iso_3 %in% reporter) |> poorman::pull(id) |> @@ -430,9 +423,9 @@ check_partnerCode <- function(partner, update = FALSE, verbose = FALSE) { ) - if (length(partner) > 1 | !any(partner == "all_countries")) { + if (length(partner) > 1 | !any(partner %in% "all_countries")) { partner <- stringr::str_squish(partner) - if (any(partner == "all_countries")) { + if (any(partner %in% "all_countries")) { rlang::abort('"all_countries" can only be provided as a single argument.') } # if one of the partnerCodes is not in the list of valid partnerCodes @@ -447,7 +440,7 @@ check_partnerCode <- function(partner, update = FALSE, verbose = FALSE) { } # create proper ids for partner - if (length(partner) > 1 | !any(partner == "all_countries")) { + if (length(partner) > 1 | !any(partner %in% "all_countries")) { partner <- partner_codes |> poorman::filter(iso_3 %in% partner) |> poorman::pull(id) |> @@ -558,10 +551,7 @@ check_motCode <- verbose = verbose ) ## check whether "everything" is selected - if (any(mode_of_transport %in% "everything")) { - mode_of_transport <- valid_codes |> - poorman::summarise(id = paste0(id, collapse = ",")) - } else { + mode_of_transport <- as.character(mode_of_transport) # remove any white space from cmd codes provided @@ -587,7 +577,6 @@ check_motCode <- poorman::filter(text %in% mode_of_transport) |> poorman::summarise(id = paste0(id, collapse = ",")) } - } mode_of_transport <- mode_of_transport$id } else { mode_of_transport <- NULL diff --git a/tests/testthat/test-ct_build_request.R b/tests/testthat/test-ct_build_request.R index 8ff6285..a490711 100644 --- a/tests/testthat/test-ct_build_request.R +++ b/tests/testthat/test-ct_build_request.R @@ -25,3 +25,27 @@ test_that('test that build returns a httr2 request',{ expect_true(stringr::str_detect(req$url, 'https://comtradeapi.un.org/data/v1/get/C/A/HS?')) }) + +test_that('test that build returns a httr2 request',{ + + expect_error(comtradr:::ct_check_params( + type = 'goods', + freq = 'A', + commodity_classification = "HS", + commodity_code = ct_get_ref_table("HS")$id, + flow_direction = "Import", + reporter = "USA", + partner = "CAN", + start_date = '2020', + end_date = '2021', + partner_2 = 'World', + mode_of_transport = 'Air', + customs_code = 'C00', + verbose = FALSE, + update = FALSE, + extra_params = NULL + ) |> + comtradr:::ct_build_request(primary_token = 'test_token'), + 'Your request exceeds 4KB or 4096 characters') + +}) diff --git a/tests/testthat/test-ct_check_parameters.R b/tests/testthat/test-ct_check_parameters.R index 87961ac..f5c471a 100644 --- a/tests/testthat/test-ct_check_parameters.R +++ b/tests/testthat/test-ct_check_parameters.R @@ -106,6 +106,10 @@ test_that("check_reporterCode function works correctly", { verbose = FALSE ) ) > 0) + expect_error(check_reporterCode("INVALID"), + regexp = "The following reporter") + expect_error(check_reporterCode(c("all_countries","USA")), + "can only be provided") }) @@ -115,6 +119,8 @@ test_that("check_partnerCode works correctly", { expect_equal(check_partnerCode(c("everything", "MEX")), NULL) expect_error(check_partnerCode(c("CAN", "all"))) expect_error(check_partnerCode("INVALID")) + expect_error(comtradr:::check_partnerCode(c("all_countries","USA")), + "can only be provided") expect_match(check_partnerCode("all_countries"), "^\\d+(,\\d+)*$") }) @@ -124,6 +130,8 @@ test_that("check_partner2Code works correctly", { expect_equal(check_partner2Code(c("everything", "MEX")), NULL) expect_error(check_partner2Code(c("CAN", "all"))) expect_error(check_partner2Code("INVALID")) + expect_error(check_partner2Code(c("all_countries","USA")), + "can only be provided") expect_match(check_partner2Code("all_countries"), "^\\d+(,\\d+)*$") }) @@ -135,6 +143,9 @@ test_that("check_motCode works correctly", { expect_error(check_motCode("INVALID")) expect_error(check_motCode("INVALID"), "The following mode_of_transport codes you") + expect_equal( + comtradr:::check_motCode("everything") + , NULL) }) test_that("check_customsCode works correctly", {