Skip to content

Commit

Permalink
Issue ##438 - Add tests to check existing data.table functions work w…
Browse files Browse the repository at this point in the history
…ith data.frames (#799)
  • Loading branch information
nikosbosse committed May 12, 2024
1 parent 7ac46f1 commit 110502d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
3 changes: 3 additions & 0 deletions tests/testthat/setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ metrics_no_cov_no_ae <- metrics_quantile(
"interval_coverage_deviation", "ae_median")
)

example_quantile_df <- as.data.frame(na.omit(example_quantile))
checkmate::assert_number(length(class(example_quantile_df)))

# compute scores
scores_quantile <- suppressMessages(score(as_forecast(example_quantile)))
scores_continuous <- suppressMessages(score(as_forecast(example_sample_continuous)))
scores_point <- suppressMessages(score(as_forecast(example_point)))
scores_binary <- suppressMessages(score(as_forecast(example_binary)))

class(as.data.frame(example_quantile))
23 changes: 23 additions & 0 deletions tests/testthat/test-forecast.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ test_that("Running `as_forecast()` twice returns the same object", {
)
})

test_that("as_forecast works with a data.frame", {
expect_no_condition(as_forecast(example_quantile_df))
})

test_that("as_forecast() works as expected", {
test <- na.omit(data.table::copy(example_quantile))
expect_s3_class(as_forecast(test), "forecast_quantile")
Expand Down Expand Up @@ -272,6 +276,13 @@ test_that("assert_forecast() complains if the forecast type is wrong", {
)
})

test_that("assert_forecast_generic() works as expected with a data.frame", {
expect_error(
assert_forecast_generic(example_quantile_df),
"Assertion on 'data' failed: Must be a data.table, not data.frame."
)
})


# ==============================================================================
# validate_forecast()
Expand All @@ -284,3 +295,15 @@ test_that("validate_forecast() works as expected", {
)
expect_true(!is.null(out))
})


# ==============================================================================
# new_forecast()
# ==============================================================================

test_that("new_forecast() works as expected with a data.frame", {
expect_s3_class(
new_forecast(example_quantile_df, "quantile"),
c("forecast_quantile", "data.table", "data.frame")
)
})
33 changes: 23 additions & 10 deletions tests/testthat/test-get_-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@
# `get_forecast_unit()`
# ==============================================================================
test_that("get_forecast_unit() works as expected", {
expect_equal(
get_forecast_unit(example_quantile),
c("location", "target_end_date", "target_type", "location_name",
"forecast_date", "model", "horizon")
fc <- c(
"location", "target_end_date", "target_type", "location_name",
"forecast_date", "model", "horizon"
)

expect_equal(
get_forecast_unit(scores_quantile),
c("location", "target_end_date", "target_type", "location_name",
"forecast_date", "model", "horizon")
)
expect_equal(get_forecast_unit(example_quantile), fc)
expect_equal(get_forecast_unit(scores_quantile), fc)

# test with data.frame
expect_equal(get_forecast_unit(as.data.frame(example_quantile)), fc)
})


Expand Down Expand Up @@ -159,16 +158,26 @@ test_that("get_duplicate_forecasts() works as expected for point", {
})


test_that("get_duplicate_forecasts() works as expected with a data.frame", {
duplicates <- get_duplicate_forecasts(
rbind(example_quantile_df, example_quantile_df[101:110, ])
)
expect_equal(nrow(duplicates), 20)
})


# ==============================================================================
# `get_forecast_type`
# ==============================================================================
test_that("get_forecast_type() works as expected", {
expect_equal(get_forecast_type(as.data.frame(example_quantile)), "quantile")
expect_equal(get_forecast_type(example_sample_continuous), "sample")
expect_equal(get_forecast_type(example_sample_discrete), "sample")
expect_equal(get_forecast_type(example_binary), "binary")
expect_equal(get_forecast_type(example_point), "point")

# works with a data.frame
expect_equal(get_forecast_type(example_quantile_df), "quantile")

expect_error(
get_forecast_type(data.frame(x = 1:10)),
"Assertion on 'data' failed: Columns 'observed', 'predicted' not found in data.",
Expand All @@ -183,6 +192,10 @@ test_that("get_forecast_type() works as expected", {
)
})

test_that("get_forecast_type() works as expected with a data.frame", {
expect_equal(get_forecast_type(as.data.frame(example_quantile)), "quantile")
})


# ==============================================================================
# `get_coverage()`
Expand Down

0 comments on commit 110502d

Please sign in to comment.