From 69b16c5c5fd1757b9f88902c8fffd77f549e6012 Mon Sep 17 00:00:00 2001 From: owen vallis Date: Tue, 17 Mar 2015 09:46:45 -0700 Subject: [PATCH] adding NA tests for AnomalyDetectionVec --- tests/testthat/test-ts.R | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/tests/testthat/test-ts.R b/tests/testthat/test-ts.R index 910a825..8ac0aa0 100644 --- a/tests/testthat/test-ts.R +++ b/tests/testthat/test-ts.R @@ -4,44 +4,44 @@ context("Evaluation: AnomalyDetectionTs") test_that("last day, both directions, with plot", { results <- AnomalyDetectionTs(raw_data, max_anoms=0.02, direction='both', only_last='day', plot=T) expect_equal(length(results$anoms), 2) - expect_equal(length(results$anoms[[2]]), 25) + expect_equal(length(results$anoms[[2L]]), 25) expect_equal(class(results$plot), c("gg", "ggplot")) }) test_that("both directions, e_value, with longterm", { results <- AnomalyDetectionTs(raw_data, max_anoms=0.02, direction='both', longterm=TRUE, e_value=TRUE) expect_equal(length(results$anoms), 3) - expect_equal(length(results$anoms[[2]]), 131) + expect_equal(length(results$anoms[[2L]]), 131) expect_equal(results$plot, NULL) }) test_that("both directions, e_value, threshold set to med_max", { results <- AnomalyDetectionTs(raw_data, max_anoms=0.02, direction='both', threshold="med_max", e_value=TRUE) expect_equal(length(results$anoms), 3) - expect_equal(length(results$anoms[[2]]), 4) + expect_equal(length(results$anoms[[2L]]), 4) expect_equal(results$plot, NULL) }) context("Evaluation: AnomalyDetectionVec") test_that("last period, both directions, with plot", { - results <- AnomalyDetectionVec(raw_data[[2]], max_anoms=0.02, direction='both', period=1440, only_last=TRUE, plot=T) + results <- AnomalyDetectionVec(raw_data[[2L]], max_anoms=0.02, direction='both', period=1440, only_last=TRUE, plot=T) expect_equal(length(results$anoms), 2) - expect_equal(length(results$anoms[[2]]), 25) + expect_equal(length(results$anoms[[2L]]), 25) expect_equal(class(results$plot), c("gg", "ggplot")) }) test_that("both directions, e_value, with longterm", { - results <- AnomalyDetectionVec(raw_data[[2]], max_anoms=0.02, direction='both', period=1440, longterm_period=1440*14, e_value=TRUE) + results <- AnomalyDetectionVec(raw_data[[2L]], max_anoms=0.02, direction='both', period=1440, longterm_period=1440*14, e_value=TRUE) expect_equal(length(results$anoms), 3) - expect_equal(length(results$anoms[[2]]), 131) + expect_equal(length(results$anoms[[2L]]), 131) expect_equal(results$plot, NULL) }) test_that("both directions, e_value, threshold set to med_max", { - results <- AnomalyDetectionVec(raw_data[[2]], max_anoms=0.02, direction='both', period=1440, threshold="med_max", e_value=TRUE) + results <- AnomalyDetectionVec(raw_data[[2L]], max_anoms=0.02, direction='both', period=1440, threshold="med_max", e_value=TRUE) expect_equal(length(results$anoms), 3) - expect_equal(length(results$anoms[[2]]), 6) + expect_equal(length(results$anoms[[2L]]), 6) expect_equal(results$plot, NULL) }) @@ -52,7 +52,7 @@ test_that("check handling of datasets with leading and trailing NAs", { raw_data[length(raw_data[[2L]]), "count"] <- NA results <- AnomalyDetectionTs(raw_data, max_anoms=0.02, direction='both', plot=T) expect_equal(length(results$anoms), 2) - expect_equal(length(results$anoms[[2]]), 131) + expect_equal(length(results$anoms[[2L]]), 131) expect_equal(class(results$plot), c("gg", "ggplot")) }) @@ -60,3 +60,17 @@ test_that("check handling of datasets with NAs in the middle", { raw_data[floor(length(raw_data[[2L]])/2), "count"] <- NA expect_error(AnomalyDetectionTs(raw_data, max_anoms=0.02, direction='both')) }) + +test_that("check handling of datasets with leading and trailing NAs", { + raw_data[1:10, "count"] <- NA + raw_data[length(raw_data[[2L]]), "count"] <- NA + results <- AnomalyDetectionVec(raw_data[[2]], max_anoms=0.02, period=1440, direction='both', plot=T) + expect_equal(length(results$anoms), 2) + expect_equal(length(results$anoms[[2L]]), 131) + expect_equal(class(results$plot), c("gg", "ggplot")) +}) + +test_that("check handling of datasets with NAs in the middle", { + raw_data[floor(length(raw_data[[2L]])/2), "count"] <- NA + expect_error(AnomalyDetectionVec(raw_data[[2L]], max_anoms=0.02, period=1440, direction='both')) +})