Skip to content

Commit

Permalink
Silence tz warnings in tests before a better solution is implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
olivroy committed Oct 3, 2023
1 parent ed8a812 commit c22d1bb
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 41 deletions.
6 changes: 6 additions & 0 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
expect_tz_warning <- function(object) {
# FIXME when the tzone warning is fixed, delete this function,
# use Replace all expect_tz_warning -> expect_no_warning
# Issue #156
expect_warning(object, regexp = "tzone")
}
4 changes: 2 additions & 2 deletions tests/testthat/test-tk_augment_timeseries.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test_xts <- FB_tbl %>%

# Test xts
test_that("tk_augment_timeseries_signature(xts) test returns correct format.", {
test <- tk_augment_timeseries_signature(test_xts)
expect_tz_warning(test <- tk_augment_timeseries_signature(test_xts))
expect_s3_class(test, "xts")
expect_equal(nrow(test), 1008)
expect_equal(ncol(test), n + 3)
Expand All @@ -32,7 +32,7 @@ test_zoo <- FB_tbl %>%
tk_zoo(silent = TRUE)

test_that("tk_augment_timeseries_signature(zoo) test returns correct format.", {
test <- tk_augment_timeseries_signature(test_zoo)
expect_tz_warning(test <- tk_augment_timeseries_signature(test_zoo))
expect_s3_class(test, "zoo")
expect_equal(nrow(test), 1008)
expect_equal(ncol(test), n + 3)
Expand Down
43 changes: 20 additions & 23 deletions tests/testthat/test-tk_index.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
context("Testing tk_index")

library(dplyr)
FB_tbl <- FANG %>% dplyr::filter(symbol == "FB")
FB_xts <- FB_tbl %>% tk_xts(silent = TRUE)
FB_zoo <- FB_tbl %>% tk_zoo(silent = TRUE)
Expand All @@ -13,29 +13,26 @@ FB_ts <- FB_tbl %>% tk_ts(start = 2015, freq = 252, silent = TRUE)
test_that("tk_index(default) test returns correct format.", {
# Not designed to work with objects of class numeric
expect_warning(tk_index(4))
expect_warning(
expect_false(
has_timetk_idx(4)
)
)
expect_warning(res <- has_timetk_idx(4))
expect_false(res)
})

test_that("tk_index(ts) test returns correct format.", {

# Test if object has timetk index
expect_true(tk_ts(FB_tbl, freq = 252, start = 2015, silent = TRUE) %>%
expect_true(tk_ts(FB_tbl, frequency = 252, start = 2015, silent = TRUE) %>%
has_timetk_idx())

# Return regularized dates
test_index_1 <- tk_ts(FB_tbl, freq = 252, start = 2015, silent = TRUE) %>%
test_index_1 <- tk_ts(FB_tbl, frequency = 252, start = 2015, silent = TRUE) %>%
tk_index(timetk_idx = FALSE)
expect_equal(class(test_index_1), "numeric")
expect_length(test_index_1, 1008)

# Return non-regularized dates (aka timetk index)
test_index_2 <- tk_ts(FB_tbl, freq = 252, start = 2015, silent = TRUE) %>%
tk_index(timetk_idx = TRUE)
expect_equal(class(test_index_2), "Date")
expect_tz_warning(test_index_2 <- tk_ts(FB_tbl, frequency = 252, start = 2015, silent = TRUE) %>%
tk_index(timetk_idx = TRUE))
expect_s3_class(test_index_2, "Date")
expect_length(test_index_2, 1008)

# No timetk index
Expand All @@ -51,19 +48,19 @@ test_that("tk_index(ts) test returns correct format.", {
test_that("tk_index(zooreg) test returns correct format.", {

# Test if object has timetk index
expect_true(tk_zooreg(FB_tbl, freq = 252, start = 2015, silent = TRUE) %>%
expect_true(tk_zooreg(FB_tbl, frequency = 252, start = 2015, silent = TRUE) %>%
has_timetk_idx())

# Return regularized dates
test_index_3 <- tk_zooreg(FB_tbl, freq = 252, start = 2015, silent = TRUE) %>%
test_index_3 <- tk_zooreg(FB_tbl, frequency = 252, start = 2015, silent = TRUE) %>%
tk_index(timetk_idx = FALSE)
expect_equal(class(test_index_3), "numeric")
expect_type(test_index_3, "double")
expect_length(test_index_3, 1008)

# Return non-regularized dates (aka timetk index)
test_index_4 <- tk_zooreg(FB_tbl, freq = 252, start = 2015, silent = TRUE) %>%
test_index_4 <- tk_zooreg(FB_tbl, frequency = 252, start = 2015, silent = TRUE) %>%
tk_index(timetk_idx = TRUE)
expect_equal(class(test_index_4), "Date")
expect_s3_class(test_index_4, "Date")
expect_length(test_index_4, 1008)

})
Expand All @@ -75,7 +72,7 @@ test_that("tk_index(tbl) test returns correct format.", {

# Return vector of dates
test_index_3 <- FB_tbl %>% tk_index(timetk_idx = FALSE)
expect_equal(class(test_index_3), "Date")
expect_s3_class(test_index_3, "Date")
expect_length(test_index_3, 1008)

# No date or date time
Expand All @@ -88,8 +85,8 @@ test_that("tk_index(xts) test returns correct format.", {
expect_false(FB_xts %>% has_timetk_idx())

# Return vector of dates
test_index_5 <- FB_xts %>% tk_index(timetk_idx = FALSE)
expect_equal(class(test_index_5), "Date")
expect_tz_warning(test_index_5 <- FB_xts %>% tk_index(timetk_idx = FALSE))
expect_s3_class(test_index_5, "Date")
expect_length(test_index_5, 1008)

})
Expand All @@ -100,9 +97,8 @@ test_that("tk_index(zoo) test returns correct format.", {
expect_false(FB_zoo %>% has_timetk_idx())

# Return vector of dates
test_index_6 <- FB_zoo %>%
tk_index(timetk_idx = FALSE)
expect_equal(class(test_index_6), "Date")
expect_tz_warning(test_index_6 <- tk_index(FB_zoo, timetk_idx = FALSE))
expect_s3_class(test_index_6, "Date")
expect_length(test_index_6, 1008)

})
Expand Down Expand Up @@ -304,7 +300,8 @@ test_that("tk_index(decomposed.ts) test returns correct format.", {
expect_true(has_timetk_idx(fit))

expect_type(tk_index(fit), "double")
expect_s3_class(tk_index(fit, timetk_idx = TRUE), "Date")
expect_tz_warning(res <- tk_index(fit, timetk_idx = TRUE))
expect_s3_class(res, "Date")


})
Expand Down
3 changes: 1 addition & 2 deletions tests/testthat/test-tk_tbl.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ test_that("mts to tbl test returns tibble with correct rows and columns.", {
)

# ts reverse coercion test ----
test_tbl_4b <- FB_mts %>%
tk_tbl(rename_index = "date", timetk_idx = TRUE)
expect_tz_warning(test_tbl_4b <- tk_tbl(FB_mts, rename_index = "date", timetk_idx = TRUE))
expect_identical(test_tbl_4b, FB_tbl %>% dplyr::select(-symbol))
})

Expand Down
23 changes: 10 additions & 13 deletions tests/testthat/test-tk_ts.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
context("Test tk_ts")

library(dplyr)
FB_tbl <- FANG %>% dplyr::filter(symbol == "FB")
FB_xts <- tk_xts(FB_tbl, silent = TRUE)
FB_zoo <- tk_zoo(FB_tbl, silent = TRUE)
Expand All @@ -9,21 +9,20 @@ FB_zoo <- tk_zoo(FB_tbl, silent = TRUE)
# tbl to ts -----
test_that("tbl to ts test returns ts with correct rows and columns.", {
# Use date column to specify order
test_ts_1 <- tk_ts(FB_tbl, select = -c(date, symbol), freq = 252, start = 2015)
test_ts_1 <- tk_ts(FB_tbl, select = -c(date, symbol), frequency = 252, start = 2015)
expect_equal(nrow(test_ts_1), 1008)
expect_equal(ncol(test_ts_1), 6)

# Reverse coercion
test_ts_2 <- test_ts_1 %>%
tk_tbl(rename_index = "date", timetk_idx = TRUE)
expect_tz_warning(test_ts_2 <- tk_tbl(test_ts_1, rename_index = "date", timetk_idx = TRUE))
expect_identical(FB_tbl %>% dplyr::select(-symbol), test_ts_2)

# Auto-drop columns
expect_warning(tk_ts(FB_tbl, freq = 252, start = 2015)) # dropping date column
expect_warning(tk_ts(FB_tbl, frequency = 252, start = 2015)) # dropping date column

# NSE
select <- "adjusted"
test_ts_3 <- tk_ts_(FB_tbl, select = select, freq = 252, start = 2015)
test_ts_3 <- tk_ts_(FB_tbl, select = select, frequency = 252, start = 2015)
expect_equal(nrow(test_ts_3), 1008)
expect_equal(ncol(test_ts_3), 1)
expect_equal(colnames(test_ts_3), select)
Expand All @@ -33,18 +32,16 @@ test_that("tbl to ts test returns ts with correct rows and columns.", {
# xts to ts -----
test_that("xts to ts test returns ts with correct rows and columns.", {
# Use date column to specify order
test_ts_4 <- tk_ts(FB_xts, freq = 252, start = 2015)
test_ts_4 <- tk_ts(FB_xts, frequency = 252, start = 2015)
expect_equal(nrow(test_ts_4), 1008)
expect_equal(ncol(test_ts_4), 6)

# Reverse coercion
test_ts_5 <- test_ts_4 %>%
tk_xts()
expect_tz_warning(test_ts_5 <- test_ts_4 %>% tk_xts())
expect_identical(FB_xts, test_ts_5)

# Warning if using select field
expect_warning(tk_ts(FB_xts, select = -date,
freq = 252, start = 2015)) # only for use with data.frames
expect_warning(tk_ts(FB_xts, select = -date, frequency = 252, start = 2015)) # only for use with data.frames

# Error if using date_var field
expect_error(tk_ts(FB_xts, date_var = date,
Expand All @@ -55,7 +52,7 @@ test_that("xts to ts test returns ts with correct rows and columns.", {
# zoo to ts -----
test_that("zoo to ts test returns ts with correct rows and columns.", {
# Use date column to specify order
test_ts_6 <- tk_ts(FB_zoo, freq = 252, start = 2015)
test_ts_6 <- tk_ts(FB_zoo, frequency = 252, start = 2015)
expect_equal(nrow(test_ts_6), 1008)
expect_equal(ncol(test_ts_6), 6)

Expand All @@ -66,7 +63,7 @@ test_that("zoo to ts test returns ts with correct rows and columns.", {

# Warning if using select field
expect_warning(tk_ts(FB_xts, select = -date,
freq = 252, start = 2015)) # only for use with data.frames
frequency = 252, start = 2015)) # only for use with data.frames

# Warning if using date_var field
expect_error(tk_ts(FB_xts, date_var = date,
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-tk_xts.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ test_that("zoo to xts test returns xts with correct rows and columns.", {
# Default is xts::xts() for other objects; only test zoo
test_that("ts to xts test returns xts with correct rows and columns.", {
# Use date column to specify order
test_xts_6 <- tk_ts(FB_tbl, silent = TRUE) %>% tk_xts()
expect_tz_warning(test_xts_6 <- tk_ts(FB_tbl, silent = TRUE) %>% tk_xts())
expect_equal(nrow(test_xts_6), 1008)
expect_equal(ncol(test_xts_6), 6)

Expand Down

0 comments on commit c22d1bb

Please sign in to comment.