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

Fix #24 #40

Merged
merged 5 commits into from
Nov 28, 2024
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
3 changes: 2 additions & 1 deletion .lintr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
linters: linters_with_defaults() # see vignette("lintr")
linters: linters_with_defaults(
cyclocomp_linter(complexity_limit = 17L))
encoding: "UTF-8"
exclusions: list(
"tests")
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ Suggests:
testthat
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
Config/testthat/edition: 3
Config/testthat/parallel: true
RoxygenNote: 7.3.2
X-schema.org-isPartOf: http://ropengov.org/
X-schema.org-keywords: ropengov
Expand Down
26 changes: 26 additions & 0 deletions R/dst_determine_overlaps.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#' Helper function to determine wether or not to include the id in a variable
#' option
#'
#' @param meta_data Meta data object for the table of inquiry
#' @noRd
dst_determine_overlaps <- function(meta_data) {
# Get variable names
var_names <- get_vars(meta_data)

# Get options for all variable names
options <- get_var_options(meta_data, var_names)

# Index over all vars to determine if there is duplicates
dup <- list()

for (i in seq_along(var_names)) {
dup[i] <- length(
options[[var_names[i]]]
) == length(
unique(options[[var_names[i]]])
)
}

# If any of the option/vars include duplicates, we should include the id
return(any(unlist(dup)))
}
20 changes: 19 additions & 1 deletion R/dst_get_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
#' requested.
#' @param format character value. "CSV" or "BULK". If you choose BULK then you
#' need to select a value for each of the parameters.
#' @param value_presentation For now, "value" or "default"
#' @param value_presentation For now, "value" or "default". When a table with
#' observations that have the same name, this is automatically changed to
#' CodeAndValue.
#' @export
#' @family Data retrival functions
#' @importFrom utils read.csv read.csv2
Expand Down Expand Up @@ -47,6 +49,11 @@ dst_get_data <- function(table,
}
}

# If meta_data is NULL then get it automatically
if (is.null(meta_data)) {
meta_data <- dst_meta(table, lang = lang)
}

# Force the names to be uppercase to match requirements from API
names(query) <- toupper(names(query))
dst_names <- names(query)
Expand All @@ -60,6 +67,11 @@ dst_get_data <- function(table,
format = format
)

# If overlaps in values are detected use CodeAndValue as presentation
if (dst_determine_overlaps(meta_data)) {
value_presentation <- "CodeAndValue"
}

query$valuePresentation <- value_presentation
query$lang <- lang

Expand Down Expand Up @@ -105,8 +117,14 @@ dst_get_data <- function(table,
} else {
stop("You haven't selected an appropiate language ('da' or 'en'")
}

names(dst_data) <- c(dst_names, "value")

# Remove the code
if (dst_determine_overlaps(meta_data)) {
dst_data$TID <- sapply(stringr::str_split(dst_data$TID, "\\s+"), `[`, 2)
}

