Skip to content

Commit

Permalink
Update tests comments
Browse files Browse the repository at this point in the history
Add comments to tests explaining what us being tested #121
  • Loading branch information
jd-otero committed Jun 18, 2024
1 parent 495d96e commit 74ccaa6
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 18 deletions.
34 changes: 34 additions & 0 deletions tests/testthat/test-aggregate_climate.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,39 @@ lon_2 <- c(-75.5221, -74.96571, -74.96571, -75.5221, -75.5221)
polygon_2 <- sf::st_polygon(x = list(cbind(lon_2, lat_2))) %>% sf::st_sfc()
ibague <- sf::st_as_sf(polygon_2)

# Base data TSSM (Temperature)
base_tssm <- download_climate_geom(
geometry = medellin,
start_date = "2014-01-01",
end_date = "2015-04-05",
tag = "TSSM_CON"
)

# Base data BSHG (Sunshine duration)
base_bshg <- download_climate_geom(
geometry = medellin,
start_date = "1990-01-01",
end_date = "1990-05-16",
tag = "BSHG_CON"
)

# Base data TMN (Minimum temperature)
base_tmn <- download_climate_geom(
geometry = ibague,
start_date = "2010-04-01",
end_date = "2010-10-31",
tag = "TMN_CON"
)

# Base data TMX (Maximum temperature)
base_tmx <- download_climate_geom(
geometry = ibague,
start_date = "2020-04-01",
end_date = "2020-04-30",
tag = "TMX_CON"
)

