tests/testthat/test_download.R

context("Test overall downloads")

expect_quality_values <- function(x) {
    x <- suppressWarnings(as.numeric(x))
    expect_true(all(is.na(x) | (x >= 0 & x <= 5)))
}

test_that("we can download mutliple indicators for a single country", {
    skip_on_cran()
    skip_if_not(identical(Sys.getenv("NOT_CRAN"), "true"))
    data <- download_wid(
        areas = "FR",
        indicators = c("sfiinc", "aptinc"),
        perc = c("p90p100", "p20p30"),
        years = 1990:2000
    )

    expect_true(all(data$year >= 1990 & data$year <= 2000))
    expect_true(all(data$country == "FR"))
    expect_true(identical(unique(data$percentile), c("p20p30", "p90p100")))
    expect_true(identical(
        unique(substr(data$variable, 1, 6)),
        sort(c("aptinc", "sfiinc"))
    ))
})

test_that("we can download a single indicator for multiple countries", {
    skip_on_cran()
    skip_if_not(identical(Sys.getenv("NOT_CRAN"), "true"))
    data <- download_wid(
        areas = c("FR", "US"),
        indicators = "sptinc",
        perc = "p90p100",
        ages = "992",
        pop = "j",
        years = 1990:2000
    )

    expect_true(all(data$percentile == "p90p100"))
    expect_true(identical(
        unique(data$country),
        sort(c("FR", "US"))
    ))
    expect_true(all(data$year >= 1990 & data$year <= 2000))
    expect_true(all(substr(data$variable, 10, 11) == "j"))
    expect_true(all(substr(data$variable, 07, 09) == "992"))
    expect_true(all(substr(data$variable, 01, 06) == "sptinc"))
})

test_that("we can download population data", {
    skip_on_cran()
    skip_if_not(identical(Sys.getenv("NOT_CRAN"), "true"))
    data <- download_wid(
        areas = "DE",
        indicators = "npopul"
    )

    expect_true(all(data$country == "DE"))
    expect_true(all(data$percentile == "p0p100"))
    expect_true(all(substr(data$variable, 1, 6) == "npopul"))
    expect_true(identical(
        unique(substr(data$variable, 10, 11)),
        sort(c("i", "f", "m"))
    ))
})

test_that("we can download metadata", {
    skip_on_cran()
    skip_if_not(identical(Sys.getenv("NOT_CRAN"), "true"))
    data <- download_wid(
        areas = "FR",
        indicators = "sptinc",
        perc = "p99p100",
        ages = "992",
        pop = "j",
        metadata = TRUE
    )

    expect_true(all(data$country == "FR"))
    expect_true(all(data$countryname == "France"))
    expect_true(all(data$variable == "sptinc992j"))
    expect_true(all(data$percentile == "p99p100"))
    expect_quality_values(data$row_score)
    expect_quality_values(data$series_score)
    expect_false("imputation" %in% names(data))
})

test_that("we can request score filter columns", {
    skip_on_cran()
    skip_if_not(identical(Sys.getenv("NOT_CRAN"), "true"))

    row_filtered <- download_wid(
        areas = "FR",
        indicators = "sptinc",
        years = "2021",
        perc = "p99p100",
        ages = "992",
        pop = "j",
        score_filter = list(row_score = c(0, 5))
    )

    expect_true("row_score" %in% names(row_filtered))
    expect_false("series_score" %in% names(row_filtered))
    expect_quality_values(row_filtered$row_score)

    series_filtered <- download_wid(
        areas = "FR",
        indicators = "sptinc",
        years = "2021",
        perc = "p99p100",
        ages = "992",
        pop = "j",
        score_filter = list(series_score = c(0, 5))
    )

    expect_true("series_score" %in% names(series_filtered))
    expect_false("row_score" %in% names(series_filtered))
    expect_quality_values(series_filtered$series_score)
})

Try the wid package in your browser

Any scripts or data that you put into this service are public.

wid documentation built on July 9, 2026, 1:07 a.m.