# Parse the dates if param is TRUE
if (parse_dst_tid) {
dst_data$TID <- dst_date_parse(dst_date = dst_data$TID)
Expand Down
4 changes: 0 additions & 4 deletions data-raw/dst_tables.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
tables <- dst_get_tables(lang = "en")

usethis::use_data(tables, overwrite = TRUE)

# tables_da <- dst_get_tables(lang = "da")
#
# usethis::use_data(tables_da, overwrite = TRUE)
4 changes: 3 additions & 1 deletion man/dst_get_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 12 additions & 14 deletions tests/testthat/test-dst_date_parse.R
Original file line number Diff line number Diff line change
@@ -1,51 +1,49 @@
context("dst_date_parse")

test_that("dst_date_parse gives the correct class.", {

exp_dates <- c("POSIXct", "POSIXt")

# Daily
expect_equal(class(dst_date_parse(dst_date = "2000M01D01")), exp_dates)
expect_equal(class(dst_date_parse(dst_date = c("2000M01D20", "2000M02D21", "2000M03D23", "2000M04D24"))), exp_dates)

# Monthly
expect_equal(class(dst_date_parse(dst_date = "2000M01")), exp_dates)
expect_equal(class(dst_date_parse(dst_date = c("2000M01", "2000M02", "2000M03", "2000M04", "2000M10", "2000M11"))), exp_dates)

# Quarterly
expect_equal(class(dst_date_parse(dst_date = "2000Q1")), exp_dates)
expect_equal(class(dst_date_parse(dst_date = "2000Q2")), exp_dates)
expect_equal(class(dst_date_parse(dst_date = "2000Q3")), exp_dates)
expect_equal(class(dst_date_parse(dst_date = "2000Q4")), exp_dates)
expect_equal(class(dst_date_parse(dst_date = c("2000Q1", "2000Q2", "2000Q3", "2000Q4"))), exp_dates)

# Yearly
expect_equal(class(dst_date_parse(dst_date = "2000")), exp_dates)
expect_equal(class(dst_date_parse(dst_date = c("2000", "2000", "2000", "2000"))), exp_dates)

expect_equal(class(dst_date_parse(dst_date = c("2015H1", "2015H2"))), exp_dates)
expect_equal(as.character(dst_date_parse(dst_date = c("2015H1", "2015H2"))), c("2015-01-01", "2015-07-01"))

})


test_that("Test that dst_date_parse stops when the input is bad.", {

# Daily
expect_error(dst_date_parse(dst_date = "2000M01D35"))
expect_error(dst_date_parse(dst_date = "2000M10D40"))

# Monthly
expect_error(dst_date_parse(dst_date = "20000M01"))
expect_error(dst_date_parse(dst_date = "2000M101"))
expect_error(dst_date_parse(dst_date = "2000M13"))

# Quarterly
expect_error(dst_date_parse(dst_date = "2000Q0"))
expect_error(dst_date_parse(dst_date = "2000Q5"))

# Yearly
expect_error(dst_date_parse(dst_date = "20000"))
expect_error(dst_date_parse(dst_date = "200"))

})
3 changes: 3 additions & 0 deletions tests/testthat/test-dst_determine_overlaps.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test_that("Overlap detection works", {
expect_true(dst_determine_overlaps(dst_meta("FV22TOTA")))
})
19 changes: 8 additions & 11 deletions tests/testthat/test-dst_get_data.R
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@

context("dst_get_data")

test_that("dst_get_data returns an error when the limit is reached.", {

expect_error(dst_get_data(table = "folk1",
query = list(CIVILSTAND = "*",
STATSB = "*",
query = list(CIVILSTAND = "*",
STATSB = "*",
HERKOMST = c("Personer med dansk oprindelse", "Efterkommere"),
TID = "*",
ALDER = "*"), lang = "da"))

})


test_that("dst_get_data is parsing the data correctly when 'en' and 'da' are selected as language. The API returns decimal numbers with both , and .", {

expect_equal(class(dst_get_data("AUP01", ALDER = "*", TID = "*", lang = "da", format = "CSV")$value), "numeric")

})

test_that("dst_get_data fails with the BULK format when not all parameters have values.", {

expect_error(dst_get_data("AUP01", ALDER = "*", TID = "*", lang = "da", format = "BULK"))

})


9 changes: 3 additions & 6 deletions tests/testthat/test-dst_get_tables.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@

context("dst_get_tables")

test_that("dst_get_tables return a data.frame",{

expect_equal(class(dst_get_tables(lang = "da")), "data.frame")
expect_equal(class(dst_get_tables(lang = "en")), "data.frame")

})

test_that("dst_get_tables failes with wrong language input",{

expect_error(dst_get_tables(lang = "uk"))
expect_error(dst_get_tables(lang = "no"))
})
Expand Down
8 changes: 2 additions & 6 deletions tests/testthat/test-dst_query_match.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@


context("dst_query_match")

test_that("The function fails when mandatory values are not supplied.",{

expect_error(dst_query_match(table = "NRHP", meta_data = NULL, lang = "en", query = list(TRANSAKT = "P.1 Output", Tid = "1993")))
})
})
5 changes: 0 additions & 5 deletions tests/testthat/test-dst_value_limit.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@



context("dst_value_limit")

test_that("dst_value_limit returns the correct value.", {

expect_equal(dst_value_limit(query = list(CIVILSTAND = "*",
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-fv22tota.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
test_that("Tables with identifyers with the same name but different ids are parsed properly", {
# This tests that the issue in https://github.com/rOpenGov/dkstat/issues/24
# does not occur

table <- dst_get_data(
table = "FV22TOTA",
VALRES = "*",
OMRÅDE = "*",
Tid = "*",
lang = "da",
format = "BULK"
)

expect_equal(nrow(unique(table)), nrow(table))
expect_equal(nrow(table) - nrow(unique(table)), 0)

})
Loading