Skip to content

Commit

Permalink
add test for tidy cols that runs if comtrade api key is present
Browse files Browse the repository at this point in the history
  • Loading branch information
datapumpernickel committed Nov 15, 2024
1 parent dacc3b4 commit b169d95
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"elapsedTime": "0.04 secs",
"elapsedTime": "0.21 secs",
"count": 4,
"data": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,6 @@ structure(list(method = "GET", url = "https://comtradeapi.un.org/data/v1/get/C/A
`X-XSS-Protection` = "1;mode=block", `strict-transport-security` = "max-age=31536000;includeSubDomains",
`content-security-policy` = "frame-src 'self'", `x-permitted-cross-domain-policies` = "none",
`Referrer-Policy` = "no-referrer-when-downgrade", `Permissions-Policy` = "accelerometer=(), camera=(), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), payment=(), usb=()",
Date = "Sun, 12 May 2024 16:49:10 GMT"), class = "httr2_headers"),
body = charToRaw("{\"elapsedTime\":\"0.14 secs\",\"count\":4,\"data\":[{\"typeCode\":\"C\",\"freqCode\":\"A\",\"refPeriodId\":20100101,\"refYear\":2010,\"refMonth\":52,\"period\":\"2010\",\"reporterCode\":156,\"reporterISO\":\"CHN\",\"reporterDesc\":\"China\",\"flowCode\":\"M\",\"flowDesc\":\"Import\",\"partnerCode\":32,\"partnerISO\":\"ARG\",\"partnerDesc\":\"Argentina\",\"partner2Code\":0,\"partner2ISO\":\"W00\",\"partner2Desc\":\"World\",\"classificationCode\":\"H3\",\"classificationSearchCode\":\"HS\",\"isOriginalClassification\":true,\"cmdCode\":\"TOTAL\",\"cmdDesc\":\"All Commodities\",\"aggrLevel\":0,\"isLeaf\":false,\"customsCode\":\"C00\",\"customsDesc\":\"TOTAL CPC\",\"mosCode\":\"0\",\"motCode\":0,\"motDesc\":\"TOTAL MOT\",\"qtyUnitCode\":-1,\"qtyUnitAbbr\":\"N/A\",\"qty\":null,\"isQtyEstimated\":false,\"altQtyUnitCode\":-1,\"altQtyUnitAbbr\":\"N/A\",\"altQty\":null,\"isAltQtyEstimated\":false,\"netWgt\":null,\"isNetWgtEstimated\":false,\"grossWgt\":null,\"isGrossWgtEstimated\":false,\"cifvalue\":6804128143.0,\"fobvalue\":null,\"primaryValue\":6804128143.0,\"legacyEstimationFlag\":0,\"isReported\":true,\"isAggregate\":false},{\"typeCode\":\"C\",\"freqCode\":\"A\",\"refPeriodId\":20100101,\"refYear\":2010,\"refMonth\":52,\"period\":\"2010\",\"reporterCode\":156,\"reporterISO\":\"CHN\",\"reporterDesc\":\"China\",\"flowCode\":\"X\",\"flowDesc\":\"Export\",\"partnerCode\":32,\"partnerISO\":\"ARG\",\"partnerDesc\":\"Argentina\",\"partner2Code\":0,\"partner2ISO\":\"W00\",\"partner2Desc\":\"World\",\"classificationCode\":\"H3\",\"classificationSearchCode\":\"HS\",\"isOriginalClassification\":true,\"cmdCode\":\"TOTAL\",\"cmdDesc\":\"All Commodities\",\"aggrLevel\":0,\"isLeaf\":false,\"customsCode\":\"C00\",\"customsDesc\":\"TOTAL CPC\",\"mosCode\":\"0\",\"motCode\":0,\"motDesc\":\"TOTAL MOT\",\"qtyUnitCode\":-1,\"qtyUnitAbbr\":\"N/A\",\"qty\":null,\"isQtyEstimated\":false,\"altQtyUnitCode\":-1,\"altQtyUnitAbbr\":\"N/A\",\"altQty\":null,\"isAltQtyEstimated\":false,\"netWgt\":null,\"isNetWgtEstimated\":false,\"grossWgt\":null,\"isGrossWgtEstimated\":false,\"cifvalue\":null,\"fobvalue\":6115764185.0,\"primaryValue\":6115764185.0,\"legacyEstimationFlag\":0,\"isReported\":true,\"isAggregate\":false},{\"typeCode\":\"C\",\"freqCode\":\"A\",\"refPeriodId\":20100101,\"refYear\":2010,\"refMonth\":52,\"period\":\"2010\",\"reporterCode\":156,\"reporterISO\":\"CHN\",\"reporterDesc\":\"China\",\"flowCode\":\"M\",\"flowDesc\":\"Import\",\"partnerCode\":276,\"partnerISO\":\"DEU\",\"partnerDesc\":\"Germany\",\"partner2Code\":0,\"partner2ISO\":\"W00\",\"partner2Desc\":\"World\",\"classificationCode\":\"H3\",\"classificationSearchCode\":\"HS\",\"isOriginalClassification\":true,\"cmdCode\":\"TOTAL\",\"cmdDesc\":\"All Commodities\",\"aggrLevel\":0,\"isLeaf\":false,\"customsCode\":\"C00\",\"customsDesc\":\"TOTAL CPC\",\"mosCode\":\"0\",\"motCode\":0,\"motDesc\":\"TOTAL MOT\",\"qtyUnitCode\":-1,\"qtyUnitAbbr\":\"N/A\",\"qty\":null,\"isQtyEstimated\":false,\"altQtyUnitCode\":-1,\"altQtyUnitAbbr\":\"N/A\",\"altQty\":null,\"isAltQtyEstimated\":false,\"netWgt\":null,\"isNetWgtEstimated\":false,\"grossWgt\":null,\"isGrossWgtEstimated\":false,\"cifvalue\":74251272075.0,\"fobvalue\":null,\"primaryValue\":74251272075.0,\"legacyEstimationFlag\":0,\"isReported\":true,\"isAggregate\":false},{\"typeCode\":\"C\",\"freqCode\":\"A\",\"refPeriodId\":20100101,\"refYear\":2010,\"refMonth\":52,\"period\":\"2010\",\"reporterCode\":156,\"reporterISO\":\"CHN\",\"reporterDesc\":\"China\",\"flowCode\":\"X\",\"flowDesc\":\"Export\",\"partnerCode\":276,\"partnerISO\":\"DEU\",\"partnerDesc\":\"Germany\",\"partner2Code\":0,\"partner2ISO\":\"W00\",\"partner2Desc\":\"World\",\"classificationCode\":\"H3\",\"classificationSearchCode\":\"HS\",\"isOriginalClassification\":true,\"cmdCode\":\"TOTAL\",\"cmdDesc\":\"All Commodities\",\"aggrLevel\":0,\"isLeaf\":false,\"customsCode\":\"C00\",\"customsDesc\":\"TOTAL CPC\",\"mosCode\":\"0\",\"motCode\":0,\"motDesc\":\"TOTAL MOT\",\"qtyUnitCode\":-1,\"qtyUnitAbbr\":\"N/A\",\"qty\":null,\"isQtyEstimated\":false,\"altQtyUnitCode\":-1,\"altQtyUnitAbbr\":\"N/A\",\"altQty\":null,\"isAltQtyEstimated\":false,\"netWgt\":null,\"isNetWgtEstimated\":false,\"grossWgt\":null,\"isGrossWgtEstimated\":false,\"cifvalue\":null,\"fobvalue\":68047133397.0,\"primaryValue\":68047133397.0,\"legacyEstimationFlag\":0,\"isReported\":true,\"isAggregate\":false}],\"error\":\"\"}"),
request = structure(list(url = "https://comtradeapi.un.org/data/v1/get/C/A/HS?cmdCode=TOTAL&partnerCode=32%2C280%2C276&reporterCode=156&period=2010&motCode=0&partner2Code=0&customsCode=C00&includeDesc=TRUE",
method = NULL, headers = structure(list(`Ocp-Apim-Subscription-Key` = "xxxx"), redact = character(0)),
body = NULL, fields = list(), options = list(), policies = list(
error_body = function (resp)
{
if (!is.null(httr2::resp_header(resp, "Content-Type"))) {
if (stringr::str_detect(httr2::resp_header(resp,
"Content-Type"), "json")) {
body <- httr2::resp_body_json(resp, simplifyVector = TRUE)
message <- body$errorObject$errorMessage
if (!is.null(message)) {
message <- c(message)
}
return(message)
}
else if (stringr::str_detect(httr2::resp_header(resp,
"Content-Type"), "text")) {
body <- httr2::resp_body_string(resp)
if (stringr::str_detect(body, "Request URL Too Long")) {
message <- c("You might have provided too many parameters and the URL got too long.")
return(message)
}
else if (stringr::str_detect(body, "The resource you are looking for has been removed")) {
message <- c("The original message is: ",
body, "But most likely you have exceeded the character value for the api.")
return(message)
}
}
}
}, throttle_delay = function (req)
{
realm <- realm %||% url_parse(req$url)$hostname
last <- the$throttle[[realm]]
if (is.null(last)) {
wait <- 0
}
else {
wait <- delay - (unix_time() - last)
}
sys_sleep(wait, "for throttling delay")
throttle_touch(realm)
wait
}, retry_max_tries = 2, retry_is_transient = function (resp)
{
(httr2::resp_status(resp) == 403 && httr2::resp_header(resp,
"Retry-After") != "0") || (httr2::resp_status(resp) ==
429 && httr2::resp_header(resp, "Retry-After") !=
"0")
}, retry_after = function (resp)
{
time <- as.numeric(httr2::resp_header(resp, "Retry-After"))
time
})), class = "httr2_request"), cache = new.env(parent = emptyenv())), class = "httr2_response")
Date = "Fri, 15 Nov 2024 16:12:34 GMT"), class = "httr2_headers"),
body = charToRaw("{\"elapsedTime\":\"0.09 secs\",\"count\":4,\"data\":[{\"typeCode\":\"C\",\"freqCode\":\"A\",\"refPeriodId\":20100101,\"refYear\":2010,\"refMonth\":52,\"period\":\"2010\",\"reporterCode\":156,\"reporterISO\":\"CHN\",\"reporterDesc\":\"China\",\"flowCode\":\"M\",\"flowDesc\":\"Import\",\"partnerCode\":32,\"partnerISO\":\"ARG\",\"partnerDesc\":\"Argentina\",\"partner2Code\":0,\"partner2ISO\":\"W00\",\"partner2Desc\":\"World\",\"classificationCode\":\"H3\",\"classificationSearchCode\":\"HS\",\"isOriginalClassification\":true,\"cmdCode\":\"TOTAL\",\"cmdDesc\":\"All Commodities\",\"aggrLevel\":0,\"isLeaf\":false,\"customsCode\":\"C00\",\"customsDesc\":\"TOTAL CPC\",\"mosCode\":\"0\",\"motCode\":0,\"motDesc\":\"TOTAL MOT\",\"qtyUnitCode\":-1,\"qtyUnitAbbr\":\"N/A\",\"qty\":null,\"isQtyEstimated\":false,\"altQtyUnitCode\":-1,\"altQtyUnitAbbr\":\"N/A\",\"altQty\":null,\"isAltQtyEstimated\":false,\"netWgt\":null,\"isNetWgtEstimated\":false,\"grossWgt\":null,\"isGrossWgtEstimated\":false,\"cifvalue\":6804128143.0,\"fobvalue\":null,\"primaryValue\":6804128143.0,\"legacyEstimationFlag\":0,\"isReported\":true,\"isAggregate\":false},{\"typeCode\":\"C\",\"freqCode\":\"A\",\"refPeriodId\":20100101,\"refYear\":2010,\"refMonth\":52,\"period\":\"2010\",\"reporterCode\":156,\"reporterISO\":\"CHN\",\"reporterDesc\":\"China\",\"flowCode\":\"X\",\"flowDesc\":\"Export\",\"partnerCode\":32,\"partnerISO\":\"ARG\",\"partnerDesc\":\"Argentina\",\"partner2Code\":0,\"partner2ISO\":\"W00\",\"partner2Desc\":\"World\",\"classificationCode\":\"H3\",\"classificationSearchCode\":\"HS\",\"isOriginalClassification\":true,\"cmdCode\":\"TOTAL\",\"cmdDesc\":\"All Commodities\",\"aggrLevel\":0,\"isLeaf\":false,\"customsCode\":\"C00\",\"customsDesc\":\"TOTAL CPC\",\"mosCode\":\"0\",\"motCode\":0,\"motDesc\":\"TOTAL MOT\",\"qtyUnitCode\":-1,\"qtyUnitAbbr\":\"N/A\",\"qty\":null,\"isQtyEstimated\":false,\"altQtyUnitCode\":-1,\"altQtyUnitAbbr\":\"N/A\",\"altQty\":null,\"isAltQtyEstimated\":false,\"netWgt\":null,\"isNetWgtEstimated\":false,\"grossWgt\":null,\"isGrossWgtEstimated\":false,\"cifvalue\":null,\"fobvalue\":6115764185.0,\"primaryValue\":6115764185.0,\"legacyEstimationFlag\":0,\"isReported\":true,\"isAggregate\":false},{\"typeCode\":\"C\",\"freqCode\":\"A\",\"refPeriodId\":20100101,\"refYear\":2010,\"refMonth\":52,\"period\":\"2010\",\"reporterCode\":156,\"reporterISO\":\"CHN\",\"reporterDesc\":\"China\",\"flowCode\":\"M\",\"flowDesc\":\"Import\",\"partnerCode\":276,\"partnerISO\":\"DEU\",\"partnerDesc\":\"Germany\",\"partner2Code\":0,\"partner2ISO\":\"W00\",\"partner2Desc\":\"World\",\"classificationCode\":\"H3\",\"classificationSearchCode\":\"HS\",\"isOriginalClassification\":true,\"cmdCode\":\"TOTAL\",\"cmdDesc\":\"All Commodities\",\"aggrLevel\":0,\"isLeaf\":false,\"customsCode\":\"C00\",\"customsDesc\":\"TOTAL CPC\",\"mosCode\":\"0\",\"motCode\":0,\"motDesc\":\"TOTAL MOT\",\"qtyUnitCode\":-1,\"qtyUnitAbbr\":\"N/A\",\"qty\":null,\"isQtyEstimated\":false,\"altQtyUnitCode\":-1,\"altQtyUnitAbbr\":\"N/A\",\"altQty\":null,\"isAltQtyEstimated\":false,\"netWgt\":null,\"isNetWgtEstimated\":false,\"grossWgt\":null,\"isGrossWgtEstimated\":false,\"cifvalue\":74251272075.0,\"fobvalue\":null,\"primaryValue\":74251272075.0,\"legacyEstimationFlag\":0,\"isReported\":true,\"isAggregate\":false},{\"typeCode\":\"C\",\"freqCode\":\"A\",\"refPeriodId\":20100101,\"refYear\":2010,\"refMonth\":52,\"period\":\"2010\",\"reporterCode\":156,\"reporterISO\":\"CHN\",\"reporterDesc\":\"China\",\"flowCode\":\"X\",\"flowDesc\":\"Export\",\"partnerCode\":276,\"partnerISO\":\"DEU\",\"partnerDesc\":\"Germany\",\"partner2Code\":0,\"partner2ISO\":\"W00\",\"partner2Desc\":\"World\",\"classificationCode\":\"H3\",\"classificationSearchCode\":\"HS\",\"isOriginalClassification\":true,\"cmdCode\":\"TOTAL\",\"cmdDesc\":\"All Commodities\",\"aggrLevel\":0,\"isLeaf\":false,\"customsCode\":\"C00\",\"customsDesc\":\"TOTAL CPC\",\"mosCode\":\"0\",\"motCode\":0,\"motDesc\":\"TOTAL MOT\",\"qtyUnitCode\":-1,\"qtyUnitAbbr\":\"N/A\",\"qty\":null,\"isQtyEstimated\":false,\"altQtyUnitCode\":-1,\"altQtyUnitAbbr\":\"N/A\",\"altQty\":null,\"isAltQtyEstimated\":false,\"netWgt\":null,\"isNetWgtEstimated\":false,\"grossWgt\":null,\"isGrossWgtEstimated\":false,\"cifvalue\":null,\"fobvalue\":68047133397.0,\"primaryValue\":68047133397.0,\"legacyEstimationFlag\":0,\"isReported\":true,\"isAggregate\":false}],\"error\":\"\"}"),
cache = new.env(parent = emptyenv())), class = "httr2_response")
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
structure(list(method = "GET", url = "https://comtradeapi.un.org/data/v1/get/S/A/EB?cmdCode=200&partnerCode=32%2C280%2C276&reporterCode=156&period=2010&motCode=0&partner2Code=0&customsCode=C00&includeDesc=TRUE", #nolint
status_code = 200L,
headers = structure(list(`Transfer-Encoding` = "chunked",
`Content-Type` = "application/json; charset=utf-8",
`Content-Encoding` = "gzip",
Vary = "Accept-Encoding",
`Request-Context` = "appId=cid-v1:9b6e1d5a-3728-46ff-b743-6d33d23e54a6",
`x-frame-options` = "deny",
`X-Content-Type-Options` = "nosniff",
`X-XSS-Protection` = "1;mode=block",
`strict-transport-security` = "max-age=31536000;includeSubDomains",
`content-security-policy` = "frame-src 'self'",
`x-permitted-cross-domain-policies` = "none",
`Referrer-Policy` = "no-referrer-when-downgrade",
`Permissions-Policy` = "accelerometer=(), camera=(), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), payment=(), usb=()", # nolint
Date = "Sun, 17 Dec 2023 21:56:49 GMT"), class = "httr2_headers"),
body = charToRaw("{\"elapsedTime\":\"0.92 secs\",\"count\":0,\"data\":[],\"error\":\"\"}")), class = "httr2_response") # nolint
structure(list(method = "GET", url = "https://comtradeapi.un.org/data/v1/get/S/A/EB?cmdCode=200&partnerCode=32%2C280%2C276&reporterCode=156&period=2010&motCode=0&partner2Code=0&customsCode=C00&includeDesc=TRUE",
status_code = 200L, headers = structure(list(`Transfer-Encoding` = "chunked",
`Content-Type` = "application/json; charset=utf-8", `Content-Encoding` = "gzip",
Vary = "Accept-Encoding", `Request-Context` = "appId=cid-v1:9b6e1d5a-3728-46ff-b743-6d33d23e54a6",
`x-frame-options` = "deny", `X-Content-Type-Options` = "nosniff",
`X-XSS-Protection` = "1;mode=block", `strict-transport-security` = "max-age=31536000;includeSubDomains",
`content-security-policy` = "frame-src 'self'", `x-permitted-cross-domain-policies` = "none",
`Referrer-Policy` = "no-referrer-when-downgrade", `Permissions-Policy` = "accelerometer=(), camera=(), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), payment=(), usb=()",
Date = "Fri, 15 Nov 2024 16:12:50 GMT"), class = "httr2_headers"),
body = charToRaw("{\"elapsedTime\":\"0.58 secs\",\"count\":0,\"data\":[],\"error\":\"\"}"),
cache = new.env(parent = emptyenv())), class = "httr2_response")
26 changes: 24 additions & 2 deletions tests/testthat/test-ct_get_bulk.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ library(comtradr)
library(httptest2)
library(testthat)


