Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better error message when no value is matched #45

Merged
merged 1 commit into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions R/dst_find_val_id.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ dst_find_val_id <- function(meta_data, variable, values_text = NULL) {
# nolint start
id_test <- as.character(values_text) %in% meta_data$values[[variable]]$text
if (sum(id_test) != length(values_text)) {
no_match_index <- !as.character(values_text) %in% meta_data$values[[variable]]$text
not_matched <- values_text[no_match_index] |>
paste(collapse = "\n")
stop(
paste0(
"All the values_text could not be matched in the text field of the values column of ",
variable,
". It might be a typo or maybe the value isn't present in the variable."
)
"\n It might be a typo or maybe the value isn't present in the variable.",
"\n Specifically, the following values were not matched:\n\n"
), paste(not_matched)
)
}
# nolint end
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/_snaps/dst_find_val_id.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# dst_find_val_id returns proper information on wrongly specified variable values

All the values_text could not be matched in the text field of the values column of OMRÅDE
It might be a typo or maybe the value isn't present in the variable.
Specifically, the following values were not matched:

Hele landets verden
København K

28 changes: 28 additions & 0 deletions tests/testthat/test-dst_find_val_id.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
test_that("dst_find_val_id returns proper information on wrongly specified variable values", {
# See issue #19 https://github.com/rOpenGov/dkstat/issues/19

folk1a_meta <- dst_meta("folk1a", lang = "da")

expect_error(dst_get_data(
table = "folk1a",
Tid = "*",
CIVILSTAND = "*",
ALDER = "*",
OMRÅDE = c("Hele landets verden", "København K", "Dragør", "Albertslund"),
lang = "da",
meta_data = folk1a_meta
))

expect_snapshot_error(
dst_get_data(
table = "folk1a",
Tid = "*",
CIVILSTAND = "*",
ALDER = "*",
OMRÅDE = c("Hele landets verden", "København K", "Dragør", "Albertslund"),
lang = "da",
meta_data = folk1a_meta
)
)

})
Loading