Skip to content

Commit

Permalink
more tests for check_params, fix for motcode where something qwas dup…
Browse files Browse the repository at this point in the history
…licated
  • Loading branch information
datapumpernickel committed Dec 22, 2023
1 parent 1992476 commit 94f0da2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
27 changes: 8 additions & 19 deletions R/ct_check_params.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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.')
}
Expand All @@ -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) |>
Expand Down Expand Up @@ -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
Expand All @@ -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) |>
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
24 changes: 24 additions & 0 deletions tests/testthat/test-ct_build_request.R
Original file line number Diff line number Diff line change
Expand Up @@ -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')

})
11 changes: 11 additions & 0 deletions tests/testthat/test-ct_check_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")

})

Expand All @@ -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+)*$")
})

Expand All @@ -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+)*$")
})

Expand All @@ -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", {
Expand Down

0 comments on commit 94f0da2

Please sign in to comment.