tests/testthat/test-xml.R

test_that("exact structure references and numeric versions are resolved", {
  s <- fixture_structure()
  expect_identical(s$dimensions$id, c("FREQ", "REF_AREA", "MEASURE", "GROUP", "TIME_PERIOD"))
  expect_identical(s$measure, "OBS_VALUE")
  expect_identical(s$labels$GROUP$code, c("00", "01"))
  expect_identical(OECDdata:::.newest(c("1.9", "1.10", "1.2")), 2L)
  expect_error(OECDdata:::.newest(c("1.0", "draft")), "Non-numeric")
  expect_error(fixture_structure("TEST,DEMO,9.9"), "Could not resolve")
})

test_that("XML namespace prefixes do not determine parsing", {
  text <- paste(readLines(fixture_path("structure.xml")), collapse = "\n")
  changed <- gsub("(<[/]?)s:", "\\1str:", gsub("xmlns:s=", "xmlns:str=", text, fixed = TRUE), perl = TRUE)
  a <- fixture_structure(); b <- fixture_structure(doc = xml2::read_xml(changed))
  expect_identical(a$dimensions, b$dimensions)
  expect_identical(a$labels, b$labels)
})

test_that("localized concept labels are preferred with English fallback", {
  local_oecd(language = "fr")
  s <- fixture_structure()
  expect_match(s$flow$name, "fictif", fixed = TRUE)
  expect_true(nzchar(s$dimensions$label[[1]]))
  local_oecd(language = "xx")
  s <- fixture_structure()
  expect_match(s$flow$name, "Synthetic", fixed = TRUE)
})

test_that("availability excludes codes only permitted by a dictionary", {
  s <- fixture_structure()
  expect_identical(s$availability, "actual")
  expect_setequal(s$available$REF_AREA, c("USA", "FRA"))
  expect_true("ZZZ" %in% s$labels$REF_AREA$code)
  local_oecd(availability = "actual")
  expect_false("ZZZ" %in% OECDdata:::.metabase_one(s)$Dim_val)
  local_oecd(availability = "allowed")
  m <- OECDdata:::.metabase_one(s)
  expect_true("ZZZ" %in% m$Dim_val)
  expect_identical(attr(m, "Availability"), "allowed")
})

test_that("missing or conditional availability fails rather than inventing observations", {
  doc <- fixture_doc()
  xml2::xml_remove(xml2::xml_find_all(doc, ".//*[local-name()='ContentConstraint']"))
  s <- fixture_structure(doc = doc)
  local_oecd(availability = "actual")
  expect_error(OECDdata:::.metabase_one(s), class = "OECDdata_availability_error")
  local_oecd(availability = "allowed")
  expect_gt(nrow(OECDdata:::.metabase_one(s)), 0)
  doc <- fixture_doc()
  node <- xml2::xml_find_first(doc, ".//*[local-name()='ContentConstraint']/*[local-name()='CubeRegion']")
  xml2::xml_set_attr(node, "include", "false")
  s <- fixture_structure(doc = doc)
  expect_identical(s$availability, "unavailable")
})

test_that("a missing DSD or dictionary never falls back to an arbitrary one", {
  doc <- fixture_doc()
  xml2::xml_remove(xml2::xml_find_all(doc, ".//*[local-name()='DataStructure']"))
  expect_error(fixture_structure(doc = doc), "DSD")
  doc <- fixture_doc()
  xml2::xml_remove(xml2::xml_find_all(doc, ".//*[local-name()='Codelist' and @id='CL_REF_AREA']"))
  expect_error(fixture_structure(doc = doc), "codelist")
})

test_that("dataset context and fully qualified dimensions resolve identically", {
  local_fixture_metadata()
  a <- importLabels("REF_AREA", "TEST,DEMO,1.0")
  b <- importLabels("TEST,DEMO,1.0::REF_AREA")
  expect_identical(a, b)
  local_oecd(dataset = "TEST,DEMO,1.0")
  expect_identical(importLabels("REF_AREA"), a)
  expect_error(importLabels("UNKNOWN"), "Unknown")
})

Try the OECDdata package in your browser

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

OECDdata documentation built on Oct. 31, 2026, 1:07 a.m.