diff --git a/R/multi_geo_fetch.R b/R/multi_geo_fetch.R index 714ed77..fe09393 100644 --- a/R/multi_geo_fetch.R +++ b/R/multi_geo_fetch.R @@ -411,7 +411,8 @@ multi_geo_prep <- function(src, msa = msa, us = us, new_england = new_england, - nhood_type = nhood_valid_fips + nhood_type = nhood_valid_fips, + use_cogs = use_cogs ) # print title rlang::exec(table_printout, !!!tbl_title) diff --git a/R/printouts.R b/R/printouts.R index 6752e7c..bfc5b77 100644 --- a/R/printouts.R +++ b/R/printouts.R @@ -4,11 +4,17 @@ bold_hdr <- function(place_name, place_type) { } ######## CENSUS: ACS + DECENNIAL ---- -geo_printout <- function(neighborhoods, tracts, blockgroups, towns, regions, pumas, counties, all_counties, drop_counties, state, msa, us, new_england, nhood_type) { +geo_printout <- function(neighborhoods, tracts, blockgroups, towns, regions, pumas, counties, all_counties, drop_counties, state, msa, us, new_england, nhood_type, use_cogs) { geos <- tibble::lst(neighborhoods, tracts, blockgroups, towns, regions, pumas, counties, state) if (drop_counties) { - geos$counties <- NULL + geos[["counties"]] <- NULL } + # rename counties to cogs + if (!is.null(counties) & use_cogs) { + geos[["cogs"]] <- geos[["counties"]] + geos[["counties"]] <- NULL + } + # basically writing own imap_at subgeos <- c("neighborhoods", "tracts", "blockgroups", "towns", "pumas") geos[subgeos] <- purrr::map(subgeos, function(geo_hdr) { @@ -17,13 +23,17 @@ geo_printout <- function(neighborhoods, tracts, blockgroups, towns, regions, pum geo_txt <- NULL } else if (identical(geo, "all")) { if (all_counties) { - county_str <- "all counties" + if (use_cogs) { + county_str <- "all COGs" + } else { + county_str <- "all counties" + } } else { county_str <- "{counties}" } geo_txt <- sprintf("all %s in %s", geo_hdr, county_str) } else { - if (geo_hdr == "towns") { + if (identical(geo_hdr, "towns")) { geo_txt <- geo } else { geo_txt <- paste(length(unique(geo)), geo_hdr) @@ -32,6 +42,7 @@ geo_printout <- function(neighborhoods, tracts, blockgroups, towns, regions, pum geo_txt }) geos <- rlang::set_names(geos, stringr::str_to_sentence) + geos <- rlang::set_names(geos, stringr::str_replace, "Cogs", "COGs") if (msa) { if (new_england) { diff --git a/tests/testthat/test-batch_csv_dump.R b/tests/testthat/test-batch_csv_dump.R index c3feb28..b04720e 100644 --- a/tests/testthat/test-batch_csv_dump.R +++ b/tests/testthat/test-batch_csv_dump.R @@ -31,9 +31,16 @@ test_that("batch_csv_dump prints messages if verbose", { }) test_that("batch_csv_dump passes arguments to write.csv", { - df <- dummy_df() - df$value[1:3] <- NA_real_ + # remember batch_csv_dump uses write.csv, not readr::write_csv! + df <- data.frame( + region = rep(c("A", "B", "C"), each = 2), + value = c(NA_real_, runif(5)) + ) dir <- tempdir() set_na <- batch_csv_dump(df, split_by = region, path = dir, base_name = "set_na", na = "") - no_names <- batch_csv_dump(df, split_by = region, path = dir, base_name = "no_names") + + # should have blank at end of line + set_na_read <- stringr::str_remove_all(readLines(file.path(dir, "set_na_A.csv")), '\"') + expect_true(grepl(",$", set_na_read[2])) + expect_false(grepl(",$", set_na_read[3])) }) diff --git a/tests/testthat/test-multi_geo_fetch.R b/tests/testthat/test-multi_geo_fetch.R index 6516690..0e411db 100644 --- a/tests/testthat/test-multi_geo_fetch.R +++ b/tests/testthat/test-multi_geo_fetch.R @@ -1,6 +1,3 @@ -library(cwi) -library(testthat) - # since there's a function that preps but doesn't make API calls, can use that for testing without doing full calls. test_that("multi_geo_* validates state names and FIPS codes", { expect_error(multi_test(state = NULL), "Must supply") diff --git a/tests/testthat/test-printouts.R b/tests/testthat/test-printouts.R new file mode 100644 index 0000000..28b938f --- /dev/null +++ b/tests/testthat/test-printouts.R @@ -0,0 +1,10 @@ +test_that("geo_printout handles counties vs COGs", { + expect_message(multi_test(year = 2021), "Counties: Fairfield County,") + expect_message(multi_test(year = 2022), "COGs: Capitol COG, ") + + expect_message(multi_test(year = 2021), "all towns in all counties") + expect_message(multi_test(year = 2022), "all towns in all COGs") + + expect_message(multi_test(year = 2021, counties = "Fairfield County"), "all towns in Fairfield County") + expect_message(multi_test(year = 2022, counties = "Capitol COG"), "all towns in Capitol COG") +})