Skip to content

Commit d4cc969

Browse files
committed
better arg defaults
1 parent dd4d927 commit d4cc969

File tree

3 files changed

+84
-45
lines changed

3 files changed

+84
-45
lines changed

R/historical-webservice.R

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
#' you need lots of data, consider using HYDAT and the `hy_` family of functions
77
#'
88
#' @param station_number Water Survey of Canada station number.
9-
#' @param start_date Accepts YYYY-MM-DD.
10-
#' If only start date is supplied (i.e. YYYY-MM-DD) values are returned from the start of that day.
11-
#' Defaults to 365 days before current date.
12-
#' @param end_date Accepts either YYYY-MM-DD.
13-
#' If only a date is supplied (i.e. YYYY-MM-DD) values are returned from the end of that day.
14-
#' Defaults to current date.
9+
#' @param start_date Accepts YYYY-MM-DD. You need to provide a start date.
10+
#' The default value is NULL
11+
#' @param end_date Accepts either YYYY-MM-DD. You need to provide an end date.
12+
#' The default value is NULL
1513
#'
1614
#'
1715
#' @format A tibble with 6 variables:
@@ -26,20 +24,26 @@
2624
#' @seealso hy_daily_flows
2725
#' @examples
2826
#' \dontrun{
29-
#'
30-
#' flow_data <- ws_daily_flows(
31-
#' station_number = c("08NL071", "08NM174")
27+
#' try(
28+
#' flow_data <- ws_daily_flows(
29+
#' station_number = c("08NL071", "08NM174"),
30+
#' start_date = Sys.Date() - 365,
31+
#' end_date = Sys.Date()
32+
#' )
3233
#' )
33-
#'
34-
#' level_data <- ws_daily_level(
35-
#' station_number = c("08NL071", "08NM174")
34+
#' try(
35+
#' level_data <- ws_daily_level(
36+
#' station_number = c("08NL071", "08NM174"),
37+
#' start_date = Sys.Date() - 365,
38+
#' end_date = Sys.Date()
39+
#' )
3640
#' )
37-
#' }
41+
#'}
3842
#' @export
3943
ws_daily_flows <- function(
4044
station_number,
41-
start_date = Sys.Date() - 365,
42-
end_date = Sys.Date()) {
45+
start_date = NULL,
46+
end_date = NULL) {
4347

4448
get_historical_data(
4549
station_number = station_number,
@@ -53,8 +57,8 @@ ws_daily_flows <- function(
5357
#' @export
5458
ws_daily_levels <- function(
5559
station_number,
56-
start_date = Sys.Date() - 365,
57-
end_date = Sys.Date()) {
60+
start_date = NULL,
61+
end_date = NULL) {
5862

5963
get_historical_data(
6064
station_number = station_number,
@@ -72,6 +76,14 @@ get_historical_data <- function(
7276
end_date) {
7377
parameters <- match.arg(parameters, choices = c("level", "flow"))
7478

79+
if (is.null(start_date)) {
80+
stop("please provide a valid date for the start_date argument", call. = FALSE)
81+
}
82+
83+
if (is.null(end_date)) {
84+
stop("please provide a valid date for the end_date argument", call. = FALSE)
85+
}
86+
7587
## Build link for GET
7688
baseurl <- "https://wateroffice.ec.gc.ca/services/daily_data/csv/inline?"
7789

man/ws_daily_flows.Rd

Lines changed: 22 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-historical-webservice.R

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
test_that("ws_daily_flows returns the correct data header", {
22
skip_on_cran()
33

4-
ws_test <- ws_daily_flows(station_number = "08MF005")
4+
ws_test <- ws_daily_flows(
5+
station_number = "08MF005",
6+
start_date = Sys.Date() - 365,
7+
end_date = Sys.Date()
8+
)
59

610
expect_identical(
711
colnames(ws_test),
@@ -19,13 +23,18 @@ test_that("ws_daily_flows is empty with a nearish date", {
1923
expect_error(ws_daily_flows(
2024
station_number = "08MF005",
2125
start_date = Sys.Date() - 2
26+
end_date = Sys.Date()
2227
), "No data exists for this station query during the period chosen")
2328
})
2429

2530
test_that("ws_daily_levels returns the correct data header", {
2631
skip_on_cran()
2732

28-
ws_test <- ws_daily_levels(station_number = "08MF005")
33+
ws_test <- ws_daily_levels(
34+
station_number = "08MF005",
35+
start_date = Sys.Date(),
36+
end_date = Sys.Date() - 365
37+
)
2938

3039
expect_identical(
3140
colnames(ws_test),
@@ -45,3 +54,25 @@ test_that("ws_daily_levels is empty with a nearish date", {
4554
start_date = Sys.Date() - 2
4655
), "No data exists for this station query during the period chosen")
4756
})
57+
58+
test_that("get_historical_data error informatively with no dates given",{
59+
expect_error(
60+
get_historical_data(
61+
station_number = "08MF005"
62+
),
63+
"please provide a valid date for the start_date argument"
64+
)
65+
66+
expect_error(
67+
get_historical_data(
68+
station_number = "08MF005",
69+
start_date = Sys.Date(),
70+
end_date = NULL
71+
),
72+
"please provide a valid date for the start_date argument"
73+
)
74+
})
75+
76+
test_that("function can take a string for date", {
77+
78+
})

0 commit comments

Comments
 (0)