Skip to content

Commit

Permalink
assertions in metrics functions. This fixes #23
Browse files Browse the repository at this point in the history
  • Loading branch information
MalditoBarbudo committed May 27, 2024
1 parent 0b91600 commit 2805476
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
38 changes: 37 additions & 1 deletion R/metrics.R
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,13 @@ daily_metrics <- function(
metadata = NULL,
...
) {


# when tidy is TRUE, ensure metadata is not null before do anything to
# catch the error before any computation is done
if (isTRUE(tidy) && is.null(metadata)) {
stop("If tidy = TRUE, a metadata object (usually the result of read_sfn_metadata) must be provided in the metadata argument")
}

# hack for cran tests
. <- NULL

Expand Down Expand Up @@ -734,6 +740,12 @@ monthly_metrics <- function(
...
) {

# when tidy is TRUE, ensure metadata is not null before do anything to
# catch the error before any computation is done
if (isTRUE(tidy) && is.null(metadata)) {
stop("If tidy = TRUE, a metadata object (usually the result of read_sfn_metadata) must be provided in the metadata argument")
}

. <- NULL

# hardcoded values
Expand Down Expand Up @@ -818,6 +830,12 @@ nightly_metrics <- function(
...
) {

# when tidy is TRUE, ensure metadata is not null before do anything to
# catch the error before any computation is done
if (isTRUE(tidy) && is.null(metadata)) {
stop("If tidy = TRUE, a metadata object (usually the result of read_sfn_metadata) must be provided in the metadata argument")
}

. <- NULL

period <- match.arg(period)
Expand Down Expand Up @@ -900,6 +918,12 @@ daylight_metrics <- function(
...
) {

# when tidy is TRUE, ensure metadata is not null before do anything to
# catch the error before any computation is done
if (isTRUE(tidy) && is.null(metadata)) {
stop("If tidy = TRUE, a metadata object (usually the result of read_sfn_metadata) must be provided in the metadata argument")
}

. <- NULL

period <- match.arg(period)
Expand Down Expand Up @@ -984,6 +1008,12 @@ predawn_metrics <- function(
...
) {

# when tidy is TRUE, ensure metadata is not null before do anything to
# catch the error before any computation is done
if (isTRUE(tidy) && is.null(metadata)) {
stop("If tidy = TRUE, a metadata object (usually the result of read_sfn_metadata) must be provided in the metadata argument")
}

. <- NULL

period <- match.arg(period)
Expand Down Expand Up @@ -1064,6 +1094,12 @@ midday_metrics <- function(
...
) {

# when tidy is TRUE, ensure metadata is not null before do anything to
# catch the error before any computation is done
if (isTRUE(tidy) && is.null(metadata)) {
stop("If tidy = TRUE, a metadata object (usually the result of read_sfn_metadata) must be provided in the metadata argument")
}

. <- NULL

period <- match.arg(period)
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-metrics.R
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,9 @@ test_that('daily metrics examples work', {
expect_s3_class(test_expr3[['ARG_TRE']][['sapf']], 'tbl')
expect_s3_class(test_expr4, 'tbl')

# expected errors
expect_error(daily_metrics(ARG_TRE, tidy = TRUE), "metadata argument")

})

test_that('monthly metrics examples work', {
Expand All @@ -702,6 +705,9 @@ test_that('monthly metrics examples work', {
expect_s3_class(test_expr3[['ARG_TRE']][['env']], 'tbl')
expect_s3_class(test_expr3[['ARG_TRE']][['sapf']], 'tbl')
expect_s3_class(test_expr4, 'tbl')

# expected errors
expect_error(monthly_metrics(ARG_TRE, tidy = TRUE), "metadata argument")

})

Expand All @@ -721,6 +727,9 @@ test_that('nightly metrics examples work', {
expect_s3_class(test_expr3[['ARG_TRE']][['env']], 'tbl')
expect_s3_class(test_expr3[['ARG_TRE']][['sapf']], 'tbl')
expect_s3_class(test_expr4, 'tbl')

# expected errors
expect_error(nightly_metrics(ARG_TRE, tidy = TRUE), "metadata argument")

})

Expand All @@ -740,6 +749,9 @@ test_that('daylight metrics examples work', {
expect_s3_class(test_expr3[['ARG_TRE']][['env']], 'tbl')
expect_s3_class(test_expr3[['ARG_TRE']][['sapf']], 'tbl')
expect_s3_class(test_expr4, 'tbl')

# expected errors
expect_error(daylight_metrics(ARG_TRE, tidy = TRUE), "metadata argument")

})

Expand All @@ -759,6 +771,9 @@ test_that('predawn metrics examples work', {
expect_s3_class(test_expr3[['ARG_TRE']][['env']], 'tbl')
expect_s3_class(test_expr3[['ARG_TRE']][['sapf']], 'tbl')
expect_s3_class(test_expr4, 'tbl')

# expected errors
expect_error(predawn_metrics(ARG_TRE, tidy = TRUE), "metadata argument")

})

Expand All @@ -778,6 +793,9 @@ test_that('midday metrics examples work', {
expect_s3_class(test_expr3[['ARG_TRE']][['env']], 'tbl')
expect_s3_class(test_expr3[['ARG_TRE']][['sapf']], 'tbl')
expect_s3_class(test_expr4, 'tbl')

# expected errors
expect_error(midday_metrics(ARG_TRE, tidy = TRUE), "metadata argument")

})

Expand Down

0 comments on commit 2805476

Please sign in to comment.