Skip to content

Commit

Permalink
Merge pull request #54 from tidy-intelligence/52-udpate-tests-to-fail…
Browse files Browse the repository at this point in the history
…-gracefully-if-world-bank-api-is-unavailable

Update tests that required internet access
  • Loading branch information
christophscheuch authored Jan 19, 2025
2 parents ee994a6 + 69d1932 commit b5bd32c
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 44 deletions.
11 changes: 7 additions & 4 deletions tests/testthat/tests-perform_request.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,21 @@ test_that("perform_request handles error responses", {
})

test_that("perform_request validates per_page parameter", {
skip_if_offline()

expect_error(perform_request("countries", per_page = 50000))
expect_silent(perform_request("countries", per_page = 1000))
})

test_that("perform_request validates max_tries parameter", {
skip_if_offline()

expect_error(perform_request("countries", max_tries = -100))
expect_silent(perform_request("countries", max_tries = 2))
})

test_that("validate_per_page handles valid per_page values", {
expect_error(validate_per_page(-1000))
expect_silent(validate_per_page(1000))
expect_silent(validate_per_page(1))
expect_silent(validate_per_page(32500))
Expand Down Expand Up @@ -83,9 +88,7 @@ test_that("is_request_error identifies error responses correctly", {
})

test_that("perform_request handles API errors gracefully", {
mock_req <- create_request(
"https://api.worldbank.org/v2", "nonexistent", NULL, 1000, NULL, NULL
)
mock_resp <- structure(list(status_code = 404), class = "httr2_response")
skip_if_offline()

expect_error(perform_request("nonexistent"), "HTTP 404 Not Found.")
})
14 changes: 14 additions & 0 deletions tests/testthat/tests-wdi_get.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ test_that("wdi_get handels invalid progress input", {
})

test_that("wdi_get creates valid output structure for single indicator", {
skip_if_offline()

result <- wdi_get(
geographies = "USA",
indicators = "NY.GDP.MKTP.CD",
Expand All @@ -48,6 +50,8 @@ test_that("wdi_get creates valid output structure for single indicator", {
})

test_that("wdi_get_creates valid output structure for multiple indicators", {
skip_if_offline()

result <- wdi_get(
geographies = "USA",
indicators = c("NY.GDP.MKTP.CD", "SP.POP.TOTL"),
Expand All @@ -62,11 +66,15 @@ test_that("wdi_get_creates valid output structure for multiple indicators", {
})

test_that("wdi_get returns a tibble", {
skip_if_offline()

result <- wdi_get("USA", "NY.GDP.PCAP.KD")
expect_s3_class(result, "tbl_df")
})

test_that("wdi_get handles single indicator, geography & default parameters", {
skip_if_offline()

result <- wdi_get("USA", "NY.GDP.PCAP.KD")
expected_colnames <- c("indicator_id", "geography_id", "year", "value")
expect_true(all(expected_colnames %in% colnames(result)))
Expand All @@ -75,6 +83,8 @@ test_that("wdi_get handles single indicator, geography & default parameters", {
})

test_that("wdi_get handles multiple indicators and multiple geographies", {
skip_if_offline()

result <- wdi_get(c("USA", "CAN"), c("NY.GDP.PCAP.KD", "SP.POP.TOTL"))
expected_colnames <- c("indicator_id", "geography_id", "year", "value")
expect_true(all(expected_colnames %in% colnames(result)))
Expand All @@ -83,6 +93,8 @@ test_that("wdi_get handles multiple indicators and multiple geographies", {
})

test_that("wdi_get handles different date ranges and frequencies", {
skip_if_offline()

result_annual <- wdi_get(
"USA", "NY.GDP.PCAP.KD",
start_date = 2010, end_date = 2015, frequency = "annual"
Expand All @@ -102,6 +114,8 @@ test_that("wdi_get handles different date ranges and frequencies", {
})

test_that("wdi_get handles format parameter (long and wide)", {
skip_if_offline()

result_long <- wdi_get("USA", "NY.GDP.PCAP.KD", format = "long")
result_wide <- wdi_get("USA", "NY.GDP.PCAP.KD", format = "wide")

Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/tests-wdi_get_geographies.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ test_that("wdi_get_geographies handles invalid per_page input", {
)
})

test_that("wdi_get_geographies returns a tibble", {
result <- wdi_get_geographies()
expect_s3_class(result, "tbl_df")
})
test_that("wdi_get_geographies returns a tibble with correct column names", {
skip_if_offline()

test_that("wdi_get_geographies has correct columns", {
result <- wdi_get_geographies()
expect_s3_class(result, "tbl_df")
expected_colnames <- c(
"geography_id", "geography_name", "geography_iso2code", "geography_type",
"region_id", "region_name", "region_iso2code", "admin_region_id",
Expand Down Expand Up @@ -76,6 +74,8 @@ test_that("wdi_get_geographies handles type conversions and missing values", {
})

test_that("wdi_get_geographies handles different language inputs", {
skip_if_offline()

result <- wdi_get_geographies(language = "fr")

expected_colnames <- c(
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/tests-wdi_get_income_levels.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
test_that("wdi_get_income_levels returns a tibble", {
result <- wdi_get_income_levels()
expect_s3_class(result, "tbl_df")
})
test_that("wdi_get_income_levels returns a tibble with correct column names", {
skip_if_offline()

test_that("wdi_get_income_levels has correct columns", {
result <- wdi_get_income_levels()
expect_s3_class(result, "tbl_df")
expected_colnames <- c(
"income_level_id", "income_level_iso2code", "income_level_name"
)
Expand All @@ -30,6 +28,8 @@ test_that("wdi_get_income_levels trims whitespace in character columns", {
})

test_that("wdi_get_income_levels handles different language inputs", {
skip_if_offline()

result <- wdi_get_income_levels(language = "es")
expected_colnames <- c(
"income_level_id", "income_level_iso2code", "income_level_name"
Expand Down
9 changes: 5 additions & 4 deletions tests/testthat/tests-wdi_get_indicators.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ test_that("wdi_get_indicators handles invalid per_page input", {
)
})

test_that("wdi_get_indicators returns a tibble", {
test_that("wdi_get_indicators returns a tibble with correct column names", {
skip_if_offline()

result <- wdi_get_indicators()
expect_s3_class(result, "tbl_df")
})

test_that("wdi_get_indicators has correct columns", {
result <- wdi_get_indicators()
expected_colnames <- c(
"indicator_id", "indicator_name", "source_id", "source_name",
"source_note", "source_organization", "topics"
Expand Down Expand Up @@ -60,6 +59,8 @@ test_that("wdi_get_indicators handles type conversions and missing values", {
})

test_that("wdi_get_indicators handles different language inputs", {
skip_if_offline()

result <- wdi_get_indicators(language = "es")
expected_colnames <- c(
"indicator_id", "indicator_name", "source_id", "source_name",
Expand Down
8 changes: 3 additions & 5 deletions tests/testthat/tests-wdi_get_languages.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
test_that("wdi_get_languages returns a tibble", {
result <- wdi_get_languages()
expect_s3_class(result, "tbl_df")
})
test_that("wdi_get_languages returns a tibble with correct column names", {
skip_if_offline()

test_that("wdi_get_languages has correct columns", {
result <- wdi_get_languages()
expect_s3_class(result, "tbl_df")
expected_columns <- c("language_code", "language_name", "native_form")
expect_true(all(expected_columns %in% colnames(result)))
})
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/tests-wdi_get_lending_types.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ test_that("wdi_get_lending_types handles invalid language input", {
)
})

test_that("wdi_get_lending_types returns a tibble", {
result <- wdi_get_lending_types()
expect_s3_class(result, "tbl_df")
})
test_that("wdi_get_lending_types returns a tibble with correct column names", {
skip_if_offline()

test_that("wdi_get_lending_types has correct columns", {
result <- wdi_get_lending_types()
expect_s3_class(result, "tbl_df")
expected_colnames <- c(
"lending_type_id", "lending_type_iso2code", "lending_type_name"
)
Expand Down Expand Up @@ -55,6 +53,8 @@ test_that("wdi_get_lending_types handles empty data gracefully", {
})

test_that("wdi_get_lending_types handles different language inputs", {
skip_if_offline()

result <- wdi_get_lending_types(language = "es")
expected_colnames <- c(
"lending_type_id", "lending_type_iso2code", "lending_type_name"
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/tests-wdi_get_regions.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ test_that("wdi_get_regions handles invalid language input", {
)
})

test_that("wdi_get_regions returns a tibble", {
result <- wdi_get_regions()
expect_s3_class(result, "tbl_df")
})
test_that("wdi_get_regions returns a tibble with correct column names", {
skip_if_offline()

test_that("wdi_get_regions has correct columns", {
result <- wdi_get_regions()
expect_s3_class(result, "tbl_df")
expected_colnames <- c(
"region_id", "region_code", "region_iso2code", "region_name"
)
Expand Down Expand Up @@ -74,6 +72,8 @@ test_that("wdi_get_regions handles empty data gracefully", {
})

test_that("wdi_get_regions handles different language inputs", {
skip_if_offline()

result <- wdi_get_regions(language = "fr")
expected_columns <- c(
"region_id", "region_code", "region_iso2code", "region_name"
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/tests-wdi_get_sources.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ test_that("wdi_get_sources handles invalid language input", {
)
})

test_that("wdi_get_sources returns a tibble", {
result <- wdi_get_sources()
expect_s3_class(result, "tbl_df")
})
test_that("wdi_get_sources returns a tibble with correct column names", {
skip_if_offline()

test_that("wdi_get_sources has correct columns", {
result <- wdi_get_sources()
expect_s3_class(result, "tbl_df")
expected_colnames <- c(
"source_id", "source_code", "source_name", "update_date",
"is_data_available", "is_metadata_available", "concepts"
Expand Down Expand Up @@ -91,6 +89,8 @@ test_that("wdi_get_sources handles empty data gracefully", {
})

test_that("wdi_get_sources handles different language inputs", {
skip_if_offline()

result <- wdi_get_sources(language = "es")
expected_colnames <- c(
"source_id", "source_code", "source_name", "update_date",
Expand Down
10 changes: 5 additions & 5 deletions tests/testthat/tests-wdi_get_topics.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ test_that("wdi_get_topics handles invalid language input", {
)
})

test_that("wdi_get_topics returns a tibble", {
result <- wdi_get_topics()
expect_s3_class(result, "tbl_df")
})
test_that("wdi_get_topics returns a tibble with correct column names", {
skip_if_offline()

test_that("wdi_get_topics has correct columns", {
result <- wdi_get_topics()
expect_s3_class(result, "tbl_df")
expected_colnames <- c("topic_id", "topic_name", "topic_note")
expect_true(all(expected_colnames %in% colnames(result)))
})
Expand Down Expand Up @@ -68,6 +66,8 @@ test_that("wdi_get_topics handles empty data gracefully", {
})

test_that("wdi_get_topics handles different language inputs", {
skip_if_offline()

result <- wdi_get_topics(language = "fr")
expected_colnames <- c("topic_id", "topic_name", "topic_note")
expect_true(all(expected_colnames %in% colnames(result)))
Expand Down
1 change: 0 additions & 1 deletion tests/testthat/tests-wdi_search.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
test_that("wdi_search returns rows with specified keywords in any column", {
# Sample data frame with both plain and nested columns
data <- data.frame(
id = 1:3,
text = c("apple pie", "banana split", "cherry tart"),
Expand Down
1 change: 1 addition & 0 deletions wbwdi.Rproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Version: 1.0
ProjectId: 2c2a9fc3-1a5e-4baa-8d35-f9e3cd5bfe58

RestoreWorkspace: Default
SaveWorkspace: Default
Expand Down

0 comments on commit b5bd32c

Please sign in to comment.