tests/testthat/test-bde-series.R

test_that("Series load returns null when catalog is empty", {
  local_mocked_bindings(bde_catalog_load = function(...) tibble::tibble())
  expect_identical(bde_series_load(573234), bde_hlp_return_null())
})

test_that("Indicators", {
  expect_error(bde_series_load(), "`series_code` cannot be missing")

  skip_on_cran()
  skip_if_bde_offline()

  # Test load series all----
  expect_null(bde_series_full_load("aa"))

  expect_message(
    bde_series_full_load("TI_1_1.csv", cache_dir = tempdir(), verbose = TRUE),
    "Reading file .*ti_1_1.csv.* from cache\\.|Downloading file from"
  )
  expect_message(
    bde_series_full_load("TI_1_1.csv", cache_dir = NULL, verbose = TRUE),
    "Reading file .*ti_1_1.csv.* from cache\\.|Downloading file from"
  )
  expect_message(
    bde_series_full_load("CF0101.csv", cache_dir = NULL, verbose = TRUE),
    "Reading file .*cf0101.csv.* from cache\\.|Downloading file from"
  )
  expect_silent(bde_series_full_load("CF0101"))

  data <- bde_series_full_load("TI_1_1.csv")
  meta <- bde_series_full_load("TI_1_1.csv", extract_metadata = TRUE)

  expect_true(nrow(data) > nrow(meta))

  # Test load series ----
  expect_warning(bde_series_load("aa"))
  expect_identical(bde_series_load(12345678910), bde_hlp_return_null())
  expect_error(
    bde_series_load(c(573234, 573214), series_label = c(1, NA)),
    "`series_label` must not contain missing values\\."
  )
  expect_error(
    bde_series_load(c(573234, 573214), series_label = c("1", "1")),
    "`series_label` and `series_code` must have the same length\\."
  )
  expect_error(
    bde_series_load(573234, series_label = c("a", "b")),
    "`series_label` and `series_code` must have the same length\\."
  )

  expect_silent(bde_series_load(c(573234, 573214), series_label = c("a", "b")))

  expect_silent(bde_series_load(573234, series_label = "a"))
  expect_silent(bde_series_load("573234", series_label = "a"))
  expect_warning(bde_series_load(c("573234", "a")))
  expect_silent(bde_series_load(573234, series_label = NULL))
  expect_silent(bde_series_load(573234, extract_metadata = TRUE))
  expect_message(bde_series_load(573234, verbose = TRUE), "Extracting series")

  meta <- bde_series_load(573234, extract_metadata = TRUE)
  data <- bde_series_load(573234, extract_metadata = FALSE)
  expect_true(nrow(data) > nrow(meta))

  # Test long and wide
  wide <- bde_series_load(c(573234, 573214), series_label = c("a", "b"))
  long <- bde_series_load(
    c(573234, 573214),
    series_label = c("a", "b"),
    out_format = "long"
  )

  expect_equal(ncol(long), 3)
  expect_true(nrow(long) > nrow(wide))
  expect_s3_class(long$serie_name, "factor")
  expect_equal(levels(long$serie_name), names(wide[, -1]))

  # Wide and long does not affect on metadata
  wide <- bde_series_load(
    c(573234, 573214),
    series_label = c("a", "b"),
    extract_metadata = TRUE
  )
  long <- bde_series_load(
    c(573234, 573214),
    series_label = c("a", "b"),
    out_format = "long",
    extract_metadata = TRUE
  )
  expect_identical(wide, long)
})

test_that("Series full", {
  skip_on_cran()
  skip_if_bde_offline()
  dir <- file.path(tempdir(), paste0("testoff", sample.int(10, 1)))

  # Test all offline

  # Get a series
  tc_catalog <- bde_catalog_load("TI", cache_dir = dir)

  all_tables <- tc_catalog$Nombre_del_archivo_con_los_valores_de_la_serie
  all_names <- as.character(unique(all_tables))

  full_1 <- bde_series_full_load(all_names[1], cache_dir = dir)

  expect_s3_class(full_1, "data.frame")
  expect_gt(nrow(full_1), 5)

  # Test offline
  local_mocked_bindings(on_cran = function(...) {
    TRUE
  })

  # Can't download series
  expect_message(bde_series_full_load(all_names[2], cache_dir = dir), "empty")

  fail <- bde_series_full_load(all_names[2], cache_dir = dir)
  expect_equal(nrow(fail), 0)

  # But can reload cached series
  expect_silent(bde_series_full_load(all_names[1], cache_dir = dir))
  full_2 <- bde_series_full_load(all_names[1], cache_dir = dir)

  expect_identical(full_1, full_2)

  # Now try online
  local_mocked_bindings(on_cran = function(...) {
    FALSE
  })

  failfix <- bde_series_full_load(all_names[2], cache_dir = dir)
  expect_gt(nrow(failfix), 10)
})
test_that("Mock files series", {
  local_mocked_bindings(
    bde_catalog_load = function(...) {
      tibble::tibble(
        catalog = c("TI", "TI"),
        Numero_secuencial = c(573234, 573214),
        Alias_de_la_serie = c("A", "B"),
        Nombre_del_archivo_con_los_valores_de_la_serie = c("a.csv", "b.csv")
      )
    },
    bde_series_full_load = function(...) {
      dplyr::tibble()
    }
  )

  expect_message(
    long <- bde_series_load(
      c(573234, 573214),
      series_label = c("a", "b"),
      out_format = "long",
      extract_metadata = TRUE
    ),
    "BdE resources are unavailable"
  )
  local_mocked_bindings(bde_series_full_load = function(...) {
    dplyr::tibble(no_name = 1, another = 2, more = 2, and_more = 2)
  })
  expect_message(
    long <- bde_series_load(
      c(573234, 573214),
      series_label = c("a", "b"),
      out_format = "long",
      verbose = TRUE
    ),
    "BdE resources are unavailable"
  )
})

test_that("Mock files all", {
  fpath <- file.path(tempdir(), "TI", "ti_1_1.csv")
  dir.create(dirname(fpath), recursive = TRUE, showWarnings = FALSE)
  writeLines(" ", fpath)

  expect_true(file.exists(fpath))
  local_mocked_bindings(bde_hlp_download = function(...) {
    TRUE
  })

  expect_silent(
    ss <- bde_series_full_load(
      "TI_1_1.csv",
      cache_dir = tempdir(),
      verbose = FALSE
    )
  )
  unlink(fpath)
})

test_that("Mock files cleanup", {
  local_mocked_bindings(
    bde_check_access = function(...) {
      TRUE
    },
    bde_hlp_download = function(url, local_file, verbose) {
      writeLines("a", local_file)
      FALSE
    }
  )

  expect_silent(
    ss <- bde_series_full_load(
      "TI_1_1.csv",
      cache_dir = tempdir(),
      verbose = FALSE
    )
  )
})

test_that("Mock files empty download", {
  local_mocked_bindings(
    bde_check_access = function(...) {
      TRUE
    },
    bde_hlp_download = function(url, local_file, verbose) {
      file.create(local_file, showWarnings = FALSE)
      TRUE
    }
  )

  expect_message(
    ss <- bde_series_full_load(
      "TI_1_1.csv",
      cache_dir = tempdir(),
      verbose = FALSE
    ),
    "is empty"
  )
})

Try the tidyBdE package in your browser

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

tidyBdE documentation built on July 7, 2026, 1:06 a.m.