Skip to content

Commit

Permalink
feat: better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
m-muecke committed Jan 24, 2024
1 parent 24a5a38 commit 2837433
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 12 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ Suggests:
Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.0
RoxygenNote: 7.3.1
22 changes: 15 additions & 7 deletions R/feed.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ tr_bill_rates <- function(date = NULL) {
#'
#' @inheritParams tr_yield_curve
#' @inherit tr_yield_curve references
#' @returns A `data.frame()` with columns `date`, `rate_type` and `rate` or
#' `NULL` when no entries were found.
#' @family treasury data
#' @export
tr_long_term_rate <- function(date = NULL) {
Expand Down Expand Up @@ -105,6 +107,8 @@ tr_long_term_rate <- function(date = NULL) {
#'
#' @inheritParams tr_yield_curve
#' @inherit tr_yield_curve references
#' @returns A `data.frame()` with columns `date`, `maturity` and `rate` or
#' `NULL` when no entries where found.
#' @family treasury data
#' @export
tr_real_yield_curve <- function(date = NULL) {
Expand Down Expand Up @@ -136,6 +140,7 @@ tr_real_yield_curve <- function(date = NULL) {
#'
#' @inheritParams tr_yield_curve
#' @inherit tr_yield_curve references
#' @returns A `data.frame()` with columns `date` and `rate`.
#' @family treasury data
#' @export
tr_real_long_term <- function(date = NULL) {
Expand Down Expand Up @@ -164,13 +169,16 @@ treasury <- function(data, date, resp_data) {
}

tr_make_request <- function(data, date) {
stopifnot(
is.null(date) ||
is.character(date) || is.numeric(date) && length(date) == 1L
)
date <- date %||% "all"
date <- as.character(date)
if (grepl("[0-9]{6}", date)) {
if (!is.null(date)) {
date <- as.character(date)
if (!(length(date) != 1L && grepl("[0-9]{4,6}", date))) {
stop("`date` must be a single value in format yyyy or yyyymm")
}
} else {
date <- "all"
}

if (nchar(date) == 6L) {
nm <- "field_tdr_date_value_month"
} else {
nm <- "field_tdr_date_value"
Expand Down
4 changes: 0 additions & 4 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
`%||%` <- function(x, y) {
if (is.null(x)) y else x
}

is_installed <- function(pkg) {
isTRUE(requireNamespace(pkg, quietly = TRUE))
}
Expand Down
4 changes: 4 additions & 0 deletions man/tr_long_term_rate.Rd

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

3 changes: 3 additions & 0 deletions man/tr_real_long_term.Rd

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

4 changes: 4 additions & 0 deletions man/tr_real_yield_curve.Rd

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

12 changes: 12 additions & 0 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -0,0 +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(treasury)

test_check("treasury")
19 changes: 19 additions & 0 deletions tests/testthat/test-feed.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
test_that("input validation works", {
fns <- list(
tr_yield_curve,
tr_bill_rates,
tr_long_term_rate,
tr_real_yield_curve,
tr_real_long_term
)
for (fn in fns) {
expect_error(fn(NA))
expect_error(fn("2020-01-01"))
expect_error(fn("202"))
expect_error(fn("abcd"))
expect_error(fn("abcdef"))
expect_error(fn(c(2020, 2021)))
expect_error(fn(c("2020", "2021")))
expect_error(fn(1L))
}
})

0 comments on commit 2837433

Please sign in to comment.