diff --git a/tests/testthat/test_anomaly_detection.R b/tests/testthat/test_anomaly_detection.R index eb7c372a..850f3a7e 100644 --- a/tests/testthat/test_anomaly_detection.R +++ b/tests/testthat/test_anomaly_detection.R @@ -1,51 +1,5 @@ context("test anomaly detection functions") -#TODO write test to check that negative/positive anomaly is actuall negative/positive compared to expected values. - -test_that("do_anomary_detection with aggregation", { - data("raw_data", package = "AnomalyDetection") - raw_data$timestamp <- as.POSIXct(raw_data$timestamp) - raw_data <- raw_data %>% rename(`time stamp`=timestamp) # test column name with space - first_ten <- raw_data %>% dplyr::slice(1:10) - raw_data <- dplyr::bind_rows(raw_data, first_ten) - ret <- raw_data %>% - do_anomaly_detection(`time stamp`, e_value=TRUE, time_unit = "hour") - expect_true(is.data.frame(ret)) -}) - -test_that("do_anomary_detection", { - data("raw_data", package = "AnomalyDetection") - raw_data$timestamp <- as.POSIXct(raw_data$timestamp) - ret <- raw_data %>% - do_anomaly_detection(timestamp, count, e_value=TRUE, time_unit = "hour") - expect_equal(ncol(ret), 7) -}) - -test_that("do_anomary_detection grouped case", { - data("raw_data", package = "AnomalyDetection") - raw_data$timestamp <- as.POSIXct(raw_data$timestamp) - expect_error({ - ret <- raw_data %>% - dplyr::group_by(timestamp) %>% - do_anomaly_detection(timestamp, count, e_value=TRUE) - }, "timestamp is grouped. Please ungroup it.") - - expect_error({ - ret <- raw_data %>% - dplyr::group_by(count) %>% - do_anomaly_detection(timestamp, count, e_value=TRUE) - }, "count is grouped. Please ungroup it.") -}) - -test_that("do_anomary_detection without value_col", { - data("raw_data", package = "AnomalyDetection") - raw_data$timestamp <- as.POSIXct(raw_data$timestamp) - raw_data <- dplyr::bind_rows(raw_data, raw_data, raw_data) - ret <- raw_data %>% - do_anomaly_detection(timestamp, e_value=TRUE, time_unit = "hour") - expect_true(is.data.frame(ret)) -}) - test_that("do_anomary_detection with Date data", { ts <- seq.Date(as.Date("2010-01-01"), as.Date("2010-12-31"), by="day") raw_data <- data.frame(timestamp=ts, y=runif(length(ts))) diff --git a/tests/testthat/test_arima_1.R b/tests/testthat/test_arima_1.R index 7452812e..26966792 100644 --- a/tests/testthat/test_arima_1.R +++ b/tests/testthat/test_arima_1.R @@ -1,8 +1,10 @@ context("test ARIMA functions") test_that("exp_arima with aggregation", { Sys.setenv(TZ="UTC") # set time zone for test stability for tests with time unit smaller than day. - data("raw_data", package = "AnomalyDetection") - raw_data$timestamp <- as.POSIXct(raw_data$timestamp) + raw_data <- data.frame( + timestamp = seq(as.POSIXct("2014-01-01 00:00:00", tz="UTC"), by="1 hour", length.out=1400), + count = round(abs(rnorm(1400, mean=1000, sd=200))) + ) raw_data <- raw_data %>% rename(`time stamp`=timestamp, `cou nt`=count) model_df <- raw_data %>% @@ -45,8 +47,10 @@ test_that("exp_arima with aggregation", { test_that("exp_arima with minutes", { Sys.setenv(TZ="UTC") # set time zone for test stability for tests with time unit smaller than day. - data("raw_data", package = "AnomalyDetection") - raw_data$timestamp <- as.POSIXct(raw_data$timestamp) + raw_data <- data.frame( + timestamp = seq(as.POSIXct("2014-01-01 00:00:00", tz="UTC"), by="1 hour", length.out=1400), + count = round(abs(rnorm(1400, mean=1000, sd=200))) + ) raw_data <- raw_data %>% rename(`time stamp`=timestamp, `cou nt`=count) ret <- raw_data %>% tail(100) %>% @@ -292,8 +296,10 @@ test_that("exp_arima test mode with extra regressor", { }) test_that("exp_arima wrong grouping case", { - data("raw_data", package = "AnomalyDetection") - raw_data$timestamp <- as.POSIXct(raw_data$timestamp) + raw_data <- data.frame( + timestamp = seq(as.POSIXct("2014-01-01 00:00:00", tz="UTC"), by="1 hour", length.out=1400), + count = round(abs(rnorm(1400, mean=1000, sd=200))) + ) expect_error({ ret <- raw_data %>% dplyr::group_by(timestamp) %>% @@ -308,8 +314,10 @@ test_that("exp_arima wrong grouping case", { }) test_that("exp_arima grouped case", { - data("raw_data", package = "AnomalyDetection") - raw_data$timestamp <- as.POSIXct(raw_data$timestamp) + raw_data <- data.frame( + timestamp = seq(as.POSIXct("2014-01-01 00:00:00", tz="UTC"), by="1 hour", length.out=1400), + count = round(abs(rnorm(1400, mean=1000, sd=200))) + ) raw_data1 <- raw_data raw_data2 <- raw_data raw_data1 <- raw_data1 %>% mutate(group='A') @@ -326,8 +334,10 @@ test_that("exp_arima grouped case", { }) test_that("exp_arima without value_col", { - data("raw_data", package = "AnomalyDetection") - raw_data$timestamp <- as.POSIXct(raw_data$timestamp) + raw_data <- data.frame( + timestamp = seq(as.POSIXct("2014-01-01 00:00:00", tz="UTC"), by="1 hour", length.out=1400), + count = round(abs(rnorm(1400, mean=1000, sd=200))) + ) ret <- raw_data %>% exp_arima(timestamp, , 10) # verify that the last forecasted_value is not NA to test #9211 @@ -335,8 +345,10 @@ test_that("exp_arima without value_col", { }) test_that("exp_arima with all-NA value col", { - data("raw_data", package = "AnomalyDetection") - raw_data$timestamp <- as.POSIXct(raw_data$timestamp) + raw_data <- data.frame( + timestamp = seq(as.POSIXct("2014-01-01 00:00:00", tz="UTC"), by="1 hour", length.out=1400), + count = round(abs(rnorm(1400, mean=1000, sd=200))) + ) data <- raw_data %>% mutate(count=NA) # Make the count column all-NA. ret <- data %>% exp_arima(timestamp, count, 10) diff --git a/tests/testthat/test_arima_2.R b/tests/testthat/test_arima_2.R index 1ad89168..062ce27a 100644 --- a/tests/testthat/test_arima_2.R +++ b/tests/testthat/test_arima_2.R @@ -1,10 +1,12 @@ context("test exp_arima") test_that("exp_arima with aggregation", { - data("raw_data", package = "AnomalyDetection") - raw_data$timestamp <- as.POSIXct(raw_data$timestamp) + raw_data <- data.frame( + timestamp = seq(as.POSIXct("2014-01-01 00:00:00", tz="UTC"), by="1 hour", length.out=1400), + count = round(abs(rnorm(1400, mean=1000, sd=200))) + ) # Create time gap intentionally. - raw_data <- raw_data %>% filter(as.Date(timestamp) != as.Date("1980-09-26")) + raw_data <- raw_data %>% filter(as.Date(timestamp) != as.Date("2014-01-02")) raw_data <- raw_data %>% rename(`time stamp`=timestamp, `cou nt`=count) raw_data$pre_col1 <- runif(nrow(raw_data)) diff --git a/tests/testthat/test_prophet_1.R b/tests/testthat/test_prophet_1.R index 3909b627..dc233917 100644 --- a/tests/testthat/test_prophet_1.R +++ b/tests/testthat/test_prophet_1.R @@ -1,8 +1,10 @@ context("test prophet functions") test_that("do_prophet with aggregation", { Sys.setenv(TZ="UTC") # set time zone for test stability for tests with time unit smaller than day. - data("raw_data", package = "AnomalyDetection") - raw_data$timestamp <- as.POSIXct(raw_data$timestamp) + raw_data <- data.frame( + timestamp = seq(as.POSIXct("2014-01-01 00:00:00", tz="UTC"), by="1 hour", length.out=1400), + count = round(abs(rnorm(1400, mean=1000, sd=200))) + ) raw_data <- raw_data %>% rename(`time stamp`=timestamp, `cou nt`=count) ret <- raw_data %>% do_prophet(`time stamp`, `cou nt`, 10, time_unit = "day") @@ -359,8 +361,10 @@ test_that("do_prophet test mode with extra regressor", { test_that("do_prophet grouped case", { - data("raw_data", package = "AnomalyDetection") - raw_data$timestamp <- as.POSIXct(raw_data$timestamp) + raw_data <- data.frame( + timestamp = seq(as.POSIXct("2014-01-01 00:00:00", tz="UTC"), by="1 hour", length.out=1400), + count = round(abs(rnorm(1400, mean=1000, sd=200))) + ) expect_error({ ret <- raw_data %>% dplyr::group_by(timestamp) %>% @@ -375,8 +379,10 @@ test_that("do_prophet grouped case", { }) test_that("do_prophet without value_col", { - data("raw_data", package = "AnomalyDetection") - raw_data$timestamp <- as.POSIXct(raw_data$timestamp) + raw_data <- data.frame( + timestamp = seq(as.POSIXct("2014-01-01 00:00:00", tz="UTC"), by="1 hour", length.out=1400), + count = round(abs(rnorm(1400, mean=1000, sd=200))) + ) ret <- raw_data %>% do_prophet(timestamp, NULL, 10) expect_true(is.data.frame(ret))