From 4022944b755cf4439906d5cfd995aa9881eac3ac Mon Sep 17 00:00:00 2001 From: Xfly <18374858141@163.com> Date: Wed, 28 Aug 2024 20:41:57 +0800 Subject: [PATCH] update add some unittest --- tests/testthat/test-evaluateOutliers.R | 20 ++++++++++++++++++++ tests/testthat/test-generateOutliers.R | 2 -- tests/testthat/test-method.R | 4 ++++ tests/testthat/test-outqrf.R | 26 ++++++++++++++++++++++++-- 4 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 tests/testthat/test-evaluateOutliers.R create mode 100644 tests/testthat/test-method.R diff --git a/tests/testthat/test-evaluateOutliers.R b/tests/testthat/test-evaluateOutliers.R new file mode 100644 index 0000000..8ac7c94 --- /dev/null +++ b/tests/testthat/test-evaluateOutliers.R @@ -0,0 +1,20 @@ +X <- data.frame(a = 1:100, b = 1:100) +X_outlier <- data.frame(a = 1:100, b = 1:100) +X_outlier[1L, "a"] <- 1000 + +test_that("Generate results as expected with iris", { + anomaly_data <- generateOutliers(iris, p = 0.05, sd_factor = 5, seed = 123) + qrf<- outqrf(anomaly_data,verbose = F) + result<- evaluateOutliers(iris,anomaly_data,qrf$outliers) + expect_type(result, "double") + expect_length(result, n = 5) + expect_true(length(result) > 0) +}) + +test_that("Generate results as expected with 2 cols data", { + qrf<- outqrf(X_outlier,verbose = F) + result<- evaluateOutliers(X,X_outlier,qrf$outliers) + expect_type(result, "double") + expect_length(result, n = 5) + expect_true(length(result) > 0) +}) diff --git a/tests/testthat/test-generateOutliers.R b/tests/testthat/test-generateOutliers.R index b11684d..cb0c05d 100644 --- a/tests/testthat/test-generateOutliers.R +++ b/tests/testthat/test-generateOutliers.R @@ -1,5 +1,3 @@ -library(testthat) -library(outqrf) test_that("generateOutliers(p = 0.2) changes vector as expected", { x <- 1:10 p <- 0.2 diff --git a/tests/testthat/test-method.R b/tests/testthat/test-method.R new file mode 100644 index 0000000..c88da9e --- /dev/null +++ b/tests/testthat/test-method.R @@ -0,0 +1,4 @@ +test_that("plot() works normally in iris", { + qrf <- outqrf(iris) + expect_no_error(plot(qrf)) +}) diff --git a/tests/testthat/test-outqrf.R b/tests/testthat/test-outqrf.R index c23cab7..3e33018 100644 --- a/tests/testthat/test-outqrf.R +++ b/tests/testthat/test-outqrf.R @@ -13,13 +13,35 @@ test_that("outqrf(iris,quantiles_type=400) Generate results as expected", { expect_true(nrow(qrf$outliers)>0) }) -test_that("outqrf(iris,quantiles_type=400,verbose=0) silent", { +test_that("verbose=0 has an effect", { expect_silent(qrf<- outqrf(iris,quantiles_type=400,verbose=0)) }) -test_that("outqrf(X_NA,quantiles_type=400,impute=TRUE) impute normally", { +test_that("imputation on data with 2 cols can work", { qrf <- outqrf(X_NA,quantiles_type=400,impute=TRUE) expect_true(nrow(qrf$outliers)>0) }) +test_that("imputation on iris data with missing can work", { + nrow <- nrow(iris) + ncol <- ncol(iris) + iris_NA<- iris + num_missing <- 20 + missing_rows <- sample(1:nrow, num_missing) + missing_cols <- sample(1:ncol, ncol) + iris_NA[missing_rows, missing_cols] <- NA + qrf <- outqrf(iris_NA,quantiles_type=400,impute=TRUE) + expect_true(nrow(qrf$outliers)>0) +}) + +test_that("threshold can work normally in marginal scenarios.", { + qrf <- outqrf(iris,threshold=0) + expect_true(nrow(qrf$outliers)==0) +}) + +test_that("threshold can work normally in marginal scenarios.", { + sum_numeric<-sum(sapply(iris, is.numeric)) + qrf <- outqrf(iris,threshold=1) + expect_true(nrow(qrf$outliers)==sum_numeric*nrow(iris)) +})