httptest2::with_mock_dir("goods_bulk_error", {
test_that("test that error works for params that have no values", {
expect_error(
Expand Down Expand Up @@ -35,8 +36,6 @@ httptest2::set_redactor(function(x) {
"")
})

options(httptest2.verbose = TRUE)

httptest2::with_mock_dir('../goods_bulk', {
test_that('returns a data.frame',
{
Expand Down Expand Up @@ -77,3 +76,26 @@ httptest2::with_mock_dir('../goods_bulk', {
)))
})
})


test_that("tidy cols are returned for bulk",{
test <- try(get_primary_comtrade_key(), silent = TRUE)
skip_if(any(class(test) %in% c("rlang_error", "error", "try-error")))
tidy <- comtradr::ct_get_bulk(
reporter = 'ARG',
start_date = '1962',
end_date = "1962",
primary_token = get_primary_comtrade_key(),
cache = FALSE
)
not_tidy <- comtradr::ct_get_bulk(
reporter = 'ARG',
start_date = '1962',
end_date = "1962",
tidy_cols = F,
primary_token = get_primary_comtrade_key(),
cache = FALSE
)
expect_true(all(names(tidy) %in% comtradr::ct_pretty_cols$to))
expect_true(!all(names(not_tidy) %in% comtradr::ct_pretty_cols$to))
})
36 changes: 36 additions & 0 deletions tests/testthat/test-ct_get_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,39 @@ httptest2::with_mock_dir("data", {
expect_true(any(grepl("Got a response object from UN Comtrade. Use `process = F` if there is an error", captured_messages))) # nolint
})
})



test_that("tidy cols are returned",{
test <- try(get_primary_comtrade_key(), silent = TRUE)
skip_if(any(class(test) %in% c("rlang_error", "error", "try-error")))
tidy <- comtradr::ct_get_data(
type = 'goods',
commodity_classification = 'HS',
commodity_code = 'TOTAL',
reporter = 'CHN',
partner = 'DEU',
start_date = '2010',
end_date = '2010',
primary_token = get_primary_comtrade_key(),
partner_2 = 'World'
)
not_tidy <- comtradr::ct_get_data(
type = 'goods',
commodity_classification = 'HS',
commodity_code = 'TOTAL',
reporter = 'CHN',
partner = 'DEU',
start_date = '2010',
end_date = '2010',
tidy_cols = FALSE,
primary_token = get_primary_comtrade_key(),
partner_2 = 'World'
)
expect_true(all(names(tidy) %in% comtradr::ct_pretty_cols$to))
expect_true(!all(names(not_tidy) %in% comtradr::ct_pretty_cols$to))
})




0 comments on commit b169d95

Please sign in to comment.