# Base data PTPM (Precipitation)
base_ptpm <- download_climate_geom(
geometry = ibague,
start_date = "2020-06-14",
Expand All @@ -42,37 +51,59 @@ base_ptpm <- download_climate_geom(
)

test_that("Aggregation throws errors from frequency", {
# Expect error when frequency is not available
expect_error(aggregate_climate(base_tssm, "week"))

# Expect error when climate data.frame is incomplete (missing columns)
expect_error(aggregate_climate(base_tssm[, 1:5], "month"))

# Expect warning when data is already in the requested frequency
expect_warning(aggregate_climate(base_ptpm, "day"))
})

test_that("Aggregation for dry-bulb temperature works as expected", {
# Expect that output has a data.frame structure from TSSM
expect_s3_class(aggregate_climate(base_tssm, "day"), "data.frame")

# Expect that non-addable dates are not aggreegated (climate aggregtion rules)
month_tssm <- aggregate_climate(base_tssm, "month")
expect_identical(month_tssm[
which(month_tssm$date == "2015-04-01"),
"value"
][[1]], c(NA_real_, NA_real_))

# Expect that the result of adding long period of time is done right (TSSM)
expect_identical(nrow(na.omit(aggregate_climate(base_tssm, "year"))), 2L)
})

test_that("Aggregation for sunshine duration works as expected", {
# Expect that output has a data.frame structure for BSHG
expect_s3_class(aggregate_climate(base_bshg, "day"), "data.frame")

# Expect that the result of adding long period of time is done right (BSHG)
expect_identical(nrow(aggregate_climate(base_bshg, "month")), 15L)

# Expect specific dataset from a proper request
expect_snapshot(aggregate_climate(base_bshg, "year"))
})

test_that("Aggregation for minimum temperature works as expected", {
# Expect that output has a data.frame structure for TMN
expect_s3_class(aggregate_climate(base_tmn, "month"), "data.frame")

# Expect that the result from climate aggregation has the same structure as
# climate data
expect_named(
aggregate_climate(base_tmn, "year"),
c("station", "longitude", "latitude", "date", "tag", "value")
)
})

test_that("Aggregation for maximum temperature works as expected", {
# Expect that output has a data.frame structure for TMX
expect_s3_class(aggregate_climate(base_tmx, "year"), "data.frame")

# Expect specific value for monthly aggregates in TMX
month_tmx <- aggregate_climate(base_tmx, "month")
expect_identical(max(month_tmx[
which(month_tmx$station == 21245010),
Expand All @@ -81,6 +112,9 @@ test_that("Aggregation for maximum temperature works as expected", {
})

test_that("Aggregation for precipitation works as expected", {
# Expect that output has a data.frame structure for PTPM
expect_s3_class(aggregate_climate(base_ptpm, "month"), "data.frame")

# Expect specific dataset from a proper request
expect_snapshot(aggregate_climate(base_ptpm, "year"))
})
64 changes: 59 additions & 5 deletions tests/testthat/test-divipola.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
test_that("Divipola department code throws errors", {
# Expect error when department_name is not a character
expect_error(name_to_code_dep(5678))

# Expect error when department_name does not exist
expect_warning(name_to_code_dep("CARTAGO"))
})

test_that("Divipola department code works as expected", {
# Expect specific vector from a proper request
expect_identical(
name_to_code_dep(c("Tolima", "Antioquia")),
c("73", "05")
)
# Expect specific vector from a proper request using multiple cases and
# including border scenarios ("Santander" and "Norte de Santander" have
# similar strings. "San Andres" is a frequent way to call the department while
# the official name is much much longer)
expect_identical(
name_to_code_dep(c(
"Santander",
Expand All @@ -19,36 +27,48 @@ test_that("Divipola department code works as expected", {
})

test_that("Divipola municipality code throws errors", {
# Expect error when department_name is not a character
expect_error(name_to_code_mun(
c(6787, 13),
c("Soplaviento", "Cartagena")
))

# Expect error when municipality_name is not a character
expect_error(name_to_code_mun(
c("Bolivar", "Bolivar"),
c(444, 555)
))
expect_error(name_to_code_mun(
"Bolivar",
c("Soplaviento", "Cartagena")
))

# Expect error warning when one of the input pairs ("Bolivar", "Soplaviento")
# is correct and the other one is not ("Panama", "Cartagena"), since Panama is
# not a department of Colombia
expect_warning(name_to_code_mun(
c("Bolivar", "Panamá"),
c("Soplaviento", "Cartagena")
))

# Expect error warning when one of the input pairs ("Bolivar", "Cartagena") is
# correct and the other one is not ("Bolivar", "S"), since S is not a municipality
# in Bolivar
expect_warning(name_to_code_mun(
c("Bolivar", "Bolivar"),
c("S", "Cartagena")
))
})

test_that("Divipola municipality code works as expected", {
# Expect specific vector from a proper request including municipalities with
# same name in different departments (This is quite common in Colombia)
expect_identical(
name_to_code_mun(
c("Santander", "Antioquia"),
c("Rionegro", "Rionegro")
),
c("68615", "05615")
)

# Expect specific vector from a proper request including municipalities with
# similar names (Same begining) in the same department (Also, quite common)
expect_identical(
name_to_code_mun(
c("Antioquia", "Antioquia"),
Expand All @@ -59,34 +79,68 @@ test_that("Divipola municipality code works as expected", {
),
c("05579", "05591")
)

# Expect specific vector from a proper request when there are multiple
# municipalities from the same department
municipalities
expect_identical(
name_to_code_mun(
c("Antioquia"),
c(
"Puerto Berrio",
"Puerto Triunfo"
)
),
c("05579", "05591")
)
})

test_that("Divipola department name throws errors", {
# Expect error when department_code is not a character
expect_error(code_to_name_dep(c(73, 05)))

# Expect warning when some of the elements in department_code are not a
# character
expect_warning(code_to_name_dep(c(73, "14")))

# Expect warning when the department code does not exist
expect_warning(code_to_name_dep("04"))
})

test_that("Divipola department name works as expected", {
# Expect specific vector from a proper request
expect_identical(code_to_name_dep("05"), "Antioquia")
})

test_that("Divipola municipality name throws errors", {
test_that("Divipola municipality and department name throws errors", {
# Expect error when department_code and municipality_code are not a character
expect_error(code_to_name_mun(c(05001, 73001)))

# Expect error when only one of the arguments is not a character
expect_error(code_to_name_mun(05678, "14"))

# Expect warning when only one of the arguments is not a real municipality
# code
expect_warning(code_to_name_mun(c("05001", "73048")))

# Expect error when introducing a municipality code inside the department
# function
expect_error(code_to_name_dep("04678"))
})

test_that("Divipola municipality name works as expected", {
# Expect specific vector from a proper request
expect_identical(code_to_name_mun("05051"), "Arboletes")
})

test_that("Translate divipola department name works as expected", {
# Expect specific vector from a proper request
expect_identical(name_to_standard_dep("Bogota"), "Bogotá, D.C.")
})

test_that("Translate divipola municipality name works as expected", {
# Expect specific vector from a proper request with a border scenario
# (Shortened municipality name, which is frequently used)
expect_identical(name_to_standard_mun(
"Antioquia", "Sta fe de antioquia"
), "Santa Fé de Antioquia")
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-documentation.R
Original file line number Diff line number Diff line change
@@ -1,32 +1,49 @@
test_that("List datasets errors are thrown", {
# Expect error when category does not exist
expect_error(list_datasets(category = "dem"))
})

test_that("List datasets works as expected", {
# Expect specific dataset from a proper request
expect_snapshot(list_datasets("geospatial"))
})

test_that("Dictionary errors are thrown", {
# Expect error when spatial_level does not exist
expect_error(geospatial_dictionary("DANE_MGN_2018_MPIO"))
})

test_that("Dictionary works as expected", {
# Expect specific dataset from a proper request
expect_snapshot(geospatial_dictionary("mpio"))
})

test_that("Climate tags works as expected", {
# Expect specific dataset from a proper request (no arguments for this
# function)
expect_snapshot(climate_tags())
})

test_that("Lookup errors are thrown", {
# Expect error when keywords is not a character
expect_error(look_up(keywords = 0L))

# Expect error when keywords are not found in any dataset
expect_error(look_up(keywords = "dog"))

# Expect error when logic is TRUE or FALSE (as presented in documentation, it
# will respond to "and" / "or")
expect_error(look_up(keywords = "households", logic = TRUE))
expect_error(look_up(keywords = "households", logic = "nor"))

# Expect error when module does not exist
expect_error(look_up(module = "population", keywords = "households"))
})

test_that("Lookup works as expected", {
# Expect that output has a data.frame structure for a proper request
expect_s3_class(look_up(keywords = "school", logic = "or"), "data.frame")

# Expect specific dataset from a proper request
expect_snapshot(look_up(keywords = c("school", "age"), logic = "and"))
})
Loading

0 comments on commit 74ccaa6

Please sign in to comment.