Skip to content

Commit

Permalink
Use snapshot tests in selection error messages (#72)
Browse files Browse the repository at this point in the history
* Fixes #71; use snapshot tests for selection errors

* Use testthat edition 3

* Update testthat snapshots

* Suggest testthat >= 3.2.0

* Update snapshots

* Require tidyselect 1.2.1 in snapshots

---------

Co-authored-by: Derek Burk <burkx031@umn.edu>
  • Loading branch information
robe2037 and dtburk committed Mar 11, 2024
1 parent 4669513 commit 1a2525d
Show file tree
Hide file tree
Showing 16 changed files with 305 additions and 133 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Expand Up @@ -53,7 +53,7 @@ Suggests:
scales,
sf,
shiny,
testthat,
testthat (>= 3.2.0),
tidyr,
vcr (>= 0.6.0),
withr
Expand All @@ -63,3 +63,4 @@ Contact: ipums@umn.edu
Encoding: UTF-8
Roxygen: list(markdown = TRUE, r6 = FALSE)
RoxygenNote: 7.2.3
Config/testthat/edition: 3
10 changes: 9 additions & 1 deletion tests/testthat.R
@@ -1,4 +1,12 @@
# This file is part of the standard setup for testthat.
# It is recommended that you do not modify it.
#
# Where should you do additional test configuration?
# Learn more about the roles of various files in:
# * https://r-pkgs.org/testing-design.html#sec-tests-files-overview
# * https://testthat.r-lib.org/articles/special-files.html

library(testthat)
library(ipumsr)

test_check("ipumsr", reporter = "location")
test_check("ipumsr")
29 changes: 29 additions & 0 deletions tests/testthat/_snaps/nhgis.md
@@ -0,0 +1,29 @@
# Can read NHGIS extract: single dataset

Code
nhgis_csv <- read_nhgis(nhgis_single_csv)
Message
Use of data from NHGIS is subject to conditions including that users should cite the data appropriately. Use command `ipums_conditions()` for more details.
Rows: 71 Columns: 25
-- Column specification --------------------------------------------------------
Delimiter: ","
chr (9): GISJOIN, STUSAB, CMSA, PMSA, PMSAA, AREALAND, AREAWAT, ANPSADPI, F...
dbl (13): YEAR, MSA_CMSAA, INTPTLAT, INTPTLNG, PSADC, D6Z001, D6Z002, D6Z003...
lgl (3): DIVISIONA, REGIONA, STATEA
i Use `spec()` to retrieve the full column specification for this data.
i Specify the column types or set `show_col_types = FALSE` to quiet this message.

# We get informative error messages when reading NHGIS extracts

Code
read_nhgis(nhgis_multi_ds, file_select = 3, verbose = FALSE)
Condition
Error in `find_files_in()`:
! Can't select files past the end.
i Location 3 doesn't exist.
i There are only 2 files.
Available files:
* nhgis0731_csv/nhgis0731_ds239_20185_nation.csv
* nhgis0731_csv/nhgis0731_ts_nominal_state.csv

14 changes: 14 additions & 0 deletions tests/testthat/_snaps/shape_read.md
@@ -0,0 +1,14 @@
# We get informative errors when reading shapefiles

Code
read_ipums_sf(nhgis_multi_shp, file_select = 4)
Condition
Error in `find_files_in()`:
! Can't select files past the end.
i Location 4 doesn't exist.
i There are only 3 files.
Available files:
* nhgis0712_shape/nhgis0712_shapefile_cenpop2000_us_state_cenpop_2000.zip
* nhgis0712_shape/nhgis0712_shapefile_tl2000_us_pmsa_1990.zip
* nhgis0712_shape/nhgis0712_shapefile_tl2000_us_pmsa_2000.zip

2 changes: 1 addition & 1 deletion tests/testthat/test_api_define_extract.R
Expand Up @@ -695,7 +695,7 @@ test_that("Can add subfields to existing `ipums_*` fields", {

expect_equal(
purrr::map(nhgis_revised$datasets, ~ .x$name),
purrr::map(nhgis_extract$datasets, ~ .x$name),
purrr::map(nhgis_extract$datasets, ~ .x$name)
)
expect_equal(
unname(purrr::map(nhgis_revised$datasets, ~ .x$data_tables)),
Expand Down
103 changes: 61 additions & 42 deletions tests/testthat/test_api_extract_info.R
Expand Up @@ -26,13 +26,15 @@ test_that("Can check microdata extract status", {
skip_if_no_api_access(have_api_access)

vcr::use_cassette("submitted-usa-extract", {
submitted_usa_extract <- submit_extract(test_usa_extract())
suppressMessages(
submitted_usa_extract <- submit_extract(test_usa_extract())
)
})

extract_number <- submitted_usa_extract$number

vcr::use_cassette("ready-usa-extract", {
wait_for_extract(submitted_usa_extract)
wait_for_extract(submitted_usa_extract, verbose = FALSE)
})

# Use same cassette for each status check. If request is incompatible,
Expand Down Expand Up @@ -71,13 +73,15 @@ test_that("Can check NHGIS extract status", {
skip_if_no_api_access(have_api_access)

vcr::use_cassette("submitted-nhgis-extract", {
submitted_nhgis_extract <- submit_extract(test_nhgis_extract())
suppressMessages(
submitted_nhgis_extract <- submit_extract(test_nhgis_extract())
)
})

extract_number <- submitted_nhgis_extract$number

vcr::use_cassette("ready-nhgis-extract", {
wait_for_extract(submitted_nhgis_extract)
wait_for_extract(submitted_nhgis_extract, verbose = FALSE)
})

vcr::use_cassette("get-nhgis-extract-info", {
Expand Down Expand Up @@ -129,10 +133,15 @@ test_that("We avoid superfluous checks when getting extract status", {
skip_if_no_api_access(have_api_access)

vcr::use_cassette("submitted-nhgis-extract", {
submitted_nhgis_extract <- submit_extract(test_nhgis_extract())
suppressMessages(
submitted_nhgis_extract <- submit_extract(test_nhgis_extract())
)
})
vcr::use_cassette("ready-nhgis-extract", {
ready_nhgis_extract <- wait_for_extract(submitted_nhgis_extract)
ready_nhgis_extract <- wait_for_extract(
submitted_nhgis_extract,
verbose = FALSE
)
})

# This should not make an API request because the extract is already
Expand Down Expand Up @@ -229,14 +238,16 @@ test_that("Can get extract info for default collection", {

withr::with_envvar(new = c("IPUMS_DEFAULT_COLLECTION" = "nhgis"), {
vcr::use_cassette("submitted-nhgis-extract", {
submitted_nhgis_extract <- submit_extract(test_nhgis_extract())
suppressMessages(
submitted_nhgis_extract <- submit_extract(test_nhgis_extract())
)
})

extract_number <- submitted_nhgis_extract$number

vcr::use_cassette("ready-nhgis-extract", {
expect_equal(
wait_for_extract(extract_number)$status,
wait_for_extract(extract_number, verbose = FALSE)$status,
"completed"
)
})
Expand Down Expand Up @@ -346,23 +357,27 @@ test_that("Can get extract history for more records than page size", {

test_that("Tibble of recent micro extracts has expected structure", {
skip_if_no_api_access(have_api_access)
withr::local_options(lifecycle_verbosity = "quiet")

usa_extract <- test_usa_extract()

vcr::use_cassette("submitted-usa-extract", {
submitted_usa_extract <- submit_extract(usa_extract)
suppressMessages(
submitted_usa_extract <- submit_extract(usa_extract)
)
})

submitted_number <- submitted_usa_extract$number

vcr::use_cassette("ready-usa-extract", {
ready_usa_extract <- wait_for_extract(submitted_usa_extract)
ready_usa_extract <- wait_for_extract(
submitted_usa_extract,
verbose = FALSE
)
})

vcr::use_cassette("recent-usa-extracts-tbl", {
lifecycle::expect_deprecated(
recent_usa_extracts_tbl <- get_recent_extracts_info_tbl("usa")
)
recent_usa_extracts_tbl <- get_recent_extracts_info_tbl("usa")
})

recent_numbers <- recent_usa_extracts_tbl$number
Expand Down Expand Up @@ -412,20 +427,24 @@ test_that("Tibble of recent micro extracts has expected structure", {

test_that("Tibble of recent NHGIS extracts has expected structure", {
skip_if_no_api_access(have_api_access)
withr::local_options(lifecycle_verbosity = "quiet")

vcr::use_cassette("submitted-nhgis-extract", {
submitted_nhgis_extract <- submit_extract(test_nhgis_extract())
suppressMessages(
submitted_nhgis_extract <- submit_extract(test_nhgis_extract())
)
})

submitted_number <- submitted_nhgis_extract$number

vcr::use_cassette("ready-nhgis-extract", {
ready_nhgis_extract <- wait_for_extract(submitted_nhgis_extract)
ready_nhgis_extract <- wait_for_extract(
submitted_nhgis_extract,
verbose = FALSE
)
})
vcr::use_cassette("recent-nhgis-extracts-tbl", {
lifecycle::expect_deprecated(
recent_nhgis_extracts_tbl <- get_recent_extracts_info_tbl("nhgis")
)
recent_nhgis_extracts_tbl <- get_recent_extracts_info_tbl("nhgis")
})

recent_numbers <- recent_nhgis_extracts_tbl$number
Expand Down Expand Up @@ -490,20 +509,18 @@ test_that("Tibble of recent NHGIS extracts has expected structure", {

test_that("Can get specific number of recent extracts in tibble format", {
skip_if_no_api_access(have_api_access)
withr::local_options(lifecycle_verbosity = "quiet")

vcr::use_cassette("recent-usa-extracts-tbl-two", {
lifecycle::expect_deprecated(
two_recent_usa_extracts <- get_recent_extracts_info_tbl(
"usa",
how_many = 2
)
two_recent_usa_extracts <- get_recent_extracts_info_tbl(
"usa",
how_many = 2
)
})
vcr::use_cassette("recent-nhgis-extracts-tbl-two", {
lifecycle::expect_deprecated(
two_recent_nhgis_extracts <- get_recent_extracts_info_tbl(
"nhgis",
how_many = 2
)
two_recent_nhgis_extracts <- get_recent_extracts_info_tbl(
"nhgis",
how_many = 2
)
})

Expand All @@ -525,11 +542,10 @@ test_that("Error on invalid number of records", {

test_that("Microdata tbl/list conversion works", {
skip_if_no_api_access(have_api_access)
withr::local_options(lifecycle_verbosity = "quiet")

vcr::use_cassette("recent-usa-extracts-tbl", {
lifecycle::expect_deprecated(
recent_usa_extracts_tbl <- get_recent_extracts_info_tbl("usa")
)
recent_usa_extracts_tbl <- get_recent_extracts_info_tbl("usa")
})
vcr::use_cassette("recent-usa-extracts-list", {
recent_usa_extracts_list <- get_extract_history("usa")
Expand All @@ -552,14 +568,15 @@ test_that("Microdata tbl/list conversion works", {

test_that("NHGIS tbl/list conversion works", {
skip_if_no_api_access(have_api_access)
withr::local_options(lifecycle_verbosity = "quiet")

vcr::use_cassette("submitted-nhgis-extract", {
submitted_nhgis_extract <- submit_extract(test_nhgis_extract())
suppressMessages(
submitted_nhgis_extract <- submit_extract(test_nhgis_extract())
)
})
vcr::use_cassette("recent-nhgis-extracts-tbl", {
lifecycle::expect_deprecated(
recent_nhgis_extracts_tbl <- get_recent_extracts_info_tbl("nhgis")
)
recent_nhgis_extracts_tbl <- get_recent_extracts_info_tbl("nhgis")
})
vcr::use_cassette("recent-nhgis-extracts-list", {
recent_nhgis_extracts_list <- get_extract_history("nhgis")
Expand Down Expand Up @@ -604,21 +621,23 @@ test_that("NHGIS tbl/list conversion works", {
# least one of datasets, time_series_tables, or shapefiles in past.
test_that("NHGIS shapefile-only tbl/list conversion works", {
skip_if_no_api_access(have_api_access)
withr::local_options(lifecycle_verbosity = "quiet")

vcr::use_cassette("submitted-nhgis-extract-shp", {
submitted_nhgis_extract_shp <- submit_extract(test_nhgis_extract_shp())
suppressMessages(
submitted_nhgis_extract_shp <- submit_extract(test_nhgis_extract_shp())
)
})
vcr::use_cassette("ready-nhgis-extract-shp", {
ready_nhgis_extract_shp <- wait_for_extract(
c("nhgis", submitted_nhgis_extract_shp$number)
c("nhgis", submitted_nhgis_extract_shp$number),
verbose = FALSE
)
})
vcr::use_cassette("recent-nhgis-extracts-tbl-one", {
lifecycle::expect_deprecated(
nhgis_extract_tbl_shp <- get_recent_extracts_info_tbl(
"nhgis",
how_many = 1
)
nhgis_extract_tbl_shp <- get_recent_extracts_info_tbl(
"nhgis",
how_many = 1
)
})

Expand Down

0 comments on commit 1a2525d

Please sign in to comment.