tests/testthat/test-get_colombia_life_expectancy.R

# ColombiAPI - Access Colombian Data via APIs and Curated Datasets
# Version 0.3.1
# Copyright (C) 2025 Renzo Caceres Rossi
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

# get_colombia_life_expectancy


library(testthat)

test_that("get_colombia_life_expectancy() returns a tibble with the correct structure and content", {
  skip_on_cran()

  result <- get_colombia_life_expectancy()

  # Should always return a tibble, never NULL
  expect_false(is.null(result))
  expect_s3_class(result, "data.frame")
  expect_named(result, c("indicator", "country", "year", "value"))

  # If data is empty (offline mode), skip deeper checks
  if (nrow(result) == 0) {
    skip("API unavailable or offline mode detected — returning empty tibble as expected.")
  }

  # Data type checks
  expect_type(result$indicator, "character")
  expect_type(result$country, "character")
  expect_type(result$year, "integer")
  expect_true(is.numeric(result$value))

  # Expected content checks
  expect_true(all(result$indicator == "Life expectancy at birth, total (years)"))
  expect_true(all(result$country == "Colombia"))

  # Range checks
  expect_true(all(result$year >= 2010 & result$year <= 2022))
  expect_equal(nrow(result), 13)
  expect_equal(ncol(result), 4)
})

test_that("get_colombia_life_expectancy() returns data for years 2010 to 2022", {
  skip_on_cran()

  result <- get_colombia_life_expectancy()
  if (nrow(result) == 0) skip("API unavailable or offline mode detected — skipping check.")

  expect_true(all(result$year %in% 2010:2022))
  expect_equal(sort(unique(result$year)), 2010:2022)
})

test_that("get_colombia_life_expectancy() year column has no NA values", {
  skip_on_cran()

  result <- get_colombia_life_expectancy()
  if (nrow(result) == 0) skip("Offline mode — skipping NA check.")

  expect_false(any(is.na(result$year)))
})

test_that("get_colombia_life_expectancy() value column allows NA values", {
  skip_on_cran()

  result <- get_colombia_life_expectancy()
  if (nrow(result) == 0) skip("Offline mode — skipping NA validation.")

  expect_true(all(is.finite(result$value) | is.na(result$value)))
  expect_true(any(is.na(result$value)) || all(!is.na(result$value)))
})

test_that("get_colombia_life_expectancy() years are sorted in descending order", {
  skip_on_cran()

  result <- get_colombia_life_expectancy()
  if (nrow(result) == 0) skip("Offline mode — skipping sorting check.")

  expect_equal(result$year, sort(result$year, decreasing = TRUE))
})

test_that("get_colombia_life_expectancy() indicator and country are consistent across rows", {
  skip_on_cran()

  result <- get_colombia_life_expectancy()
  if (nrow(result) == 0) skip("Offline mode — skipping consistency checks.")

  expect_equal(length(unique(result$indicator)), 1)
  expect_equal(length(unique(result$country)), 1)
})

test_that("get_colombia_life_expectancy() returns exactly 13 rows for the specified period", {
  skip_on_cran()

  result <- get_colombia_life_expectancy()
  if (nrow(result) == 0) skip("Offline mode — skipping row count check.")

  expect_equal(nrow(result), 13)
})

test_that("get_colombia_life_expectancy() non-NA values are within reasonable range", {
  skip_on_cran()

  result <- get_colombia_life_expectancy()
  if (nrow(result) == 0) skip("Offline mode — skipping range validation.")

  non_na_values <- result$value[!is.na(result$value)]
  if (length(non_na_values) > 0) {
    expect_true(all(non_na_values > 0))
    expect_true(all(non_na_values <= 120))
    expect_true(all(non_na_values >= 30))
  }
})

test_that("get_colombia_life_expectancy() value column is numeric", {
  skip_on_cran()

  result <- get_colombia_life_expectancy()
  if (nrow(result) == 0) skip("Offline mode — skipping numeric type check.")

  expect_true(is.numeric(result$value))
  expect_true(is.double(result$value))
})

test_that("get_colombia_life_expectancy() handles API errors gracefully", {
  skip_on_cran()

  result <- get_colombia_life_expectancy()

  # Should always return a data.frame (tibble), even on error
  expect_false(is.null(result))
  expect_s3_class(result, "data.frame")

  # Should have correct column structure
  expect_named(result, c("indicator", "country", "year", "value"))
  expect_equal(ncol(result), 4)
})

test_that("get_colombia_life_expectancy() returns consistent data types", {
  skip_on_cran()

  result <- get_colombia_life_expectancy()
  if (nrow(result) == 0) skip("Offline mode — skipping type consistency check.")

  # Verify all columns have consistent types throughout
  expect_true(all(sapply(result$indicator, is.character)))
  expect_true(all(sapply(result$country, is.character)))
  expect_true(all(sapply(result$year, is.integer)))
  expect_true(all(sapply(result$value, function(x) is.numeric(x) || is.na(x))))
})

Try the ColombiAPI package in your browser

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

ColombiAPI documentation built on Nov. 5, 2025, 6:59 p.m.